C Structures Quiz - MCQ Questions and Answers

1. What is a structure in C?

a) A collection of functions
b) A collection of variables of different types
c) A type of function declaration
d) A preprocessor directive

Answer:

b) A collection of variables of different types

Explanation:

In C, a structure is a user-defined data type that allows grouping variables of different types under a single name.

2. How is a structure declared in C?

a) using the keyword 'struct'
b) using the keyword 'structure'
c) using the keyword 'array'
d) using the keyword 'group'

Answer:

a) using the keyword 'struct'

Explanation:

Structures in C are declared using the 'struct' keyword followed by the structure name and its members.

3. Which of the following is the correct way to declare a structure in C?

a) struct Person { int age; char *name; };
b) Person { int age; char *name; };
c) struct Person = { int age; char *name; };
d) struct { int age; char *name; } Person;

Answer:

a) struct Person { int age; char *name; };

Explanation:

The correct syntax for declaring a structure in C is to use the 'struct' keyword, followed by the structure name and its members enclosed in braces.

4. How do you access a member of a structure in C?

a) Using the dot operator (.)
b) Using the arrow operator (->)
c) Using the colon operator (:)
d) Using the ampersand operator (&)

Answer:

a) Using the dot operator (.)

Explanation:

The dot operator (.) is used to access a member of a structure when using a structure variable.

5. How can a structure variable be initialized in C?

a) By using the assignment operator after declaration
b) By listing values in braces {} immediately after declaration
c) By using a function
d) Structures cannot be initialized

Answer:

b) By listing values in braces {} immediately after declaration

Explanation:

A structure variable can be initialized by listing values in braces immediately after its declaration.

6. What is the purpose of a 'typedef' in relation to a structure in C?

a) To define a new data type
b) To create a new structure
c) To define the size of the structure
d) To initialize a structure

Answer:

a) To define a new data type

Explanation:

'typedef' is used to create an alias for a data type, which can simplify the syntax for declaring variables of complex types like structures.

7. How are structure members stored in memory?

a) Sequentially, in the order they are declared
b) In random order
c) All members are stored at the same memory address
d) Depends on the compiler

Answer:

a) Sequentially, in the order they are declared

Explanation:

Structure members are stored in contiguous memory locations in the order in which they are declared.

8. Which of the following allows structures to be assigned to each other in C?

a) Direct assignment using the assignment operator
b) Using a special structure assignment function
c) Structures cannot be assigned to each other
d) Using a loop to assign each member individually

Answer:

a) Direct assignment using the assignment operator

Explanation:

In C, structures can be assigned to each other directly using the assignment operator.

9. How can you pass a structure to a function in C?

a) By value only
b) By reference only
c) Either by value or by reference
d) Structures cannot be passed to functions

Answer:

c) Either by value or by reference

Explanation:

Structures in C can be passed to functions either by value (copying the entire structure) or by reference (using pointers).

10. What is the output of the following C code?

   struct Point { int x, y; };
   struct Point p1 = {1, 2};
   printf("%d, %d", p1.x, p1.y);
a) 1, 2
b) 2, 1
c) 0, 0
d) Error

Answer:

a) 1, 2

Explanation:

The structure 'Point' is correctly initialized with x=1 and y=2, and these values are printed.

11. What is a nested structure in C?

a) A structure within a function
b) A structure with nested loops
c) A structure within another structure
d) A recursive structure

Answer:

c) A structure within another structure

Explanation:

A nested structure in C is a structure that contains one or more other structures as its members.

12. Which keyword is used to access a member of a structure pointer in C?

a) Dot (.)
b) Arrow (->)
c) Ampersand (&)
d) Colon (:)

Answer:

b) Arrow (->)

Explanation:

The arrow operator (->) is used to access members of a structure when you have a pointer to that structure.

13. How can the size of a structure be determined in C?

a) Using the sizeof operator
b) By adding the sizes of all the members
c) By using the length function
d) By checking the structure definition

Answer:

a) Using the sizeof operator

Explanation:

The sizeof operator can be used to determine the size of a structure in bytes.

14. Can a structure in C contain a pointer to itself?

a) Yes
b) No
c) Only in certain cases
d) It causes a compile-time error

Answer:

a) Yes

Explanation:

A structure in C can contain a pointer to its own type, which is useful in creating data structures like linked lists.

15. What happens when a structure is passed to a function by value in C?

a) The function can modify the original structure
b) A copy of the structure is passed to the function
c) The function receives a pointer to the structure
d) It results in a runtime error

Answer:

b) A copy of the structure is passed to the function

Explanation:

When a structure is passed by value, a copy of the structure is created and passed to the function, so any modifications do not affect the original structure.

16. Is it possible to have an array of structures in C?

a) Yes
b) No
c) Only if the structures are of the same type
d) Only in special cases

Answer:

a) Yes

Explanation:

In C, you can create an array of structures, allowing you to store multiple structures of the same type in an array format.

17. How do you initialize a pointer to a structure in C?

a) By using the & operator on the structure variable
b) By using the * operator
c) By using the -> operator
d) By assigning the address of a function

Answer:

a) By using the & operator on the structure variable

Explanation:

A pointer to a structure is initialized by assigning it the address of a structure variable using the & operator.

18. Can a structure in C contain a member that is an array?

a) Yes
b) No
c) Only if the array is static
d) Only if the array size is known at compile-time

Answer:

a) Yes

Explanation:

Structures in C can contain array members, and the arrays can be of any type and size.

19. How can structures be compared in C?

a) Using the == operator
b) By comparing each member individually
c) Structures cannot be compared
d) Using a special compare function

Answer:

b) By comparing each member individually

Explanation:

In C, structures are compared by manually comparing each of their members. The == operator cannot be used to compare structures directly.

20. What is a union in C?

a) A special type of structure
b) A collection of variables of the same type
c) A collection of variables where all members share the same memory location
d) A collection of functions

Answer:

c) A collection of variables where all members share the same memory location

Explanation:

A union is similar to a structure in that it is a collection of variables, but in a union, all members share the same memory location.

Comments