Skip to content

University Exam Prep

Revise like the real paper: Section A MCQs, Section B short questions, Section C programming. Then try a timed mock exam.

Section A — MCQs

  1. 1. What is the output of: int x = 5; cout << x++ + ++x;
  2. 2. sizeof(char) is always…
  3. 3. Which access specifier is default for members of a class?
  4. 4. Which operator cannot be overloaded?
  5. 5. A constructor has…
  6. 6. What does 'virtual' enable?
  7. 7. Time complexity of binary search?
  8. 8. Which container gives O(1) average lookup by key?
  9. 9. int arr[5]; the valid indexes are…
  10. 10. What does 'delete[] p' do?
  11. 11. Which is a pure virtual function?
  12. 12. Passing by const reference is used to…
  13. 13. Stack follows which order?
  14. 14. What is the output: cout << 7 / 2;
  15. 15. Which smart pointer allows shared ownership?

Section B — Short questions

Answer in your own words first, then open the model answer.

1. Differentiate between a compiler and an interpreter.

A compiler translates the whole program into machine code before running (C++); an interpreter translates and executes line by line. Compiled code usually runs faster and reports errors before execution.

2. What is the difference between call by value and call by reference?

By value passes a copy, so changes don't affect the caller. By reference (T&) passes an alias, so changes affect the original and no copy is made.

3. Define encapsulation with an example.

Bundling data with the functions that use it and hiding the data (private). Example: BankAccount keeps balance private and exposes deposit()/withdraw() that validate input.

4. What is a destructor? When is it called?

A special member ~ClassName() with no parameters or return type that releases resources. Called automatically when an object goes out of scope or is deleted.

5. Differentiate between function overloading and overriding.

Overloading: same name, different parameters, in the same scope, chosen at compile time. Overriding: a derived class redefines a virtual base function with the same signature, chosen at runtime.

6. What is a dangling pointer?

A pointer that still holds the address of memory that has been freed or gone out of scope. Using it is undefined behaviour; set it to nullptr or use smart pointers.

7. Explain the difference between struct and class in C++.

They are the same except defaults: struct members and inheritance are public by default, class members are private by default.

8. What is recursion? Name its two essential parts.

A function that calls itself. It needs a base case (stops recursion) and a recursive case that moves toward the base case.

9. What is the difference between an array and a vector?

A built-in array has a fixed size known at compile time and doesn't know its length. std::vector grows dynamically, knows its size(), and manages its own memory.

10. What is an abstract class?

A class with at least one pure virtual function (= 0). It cannot be instantiated and is used as an interface for derived classes.

Section C — Programming (past-paper style)

1. Write a program that reads an integer and prints whether it is prime. 5 marks

Marking outline:

  • Handle n < 2 as not prime
  • Loop i from 2 while i * i <= n
  • If n % i == 0 it is not prime
2. Write a function to reverse a string without using std::reverse. 5 marks

Marking outline:

  • Two indexes: i = 0, j = size - 1
  • Swap s[i] and s[j] while i < j
  • Return or print the string
3. Create a class Rectangle with length and width, a constructor, area() and perimeter(). Create two objects and print which is larger. 8 marks

Marking outline:

  • Private members, public constructor with initialiser list
  • const member functions for area and perimeter
  • Compare area() of both objects in main
4. Read n numbers into an array, then print the largest, smallest and average. 8 marks

Marking outline:

  • Store in vector<int>
  • Track min and max in one loop
  • Average as static_cast<double>(sum) / n
5. Implement a stack using an array with push, pop, peek and isEmpty, and handle overflow/underflow. 10 marks

Marking outline:

  • int data[MAX]; int top = -1;
  • push checks top == MAX - 1
  • pop and peek check top == -1
6. Write a base class Shape with a virtual area() and derived classes Circle and Rectangle. Store them in a vector of pointers and print each area. 10 marks

Marking outline:

  • virtual double area() const = 0; and a virtual destructor
  • override in each derived class
  • vector<unique_ptr<Shape>> and a loop calling area()
7. Write a program to print the Fibonacci series up to n terms. 5 marks

Marking outline:

  • a = 0, b = 1
  • Print a, then next = a + b; a = b; b = next
  • Repeat n times
8. Implement bubble sort on an array and count the number of swaps. 8 marks

Marking outline:

  • Nested loops, inner to n - i - 1
  • Swap adjacent out-of-order items and count
  • Optional: stop early if no swaps in a pass

Write and test your answers in the C++ compiler, then take a mock exam.