





















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The PrepIQ 17COMPA4 Program Design and Data Structures C Ultimate Exam covers advanced algorithms, graph structures, complexity analysis, optimization techniques, and software design principles.
Typology: Exams
1 / 61
This page cannot be seen from the preview
Don't miss anything!






















































Question 1. Which of the following C statements correctly declares a constant integer named MAX with a value of 100? A) int MAX = 100; B) const int MAX = 100; C) #define MAX 100 D) Both B and C are correct Answer: D Explanation: const int MAX = 100; creates a typed constant, while #define MAX 100 creates a pre-processor macro; both are valid ways to define a constant integer. Question 2. In C, what is the result of the expression (int)5.9 / 2? A) 2 B) 2. C) 3 D) 4 Answer: A Explanation: The cast converts 5.9 to 5, then integer division 5/2 yields 2. Question 3. Which loop guarantees that its body executes at least once? A) for B) while C) do … while D) None of the above Answer: C Explanation: A do … while loop checks its condition after executing the body, ensuring one execution.
Question 4. What is the output of the following code snippet?
int a = 3, b = 4; printf("%d", a++ + ++b);A) 7 B) 8 C) 9 D) Compilation error Answer: B Explanation: a++ yields 3 (a becomes 4), ++b increments b to 5 and yields 5; sum = 3+5 = 8. Question 5. Which of the following statements about switch in C is FALSE? A) Cases must be constant integral expressions. B) switch can be used with floating-point values. C) break terminates the current case. D) Multiple cases can share the same block. Answer: B Explanation: switch only works with integral or enumeration types; floating-point is not allowed. Question 6. Which of the following correctly reads an integer from standard input and stores it in variable n? A) scanf("%d", n); B) scanf("%d", &n);
B) if (n == 1) return 0; C) if (n < 0) return -1; D) No base case is needed. Answer: A Explanation: Factorial of 0 is defined as 1; this stops further recursive calls. Question 10. Which statement about recursion depth in C is TRUE? A) It is unlimited. B) It is limited only by CPU speed. C) It is limited by the size of the call stack. D) It is limited by the number of functions defined. Answer: C Explanation: Each recursive call consumes stack space; overflow occurs when the stack limit is reached. Question 11. Which operator yields the memory address of a variable? A) * B) & C) -> D) sizeof Answer: B Explanation: The address-of operator & returns the variable’s memory address. Question 12. Given int *p = NULL;, which statement is safe? A) *p = 5;
B) p++; C) p = malloc(sizeof(int)); D) printf("%d", *p); Answer: C Explanation: Allocating memory with malloc gives p a valid address; the others dereference a NULL pointer. Question 13. If int arr[5] = {10,20,30,40,50};, what is the value of *(arr + 3)? A) 10 B) 20 C) 30 D) 40 Answer: D Explanation: Pointer arithmetic adds 3 elements; the fourth element (arr[3]) is 40. Question 14. Which of the following statements about malloc is FALSE? A) It returns a void pointer that can be cast. B) It initializes allocated memory to zero. C) It may return NULL if allocation fails. D) The size argument is specified in bytes. Answer: B Explanation: malloc does not zero-initialize memory; calloc does. Question 15. After executing int *p = malloc(10 * sizeof(int));, which statement correctly releases the memory?
Question 18. Which pointer is used to traverse a singly linked list without losing the head reference? A) head B) temp C) prev D) tail Answer: B Explanation: A temporary pointer (temp) moves through nodes while head remains unchanged. Question 19. In a doubly linked list, which pointer is NOT required in each node? A) next B) prev C) data D) size Answer: D Explanation: Nodes store data and two links; size is a property of the list, not each node. Question 20. Which of the following correctly creates a circular linked list of three nodes? A) head->next = head; B) head->next = second; second->next = third; third->next = head; C) head->prev = third; D) All of the above Answer: B Explanation: Linking the last node back to head makes the list circular.
Question 21. Which stack implementation method provides O(1) push and pop without overflow until memory is exhausted? A) Fixed-size array B) Linked list C) Both A and B D) None of the above Answer: B Explanation: A linked-list stack grows dynamically; an array stack can overflow when its capacity is reached. Question 22. In infix to postfix conversion, which symbol has the highest precedence? A) + B) - C) * D) ( Answer: C Explanation: Multiplication (*) and division have higher precedence than addition/subtraction; parentheses are not operators but control grouping. Question 23. Which of the following correctly evaluates the postfix expression 6 2 / 3 *? A) 9 B) 12 C) 4 D) 0
D) Level order Answer: B Explanation: Inorder traversal follows left-root-right order. Question 27. In a binary search tree, where is a newly inserted node with key 15 placed if the root key is 20 and its left child key is 10? A) As left child of 10 B) As right child of 10 C) As left child of 20 D) As right child of 20 Answer: B Explanation: 15 > 10, so it becomes the right child of node 10. Question 28. Which of the following statements about AVL trees is TRUE? A) All leaf nodes are at the same depth. B) Balance factor of each node is -1, 0, or +1. C) Rotations are never needed after insertion. D) AVL trees are a type of B-tree. Answer: B Explanation: AVL trees maintain balance factors in the set {-1,0,+1}. Question 29. In a B-tree of order 4, what is the maximum number of keys a non-root node can contain? A) 2 B) 3 C) 4
Answer: B Explanation: For order m, a node can have at most m-1 keys; with m=4, max keys = 3. Question 30. Which graph representation is most space-efficient for a sparse graph? A) Adjacency matrix B) Adjacency list C) Edge list array D) Incidence matrix Answer: B Explanation: An adjacency list stores only existing edges, using O(V+E) space. Question 31. In BFS, which data structure is used to keep track of the next vertices to explore? A) Stack B) Queue C) Priority queue D) Linked list Answer: B Explanation: BFS explores vertices level by level, requiring a FIFO queue. Question 32. Which of the following statements about DFS is FALSE? A) It can be implemented recursively. B) It uses a stack (explicit or implicit).
A) Queue B) Stack C) Disjoint-set (Union-Find) D) Binary Search Tree Answer: C Explanation: Union-Find efficiently tracks which vertices belong to the same component, preventing cycles while selecting the next smallest edge. Question 36. Which of the following C statements correctly swaps two integers a and b using pointers? A) int *p=&a, *q=&b; *p=*q; *q=*p; B) int *p=&a, *q=&b; int temp=*p; *p=*q; *q=temp; C) int *p=&a, *q=&b; p=q; q=p; D) int *p=&a, *q=&b; *p=*p+*q; *q=*p-*q; *p=*p-*q; Answer: B Explanation: The classic three-step swap uses a temporary variable to hold one value while exchanging the contents via dereferencing. Question 37. What is the time complexity of inserting a new node at the head of a singly linked list? A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: A Explanation: Insertion at the front only requires updating a couple of pointers, independent of list length.
Question 38. Which of the following statements about a circular queue implemented with an array of size N is TRUE when the queue becomes full? A) front == rear B) front == (rear + 1) % N C) rear == (front + N - 1) % N D) Both B and C Answer: D Explanation: In a circular array queue, the condition front == (rear + 1) % N (or equivalently rear == (front + N - 1) % N) indicates the queue is full. Question 39. In a doubly linked list, which operation is required to delete a node p given only a pointer to p? A) Update p->prev->next and p->next->prev B) Update only p->next->prev C) Update only p->prev->next D) No updates are needed Answer: A Explanation: To remove p, the previous node’s next must point to p->next, and the next node’s prev must point to p->prev. Question 40. Which of the following correctly declares a C structure for a complex number with real and imaginary parts as doubles? A) struct complex { double real, imag; }; B) typedef struct { double real; double imag; } complex; C) struct complex { double real; double imag; } complex; D) All of the above
C) strlcpy(dest, src, n); D) memcpy(dest, src, n); Answer: C Explanation: strlcpy (available on many BSD systems) always null-terminates the destination. strncpy does not guarantee null-termination if the source length ≥ n. Question 44. Which of the following correctly reverses a null-terminated string s in place? A) Use two indices i=0, j=strlen(s)-1 and swap s[i] with s[j] while `i
Question 46. Which of the following statements about binary search is FALSE? A) The array must be sorted in non-decreasing order. B) It has O(log n) worst-case time complexity. C) It can be applied to a linked list without extra space. D) It can be implemented recursively. Answer: C Explanation: Binary search requires random access (array indexing). Linked lists lack O(1) middle element access, making binary search inefficient. Question 47. In the Quick Sort algorithm, which pivot selection strategy guarantees O(n log n) worst-case time? A) First element as pivot B) Random element as pivot C) Median-of-three (first, middle, last) D) No strategy can guarantee O(n log n) worst case Answer: D Explanation: Quick Sort’s worst case is O(n²) regardless of deterministic pivot choices; only randomized or hybrid approaches improve expected performance but cannot guarantee O(n log n) worst case. Question 48. Which sorting algorithm is stable and has a worst-case time complexity of O(n log n)? A) Heap Sort B) Merge Sort C) Quick Sort D) Selection Sort Answer: B
Answer: D Explanation: Separate chaining stores all colliding keys in a linked list (or another container) at each bucket. Question 52. If a hash table uses open addressing with linear probing and the load factor exceeds 0.7, what is the most appropriate action? A) Increase the table size and rehash all entries. B) Switch to separate chaining. C) Decrease the load factor threshold to 0.5. D) No action; performance remains unchanged. Answer: A Explanation: Open addressing degrades sharply as load factor grows; resizing and rehashing restores O(1) expected performance. Question 53. Which of the following is a correct prototype for a function that returns a pointer to an array of 10 integers? A) int (*func())[10]; B) int *func[10](); C) int func()[10]; D) int (*func)[10]; Answer: A Explanation: int (*func())[10]; declares a function returning a pointer to an array of 10 int. Option D declares a pointer to an array, not a function. Question 54. In C, what does the sizeof operator yield when applied to a pointer variable p? A) Size of the object pointed to by p.
B) Size of the pointer itself (typically 4 or 8 bytes). C) Number of elements the pointer can address. D) Undefined behavior. Answer: B Explanation: sizeof(p) returns the size of the pointer type, not the pointed-to object. Question 55. Which of the following statements about the static storage class inside a function is correct? A) The variable retains its value between function calls. B) The variable is visible to other translation units. C) The variable is allocated on the heap. D) The variable cannot be initialized. Answer: A Explanation: static local variables have static (program-lifetime) storage but block scope, preserving their value across invocations. Question 56. Which of the following correctly opens a binary file named data.bin for reading and writing without truncating it? A) fopen("data.bin","rb+"); B) fopen("data.bin","wb+"); C) fopen("data.bin","ab+"); D) fopen("data.bin","r+"); Answer: A Explanation: Mode "rb+" opens an existing binary file for update (read/write) without truncation. "wb+" would truncate, "ab+" appends, "r+" is text mode.