C++ Programming MCQs

Q.1 Identify the error:
int x = 10;
int y = ++x + 2;
cout << x << ” ” << y;

A. Incorrect use of increment operator
B. No error
C. Syntax error in cout statement
D. Misuse of assignment operator

Shw me The Right Answer

Answer . B 

Q.2 Find the mistake:
int num;
num == 5;
cout << num;

A. Misuse of equality operator
B. Undeclared variable
C. No error
D. Syntax error in cout statement

Shw me The Right Answer

Answer . A 

Q.3 Pseudocode:
IF (5 > 3) OR (8 < 6) THEN PRINT “True” ELSE PRINT “False” What is the output?

A. True
B. False
C. Error
D. None

Shw me The Right Answer

Answer . A 

Q.4 Pseudocode:
SET num TO 15 SET result TO num DIV 2 PRINT result What does this print?

A. 7.5
B.  7
C. 15
D. Error

Shw me The Right Answer

Answer . B 

Q.5 Pseudocode:
SET val TO 7 IF val < 10 AND val > 5 THEN PRINT “In range” ELSE PRINT “Out of range” What is the output?

A. In range
B. Out of range
C. Error
D. None

Shw me The Right Answer

Answer . A 

Q.6 Pseudocode:
SET x TO 10 SET y TO x MOD 4 PRINT y What does this print?

A. 2
B. 4
C. 10
D. 0

Shw me The Right Answer

Answer . A 

Q.7 Pseudocode:
SET a TO 5 IF a > 3 THEN PRINT “Yes” ELSE PRINT “No” What is the output?

A. Yes
B. No
C. Error
D. None

Shw me The Right Answer

Answer . A 

Q.8 What is the result of int x = 10; x *= 3; in C++?

A. 30
B. 13
C. 33
D. 10

Shw me The Right Answer

Answer . A 

Q.9 What is the output of the following code?
int a = 1;
int b = 2;
cout << (a++ + b);

A. 3
B. 4
C. Syntax error
D. 2

Shw me The Right Answer

Answer . A 

Q.10 What is the result of the expression !true && false in C++?

A. true
B. false
C. Syntax error
D. None of the above

Shw me The Right Answer

Answer . B 

Q.11 In C++, what is the difference between == and =?

A. == is used for assignment, while = is used for comparison
B. == is used for comparison, while = is used for assignment
C. Both are used for assignment
D. Both are used for comparison

Shw me The Right Answer

Answer . B 

Q.12 What does the ++ operator do in C++?

A. Divides the value by two
B. Decreases the value by one
C. Increases the value by one
D. Squares the value

Shw me The Right Answer

Answer . C 

Q.13 Which operator is used for logical AND in C++?

A. &&
B. &
C. ||
D. ==

Shw me The Right Answer

Answer . A 

Q.14 What is the output of the expression 5 + 3 * 2 in C++?

A. 16
B. 11
C. 10
D. 8

Shw me The Right Answer

Answer . B 

Q.15 Spot the error in this C++ code:
const int num; num = 10;
cout << num;

A. Missing variable initialization
B. Incorrect use of const
C. Syntax error in cout statement
D. No error

Shw me The Right Answer

Answer . A 

Q.16 Identify the error in this C++ code:
int main() {
int 123number;
return 0;
}

A. Variable name starting with a digit
B. Incorrect data type
C. Syntax error in return statement
D. No error

Shw me The Right Answer

Answer . A 

Q.17 What is the output of this C++ code?
int main() {
int x = 5;
cout << x;
return 0;
}

A. 0
B. 5
C. x
D. Error

Shw me The Right Answer

Answer . B 

Q.18 What is a constant in C++?

A. A variable whose value can change
B. A fixed value that cannot be altered during execution
C. A type of operator
D. A special function

Shw me The Right Answer

Answer . B 

Q.19 Which data type would be best to store a person’s age?

A. int
B. double
C. bool
D. char

Shw me The Right Answer

Answer . A 

Q.20 What is a variable in C++?

A. A constant value
B. A data type
C. A storage location with a name
D. A keyword

Shw me The Right Answer

Answer . C 

Q.21 Which of these is not a fundamental data type in C++?

A. int
B. float
C. string
D. char

Shw me The Right Answer

Answer . C 

Q.22 What is the role of comments in a C++ program?

A. To optimize code execution
B. To define variables
C. To provide documentation and explanation
D. To declare constants

Shw me The Right Answer

Answer . C 

