Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

303 Q 10 Topics Take Test
Advertisement
Difficulty: All Easy Medium Hard 91–100 of 303
Topics in C Programming
What is the primary difference between a struct and a union in C?
A Struct members are stored sequentially; union members share the same memory location
B Union members are stored sequentially; struct members share memory
C Both allocate separate memory for each member
D There is no functional difference
Correct Answer:  A. Struct members are stored sequentially; union members share the same memory location
EXPLANATION

In a union, all members occupy the same memory space, while struct members have individual memory locations.

Take Test
Consider the following code:
struct Data {
int a;
char b;
};
struct Data d = {10, 'A'};
Which method of initialization is used here?
A Designated initialization
B Positional initialization
C Named initialization
D Array initialization
Correct Answer:  B. Positional initialization
EXPLANATION

Values are assigned in the order they appear in the structure definition, which is positional initialization.

Take Test
Which of the following statements about structures is TRUE?
A A structure is a collection of variables of the same data type
B A structure is a collection of variables of different data types grouped under a single name
C A structure cannot contain pointers
D A structure occupies memory equal to the sum of its members without any padding
Correct Answer:  B. A structure is a collection of variables of different data types grouped under a single name
EXPLANATION

A structure allows grouping of different data types. Padding may be added by the compiler for alignment.

Take Test
Which of the following is the correct way to initialize a structure in C?
A struct Point p = {10, 20};
B struct Point p; p = {10, 20};
C Point p = {10, 20};
D p.x = 10; p.y = 20; (without declaration)
Correct Answer:  A. struct Point p = {10, 20};
EXPLANATION

Structures must be initialized at declaration time using curly braces. Assignment of initializer list is not valid after declaration.

Take Test
Which of the following correctly demonstrates a typedef for a structure?
A typedef struct { int x; int y; } Point;
B struct typedef Point { int x; int y; };
C define struct Point { int x; int y; };
D struct Point typedef { int x; int y; };
Correct Answer:  A. typedef struct { int x; int y; } Point;
EXPLANATION

The correct syntax for typedef struct is: typedef struct { members } StructName;

Take Test
How do you access a member of a structure through a pointer in C?
A Using dot operator (.)
B Using arrow operator (->)
C Using asterisk (*)
D Using ampersand (&)
Correct Answer:  B. Using arrow operator (->)
EXPLANATION

The arrow operator (->) is used to access structure members through a pointer. The dot operator is used with direct variables.

Take Test
Consider: union Data { int i; float f; char c; }; What is the size of this union?
A 4 bytes
B 9 bytes
C 5 bytes
D 12 bytes
Correct Answer:  A. 4 bytes
EXPLANATION

The size of a union equals the size of its largest member. Here, int is 4 bytes (largest), so union size is 4 bytes.

Take Test
Which of the following statements about unions is correct?
A Union members share the same memory location
B Union size equals the sum of all member sizes
C All union members can hold values simultaneously
D Union requires more memory than structure
Correct Answer:  A. Union members share the same memory location
EXPLANATION

Union members share the same memory location, and the union size equals the size of its largest member.

Take Test
What is the correct way to dynamically allocate an array of structures?
struct Student { int roll; char name[50]; };
A struct Student *arr = (struct Student*)malloc(n * sizeof(struct Student));
B struct Student arr = (struct Student*)malloc(n * sizeof(char));
C struct Student *arr = malloc(n);
D Student *arr = malloc(n * Student);
Correct Answer:  A. struct Student *arr = (struct Student*)malloc(n * sizeof(struct Student));
EXPLANATION

Correct allocation requires pointer declaration, proper cast, and multiplying n with sizeof(struct Student).

Take Test
When structure is passed by value to a function, which of these is true?
A Changes to structure members inside function affect the original
B The entire structure is copied onto the stack
C Only pointers within the structure are copied
D Memory is shared between function and caller
Correct Answer:  B. The entire structure is copied onto the stack
EXPLANATION

Pass by value creates a complete copy of the structure on the function's stack frame.

Take Test
IGET
iget AI
Online · Ask anything about exams
Hi! 👋 I'm your iget AI assistant.

Ask me anything about exam prep, MCQ solutions, study tips, or strategies! 🎯
UPSC strategy SSC CGL syllabus Improve aptitude NEET Biology tips