MCQs on Advanced Topics | C Programming

Dynamic Data Structures (Linked Lists, Stacks, Queues)

  1. Which of the following is true about a linked list?
    • A) It allows for efficient random access
    • B) It requires contiguous memory allocation
    • C) It allows efficient insertion and deletion at both ends
    • D) It uses an array for storage
  2. In a singly linked list, each node contains:
    • A) Only data
    • B) Data and a pointer to the previous node
    • C) Data and a pointer to the next node
    • D) Data and a pointer to both the previous and next node
  3. What is the main advantage of a stack over a queue?
    • A) FIFO (First In, First Out) access
    • B) LIFO (Last In, First Out) access
    • C) Efficient insertion and deletion at both ends
    • D) Unlimited capacity
  4. Which of the following operations is not typically associated with a stack?
    • A) Push
    • B) Pop
    • C) Peek
    • D) Dequeue
  5. Which data structure is used to implement a queue?
    • A) Stack
    • B) Linked list
    • C) Array
    • D) Both B and C
  6. What is the time complexity for the enqueue operation in a queue implemented using a linked list?
    • A) O(1)
    • B) O(n)
    • C) O(log n)
    • D) O(n log n)
  7. What does the term “dynamic memory allocation” refer to?
    • A) Allocating memory at compile-time
    • B) Allocating memory at runtime
    • C) Allocating memory for constants
    • D) Using stack memory for data structures
  8. Which of the following is an example of dynamic data structure?
    • A) Arrays
    • B) Linked Lists
    • C) Stacks
    • D) All of the above

Memory Management in Depth

  1. Which function is used to allocate memory dynamically in C?
    • A) malloc()
    • B) calloc()
    • C) realloc()
    • D) All of the above
  2. What is the purpose of the free() function in C?
    • A) To allocate memory
    • B) To deallocate dynamically allocated memory
    • C) To clear memory cache
    • D) To initialize memory
  3. Which of the following functions can be used to resize previously allocated memory?
    • A) malloc()
    • B) calloc()
    • C) realloc()
    • D) free()
  4. What is the result of calling free() on a pointer that is already NULL?
    • A) Undefined behavior
    • B) No effect
    • C) Runtime error
    • D) Stack overflow
  5. What is a memory leak in C programming?
    • A) Memory that is freed but not reused
    • B) Memory that is never allocated
    • C) Memory that is allocated but never freed
    • D) Memory that is allocated on the stack
  6. How does calloc() differ from malloc()?
    • A) malloc() initializes memory to zero; calloc() does not
    • B) calloc() initializes memory to zero; malloc() does not
    • C) calloc() takes one argument; malloc() takes two
    • D) malloc() is faster than calloc()
  7. What is the consequence of not freeing dynamically allocated memory?
    • A) Memory overflow
    • B) Memory leak
    • C) Stack corruption
    • D) Segmentation fault

Function Pointers and Callbacks

  1. What is a function pointer in C?
    • A) A pointer that points to a variable
    • B) A pointer that points to a function
    • C) A pointer to an array
    • D) A pointer to a struct
  2. Which of the following is the correct syntax to declare a function pointer in C?
    • A) int *fptr(int a);
    • B) int (*fptr)(int a);
    • C) int fptr(*)(int a);
    • D) int (*fptr)(int*);
  3. What is a callback function?
    • A) A function that calls another function
    • B) A function that gets passed as an argument to another function
    • C) A function that calls itself
    • D) A function that returns another function
  4. Which of the following best describes the use of function pointers?
    • A) They allow dynamic function calls
    • B) They are used to access global variables
    • C) They are used to store constants
    • D) They allow conditional compilation
  5. Which of the following is an example of passing a function as a callback?
    • A) func(callback_function);
    • B) callback_function(func);
    • C) callback(&func);
    • D) func() -> callback();
  6. Which type of function is typically used as a callback function?
    • A) Functions with a fixed signature
    • B) Functions that modify global variables
    • C) Functions with no return value
    • D) Functions with variable arguments
  7. What happens when a function pointer is called?
    • A) It executes the function to which it points
    • B) It allocates memory for the function
    • C) It defines a new function
    • D) It modifies the function signature