Q.23 Identify the issue in this C++ code snippet:
class Car {
public:
int speed;
void Car() { speed = 0; }
};

A. Incorrect class constructor syntax
B. Missing data encapsulation
C. No error
D. Inheritance misuse

Shw me The Right Answer

Answer . A 

Q.24 Consider the following code snippet:
class Animal { public: void eat() { cout << “Eating…”; } };
What is demonstrated in this code?

A. Inheritance
B. Encapsulation
C. Class definition
D. Polymorphism

Shw me The Right Answer

Answer . C 

Q.25 Which concept in OOP allows for the same function to be used in different ways based on the object it is associated with?

A. Encapsulation
B. Abstraction
C. Inheritance
D. Polymorphism

Shw me The Right Answer

Answer . D 

Q.26 What distinguishes a class from an object in OOP?

A. A class is a blueprint, while an object is an instance of a class
B. A class cannot contain methods, while an object can
C. Objects are templates, while classes are real entities
D. There is no difference

Shw me The Right Answer

Answer . A 

Q.27 Which principle of OOP is illustrated by defining different methods with the same name but different parameters?

A. Inheritance
B. Polymorphism
C. Encapsulation
D. Abstraction

Shw me The Right Answer

Answer . B 

Q.28 In OOP, what does encapsulation refer to?

A. Storing data in arrays
B. The process of inheritance
C. Combining data and methods
D. A type of loop

Shw me The Right Answer

Answer . C 

Q.29 Which of the following is a feature of procedure-oriented programming?

A. Class-based
B. Focus on functions
C. Inheritance
D. Polymorphism

Shw me The Right Answer

Answer . B 

Q.30 What is an object in object-oriented programming?

A. A code snippet
B. A variable
C. An instance of a class
D. A function

Shw me The Right Answer

Answer . C 

Q.31 What is an array in C++?

A. A collection of variables of different types
B. A single variable that can store multiple values
C. A function that returns multiple values
D. A data structure that allows recursion

Shw me The Right Answer

Answer . B 

Q.32 Identify the mistake in this code:
void printNum(int number = 5) {
cout << number;
}
printNum(10, 5);

A. Incorrect number of arguments
B. No error
C. Syntax error in function definition
D. Error in default argument

Shw me The Right Answer

Answer . A 

Q.33 Find the error in this function:
int square(int n) {
return n * n;
}
cout << square();

A. Missing argument in function call
B. Syntax error in return statement
C. No return type for function
D. No error

Shw me The Right Answer

Answer . A 

Q.34 Pseudocode:
FUNCTION CalculateFactorial(n) IF n <= 1 THEN RETURN 1 ELSE RETURN n * CalculateFactorial(n – 1) END What does this function calculate?

A. The sum of numbers up to n
B. The factorial of n
C. The product of n and n-1
D. The nth number in the Fibonacci sequence

Shw me The Right Answer

Answer . B 

Q.35 Pseudocode:
FUNCTION FindMax(x, y) IF x > y THEN RETURN x ELSE RETURN y END What does this function return?

A. The sum of x and y
B. The smaller of x and y
C. The larger of x and y
D. Nothing if x equals y

Shw me The Right Answer

Answer . C 

Q.36 Pseudocode:
FUNCTION PrintMessage() PRINT “Hello” END What is the purpose of this function?

A. To return the string “Hello”
B. To print “Hello” to the console
C. To store the message “Hello”
D. To check if “Hello” is printed

Shw me The Right Answer

Answer . B 

Q.37 Pseudocode:
FUNCTION AddNumbers(x, y) RETURN x + y END What does this function do?

A. Adds two numbers and prints the result
B. Subtracts two numbers
C. Adds two numbers and returns the result
D. Multiplies two numbers

Shw me The Right Answer

Answer . C 

Q.38 Consider the function:
int multiply(int x, int y) { return x * y; }
What is returned by multiply(5, 4);?

A. 20
B. 9
C. Syntax error
D. 0

Shw me The Right Answer

Answer . A 

Q.39 What is the output of this function call?
int add(int x, int y = 5) {
return x + y;
} cout << add(3);

A. 8
B. 3
C. 5
D. Syntax error

Shw me The Right Answer

Answer . A 

Q.40 What does this function do?
void printHello() {
cout << “Hello, World!”;
}

A. Prints “Hello, World!” to the console repeatedly
B. Prints “Hello, World!” once to the console
C. Does nothing
D. Causes a runtime error

Shw me The Right Answer

