5.2 - Structures.

5.2.1 - Overview of Structures.


1. What is a structure in C?
Correct Answer: (B) A user-defined data type that can hold multiple values of different types
2. Which keyword is used to define a structure in C?
Correct Answer: (A) struct
3. How do you access members of a structure?
Correct Answer: (B) Using the . operator and -> operator
4. What does the following code declare?
            struct stud {
                char name[80];
                int roll;
                float mark;
            };
                
Correct Answer: (C) A structure with three different members
5. What will be the output of this code?
            struct stud {
                char name[80];
                int roll;
                float mark;
            } st;
            
            st.roll = 100;
            printf("%d", st.roll);
                
Correct Answer: (B) 100
6. How can you define a structure variable without giving it a name?
Correct Answer: (B) struct { char name[80]; int roll; } st;
7. What does the sizeof operator return when used with a structure?
Correct Answer: (B) The size of the entire structure
8. Which of the following correctly defines a function that takes a structure as an argument?
Correct Answer: (D) Both A and C
9. What will happen if you try to access a member of a structure through a null pointer?
Correct Answer: (B) It will cause a segmentation fault
10. How do you return a structure from a function?
Correct Answer: (C) Both A and B
11. Which operator is used to access structure members through a pointer?
Correct Answer: (C) -> (arrow)
12. What is the output of the following code?
            struct Point {
                int x;
                int y;
            };
            
            struct Point p = {3, 4};
            printf("%d", p.x);
                
Correct Answer: (B) 3
13. What type of variable is created when defining a structure without a name?
Correct Answer: (C) An unnamed structure variable
14. What is the primary use of using structures in C?
Correct Answer: (A) To group related data
15. How can you initialize a structure in C?
Correct Answer: (A) By assigning values directly
16. What will be the output of this code?
            struct stud {
                char name[80];
                int roll;
                float mark;
            } s1 = {"John", 101, 85.5};
            
            printf("%s", s1.name);
                
Correct Answer: (B) John
17. Which of the following can be a member of a structure?
Correct Answer: (D) All of the above
18. Which statement is true regarding unions and structures?
Correct Answer: (B) Structures can hold different types of data, while unions hold one type
19. How do you define a structure with a member that is another structure?
Correct Answer: (B) struct outer { struct inner innerStruct; };
20. What is the default access specifier for structure members in C?
Correct Answer: (D) There are no access specifiers in C

5.2.2 - Arrays vs Structures.


21. What is an array in C?

Correct Answer: (A) A collection of elements of the same data type

22. What is a structure in C?

Correct Answer: (B) A collection of elements of different data types

23. How do you access an array element?

Correct Answer: (C) Using the [] operator

24. How do you access a structure member?

Correct Answer: (B) Using the . operator or -> operator

25. Can an array hold elements of different data types?

Correct Answer: (B) No

26. Can a structure hold an array as a member?

Correct Answer: (A) Yes

27. What is the default size of an array in C?

Correct Answer: (D) There is no default size

28. How do you declare a structure that includes an array?

Correct Answer: (D) struct myStruct { int arr[10]; };

29. What happens if you access an out-of-bounds index in an array?

Correct Answer: (B) It will print garbage value

30. What happens if you access a member of a structure that is not initialized?

Correct Answer: (B) It will print garbage value

31. How do you define an array of structures?

Correct Answer: (A) struct stud arr[10];

32. Which of the following can be members of a structure?

Correct Answer: (D) All of the above

33. How do you initialize an array of integers with 5 elements?

Correct Answer: (A) int arr[5] = {1, 2, 3, 4, 5};

34. Can a structure contain another structure?

Correct Answer: (A) Yes

35. Which has a fixed size: array or structure?

Correct Answer: (A) Array

36. What is the size of a structure with two int members?

Correct Answer: (D) Depends on the compiler and architecture

37. What is the output of this code?

            struct stud {
                int roll;
                char name[20];
            } student = {101, "Alice"};
            
            printf("%d", student.roll);
                
Correct Answer: (C) 101

38. Can you use an array in a function as a parameter?

Correct Answer: (A) Yes

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

Correct Answer: (C) Both A and B

40. Which is more memory efficient for storing multiple related data of the same type?

Correct Answer: (B) Array

5.2.3 - Nested Structures.


41. What is a nested structure in C?

Correct Answer: (B) A structure that contains another structure

42. How do you access a member of a nested structure?

Correct Answer: (B) Using the . operator twice