C Standard Library Functions (stdlib.h, time.h, etc.)

  1. Which function from stdlib.h is used to generate random numbers?
    • A) rand()
    • B) random()
    • C) rand_range()
    • D) randomize()
  2. What does the function exit() in stdlib.h do?
    • A) Allocates memory dynamically
    • B) Terminates the program with a specified status code
    • C) Prints the program’s current state
    • D) Initiates a system shutdown
  3. Which function in time.h returns the current system time?
    • A) clock()
    • B) time()
    • C) gettime()
    • D) system_time()
  4. Which of the following standard library functions is used to allocate memory dynamically?
    • A) alloc()
    • B) malloc()
    • C) calloc()
    • D) realloc()
  5. What does the strcmp() function from string.h do?
    • A) Copies one string to another
    • B) Compares two strings
    • C) Concatenates two strings
    • D) Checks if two strings are equal
  6. Which header file is required for memory allocation functions like malloc() and free()?
    • A) stdio.h
    • B) stdlib.h
    • C) time.h
    • D) math.h
  7. What does the localtime() function in time.h do?
    • A) Converts time to a readable string format
    • B) Returns the current time in GMT
    • C) Converts the time to local time representation
    • D) Sets the system time to the local time
  8. Which of the following functions is used to generate random numbers within a specified range?
    • A) rand_range()
    • B) rand() % range
    • C) randomize()
    • D) generate_random()

Advanced Pointer Concepts (Double Pointers, Array of Pointers)

  1. What is a double pointer in C?
    • A) A pointer that points to another pointer
    • B) A pointer to a function
    • C) A pointer that points to a variable
    • D) A pointer that points to an array
  2. Which of the following is the correct syntax for declaring a double pointer?
    • A) int **ptr;
    • B) int *ptr;
    • C) int ptr**;
    • D) int ptr[];
  3. When do you typically use a double pointer in C?
    • A) When you need to dynamically allocate memory
    • B) When you need to access arrays of pointers
    • C) When passing a pointer to a function and modifying the pointer value
    • D) When working with strings
  4. What does the following code do?cCopy codeint a = 10; int *ptr = &a; int **dptr = &ptr;
    • A) It creates a pointer to a pointer to an integer
    • B) It creates a pointer to an integer
    • C) It creates an array of pointers
    • D) It dereferences the pointer twice
  5. How do you access the value of a variable using a double pointer?
    • A) *dptr
    • B) **dptr
    • C) dptr*
    • D) *ptr
  6. What is the benefit of using an array of pointers?
    • A) It allows efficient dynamic memory allocation
    • B) It allows random access to array elements
    • C) It allows storing multiple types of data
    • D) It allows storing multiple pointers to strings
  7. Which of the following is an example of an array of pointers?
    • A) int arr[5];
    • B) int *arr[5];
    • C) int *arr;
    • D) int arr[] = {1, 2, 3, 4};
  8. What is the primary use case for an array of pointers?
    • A) To store multiple types of data
    • B) To store pointers to strings or arrays
    • C) To store integers and floating-point numbers
    • D) To store data in a fixed-size array
  9. What will the following code output?cCopy codeint a = 5; int *p = &a; printf("%d", *p);
    • A) 5
    • B) Address of a
    • C) Undefined behavior
    • D) Error
  10. Which of the following would you use to pass a pointer to a function and modify the pointer value?
    • A) Single pointer
    • B) Double pointer
    • C) Array of pointers
    • D) Function pointer

Answers Table:

QnoAnswer
1C) It allows efficient insertion and deletion at both ends
2C) Data and a pointer to the next node
3B) LIFO (Last In, First Out) access
4D) Dequeue
5D) Both B and C
6A) O(1)
7B) Allocating memory at runtime
8B) Linked Lists
9A) malloc()
10B) To deallocate dynamically allocated memory
11C) realloc()
12B) No effect
13C) Memory that is allocated but never freed
14B) calloc() initializes memory to zero; malloc() does not
15B) Memory leak
16B) A pointer that points to a function
17B) int (*fptr)(int a);
18B) A function that gets passed as an argument to another function
19A) They allow dynamic function calls
20A) func(callback_function);
21A) Functions with a fixed signature
22A) It executes the function to which it points
23A) rand()
24B) Terminates the program with a specified status code
25B) time()
26B) malloc()
27B) Compares two strings
28B) stdlib.h
29C) Converts the time to local time representation
30B) rand() % range
31A) A pointer that points to another pointer
32A) int **ptr;
33C) When passing a pointer to a function and modifying the pointer value
34A) It creates a pointer to a pointer to an integer
35B) **dptr
36B) It allows random access to array elements
37B) int *arr[5];
38B) To store pointers to strings or arrays
39A) 5
40B) Double pointer

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top