Answer . B 

Q.41 How does a recursive function in C++ stop calling itself?

A. By using a for loop
B. By reaching a return statement
C. Through a conditional statement that leads to a return
D. By calling another function

Shw me The Right Answer

Answer . C 

Q.42 In C++, what is recursion?

A. A function calling another function
B. A function that calls itself
C. Repeatedly executing a loop
D. A type of data structure

Shw me The Right Answer

Answer . B 

Q.43 What is the purpose of default arguments in function definitions?

A. To specify what value a function returns
B. To give a default value to a parameter if no argument is passed
C. To make a parameter optional
D. To overload a function

Shw me The Right Answer

Answer . B 

Q.44 What is function overloading in C++?

A. Calling a function multiple times
B. Defining multiple functions with the same name but different parameters
C. Defining a function inside another function
D. Using default arguments in a function

Shw me The Right Answer

Answer . B 

Q.45 What is a function prototype in C++?

A. A function that always returns an integer
B. The first call to a function in a program
C. A declaration of a function before its definition
D. A function without arguments

Shw me The Right Answer

Answer . C 

Q.46 Spot the mistake:
int num = 0;
do {
cout << num;
} while(num);

A. Infinite loop
B. Syntax error
C. Should use a for loop
D. No error

Shw me The Right Answer

Answer . D 

Q.47 Identify the error in the code:
int i = 1;
for(; i <= 5; i++) {
cout << i;
}

A. Missing initialization in for loop
B. No error
C. Syntax error in cout statement
D. Incorrect loop condition

Shw me The Right Answer

Answer . B 

Q.48 Pseudocode:
SET count TO 0 REPEAT PRINT count INCREMENT count UNTIL count EQUALS 5 What is the output?

A. 01234
B. 012345
C. Infinite loop
D. Error

Shw me The Right Answer

Answer . A 

Q.49 Pseudocode:
FOR i FROM 0 TO 2 DO PRINT i What is the output?

A. 012
B. 123
C. Error
D. Nothing

Shw me The Right Answer

Answer . A 

Q.50 Pseudocode:
SET i TO 0 WHILE i < 3 DO PRINT i INCREMENT i What does this print?

A. 012
B. 123
C. Infinite loop
D. Nothing

Shw me The Right Answer

Answer . A 

Q.51 Pseudocode:
IF x > 10 THEN PRINT “Greater” ELSE PRINT “Smaller or Equal” If x is 8, what is printed?

A. Greater
B. Smaller or Equal
C. Error
D. Nothing

Shw me The Right Answer

Answer . B 

Q.52 What is the output of the following code?
int x = 10;
do {
cout << x;
x–; } while (x > 10);

A. Prints 10 once
B. Prints 10 and then 9
C. Prints numbers from 10 to 1
D. No output

Shw me The Right Answer

Answer . A 

Q.53 Consider the code:
for(int i = 0; i <= 2; i++) {
cout << i;
}.
What does it print?

A. 012
B. 123
C. Nothing
D. Error

Shw me The Right Answer

Answer . A 

Q.54 What is the output of this code?
int i = 0;
while(i < 3) {
cout << i; i++;
}

A. 012
B. 123
C. Infinite loop
D. Syntax error

Shw me The Right Answer

Answer . A 

Q.55 What happens if the condition in an if statement is false?

A. The program terminates
B. The else part is executed
C. The program skips to the next loop
D. None of the above

Shw me The Right Answer

Answer . B 

Q.56 What is the primary difference between a for loop and a while loop in C++?

A. Syntax only
B. While loop can run infinitely, for loop cannot
C. For loop is used for arrays, while loop is not
D. Initialization, condition checking, and increment/decrement are part of the for loop statement

Shw me The Right Answer

Answer . B 

Q.57 In C++, the do-while loop is unique because it…

A. executes its block of code at least once
B. is faster than other loops
C. does not check a condition
D. can only run a fixed number of times

Shw me The Right Answer

Answer . A 

Q.58 What is the function of the break statement in a loop?

A. To pause the loop
B. To skip an iteration of the loop
C. To terminate the loop
D. To continue to the next iteration of the loop

Shw me The Right Answer

Answer . C 

Q.59 Which control flow statement is used for executing a block of code repeatedly based on a condition?

A. if
B. else
C. for
D. switch

Shw me The Right Answer

Answer . C 

Q.60 What is the purpose of the if statement in C++?

A. To execute a block of code multiple times
B. To stop the execution of the program
C. To make a decision
D. To declare a variable

