












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













































Question 1. Which operator yields the memory address of a variable in C? A) * B) & C) % D) # Answer: B Explanation: The address-of operator & returns the memory address where a variable is stored. Question 2. What does the expression *(ptr + 3) evaluate to? A) The address of the fourth element after ptr B) The value stored at the fourth element after ptr C) The value of ptr plus 3 D) A compile-time error Answer: B Explanation: Pointer arithmetic moves the pointer by 3 elements; dereferencing * yields the value at that location. Question 3. Which function allocates memory for an array of n elements, each of size size, and initializes all bytes to zero? A) malloc B) calloc C) realloc D) free Answer: B Explanation: calloc(n, size) allocates and zero-initializes the requested memory. Question 4. After calling free(p), what must you NOT do with pointer p?
A) Assign it a new address with malloc B) Compare it to NULL C) Use it to access memory D) Pass it to realloc Answer: C Explanation: Once freed, the memory is invalid; dereferencing p results in undefined behavior. Question 5. In a struct node { int data; struct node *next; };, how do you access the data field of a pointer head? A) head.data B) (*head).data C) head->data D) Both B and C are correct Answer: D Explanation: (*head).data and head->data are equivalent; the arrow operator is syntactic sugar for dereferencing then accessing. Question 6. Which of the following statements about recursion is FALSE? A) Each recursive call creates a new activation record on the call stack. B) Tail recursion can be optimized into iteration by most compilers. C) A recursive function must always have a base case. D) Recursive algorithms always use more memory than their iterative counterparts. Answer: D Explanation: While recursion often uses more stack space, it is not guaranteed; tail-call optimization can eliminate extra memory. Question 7. The time complexity of malloc is generally considered to be:
Question 10. The Master Theorem applies to recurrences of the form T(n) = a T(n/b) + f(n). What does the parameter a represent? A) Number of sub-problems created at each level B) Size reduction factor of each sub-problem C) The non-recursive work at each level D) The height of the recursion tree Answer: A Explanation: a is the number of recursive calls (sub-problems) generated from each problem instance. Question 11. Which of the following is a characteristic of a complete binary tree? A) All levels are completely filled except possibly the last, which is filled from left to right. B) Every node has either 0 or 2 children. C) All leaf nodes are at the same depth. D) The number of nodes equals 2^h – 1, where h is height. Answer: A Explanation: A complete binary tree is filled level-by-level, left-to-right, with the last level possibly incomplete. Question 12. In an array implementation of a stack, which operation can cause a stack overflow? A) push when the array is full B) pop when the array is empty C) peek on a non-empty stack D) size query Answer: A Explanation: Pushing onto a full array exceeds its capacity, leading to overflow.
Question 13. Which traversal of a binary search tree yields the keys in ascending order? A) Pre-order B) In-order C) Post-order D) Level-order Answer: B Explanation: In-order traversal visits left subtree, node, then right subtree, producing sorted order for BSTs. Question 14. In a doubly linked list, which pointer must be updated when deleting a node p that is not the head or tail? A) p->next->prev only B) p->prev->next only C) Both p->prev->next and p->next->prev D) No pointers need updating; free(p) is enough Answer: C Explanation: Both neighboring nodes must bypass p by adjusting their next and prev links. Question 15. The time complexity of inserting an element into a max-heap of size n is: A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: B
Answer: C Explanation: A min-heap lets us extract the smallest weight edge (or vertex key) in O(log V) time. Question 19. Which sorting algorithm has the best worst-case time complexity? A) Bubble sort B) Quick sort C) Merge sort D) Insertion sort Answer: C Explanation: Merge sort guarantees O(n log n) worst-case performance, unlike quick sort which can degrade to O(n²). Question 20. Counting sort is NOT suitable for sorting which type of data? A) Integers in a small range B) Strings of equal length C) Floating-point numbers with large range and high precision D) Characters (ASCII) Answer: C Explanation: Counting sort relies on a bounded integer range; large-range floating values would require impractically large count arrays. Question 21. Which hash collision-resolution technique stores all colliding keys in a linked list attached to the same bucket? A) Linear probing B) Quadratic probing C) Double hashing D) Chaining
Answer: D Explanation: Chaining uses a list (or another structure) per bucket to hold multiple keys that hash to the same index. Question 22. The KMP string-matching algorithm achieves O(n + m) time by: A) Pre-computing a hash of the pattern B) Using a suffix array C) Building a longest-prefix-suffix (LPS) table for the pattern D) Recursively dividing the text Answer: C Explanation: KMP’s LPS table allows the algorithm to skip unnecessary comparisons, yielding linear time. Question 23. Which of the following statements about AVL trees is FALSE? A) The height difference between left and right sub-trees of any node is at most 1. B) Rotations are used to restore balance after insertions and deletions. C) AVL trees guarantee O(log n) search time. D) An AVL tree can become completely unbalanced after a series of deletions without any rotations. Answer: D Explanation: AVL trees perform rotations on deletions as needed, preserving the balance property. Question 24. In a B-tree of order m, what is the minimum number of keys a non-root node must contain? A) ⌈m/2⌉ – 1 B) m – 1 C) ⌈m/2⌉
C) float func(int); D) float (func)(int); Answer: B Explanation: float (*func)(int) declares func as a pointer to a function with signature float f(int). Question 28. When using realloc(ptr, newSize), which of the following is guaranteed? A) The original data is always preserved. B) The returned pointer is always different from ptr. C) If newSize is zero, the memory is freed and NULL is returned. D) The operation cannot fail. Answer: C Explanation: According to the C standard, realloc(p,0) behaves like free(p) and may return NULL. Question 29. In a circular queue implemented with an array of size N, which condition indicates that the queue is full? A) front == rear B) (rear + 1) % N == front C) front == (rear + 1) % N D) rear == (front + N) % N Answer: B Explanation: One slot is left empty to differentiate full from empty; when next position of rear equals front, the queue is full. Question 30. Which of the following statements about the sizeof operator in C is FALSE? A) sizeof yields the size in bytes of the operand’s type.
B) sizeof can be applied to variable-length array (VLA) expressions at runtime. C) sizeof is evaluated at compile time for non-VLA types. D) sizeof can be used on function pointers to obtain the size of the function code. Answer: D Explanation: Functions do not have a size that sizeof can determine; applying sizeof to a function name is illegal. Question 31. Which of the following best describes a “memory leak”? A) Accessing memory after it has been freed. B) Allocating memory without ever freeing it. C) Writing outside the bounds of an allocated block. D) Using uninitialized pointers. Answer: B Explanation: A leak occurs when allocated heap memory is lost (no pointer references it) and never released. Question 32. In a hash table using open addressing with linear probing, what is the primary cause of “primary clustering”? A) All keys hashing to the same bucket. B) Collisions causing long consecutive runs of occupied slots. C) Use of a poor hash function that returns only even numbers. D) Deleting entries without rehashing. Answer: B Explanation: Linear probing fills the next free slot, producing clusters that grow and degrade performance. Question 33. Which of the following is a property of a min-heap stored in an array A[0…n-1]? A) For any index i, A[i] ≤ A[2i+1] and A[i] ≤ A[2i+2] (if children exist).
A) The middle element of the array. B) The median value among the first, middle, and last elements. C) The element with median frequency in the array. D) A random element. Answer: B Explanation: Median-of-three reduces the chance of worst-case partitions by using the median of three sampled elements. Question 37. Which of the following statements about the static storage class in C is FALSE? A) A static variable inside a function retains its value between calls. B) A static global variable is visible only within its translation unit. C) static can be applied to function parameters. D) Static variables are allocated in the data segment. Answer: C Explanation: static is not valid on function parameters; it can be applied to variables and functions only. Question 38. In a directed acyclic graph (DAG), a topological sort can be obtained by: A) Performing BFS and outputting vertices in order of discovery. B) Performing DFS and outputting vertices in reverse order of finishing times. C) Sorting vertices by their out-degree. D) Applying Dijkstra’s algorithm. Answer: B Explanation: DFS finishing times give a reverse post-order that yields a valid topological ordering for a DAG.
Question 39. Which of the following best describes the “worst-case” time complexity of binary search on a sorted array of size n? A) O(1) B) O(log n) C) O(n) D) O(n log n) Answer: B Explanation: Binary search halves the search interval each step, leading to logarithmic worst-case time. Question 40. In a union type union U { int i; float f; char c; };, what is the size of U? A) Sum of sizes of all members. B) Size of the largest member, possibly padded. C) Size of the first member only. D) Undefined. Answer: B Explanation: A union occupies enough memory to hold its largest member; additional padding may be added for alignment. Question 41. Which of the following is true about the space complexity of merge sort? A) It is O(1) because it sorts in place. B) It is O(log n) due to recursion depth only. C) It is O(n) because of the auxiliary array used for merging. D) It is O(n²) because of repeated copying. Answer: C Explanation: Merge sort requires an auxiliary array of size n for merging, leading to linear extra space.
Question 45. Which of the following statements about recursion depth in C is FALSE? A) The maximum recursion depth is limited only by available stack space. B) Tail recursion is guaranteed to be optimized by the C standard. C) Deep recursion can cause a stack overflow. D) Each recursive call consumes at least one stack frame. Answer: B Explanation: The C standard does not require tail-call optimization; it is implementation-dependent. Question 46. In a graph with V vertices, the adjacency matrix representation uses how much memory? A) O(V) B) O(E) C) O(V + E) D) O(V²) Answer: D Explanation: An adjacency matrix stores a V × V boolean or weight entry for every possible vertex pair. Question 47. Which of the following sorting algorithms is stable by default? A) Quick sort B) Heap sort C) Merge sort D) Selection sort Answer: C Explanation: Merge sort maintains the relative order of equal keys during the merge step, making it stable.
Question 48. In a hash table that uses chaining, what is the average search time when the load factor α = n/m (n keys, m buckets) is O(1)? A) α B) 1 + α/ C) O(α) D) O(1) only if α is constant and small Answer: D Explanation: Expected search time is O(1 + α); when α remains bounded (constant), the complexity is O(1). Question 49. The time complexity of the Floyd-Warshall algorithm for all-pairs shortest paths on a graph with V vertices is: A) O(V log V) B) O(V²) C) O(V³) D) O(E + V log V) Answer: C Explanation: Floyd-Warshall uses three nested loops over vertices, giving Θ(V³) time. Question 50. Which of the following is NOT a characteristic of a “full” binary tree? A) Every node has either 0 or 2 children. B) All leaves are at the same depth. C) The number of nodes equals 2^h – 1, where h is height. D) It may have some missing nodes on the last level. Answer: D
Answer: D Explanation: Leaves (NIL nodes) are considered black in a red-black tree. Question 54. Which of the following statements about the goto statement in C is TRUE? A) It can be used to jump out of a function. B) It can only jump forward within the same block. C) It can jump to any label within the same function, regardless of block scope. D) Its use is recommended for implementing recursion. Answer: C Explanation: goto may transfer control to any label in the same function, even across block boundaries. Question 55. In the context of algorithm analysis, which term describes the number of times an algorithm’s basic operation is executed as a function of input size n? A) Space complexity B) Time complexity C) Recurrence relation D) Asymptotic bound Answer: B Explanation: Time complexity quantifies the count of elementary steps relative to input size. Question 56. Which of the following is a correct way to declare a two-dimensional array of integers with 3 rows and 5 columns? A) int arr[3][5]; B) int arr[5][3]; C) int arr[3*5];
D) int arr[3][5][1]; Answer: A Explanation: int arr[3][5]; creates a matrix with 3 rows and 5 columns. Question 57. Which traversal order of a binary tree visits nodes in the sequence: root, left subtree, right subtree? A) In-order B) Pre-order C) Post-order D) Level-order Answer: B Explanation: Pre-order traversal processes the current node before its subtrees. Question 58. In a circular singly linked list, which condition indicates that the list contains only one node? A) head == NULL B) head->next == NULL C) head->next == head D) head->prev == head Answer: C Explanation: In a circular list, a single node points to itself. Question 59. Which of the following is true about the calloc function compared to malloc? A) calloc can only allocate memory for arrays of char. B) calloc does not initialize the allocated memory. C) calloc takes two arguments: number of elements and size of each element. D) calloc always returns a pointer to the middle of the allocated block.