C++ Practice Test (Topic 1 – 4) 25 Mar 2025
Section 1: Theoretical Questions (Multiple Choice and Short Answer)
1. Which of the following best describes the Object-Oriented Programming (OOP)
paradigm?
A) Focuses on functions and procedures to operate on data
B) Organizes code around objects that contain both data and methods
C) Treats functions as first-class citizens
D) Uses rules based on logical reasoning and inference
2. Which of the following is NOT a feature of object-oriented programming?
A) Inheritance
B) Polymorphism
C) Encapsulation
D) Recursion
3. Which programming paradigm does C++ primarily support?
A) Functional Programming
B) Object-Oriented Programming (OOP)
C) Procedural Programming
D) All of the above
4. In functional programming, what is typically emphasized over the use of variables
and loops?
A) Functions that take and return other functions
B) Classes and objects
C) Global variables
D) Iteration and mutation of state
5. Which of the following C++ data types is used to store a decimal number?
A) int
B) char
C) float
D) double
6. Which operator is used to get the memory address of a variable in C++?
A) *
B) &
C) #
D) @
7. What is the purpose of the #include directive in C++?
A) To declare functions
B) To define constants
C) To include external libraries or header files
D) To start a new line of code
8. What will the following C++ code output?
#include <iostream>
using namespace std;
int main() {
int a = 10, b = 20;
cout << a + b << endl;
return 0;
}
A) 10
B) 20
C) 30
D) Error
9. Which control structure would you use to repeat a block of code a certain number
of times?
A) if
B) while
C) for
D) switch
10. What is the output of the following code snippet?
#include <iostream>
using namespace std;
int main() {
int i = 0;
while (i < 5) {
cout << i << " ";
i++;
}
return 0;
}
A) 0 1 2 3 4
B) 1 2 3 4 5
C) 0 1 2 3 4 5
D) Error
11. Which of the following is true about the switch statement in C++?
A) It can only be used with integers.
B) It compares an expression to multiple possible values.
C) It automatically terminates when it encounters a break statement.
D) Both B and C
12. Which of the following is true about functions in C++?
A) Functions must always return a value.
B) A function can return multiple values if passed by reference.
C) A function must be declared before it is called.
D) Functions are only used in Object-Oriented Programming.
13. In C++, the void keyword is used for:
A) Functions that return no value
B) Variables that do not store a value
C) The main function to indicate no return value
D) Functions that take no parameters
14. What is the purpose of the return statement in a function?
A) To stop the function from executing
B) To call another function
C) To return a value from the function to the caller
D) To pass parameters to the function
Section 2: Practical Questions (Coding)
1. Write a C++ program that declares an integer variable, assigns it a value of 50, and
prints it to the console.
2. Write a C++ program to input two integers from the user, add them together, and
display the result.
3. Write a C++ program that uses a for loop to print the numbers from 1 to 10.
4. Write a C++ program that uses a while loop to find the sum of all even numbers from
1 to 50.
5. Write a C++ program that uses a switch statement to display the name of the day for
a given number (1 for Monday, 2 for Tuesday, etc.).
6. Write a C++ function isPrime that takes an integer and returns true if the number is
prime, and false otherwise.
7. Write a C++ program that takes two numbers from the user and returns the larger of
the two using a function.