43. What is the purpose of using nested structures?

Correct Answer: (B) To combine related data types into a single structure

44. How do you declare a nested structure?

Correct Answer: (A) struct outer { struct inner { int a; }; };

45. What happens if you access a member of a nested structure that is not initialized?

Correct Answer: (B) It prints garbage value

46. Which operator is used to access members of a nested structure?

Correct Answer: (C) .

47. Can a structure member itself be a nested structure?

Correct Answer: (A) Yes

48. How do you define a nested structure in a function?

Correct Answer: (B) By using the same syntax as outside the function

49. How can you initialize a nested structure?

Correct Answer: (A) struct outer o = {1, {2}};

50. In a nested structure, which keyword is used to define an inner structure?

Correct Answer: (A) struct

51. What is the output of the following code?

            struct emp {
                char name[50];
                struct dept {
                    int sal;
                    char degi[50];
                } d;
            };
            struct emp e = {"Alice", {50000, "Manager"}};
            printf("%s %d", e.name, e.d.sal);
                
Correct Answer: (B) Alice 50000

52. Can a structure contain a pointer to another structure?

Correct Answer: (A) Yes

53. What is the correct way to declare a variable of a nested structure?

Correct Answer: (A) struct outer o;

54. What keyword is used to define a structure?

Correct Answer: (B) struct

55. How do you access the designation of an employee in a nested structure?

Correct Answer: (A) e.d.degi

56. Which of the following statements is true about nested structures?

Correct Answer: (B) They can contain arrays and other structures

57. What is the size of a structure that contains two int members and one char member?

Correct Answer: (D) Depends on the compiler and architecture

58. How can you pass a nested structure to a function?

Correct Answer: (C) Both by reference and by value

59. How do you declare a structure with a nested structure and an array?

Correct Answer: (D) Both A and B

60. Can you use a nested structure to represent a complex data type such as a student with courses?

Correct Answer: (A) Yes

5.2.4 - Array of Structures.


61. What is an array of structures in C?
Correct Answer: (B) An array containing similar structure data types
62. How do you declare an array of structures?
Correct Answer: (C) struct name variable[size];
63. Which of the following is true about the members of a structure?
Correct Answer: (B) Members can be of different data types
64. What is the output of the following code snippet?
     struct student {
         char name[20];
         int roll;
     };
     struct student s[2] = {{"Alice", 1}, {"Bob", 2}};
     printf("%s", s[0].name);
                
Correct Answer: (B) Alice
65. What does the fflush(stdin) function do in the context of input?
Correct Answer: (B) It clears the input buffer
66. What is the purpose of passing structures to functions?
Correct Answer: (B) To enable access to structure members
67. How can you pass a structure to a function by reference?
Correct Answer: (D) Both A and C
68. In the context of structures, what does getch() do?
Correct Answer: (D) Both B and C
69. What will be the output of this code?
                struct stud {
                    char name[20];
                    int roll;
                };
                struct stud s = {"Alice", 1};
                printf("%d", s.roll);
                
Correct Answer: (B) 1
70. How do you access a member of a structure within an array?
Correct Answer: (A) array[index].member
71. Which of the following statements is true about structure members?
Correct Answer: (D) They are stored in contiguous memory locations
72. What happens if you try to access an uninitialized member of a structure?
Correct Answer: (C) It will return garbage value
73. Can a structure contain another structure as a member?
Correct Answer: (A) Yes
74. Which of the following is a valid way to declare a structure variable?
Correct Answer: (A) struct student s;
75. What is a self-referential structure?
Correct Answer: (B) A structure that contains a pointer to itself
76. Which statement about structure members is correct?
Correct Answer: (C) They are public by default
77. Which function is used to read a string from the standard input?
Correct Answer: (D) Both B and C
78. How do you display the entire content of an array of structures?
Correct Answer: (B) By using a loop to access each member
79. What happens when a structure is passed by value to a function?
Correct Answer: (B) A copy of the structure is created
80. Which of the following is an advantage of using structures?
Correct Answer: (B) They allow grouping of related data

5.2.5 - Passing Structures as Arguments in Functions.