Shw me The Right Answer

Answer . C 

Q.61 What does encapsulation in C++ refer to?

A. The process of combining data and functions into a single unit
B. Creating multiple copies of the same function
C. Protecting data by making it private
D. Linking different classes together

Shw me The Right Answer

Answer . A 

Q.62 What is inheritance in OOP?

A. The process of acquiring properties from one class to another
B. Copying of methods between classes
C. Linking of two classes
D. Creating new classes

Shw me The Right Answer

Answer . A 

Q.63 What is a class in C++?

A. A blueprint for creating objects
B. A function in OOP
C. A variable type
D. An operator

Shw me The Right Answer

Answer . A 

Q.64 Find the mistake:
int num = 10;
int &ref = num;
ref = nullptr;
cout << num;

A. Assigning nullptr to a reference
B. Syntax error
C. No error
D. Incorrect initialization of the reference

Shw me The Right Answer

Answer . A 

Q.65 Identify the error in this code:
int x = 5;
int *ptr;
*ptr = &x;
cout << *ptr;

A. Incorrect assignment to pointer
B. Syntax error
C. No error
D. Dereferencing an uninitialized pointer

Shw me The Right Answer

Answer . A 

Q.66 Pseudocode:
SET arr TO [10, 20, 30] SET ptr TO ADDRESS OF arr[0] PRINT ptr[2] What is the output?

A. 10
B. 20
C. 30
D. ADDRESS OF arr[2]

Shw me The Right Answer

Answer . C 

Q.67 Pseudocode:
SET x TO 20 SET ptr TO ADDRESS OF x PRINT VALUE AT ptr What does this print?

A. 20
B. ADDRESS OF x
C. ptr
D. VALUE AT ptr

Shw me The Right Answer

Answer . A 

Q.68 Given the code
int arr[] = {1, 2, 3};
int *ptr = arr;
cout << ptr[1];,
what is the output?

A. 1
B. 2
C. 3
D. &arr[1]

Shw me The Right Answer

Answer . B 

Q.69 What is the output of the following code?
int x = 10;
int *ptr = &x;
cout << *ptr;

A. 10
B. &x
C. ptr
D. *ptr

Shw me The Right Answer

Answer . A 

Q.70 How are arrays and pointers related in C++?

A. Pointers can only point to the first element of an array
B. Arrays and pointers are fundamentally different
C. An array name can be used as a pointer to its first element
D. Pointers cannot be used to access array elements

Shw me The Right Answer

Answer . C 

Q.71 What does the dereference operator (*) do in C++?

A. It multiplies two values
B. It returns the address of a pointer
C. It accesses the value at the address a pointer is pointing to
D. It creates a pointer

Shw me The Right Answer

Answer . C 

Q.72 What is the purpose of the reference operator (&) in C++?

A. To create a reference variable
B. To access the address of a variable
C. To dereference a pointer
D. To perform bitwise AND

Shw me The Right Answer

Answer . B 

Q.73 How do you declare a pointer to an integer in C++?

A. int *ptr;
B. int &ptr;
C. int ptr[];
D. int ptr;

Shw me The Right Answer

Answer . A 

Q.74 What is a pointer in C++?

A. A variable that stores the address of another variable
B. A reference to another variable
C. A type of array
D. A function parameter

Shw me The Right Answer

Answer . A 

Q.75 Find the error in this code:
char str[5]; str = “Hello”;
cout << str;

A. Invalid assignment to char array
B. No error
C. Syntax error in cout statement
D. Array size mismatch

Shw me The Right Answer

Answer . A 

Q.76 Spot the mistake:
int nums[] = {1, 2, 3};
for(int i = 0; i <= 3; i++) {
cout << nums[i];
}

A. Incorrect loop condition
B. Missing array initialization
C. No error
D. Syntax error in cout statement

Shw me The Right Answer

Answer . A 

Q.77 Identify the error in this code:
int arr[3];
arr[3] = 10;
cout << arr[3];

A. Out of bounds array access
B. Syntax error
C. Type mismatch
D. No error

Shw me The Right Answer

Answer . A 

Q.78 Pseudocode:
SET arr[3] TO [0] FOR i FROM 0 TO 2 DO SET arr[i] TO i + 1 END PRINT arr[2] What is the output?

A. 1
B. 2
C. 3
D. 0

Shw me The Right Answer

Answer . C 

Q.79 Pseudocode:
SET charArray TO [‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’] PRINT charArray What is printed?

