Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


For struct X with a character, an integer, and a float member, which constructor call correctly initializes it using aggregate initialization?

  1. X x1 = { 'a', 2, 3.0 }

  2. X x1 = X('a', 2, 3.0)

  3. X x1('a', 2, 3.0)

  4. X x1 = new X('a', 2, 3.0)

The correct answer is: X x1 = { 'a', 2, 3.0 }

Option B is incorrect because it uses the constructor syntax before the object name, which is not how aggregate initialization works. Option C is also incorrect because it uses the constructor syntax without an equals sign, which is not valid for aggregate initialization. Option D is incorrect because it uses the "new" keyword, which is used for dynamic allocation and not for aggregate initialization. Option A correctly initializes the struct X using aggregate initialization, which consists of using curly brackets to enclose the values for each member.