C Structures MCQ

In C programming, structures (often referred to as “structs”) provide a way to group together variables of different data types under a single name. These variables can be of primitive data types such as int, float, or char, and they can also be arrays or other structures. If you’re a novice C programmer or brushing up on foundational concepts, this quiz on structures will test your understanding!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which keyword is used to define a structure?

A) struct
B) structure
C) strct
D) typedef

Answer:

A) struct

Explanation:

In C, the struct keyword is used to define a structure.

2. Consider the following code: 

struct student {
    int id;
    char name[20];
};
What is ‘student’ here?

A) Variable
B) Value
C) Structure type
D) Function

Answer:

C) Structure type

Explanation:

‘student’ is a structure type. We can use this type to declare variables of this structure.

3. How do you access the ‘id’ of a structure variable ‘stu’ of type ‘student’?

A) stu.id
B) student.id
C) stu->id
D) student->id

Answer:

A) stu.id

Explanation:

The dot operator . is used to access the members of a structure variable.

4. What will be the size of the structure student in memory (assuming an int takes 4 bytes and a char takes 1 byte)?

A) 20 bytes
B) 24 bytes
C) 5 bytes
D) 21 bytes

Answer:

B) 24 bytes

Explanation:

The size would be the sum of the size of an int (4 bytes) and the size of the character array (20 bytes) = 24 bytes.

5. What does the following code do?

typedef struct student {
    int id;
    char name[20];
} Student;
A) Defines a function named Student
B) Defines a variable named Student
C) Creates an alias ‘Student’ for the structure
D) Creates a new structure named Student

Answer:

C) Creates an alias ‘Student’ for the structure

Explanation:

The typedef keyword is used to create an alias. Here, ‘Student’ becomes an alias for ‘struct student’.

6. How can you define a structure variable at the time of structure declaration?

A) struct student { int id; char name[20]; } stu;
B) struct student = { int id; char name[20]; } stu;
C) struct student { int id; char name[20]; stu; }
D) struct { int id; char name[20]; } student stu;

Answer:

A) struct student { int id; char name[20]; } stu;

Explanation:

This code snippet defines a structure of type ‘student’ and also declares a variable ‘stu’ of that type.

7. If ‘ptr’ is a pointer to a structure variable, how do you access the members of the structure?

A) ptr.member
B) *ptr.member
C) ptr->member
D) &ptr->member

Answer:

C) ptr->member

Explanation:

When you have a pointer to a structure, the ‘->’ operator is used to access its members.

8. Can a structure in C contain a member of its own type?

A) No, it’s forbidden.
B) Yes, but only as a direct member.
C) Yes, but only as a pointer.
D) Yes, without any restrictions.

Answer:

C) Yes, but only as a pointer.

Explanation:

A structure cannot contain a member of its own type directly because the size of the structure would become indeterminate. However, it can contain a pointer to its own type.

9. Which of the following initializes a structure variable stu with id as 100 and name as “John”?

A) struct student stu = {100, “John”};
B) struct student stu = {id: 100, name: “John”};
C) student stu = {100, “John”};
D) student stu = {id = 100, name = “John”};

Answer:

A) struct student stu = {100, “John”};

Explanation:

This is the correct way to initialize a structure variable with given values.

10. Structures in C can contain:

A) Only data members
B) Data members and member functions
C) Only member functions
D) None of the above

Answer:

A) Only data members

Explanation:

In C, structures can contain only data members. Member functions are a feature of classes in C++.


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top