A. H
B. Hello
C. ello
D. Helloworld

Shw me The Right Answer

Answer . B 

Q.80 Pseudocode:
SET nums[5] TO [1, 2, 3, 4, 5] FOR EACH num IN nums DO PRINT num What does this print?

A. 12345
B. 54321
C. 15
D. Error

Shw me The Right Answer

Answer . A 

Q.81 Pseudocode:
SET matrix[2][2] TO [[1, 2], [3, 4]] PRINT matrix[0][1] What is the output?

A. 1
B. 2
C. 3
D. 4

Shw me The Right Answer

Answer . B 

Q.82 Pseudocode:
SET arr[3] TO [10, 20, 30] PRINT arr[0] What does this print?

A. 10
B. 20
C. 30
D. 0

Shw me The Right Answer

Answer . A 

Q.83 What is the output of the following code?
char chars[] = {‘A’, ‘B’, ‘C’, ‘\0’};
cout << chars;

A. A
B. AB
C. ABC
D. ABCD

Shw me The Right Answer

Answer . C 

Q.84 Consider the code:
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
cout << matrix[1][1];
What does it print?

A. 1
B. 2
C. 5
D. 6

Shw me The Right Answer

Answer . C 

Q.85 What is the output of this code?
int arr[] = {1, 2, 3};
cout << arr[1];

A. 1
B. 2
C. 3
D. Error

Shw me The Right Answer

Answer . B 

Q.86 Which statement is true about passing an array to a function in C++?

A. The array is always passed by value
B. The array is always passed by reference
C. A copy of the array is created
D. The size of the array must be known to the function

Shw me The Right Answer

Answer . B 

Q.87 What is the size of a character array initialized as char arr[] = “Hello”;?

A. 4
B. 5
C. 6
D. 7

Shw me The Right Answer

Answer . C 

Q.88 In C++, how can you initialize an array of integers named ‘num’ with the first four natural numbers?

A. int num[] = {1, 2, 3, 4};
B. int num[4] = {1, 2, 3, 4};
C. int num[4] = {1, 2, 3};
D. int num[] = {1, 2, 3, 4, 5};

Shw me The Right Answer

Answer . B 

Q.89 What are multidimensional arrays in C++?

A. Arrays with only one row or column
B. Arrays with more than one dimension, like 2D or 3D arrays
C. Arrays containing elements of different data types
D. Dynamic arrays

Shw me The Right Answer

Answer . B 

Q.90 How do you access the third element in an array named ‘arr’ in C++?

A. arr[2]
B. arr[3]
C. arr(2)
D. arr(3)

Shw me The Right Answer

Answer . A 

Q.91 Identify the issue in this code:
int *arr = new int[5];
delete arr;

A. Incorrect syntax for deleting an array
B. No error
C. Memory leak
D. Access violation

Shw me The Right Answer

Answer . A 

Q.92 Given the C++ code:
int *ptr = new int(5);
*ptr = 10;
delete ptr;,
what does this code do?

A. Allocates memory for an integer, sets it to 10, then releases the memory
B. Causes a memory leak
C. Generates a runtime error
D. Allocates memory for an integer without releasing it

Shw me The Right Answer

Answer . A 

Q.93 How can you prevent a memory leak in a C++ program?

A. By using automatic variables instead of dynamic variables
B. By ensuring every new operator is matched with delete
C. By avoiding the use of pointers
D. By only using static memory allocation

Shw me The Right Answer

Answer . B 

Q.94 What is memory leak in the context of dynamic memory allocation?

A. An error that occurs when memory allocation fails
B. The process of deleting unused memory
C. Memory that is not properly released after use
D. A special technique to clean memory

Shw me The Right Answer

Answer . C 

Q.95 Which of the following statements about dynamic memory allocation is true?

A. Memory allocated by new is automatically deallocated
B. new and delete can only be used with primitive data types
C. new returns null if memory allocation fails
D. Dynamic memory allocation is less efficient than static allocation

Shw me The Right Answer

Answer . C 

Q.96 What does the delete operator do in C++?

A. Deletes a variable from the program
B. Frees memory allocated by new
C. Removes a file from the disk
D. Erases a class definition

Shw me The Right Answer

Answer . B 

Q.97 What is the purpose of the new operator in C++?

A. To create a new class
B. To allocate memory on the stack
C. To allocate memory on the heap
D. To define a new data type

Shw me The Right Answer

Answer . C 

Q.98 Spot the error:
ofstream file(“example.txt”);
file >> “Text”;
file.close();

A. Wrong operator used for writing to file
B. File is not opened correctly
C. File is not closed properly
D. No error

Shw me The Right Answer

Answer . A 

Q.99 Identify the issue in this C++ code:
ifstream file;
file.open(“data.txt”);

A. Incorrect file mode
B. File not closed
C. Missing file path
D. No error

Shw me The Right Answer

Answer . D 

Q.100 Pseudocode:
OPEN file.txt AS file WRITE “Data” TO file CLOSE file What is the primary action performed by this code?

A. Reading data from file.txt
B. Writing data to file.txt
C. Deleting data from file.txt
D. Creating a new file named file.txt

Shw me The Right Answer

Answer . B 

Q.101 Given the C++ code
ofstream file(“example.txt”);
file << “Hello, World!”;
file.close();,
what does this code do?

A. Reads “Hello, World!” from example.txt
B. Writes “Hello, World!” to example.txt
C. Deletes “Hello, World!” from example.txt
D. Opens example.txt without modifying it

Shw me The Right Answer

Answer . B 

Q.102 How do you move the file pointer to a specific location for reading or writing in a file in C++?

A. Using the move() function
B. Using the relocate() function
C. Using the seekg() and seekp() functions
D. Using the transfer() function

Shw me The Right Answer

Answer . C 

Q.103 What is a file pointer in C++?

A. A pointer to the contents of a file
B. A variable that stores the memory address of a file
C. A marker that keeps track of the file’s current read/write position
D. A pointer to the file’s metadata

Shw me The Right Answer

Answer . C 

Q.104 In C++, which function is used to open a file?

A. file.open()
B. file.read()
C. file.write()
D. file.close()

Shw me The Right Answer

Answer . A 

Q.105 What is the primary purpose of the fstream library in C++?

A. To perform mathematical operations
B. To manage screen output
C. To handle file input and output
D. To process user input

Shw me The Right Answer

Answer . C 

Q.106 Spot the error:
class Test {
public:
int x;
void Test() {
x = 0; }
};

A. Incorrect constructor syntax
B. No error
C. Syntax error in variable declaration
D. Member initialization error

Shw me The Right Answer

Answer . A 

Q.107 Identify the mistake:
class Base {
public:
virtual void func() {} };
class Derived : public Base {
void func();
};

A. Missing function body in Derived
B. Incorrect use of virtual function
C. No error
D. Syntax error in class declaration

Shw me The Right Answer

Answer . A 

Q.108 Find the error:
class MyClass { public: MyClass(int x) {
cout << x; }
};
MyClass obj;

A. Missing default constructor in class definition
B. Syntax error
C. No error
D. Incorrect constructor usage

Shw me The Right Answer

Answer . A 

Q.109 Pseudocode:
CLASS Shape ABSTRACT FUNCTION draw() END CLASS Circle EXTENDS Shape FUNCTION draw() PRINT “Circle” What does this illustrate?

A. Encapsulation
B. Abstraction
C. Inheritance
D. Polymorphism

Shw me The Right Answer

Answer . B 

Q.110 Pseudocode: CLASS Bird EXTENDS Animal FUNCTION makeSound() PRINT “Chirp” What concept is shown here?

A. Inheritance
B. Polymorphism
C. Encapsulation
D. Abstraction

Shw me The Right Answer

Answer . A 

Q.111 Pseudocode:
CLASS Rectangle PRIVATE width, height PUBLIC FUNCTION setDimensions(w, h) SET width TO w SET height TO h END What OOP concept is demonstrated here?

A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction

Shw me The Right Answer

Answer . A 

Q.112 Pseudocode:
CLASS Animal FUNCTION makeSound() PRINT “Sound” What is this an example of?

A. A function in C++
B. A class in C++
C. Polymorphism in C++
D. Encapsulation in C++

Shw me The Right Answer

Answer . B 

Q.113 Consider the code:
class Shape {
public:
virtual void draw() = 0;
}; class Circle : public Shape {
public: void draw() {
cout << “Circle”; }
};
What concept does this illustrate?

A. Encapsulation
B. Inheritance
C. Polymorphism
D. Abstraction

Shw me The Right Answer

Answer . C 

Q.114 In the code class Base {
};
class Derived : public Base {
};,
which OOP concept is illustrated?

A. Encapsulation
B. Polymorphism
C. Inheritance
D. Abstraction

