Bird
Raised Fist0
C++programming~10 mins

Why structures are needed in C++ - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a structure named Person.

C++
struct [1] {
    int age;
    float height;
};
Drag options to blanks, or click blank then click option'
AInfo
Bperson
CData
DPerson
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase or unrelated names for the structure.
2fill in blank
medium

Complete the code to create a variable of type Person.

C++
Person [1];
Drag options to blanks, or click blank then click option'
APerson
Bstruct
Cperson1
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the structure name as the variable name.
3fill in blank
hard

Fix the error in accessing the age member of person1.

C++
person1.[1] = 25;
Drag options to blanks, or click blank then click option'
Aages
Bage
CAGE
DAge
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect capitalization for member names.
4fill in blank
hard

Fill both blanks to define and initialize a Person variable.

C++
Person [1] = { [2], 5.9f };
Drag options to blanks, or click blank then click option'
Aperson2
Bperson1
C30
D25
Attempts:
3 left
💡 Hint
Common Mistakes
Using existing variable names or wrong age values.
5fill in blank
hard

Fill all three blanks to create a structure and print a member.

C++
#include <iostream>

struct [1] {
    int age;
    float height;
};

int main() {
    [2] p = { [3], 6.1f };
    std::cout << p.age << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
APerson
C28
Dperson
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatched names or wrong initialization values.

Practice

(1/5)
1. Why do we need structures in C++ programming?
easy
A. To write code without using variables
B. To group different types of data into a single unit
C. To replace all functions in a program
D. To perform mathematical calculations faster

Solution

  1. Step 1: Understand the purpose of structures

    Structures allow grouping of different data types (like int, float, char) into one unit.
  2. Step 2: Compare with other options

    Options A, B, and C do not describe the purpose of structures correctly.
  3. Final Answer:

    To group different types of data into a single unit -> Option B
  4. Quick Check:

    Structures group data = D [OK]
Hint: Structures group mixed data types together [OK]
Common Mistakes:
  • Thinking structures speed up math calculations
  • Confusing structures with functions
  • Believing structures remove the need for variables
2. Which of the following is the correct way to define a structure in C++?
easy
A. define Person { int age; float height; };
B. structure Person { int age; float height; }
C. class Person { int age; float height; }
D. struct Person { int age; float height; };

Solution

  1. Step 1: Recall C++ syntax for structures

    The keyword to define a structure is struct, followed by the name and members inside braces.
  2. Step 2: Check each option

    struct Person { int age; float height; }; uses correct syntax. Options B, C, and D use wrong keywords or missing semicolons.
  3. Final Answer:

    struct Person { int age; float height; }; -> Option D
  4. Quick Check:

    Correct struct syntax = A [OK]
Hint: Use 'struct' keyword and end with semicolon [OK]
Common Mistakes:
  • Using 'structure' instead of 'struct'
  • Omitting semicolon after structure definition
  • Confusing struct with class keyword
3. What will be the output of this C++ code?
struct Point { int x; int y; };
int main() {
  Point p = {3, 4};
  std::cout << p.x << "," << p.y;
  return 0;
}
medium
A. Point
B. x,y
C. 3,4
D. Error: missing semicolon

Solution

  1. Step 1: Understand structure initialization

    The structure Point has members x and y. It is initialized with values 3 and 4 respectively.
  2. Step 2: Trace the output statement

    The code prints p.x and p.y separated by a comma, so output is "3,4".
  3. Final Answer:

    3,4 -> Option C
  4. Quick Check:

    Print members x and y = 3,4 [OK]
Hint: Print struct members with dot operator [OK]
Common Mistakes:
  • Confusing member names with strings
  • Forgetting to initialize structure
  • Missing semicolon after struct definition
4. Identify the error in this structure usage:
struct Car {
  int speed;
  float price
};
int main() {
  Car c = {100, 25000.5};
  return 0;
}
medium
A. Missing semicolon after float price declaration
B. Wrong initialization of structure
C. Structure name should be lowercase
D. Cannot use float in structures

Solution

  1. Step 1: Check structure member declarations

    Each member declaration must end with a semicolon. The line float price is missing a semicolon.
  2. Step 2: Verify other parts

    Initialization and structure name are correct. Using float is allowed.
  3. Final Answer:

    Missing semicolon after float price declaration -> Option A
  4. Quick Check:

    Every member ends with ; = A [OK]
Hint: Check semicolons after each member declaration [OK]
Common Mistakes:
  • Forgetting semicolon after member variables
  • Thinking structure names must be lowercase
  • Believing floats are not allowed in structures
5. You want to store information about a book including title (string), pages (int), and price (float). How does using a structure help in this case?
hard
A. It groups all book details into one variable for easy access
B. It automatically sorts books by price
C. It replaces the need for functions to print book info
D. It prevents storing multiple books in the program

Solution

  1. Step 1: Understand the problem of storing related data

    Book details include different types: string, int, float. Grouping them helps manage data easily.
  2. Step 2: How structures help

    Structures combine these different data types into one variable, making access and management simpler.
  3. Final Answer:

    It groups all book details into one variable for easy access -> Option A
  4. Quick Check:

    Structures group related data = B [OK]
Hint: Use structures to bundle related data together [OK]
Common Mistakes:
  • Thinking structures sort data automatically
  • Confusing structures with functions
  • Believing structures limit data storage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard