This will be a full-length practice paper designed to help students prepare for a C++ mid-term
exam, covering a range of topics from basic syntax to OOP concepts. It will include questions
and their corresponding answers. I'll generate both DOCX and PDF versions, with an
anonymous author.
C++ Mid-Term Practice Paper
Instructions:
● Answer all questions.
● Show your work for programming questions where applicable.
● Time: Approximately 90 minutes (adjust as needed)
● Total Marks: 50
Section A: Conceptual Questions (20 Marks)
1. Fill in the Blanks (5 Marks)
a) A ________ is a block of code that performs a specific task and promotes code reusability. b)
In C++, default arguments must be placed from ________ to ________ in the function's
parameter list. c) Function ________ allows multiple functions to have the same name but
different parameter lists. d) An ________ is an instance of a class. e) A ________ is a special
member function automatically called when an object is destroyed.
2. True/False (5 Marks)
a) A function's return type is part of its signature for overloading. (True/False) b) If you define a
parameterized constructor, the compiler will still provide an implicit default constructor.
(True/False) c) private members of a class are accessible from derived classes. (True/False) d)
The dot operator (.) is used to access members of an object. (True/False) e) A destructor can
take arguments. (True/False)
3. Short Answer Questions (10 Marks)
a) Briefly explain the concept of encapsulation in Object-Oriented Programming (OOP). (2
Marks) b) What is the main difference between a function declaration and a function
definition? (2 Marks) c) When would you prefer to use protected access modifier over private?
(2 Marks) d) Explain why a constructor does not have a return type. (2 Marks) e) Describe one
scenario where default arguments would be a better choice than function overloading. (2
Marks)
Section B: Programming Questions (30 Marks)
4. C++ Functions (8 Marks)
Write a C++ program that defines a function isEven(int number) which takes an integer as input
and returns true if the number is even, and false otherwise. In main(), use this function to check
if numbers entered by the user are even or odd until the user enters 0.
5. Classes and Objects (10 Marks)
Design a C++ class named Circle with the following:
● A private data member radius (of type double).
● A public parameterized constructor that initializes the radius.
● A public member function calculateArea() that returns the area of the circle (\\pi \*
radius^2). (Use \\pi = 3.14159)
● A public member function displayRadius() that prints the current radius.
In your main() function: a) Create an object of Circle with a radius of 7.5. b) Call displayRadius()
, for this object. c) Call calculateArea() and print the calculated area.
6. Constructor & Destructor (12 Marks)
Create a C++ class named DynamicArray that dynamically allocates an integer array.
● It should have a private data member int* arr and int size.
● Implement a parameterized constructor that takes an integer s (for size), allocates
memory for s integers, and initializes them to their index value (i.e., arr[i] = i).
● Implement a destructor that properly deallocates the memory pointed to by arr. Print a
message inside the constructor and destructor indicating their execution (e.g.,
"Constructor: Array of size X created." and "Destructor: Array deallocated.").
● Add a public member function displayArray() to print the contents of the array.
In your main() function: a) Create a DynamicArray object arr1 of size 5. b) Call displayArray() for
arr1. c) Create another DynamicArray object arr2 of size 3 within a nested scope (e.g., inside
curly braces {}). d) Observe the order of constructor and destructor calls for arr1 and arr2.
Answer Key
Section A: Conceptual Questions (20 Marks)
1. Fill in the Blanks (5 Marks)
a) A function is a block of code that performs a specific task and promotes code reusability. b)
In C++, default arguments must be placed from right to left in the function's parameter list. c)
Function overloading allows multiple functions to have the same name but different parameter
lists. d) An object is an instance of a class. e) A destructor is a special member function
automatically called when an object is destroyed.
2. True/False (5 Marks)
a) A function's return type is part of its signature for overloading. (False) b) If you define a
parameterized constructor, the compiler will still provide an implicit default constructor. (False)
c) private members of a class are accessible from derived classes. (False) d) The dot operator
(.) is used to access members of an object. (True) e) A destructor can take arguments. (False)
3. Short Answer Questions (10 Marks)
a) Encapsulation in OOP is the bundling of data (attributes) and the methods (functions) that
operate on that data into a single unit, which is typically a class. It also involves restricting direct
access to some of an object's components, meaning that internal representation of an object is
hidden from the outside world. This promotes data hiding and protects data integrity.
b) A function declaration (prototype) tells the compiler about the function's name, return type,
and parameters. It announces that a function with a certain signature exists, allowing the
compiler to check for correct usage. A function definition contains the actual code (body) of the
function that performs the task.
c) You would prefer to use protected over private when you want a class's members to be
accessible to its derived (child) classes while still keeping them inaccessible to the outside
world. private members are completely hidden from derived classes, whereas protected
members allow a controlled form of inheritance for internal implementation details.
d) A constructor does not have a return type (not even void) because its primary job is to
initialize the object itself, not to compute and return a value. The object's creation and
initialization are the "return value" implicitly. The compiler knows what a constructor does and
expects no explicit return.
e) Default arguments would be a better choice than function overloading when you have a
function that performs essentially the same operation but some parameters have common,
frequently used values. Instead of writing multiple overloaded functions for each combination,
you can provide default values for those optional parameters in a single function definition,