Shw me The Right Answer

Answer . C 

Q.115 Given the following C++ code, what is the output?
class MyClass { public: MyClass() {
cout << “Hello”; }
};
MyClass obj;

A. Hello
B. MyClass
C. Error
D. Nothing

Shw me The Right Answer

Answer . A 

Q.116 What is the output of this C++ code?
class Test {
public:
int x;
}; Test obj;
obj.x = 5;
cout << obj.x;

A. 5
B. 0
C. Error
D. Undefined

Shw me The Right Answer

Answer . A 

Q.117 How is abstraction different from encapsulation?

A. Abstraction hides complexity by showing only essential features, while encapsulation hides the internal state of an object
B. Abstraction and encapsulation are the same
C. Abstraction is used only in inheritance, while encapsulation is not
D. Encapsulation cannot be used in conjunction with polymorphism

Shw me The Right Answer

Answer . A 

Q.118 What is operator overloading in C++?

A. Giving additional meaning to C++ operators
B. Creating new operators in C++
C. Using existing operators in different contexts
D. Defining operators only in classes

Shw me The Right Answer

Answer . A 

Q.119 What is the purpose of a constructor in a C++ class?

A. To initialize the class’s member variables
B. To delete objects of the class
C. To allocate memory for objects
D. To declare a new class

Shw me The Right Answer

Answer . A 

Q.120 In C++, what is polymorphism?

A. The ability of different classes to have methods with the same name
B. The capability of a class to have multiple constructors
C. The ability of a function to perform different tasks based on the context
D. The concept of creating many objects from a single class

Shw me The Right Answer

Answer . C 

Q.121 Given the C++ code snippet:
auto lambda = [](int x, int y) { return x + y; }; cout << lambda(2, 3);,
what is the output?

A. 5
B. Compilation error
C. 2
D. 3

Shw me The Right Answer

Answer . A 

Q.122 What is the significance of std::regex in C++?

A. It is used for creating regular expressions
B. It is a class template for sequence containers
C. It is a function for memory allocation
D. It is a method for string manipulation

Shw me The Right Answer

Answer . A 

Q.123 In C++, which smart pointer type automatically deallocates the memory when it is no longer in use?

A. auto_ptr
B. shared_ptr
C. unique_ptr
D. weak_ptr

Shw me The Right Answer

Answer . C 

Q.124 What are regular expressions used for in C++?

A. To optimize algorithms
B. To create complex data structures
C. To search, edit, or manipulate text
D. To manage file input/output operations

Shw me The Right Answer

Answer . C 

Q.125 How is multithreading achieved in C++?

A. Using the std::thread library
B. By creating multiple processes
C. Through lambda expressions
D. By using dynamic memory allocation

Shw me The Right Answer

Answer . A 

Q.126 What is a lambda expression in C++?

A. A function that cannot have its address taken
B. A compact way of defining anonymous functions
C. A template for creating multiple functions
D. A method in the STL

Shw me The Right Answer

Answer . B 

Q.127 What are smart pointers in C++?

A. Pointers that automatically manage memory allocation and deallocation
B. Pointers that increase execution speed
C. Arrays with advanced features
D. Iterators in the STL

Shw me The Right Answer

Answer . A 

Q.128 What is the purpose of namespaces in C++?

A. To optimize code execution speed
B. To handle exceptions
C. To avoid name conflicts in larger programs
D. To manage dynamic memory

Shw me The Right Answer

Answer . C 

Q.129 Spot the error:
try { int x = -1;
if (x < 0) { throw x; } } catch (unsigned int e) {
cout << “Caught an exception: ” << e; }

A. The throw statement should not throw integers
B. The catch block is catching the wrong type of exception
C. There is no error
D. The if condition is incorrect

Shw me The Right Answer

Answer . B 

Q.130 Identify the issue in this code:
try { throw “Error occurred”; } catch (…) {
cout << “An error occurred”;
}

A. The catch block does not specify the exception type
B. The throw statement is incorrect
C. There is no error
D. The try block is empty

Shw me The Right Answer

Answer . A 

Q.131 Given the C++ code snippet:
try { int* myarray = new int[1000]; } catch (std::bad_alloc& e) {
cout << “Allocation failed: ” << e.what();
},
what does this code do?

A. It handles an out-of-memory error
B. It creates an array of 1000 integers
C. It throws a bad_alloc exception
D. It catches syntax errors

Shw me The Right Answer

Answer . A 