81. What happens when a structure variable is passed to a function in C?
Correct Answer: (B) A copy of the structure is passed to the function
82. How do you define a structure in C?
Correct Answer: (A) struct name{...};
83. In the given program, what is the type of the member "name" in the "student" structure?
Correct Answer: (C) char *
84. What does the display() function do in the provided code?
Correct Answer: (B) Displays the structure member values
85. Which operator is used to access the members of a structure?
Correct Answer: (B) .
86. How can you ensure that a pointer to a character in a structure points to valid memory?
Correct Answer: (C) Both A and B
87. In the function call display(o);, what does o represent?
Correct Answer: (B) The structure itself
88. What will be the output of the given program?
            struct student {
                char *name;
                int age;
                float per;
            };
            struct student o = {"RAM", 25, 75.5};
            display(o);
                
Correct Answer: (B) Name: RAM, Age: 25, Percent: 75.5
89. What does the return 0; statement indicate in the main() function?
Correct Answer: (B) The program completed successfully
90. If the structure is modified in the display() function, what happens to the original structure in the main() function?
Correct Answer: (B) It remains unchanged
91. What is the purpose of using a pointer for the "name" member in the structure?
Correct Answer: (D) All of the above
92. Which of the following statements is true about passing structures to functions?
Correct Answer: (B) Structures are passed by value by default
93. Can a function return a structure in C?
Correct Answer: (A) Yes
94. What is the output of the following code snippet?
            struct student {
                char *name;
                int age;
                float per;
            };
            struct student o = {"Alice", 22, 88.5};
            printf("%s", o.name);
                
Correct Answer: (A) Alice
95. What will happen if you try to print an uninitialized pointer member in a structure?
Correct Answer: (C) It will print garbage value
96. What does the printf() function do in the display() function?
Correct Answer: (B) Displays output to the console
97. How do you pass a structure to a function by reference?
Correct Answer: (D) Both B and C
98. Which of the following correctly defines a structure in C?
Correct Answer: (A) struct name {int a; float b;};
99. What is the effect of using void display(struct student o) instead of void display(struct student *o)?
Correct Answer: (B) The original structure cannot be modified
100. Which of the following is NOT a valid way to declare a structure variable?
Correct Answer: (D) student o;

5.2.6 - Typedef.


101. What is the purpose of the typedef keyword in C?
Correct Answer: (B) To provide existing data types with meaningful names
102. How do you define a new alias for an existing data type using typedef?
Correct Answer: (B) typedef existing_name alias_name;
103. Which of the following correctly defines an alias for unsigned int as unit?
Correct Answer: (A) typedef unsigned int unit;
104. What is the output of the following code?
typedef struct {
                int x;
                int y;
            } point;
            point p = {10, 20};
            printf("%d %d", p.x, p.y);
Correct Answer: (A) 10 20
105. What does typedef struct students { ... } stu; achieve?
Correct Answer: (B) It creates an alias stu for the structure students.
106. In the statement typedef int* ptr;, what does ptr represent?
Correct Answer: (C) A pointer to an integer
107. How can typedef improve code readability when using structures?
Correct Answer: (B) By avoiding the use of struct repeatedly
108. What is the output of the following code snippet?
typedef int Arr[4];
            Arr temp = {1, 2, 3, 4};
            printf("%d", temp[2]);
Correct Answer: (C) 3
109. Which statement is true about typedef compared to #define?
Correct Answer: (D) All of the above
110. Which of the following is NOT a valid use of typedef?
Correct Answer: (D) typedef #define MAX 100;
111. What is the main difference between typedef and #define?
Correct Answer: (B) typedef defines data types, while #define defines constants.
112. How would you declare a pointer to a structure using typedef?
Correct Answer: (A) typedef struct { ... } *ptr;
113. What is the output of the following code?
typedef int* ptr;
            ptr p1, p2;
            int x = 10;
            p1 = &x;
            printf("%d", *p1);
Correct Answer: (A) 10
114. When using typedef with arrays, what does it allow you to do?
Correct Answer: (B) Declare multiple variables of the same type in one line
115. How would you declare an array of 10 integers using typedef?
Correct Answer: (A) typedef int arr[10];
116. Which statement about typedef is FALSE?
Correct Answer: (C) It can be used to define constants.
117. Can you use typedef to create an alias for a function pointer?
Correct Answer: (A) Yes
118. What will happen if you do not include a semicolon at the end of a typedef declaration?
Correct Answer: (B) It will cause a syntax error
119. Which of the following is a valid definition of a typedef for a function that returns an integer and takes two integers as arguments?
Correct Answer: (B) typedef int (*func)(int, int);
120. How do you declare a variable st of type stu, where stu is defined using typedef for a structure?
Correct Answer: (A) stu st;