C++ Struct Exercises

C++ Level 4 Questions Organized by Knowledge Points

C++ Struct

Question 1

  • Question: Which of the following options is correct regarding struct initialization? ( )

cppRun

struct Point{int x,y;};
  • Options:

    A. Point p = (1,2);

    B. Point p = {1,2};

    C. Point p = new {1,2};

    D. Point p = <1,2>;

  • Answer:B

  • Explanation: In C++, struct initialization can use curly braces {} for assignment, making option B correct. Option A uses parentheses, which is a syntax error; option C uses new for dynamic memory allocation, returning a pointer, which cannot be directly assigned to a struct variable; option D uses angle brackets, which is a syntax error.

Question 2

  • Question: Which of the following correctly defines a struct named Person and correctly initializes a variable p of type Person?

  • Options:

    A.

cppRun

struct Person{
    string name;
    int age;
};
Person p("Yang",10);

B.cppRun

struct Person{
    string name;
    int age;
};
Person p;
p.age =10;
p.name ="Yang";

C.cppRun

struct Person{
    string name;
    int age;
};
Person p ={"Yang",10};

D.cppRun

struct Person{
    string name;
    int age;
};
Person p =new Person("Yang",10);
  • Answer:C

  • Explanation: Option A is incorrect because the struct does not define a constructor with parameters, so it cannot be initialized with parentheses. Option B initializes correctly but does not meet the requirement of “correctly defining and initializing”. Option C uses aggregate initialization, which is more direct, while option B is technically an assignment operation, not initialization. Option C is correct; option D is incorrect because new returns a pointer, which cannot be assigned to a struct variable.

Question 3

  • Question: Given the following code, which of the following descriptions is incorrect? ( )

cppRun

struct Person{
    std::string name;
    int age;
    struct Address{
        std::string street;
        std::string city;
    };
    Address address;
};
  • Options:

    A. The struct Person contains a nested struct Address.

    B. Person has a member of type Address named address.

    C. The initialization of address for a variable p of type Person can be written as: p.address.street = “123 Main St”; p.address.city = “Anytown”;

    D. Nesting of structs can reduce naming conflicts, so there is no need to control the nesting level.

  • Answer:D

  • Explanation: Option A is correct; the Address struct is nested within Person. Option B is correct; Person contains a member of type Address named address. Option C is correct; members of the nested struct can be accessed and assigned using the member access operator. Option D is incorrect; excessive nesting can reduce code readability and maintainability, and should be controlled.

Question 4

  • Question: The following C++ code correctly declares a function that returns an int type and accepts two int parameters. (True/False)

cppRun

int add(int,int);
  • Answer:True

  • Explanation: In a function declaration, parameter names can be omitted, specifying only the parameter types. This declaration clearly states that the return type is int and the parameters are two int types, which conforms to the function declaration syntax, thus it is correct.

Question 5

  • Question: The output of the following C++ code is 15. (True/False)

cppRun

void foo(int x){
    x +=5;
}
int main(){
    int a =10;
    foo(a);
    cout &lt;&lt; a &lt;&lt; endl;
}
  • Answer:False

  • Explanation: The foo function uses pass-by-value, so x is a copy of a. The statement x += 5 only modifies the copy, leaving a unchanged at 10, thus the output is 10, not 15, making it false.

Question 6

  • Question: The following C++ code defines another struct within a struct. This way of nested definition is syntactically incorrect. (True/False)

cppRun

#include &lt;string&gt;
#include &lt;vector&gt;
using namespace std;
struct Library{
    struct Book{
        struct Author{
            string name;
            int birthYear;
        };
        int year;
        string title;
        Author author;
    };
    string name;
    vector&lt;Book&gt; books;
};
  • Answer:False

  • Explanation: C++ supports nested struct definitions. In this code, the Book struct is nested within Library, and the Author struct is nested within Book, which is syntactically correct, making the statement false.

Leave a Comment