Home Subjects C Programming

C Programming

C language from basics to advanced placement prep

291 Q 10 Topics Take Mock Test
Advertisement
Difficulty: All Easy Medium Hard 101–110 of 291
Topics in C Programming
Consider union u { int i; char c; float f; }; What is the size of this union?
A 4 bytes
B 9 bytes
C 7 bytes
D Depends on compiler
Correct Answer:  A. 4 bytes
EXPLANATION

Union size equals the largest member size. float is typically 4 bytes, which is largest.

Test
Which of the following is true about typedef struct?
A typedef struct creates a named type, eliminating need for 'struct' keyword in declarations
B typedef struct is less efficient than regular struct
C typedef struct cannot be nested
D typedef struct allocates memory immediately
Correct Answer:  A. typedef struct creates a named type, eliminating need for 'struct' keyword in declarations
EXPLANATION

typedef creates an alias for the struct type, allowing direct use without 'struct' keyword.

Test
What is the output of the following code?
struct Point { int x; int y; };
struct Point p = {5};
printf("%d %d", p.x, p.y);
A 5 0
B 5 garbage
C 0 5
D Compilation error
Correct Answer:  A. 5 0
EXPLANATION

When initializing a struct with fewer values than members, remaining members are zero-initialized. p.x=5, p.y=0.

Test
What is the primary advantage of using structures in C?
A To reduce program execution time
B To group related data of different types together
C To automatically initialize variables
D To prevent memory leaks
Correct Answer:  B. To group related data of different types together
EXPLANATION

Structures allow you to combine multiple data types into a single composite type, making code organization and data management more efficient.

Test
How do you access a structure member through a pointer in C?
A ptr.member
B ptr->member
C *ptr.member
D &ptr->member
Correct Answer:  B. ptr->member
EXPLANATION

The arrow operator (->) is used to access structure members through a pointer. It dereferences the pointer and accesses the member in one operation.

Test
What is the purpose of padding in C structures?
A To reduce memory usage
B To align data members on word boundaries for efficient access
C To increase security of the structure
D To prevent structure modification
Correct Answer:  B. To align data members on word boundaries for efficient access
EXPLANATION

Padding adds unused bytes between members to align them on word boundaries, improving CPU access efficiency and performance.

Test
Which keyword is used to define a new name for a structure in C?
A define
B typedef
C alias
D rename
Correct Answer:  B. typedef
EXPLANATION

The typedef keyword creates an alias for a data type, allowing you to use a shorter name instead of the full struct declaration.

Test
In C, what is a self-referential structure?
A A structure that contains a member pointing to itself
B A structure that calls its own constructor
C A structure with recursive function definitions
D A structure that inherits from itself
Correct Answer:  A. A structure that contains a member pointing to itself
EXPLANATION

A self-referential structure contains a pointer to its own type, commonly used in creating linked lists, trees, and other data structures.

Test
What will be the output of sizeof() for the following union? union data { int a; float b; double c; }
A 4 bytes
B 8 bytes
C 12 bytes
D 16 bytes
Correct Answer:  B. 8 bytes
EXPLANATION

The size of a union is the size of its largest member. Here, double (8 bytes) is the largest, so sizeof(union data) = 8 bytes.

Test
Which of the following is a key difference between struct and union in C?
A Struct members share memory while union members don't
B Union members share memory while struct members don't
C Both allocate same memory to all members
D Union cannot have more than 2 members
Correct Answer:  B. Union members share memory while struct members don't
EXPLANATION

In unions, all members share the same memory location, so only one member can hold a value at a time. In structs, each member has its own memory space.

Test
IGET
IGET AI
Online · Exam prep assistant
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