Site icon T4Tutorials.com

Defining and using structures — C++ MCQs

1. What is a structure in C++?

(A) A data type that stores multiple values of different types


(B) A pointer type


(C) A function


(D) An array of integers



2. How do you define a structure named Person with members name and age?

(A) struct Person { string name; int age; };


(B) struct Person(name, age);


(C) struct Person = { name, age };


(D) struct Person();



3. How do you declare a variable p1 of structure Person?

(A) Person p1;


(B) struct Person p1;


(C) Both A and B


(D) None of these



4. How do you access age of structure p1?

(A) p1->age


(B) p1.age


(C) age.p1


(D) p1[age]



5. How do you initialize structure Person p1 with name “Ali” and age 25?

(A) Person p1 = {“Ali”, 25};


(B) Person p1(“Ali”, 25);


(C) p1 = {“Ali”, 25};


(D) p1 = Person(“Ali”,25);



6. How do you declare a pointer to structure Person?

(A) Person *ptr;


(B) struct Person *ptr;


(C) Both A and B


(D) Person &ptr;



7. How do you access name through pointer ptr?

(A) ptr.name


(B) ptr->name


(C) (*ptr).name


(D) Both B and C



8. Can a structure contain another structure as a member?

(A) Yes


(B) No


(C) Only if both are same type


(D) Only if declared globally



9. Which of the following is correct syntax to declare an array of 5 Person structures?

(A) Person arr[5];


(B) struct Person arr[5];


(C) Both A and B


(D) None



10. What is the default access specifier for structure members in C++?

(A) private


(B) public


(C) protected


(D) None



11. Can structure members be functions?

(A) Yes


(B) No


(C) Only static functions


(D) Only inline functions



12. Which keyword is used to define a structure type?

(A) class


(B) struct


(C) union


(D) enum



13. Which of the following is correct to copy structure p1 to p2?

(A) p2 = p1;


(B) copy(p1, p2);


(C) p2.copy(p1);


(D) memcpy(p2,p1,sizeof(Person));



14. How do you pass a structure to a function?

(A) By value


(B) By reference


(C) By pointer


(D) All of the above



15. How do you return a structure from a function?

(A) Use return type as struct type


(B) Return pointer to structure


(C) Both A and B


(D) Not possible



16. Can structures have constructors in C++?

(A) Yes


(B) No


(C) Only default constructor


(D) Only parameterized constructor



17. Can structure members be private in C++?

(A) Yes


(B) No


(C) Only public


(D) Only protected



18. What is the size of an empty structure in C++?

(A) 0


(B) 1


(C) Compiler-dependent


(D) Undefined



19. Can a structure contain an array?

(A) Yes


(B) No


(C) Only 1D arrays


(D) Only pointers



20. Which of the following allows structure members to be accessed outside the structure?

(A) public


(B) private


(C) protected


(D) friend



21. Which of the following is correct to declare a structure inside another structure?

(A) struct Address { string city; string country; };


(B) struct Person { string name; struct Address addr; };


(C) Both A and B


(D) None



22. What is output?
struct Test { int x; };
int main(){ Test t1={5}; cout << t1.x; }

(A) 0


(B) 5


(C) Garbage


(D) Compile error



23. How do you dynamically allocate a structure?

(A) Person *p = new Person;


(B) Person p = new Person;


(C) Person *p = malloc(sizeof(Person));


(D) Person p;



24. How do you free dynamically allocated structure memory?

(A) free(p);


(B) delete p;


(C) delete[] p;


(D) release(p);



25. Can structure have static members?

(A) Yes


(B) No


(C) Only if public


(D) Only if private



26. Which operator is used to access structure member through pointer?

‘)”> (A)


‘)”> (B) >


‘)”> (C) &


‘)”> (D) *



27. Which of the following is valid declaration of structure variable with initialization?

(A) Person p = {“Ali”, 20};


(B) Person p = Person{“Ali”, 20};


(C) Both A and B


(D) None



28. Can structures have constant members?

(A) Yes


(B) No


(C) Only in C


(D) Only static members



29. Can a structure member be a pointer to the same structure type?

(A) Yes


(B) No


(C) Only if forward declared


(D) Only static



30. Which of the following is true for structures in C++?

(A) Can contain functions


(B) Can have constructors/destructors


(C) Can have access specifiers


(D) All of the above



Exit mobile version