Site icon T4Tutorials.com

Nested structures — C++ MCQs

1. What is a nested structure in C++?

(A) A structure defined inside another structure


(B) A pointer to a structure


(C) An array of structures


(D) A structure with functions only



2. How do you define a nested structure?

(A) struct Address { string city; }; struct Person { string name; Address addr; };


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


(C) Both A and B


(D) struct Person { Address addr; };



3. How do you access the city member of Address in Person p?

(A) p->addr.city


(B) p.addr.city


(C) p.city.addr


(D) p.addr->city



4. Which of the following is correct declaration of nested structures?

(A) struct Person { string name; struct Address { string city; string country; } addr; };


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


(C) Both A and B


(D) None of these



5. Can a nested structure contain another structure?

(A) Yes


(B) No


(C) Only one level deep


(D) Only static members



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

(A) Person *p;


(B) struct Person *p;


(C) Both A and B


(D) Nested structures cannot have pointers



7. How do you access a nested member via pointer?

(A) p->addr.city


(B) (*p).addr.city


(C) Both A and B


(D) p.addr->city



8. Can nested structures have functions?

(A) Yes


(B) No


(C) Only static


(D) Only friend functions



9. Can you initialize a nested structure using curly braces?

(A) Yes


(B) No


(C) Only for pointers


(D) Only in C



10. How do you dynamically allocate memory for a nested structure Person?

(A) Person p;


(B) Person p = new Person;


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


(D) Person *p = new Person;



11. How do you free memory allocated for nested structure pointer?

(A) free(p);


(B) delete[] p;


(C) delete p;


(D) release(p);



12. Can a nested structure be defined inside a class?

(A) Yes


(B) No


(C) Only as private


(D) Only as static



13. Can a nested structure be forward declared?

(A) Only inside functions


(B) No


(C) Only in global scope


(D) Yes



14. What is output?
struct Address { string city; };
struct Person { string name; Address addr; };
int main() { Person p = {"Ali", {"Karachi"}}; cout << p.addr.city; }

(A) Ali


(B) Karachi


(C) Garbage


(D) Compile error



15. Can nested structures contain arrays?

(A) Yes


(B) No


(C) Only static arrays


(D) Only pointers



16. Can you pass a nested structure to a function?

(A) By value


(B) By reference


(C) By pointer


(D) All of the above



17. Can nested structures have constructors and destructors?

(A) Yes


(B) No


(C) Only constructors


(D) Only destructors



18. Can you assign one nested structure variable to another?

(A) Only dynamically allocated


(B) No


(C) Only pointers


(D) Yes



19. What is the default access specifier in nested structures?

(A) private


(B) public


(C) protected


(D) Depends on compiler



20. Can nested structures contain pointers to themselves?

(A) Only static pointers


(B) No


(C) Yes, with forward declaration


(D) Only if global



21. Which of the following is correct to define a nested structure pointer?

(A) Person *p = new Person;


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


(C) Person p;


(D) Both A and B



22. Can nested structures be private members of a class?

(A) Only static


(B) No


(C) Yes


(D) Only public



23. Can nested structures be used in arrays?

(A) Only dynamic arrays


(B) No


(C) Only pointers


(D) Yes



24. How do you access nested structure in an array of structures?

(A) arr[i].addr.city


(B) arr->addr->city


(C) arr[i]->addr.city


(D) arr[i].city.addr



25. Can nested structures be passed to a function as a pointer?

(A) Yes


(B) No


(C) Only reference


(D) Only by value



26. Can nested structures have static members?

(A) Yes


(B) No


(C) Only integers


(D) Only pointers



27. Which of the following allows you to define nested structure inside another structure and access members?

(A) struct Outer { struct Inner { int x; } in; };


(B) struct Outer { int x; struct Inner { int y; } in; };


(C) Both A and B


(D) None



28. What is output?
struct Address { string city; };
struct Person { string name; Address addr; };
int main() { Person p = {"Ali", {"Lahore"}}; cout << p.name; }

(A) Ali


(B) Lahore


(C) Garbage


(D) Compile error



29. Can nested structures be assigned using = operator?

(A) Only dynamic memory


(B) No


(C) Only for pointers


(D) Yes



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

(A) Can contain other structures


(B) Can have arrays and pointers


(C) Can have constructors and functions


(D) All of the above



Exit mobile version