an HCL GUVI product

c++ programming banner

C++ Programming Multiple Choice Questions (MCQs) and Answers

Master C++ Programming with Practice MCQs. Explore our curated collection of Multiple Choice Questions. Ideal for placement and interview preparation, our questions range from basic to advanced, ensuring comprehensive coverage of C++ Programming. Begin your placement preparation journey now!

Q1

Q1 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

Q2

Q2 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

Q3

Q3 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

Q4

Q4 What is a variable in C++?

A

A constant value

B

A data type

C

A storage location with a name

D

A keyword

Q5

Q5 Which data type would be best to store a person's age?

A

int

B

double

C

bool

D

char

Q6

Q6 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

Q7

Q7 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

Q8

Q8 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

Q9

Q9 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

Q10

Q10 Which operator is used for logical AND in C++?

A

&&

B

&

C

||

D

==

Q11

Q11 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

Q12

Q12 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

Q13

Q13 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

Q14

Q14 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

Q15

Q15 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

Q16

Q16 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

Q17

Q17 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

Q18

Q18 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

Q19

Q19 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

Q20

Q20 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

Q21

Q21 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

Q22

Q22 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

Q23

Q23 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

Q24

Q24 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

Q25

Q25 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

Q26

Q26 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

Q27

Q27 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

Q28

Q28 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

Q29

Q29 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

Q30

Q30 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

ad vertical