Q.132 What is the output of this code?
try { throw 20; } catch (int e) {
cout << “An exception occurred. Exception Nr. ” << e;
}

A. An error message
B. An exception occurred. Exception Nr. 20
C. 20
D. The program crashes

Shw me The Right Answer

Answer . B 

Q.133 What is the use of the std::exception class in C++?

A. To terminate a program
B. To catch syntax errors
C. As a base class for standard exception types
D. To declare new variables

Shw me The Right Answer

Answer . C 

Q.134 In C++, what happens if an exception is not caught?

A. The program will continue to execute
B. The exception gets rethrown automatically
C. The program terminates abnormally
D. The exception is ignored

Shw me The Right Answer

Answer . C 

Q.135 What is the correct way to handle multiple exceptions in C++?

A. Using multiple throw statements
B. Using several try blocks
C. Using multiple catch blocks after a single try block
D. Using nested try-catch blocks

Shw me The Right Answer

Answer . C 

Q.136 Which block of code is used to handle exceptions in C++?

A. try block
B. catch block
C. throw block
D. error block

Shw me The Right Answer

Answer . B 

Q.137 What keyword is used in C++ to throw an exception?

A. throw
B. error
C. exception
D. exit

Shw me The Right Answer

Answer . A 

Q.138 What is the purpose of exception handling in C++?

A. To handle syntax errors in the code
B. To increase the execution speed of programs
C. To manage runtime errors in a controlled way
D. To debug the program

Shw me The Right Answer

Answer . C 

Q.139 Spot the error:
template class MyContainer { public:
T value; void setValue(T val) { value = val; }
};

A. Incorrect template syntax
B. Missing constructor
C. No error
D. Error in setValue function

Shw me The Right Answer

Answer . C 

Q.140 Identify the issue in this C++ code:
std::vector v;
for (int i = 0; i <= v.size(); ++i) {

cout << v[i]; }

A. Using a wrong loop condition
B. Accessing elements outside the vector’s bounds
C. Incorrect use of the vector container
D. No error

Shw me The Right Answer

Answer . B 

Q.141 Given the C++ code snippet:
template T add(T a, T b) {
return a + b;
}
What is this an example of?

A. A class template
B. A function template
C. An overloaded operator
D. An STL container

Shw me The Right Answer

Answer . B 

Q.142 Why are templates used in C++?

A. To create error-free code
B. To provide predefined functionality
C. To allow classes and functions to operate with generic types
D. To optimize code execution

Shw me The Right Answer

Answer . C 

Q.143 What distinguishes std::set from other STL containers?

A. It allows for rapid insertion of elements
B. It automatically sorts the elements in ascending order
C. It stores each element in duplicate
D. It is the only STL container that uses templates

Shw me The Right Answer

Answer . B 

Q.144 In C++ STL, what is an iterator?

A. A loop that iterates over a container
B. A pointer-like object used to access elements in a container
C. A function to traverse arrays
D. A class template for container classes

Shw me The Right Answer

Answer . B 

Q.145 What is the primary difference between std::list and std::vector in C++ STL?

A. std::list is for integer types only, while std::vector can hold any type
B. std::list provides sequential access, while std::vector provides random access
C. std::list is a dynamic array, while std::vector is a linked list
D. std::list is a doubly linked list, while std::vector is a dynamic array

Shw me The Right Answer

Answer . D 

Q.146 How does the std::map container in C++ STL store elements?

A. In a single list
B. In key-value pairs, with unique keys
C. As an array of elements
D. In a stack-like structure

Shw me The Right Answer

Answer . B 

Q.147 Which STL container is typically used for implementing a dynamic array?

A. std::map
B. std::vector
C. std::list
D. std::set

Shw me The Right Answer

Answer . B 

Q.148 What is a class template in C++?

A. A class defined inside another class
B. A class that serves as a blueprint for creating other classes
C. A class that can handle multiple data types generically
D. A class used exclusively for STL operations

Shw me The Right Answer

Answer . C 

Q.149 What is a function template in C++?

A. A predefined function in the C++ Standard Library
B. A special function used for debugging
C. A way to create functions with a generic type
D. A container in the STL

Shw me The Right Answer

Answer . C 

Q.150 Spot the error:
int *ptr;
*ptr = new int;
*ptr = 5;
delete ptr;

A. Dereferencing an uninitialized pointer
B. Syntax error in memory allocation
C. No error
D. Incorrect use of delete

Shw me The Right Answer

Answer . A 

You may also like...

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments