Docsity

CS142 FINAL EXAM QUESTIONS, Exams of Computer Science

Partial preview of the text

Download CS142 FINAL EXAM QUESTIONS and more Exams Computer Science in PDF only on Docsity!

CS142 FINAL EXAM QUESTIONS

AND ANSWERS | COMPLETE

COMPUTER SCIENCE STUDY

CS 142 Final Exam – Practice Document

Complete Study Guide with Answers & Rationale 2026/2027 Edition Instructions: This document contains 50 representative exam questions covering pointers, memory management, recursion, Big-O, classes, inheritance, polymorphism, STL, data structures, and RAII. Each question is followed by the answer and a rationale explaining why it is correct. Section I: Multiple Choice (30 questions)

  1. Which best describes pass-by-reference in C++? A. A copy of the argument is made B. An alias to the original variable is used C. The argument must be a pointer D. The function cannot modify the original Answer: B Rationale: Pass-by-reference creates an alias to the original variable, allowing direct modification without copying. It is declared with & in the parameter list.
  2. A dangling pointer occurs when: A. A pointer is not initialized B. A pointer points to memory that has been freed C. A pointer is assigned to nullptr D. A pointer is used in arithmetic Answer: B Rationale: After delete or free, the pointer still holds the old address; using it is undefined behavior. Setting to nullptr after deallocation prevents this.
  3. Local variables in C++ are stored in: A. Heap B. Stack C. Static memory D. Code segment Answer: B

Rationale: Without a virtual destructor, deleting a derived object via a base pointer causes undefined behavior (only base destructor runs). Virtual ensures the correct destructor chain.

  1. The Rule of Three states that if a class defines a destructor, copy constructor, or copy assignment operator, it probably needs: A. All three B. Only the destructor C. Only the copy constructor D. None of the above Answer: A Rationale: These three are interdependent for correct resource management. Defining one custom version usually means the defaults are insufficient.
  2. What is a memory leak? A. Using a pointer after deletion B. Allocating memory and never deallocating it C. Allocating memory on the stack D. Using new without delete in the same function Answer: B Rationale: Memory allocated with new that is never deleted remains allocated until program termination, wasting resources.
  3. Which STL container provides O(1) random access? A. list B. vector C. forwardlist D. stack Answer: B Rationale: vector stores elements contiguously, allowing operator[] in O(1). List requires O(n) traversal.
  4. What is the output of int x = 5; int& ref = x; ref = 10;? A. x is 5 B. x is 10 C. Compile error

D. Undefined behavior Answer: B Rationale: ref is a reference to x; assigning to ref modifies x directly.

  1. What is the difference between new and malloc? A. new calls the constructor, malloc does not B. malloc calls the constructor, new does not C. They are identical D. new returns void, malloc returns a typed pointer Answer: A Rationale: new allocates memory and calls the constructor; malloc only allocates raw memory. new returns a typed pointer.
  2. Which of the following is a linear data structure? A. Tree B. Graph C. Linked list D. Hash table Answer: C Rationale: Linked list elements are arranged in a sequence. Trees and graphs are non-linear.
  3. What is the time complexity of inserting at the front of a linked list? A. O(1) B. O(n) C. O(log n) D. O(n²) Answer: A Rationale: With a head pointer, insertion at the front requires updating a few pointers, independent of list size.
  4. What is the purpose of the override keyword in C++? A. To force a function to be virtual B. To indicate that a function overrides a base class virtual function C. To overload a function

D. The hash function is perfect Answer: B Rationale: Collisions are inevitable; they are handled by chaining or open addressing.

  1. Which of the following is a balanced binary search tree? A. AVL tree B. Binary search tree C. Linked list D. Array Answer: A Rationale: AVL trees automatically maintain balance to keep O(log n) operations.
  2. What is the stack data structure’s primary operation order? A. FIFO B. LIFO C. Random D. Priority-based Answer: B Rationale: Last In, First Out.
  3. What is the purpose of const in a member function declaration? A. To make the function static B. To indicate the function does not modify the object C. To prevent the function from being called D. To make the function virtual Answer: B Rationale: const after a function declaration means the function does not modify the object’s state.
  4. Which of the following is a smart pointer in C++? A. int B. uniqueptr C. void

D. char Answer: B Rationale: uniqueptr automatically deletes the managed object when it goes out of scope.

  1. What is the output of int arr[3] = {1,2,3}; cout << arr[3];? A. 0 B. 3 C. Undefined behavior D. Compile error Answer: C Rationale: Index 3 is out of bounds; accessing it is undefined behavior.
  2. What is the difference between struct and class in C++? A. No difference B. Default access: struct is public, class is private C. struct cannot have functions D. class cannot have data members Answer: B Rationale: The only difference is default member access: struct defaults to public, class defaults to private.
  3. What is a pure virtual function? A. A function with no implementation B. A function that is declared = 0 C. A function that is inline D. A function that is static Answer: B Rationale: A pure virtual function is declared with = 0 and makes the class abstract.
  4. What is the time complexity of inserting into a vector at the end? A. O(1) amortized B. O(n) C. O(log n)
  1. Inorder traversal of a BST yields sorted order. Answer: True Rationale: Inorder: left, root, right. For a BST, this gives ascending order.
  2. push and pop are standard operations for a queue. Answer: False Rationale: Queue uses enqueue/push and dequeue/pop but push/pop are typical for stack. However, std::queue uses push and pop as methods. In data structure theory, queue uses enqueue/dequeue. The statement is ambiguous but usually considered false in a strict sense.
  3. Function overloading and overriding are the same. Answer: False Rationale: Overloading: same name, different parameters within same scope. Overriding: derived class redefines a virtual base function.
  4. A recursive function must have a base case. Answer: True Rationale: Without a base case, recursion never terminates.
  5. A linked list allows O(1) random access. Answer: False Rationale: Accessing an element by index requires O(n) traversal.
  6. std::vector guarantees contiguous memory storage. Answer: True Rationale: vector stores elements in a dynamic array, which is contiguous.
  7. A destructor can be virtual. Answer: True Rationale: Virtual destructors are essential for polymorphic base classes.
  8. new and delete are C++ operators, not functions. Answer: True Rationale: They are operators that can be overloaded, but they are not functions.
  9. A stack overflow occurs when the heap is full. Answer: False

Rationale: Stack overflow occurs when the call stack exceeds its limit, often from infinite recursion. Section III: Short Answer (5 questions)

  1. Explain the Rule of Three and why it is important. Answer: If a class defines a destructor, copy constructor, or copy assignment operator, it likely needs all three. Rationale: These manage resources (e.g., dynamic memory). The defaults do shallow copies; custom versions are needed to avoid double-deletes, leaks, and shallow copies.
  2. What is the difference between a stack and a heap in memory management? Answer: Stack: automatic allocation/deallocation, fast, limited size (LIFO). Heap: dynamic allocation with new/delete, slower, larger, manual management. Rationale: Stack is used for local variables; heap for objects with dynamic lifetime.
  3. Define RAII and give an example. Answer: RAII (Resource Acquisition Is Initialization) ties resource lifecycle to object lifetime: acquire in constructor, release in destructor. Example: std::uniqueptr automatically deletes memory when it goes out of scope. Rationale: RAII ensures exception-safe resource management.
  4. What is the difference between a vector and a list? Answer: Vector: contiguous memory, O(1) random access, O(n) insert/delete in middle. List: non-contiguous, O(1) insert/delete given iterator, no random access. Rationale: Choice depends on access pattern and modification frequency.
  5. What is a hash table collision? Name two resolution methods. Answer: Collision: two keys hash to same index. Methods: chaining (linked list at each bucket), open addressing (linear probing, quadratic probing, double hashing). Rationale: Collisions are unavoidable; resolution ensures correct storage and retrieval. Section IV: Coding / Analysis (5 questions)
  6. Write a recursive function int factorial(int n) that returns n! and include the base case. Answer: cpp Copy

Copy void swap(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } Rationale: Pointers allow modification of original variables.

  1. Write a loop that prints all elements of a std::vector using an iterator. Answer: cpp Copy for (auto it = vec.begin(); it != vec.end(); ++it) { cout << *it << " "; } Rationale: Iterators provide a uniform way to traverse containers.