wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CPP Midterm

Total questions: 111

Worksheet time: 1hrs 26mins

Name
Class
Date
1.

What will be the output of the following code?

a)

A

b)

B

c)

C

d)

None of the above

2.

What will be the output of the following code?

a)

Compilation error

b)

A

c)

B

d)

Runtime error

3.

What is the value of x after the following code is executed?

a)

0

b)

1

c)

2

d)

3

4.

Which of the following is not a valid C++ comparison operator?

a)

==

b)

=

c)

>=

d)

!=

5.

What is the correct way to comment out a single line in C++?

a)

// This is a comment

b)

/* This is a comment */

c)

' This is a comment

d)

# This is a comment

6.

Given the following code, what is the value of result?


a)

3

b)

13

c)

8

d)

40

7.

Which of the following statements is true about the --- if-else --- statement in C++?

a)

An if-else statement can have multiple else blocks.

b)

An if-else statement can only have one if block.

c)

The else block is optional.

d)

if-else statements can only be used for numerical comparisons.

8.

What is the value of result after executing the following code?

a)

10

b)

20

c)

0

d)

30

9.

What is the output of the following code?

a)

A

b)

B

c)

C

d)

AB

10.

In C++, how is the if-else if ladder different from a series of independent if statements?

a)

The if-else if ladder only executes the first true condition.

b)

The if-else if ladder allows for multiple conditions to be true.

c)

The if-else if ladder is more efficient and faster.

d)

The if-else if ladder is not used in C++.

11.

What is the purpose of the else statement in an if-else statement?

a)

It provides an alternative block of code to execute when the if condition is false.

b)

It terminates the program.

c)

It repeats the if block.

d)

It is not used in C++.

12.

What is the value of x after the following code is executed?

a)

1

b)

2

c)

3

d)

4

13.

True or False: An if-else if ladder can have multiple else blocks.

a)

TRUE

b)

FALSE

14.

What is the purpose of the following code?

a)

It ensures that code block A always executes.

b)

It ensures that code block B always executes.

c)

It executes code block A if the condition is true, otherwise, it executes code block B.

d)

It is invalid C++ code.

15.

What is the possible output of the following code?

a)

A

b)

B

c)

C

d)

BC

16.

In C++, what does the else if statement allow you to do?

a)

Define a new if condition within the same block.

b)

Create a nested if statement.

c)

Define an alternative condition to check if the initial if condition is false.

d)

Skip the if condition and directly execute the code in the else block.

17.

What is the value of x after the following code is executed?

a)

0

b)

1

c)

2

d)

3

18.

What is the purpose of the following code?

a)

It ensures that code block X always executes.

b)

It executes code block X if condition is true, code block Y if another_condition is true, and code block Z if both conditions are false.

c)

It is invalid C++ code.

d)

It skips code block X and always executes code block Y.

19.

What will be the output of the following code?

a)

A

b)

B

c)

C

d)

AB

20.

What is the possible output of the following code?

a)

X

b)

Y

c)

Z

d)

XY

21.

What is the output of the following code?

a)

A

b)

B

c)

AB

d)

No output

22.

Which of the following is true about the if-else statement in C++?

a)

The if-else statement can only have one if block.

b)

An if-else statement can have multiple else blocks.

c)

The else block is mandatory.

d)

The if-else statement is not used in C++.

23.

What is the value of result after executing the following code?


a)

w

b)

e

c)

5

d)

8

24.

In C++, what is the role of the else keyword?

a)

To terminate the program.

b)

To repeat the if block.

c)

To provide an alternative block of code to execute when the if condition is false.

d)

The else keyword is not used in C++.

25.

What is the output of the following code?


a)

A

b)

B

c)

C

d)

AB

26.

What is the output of the following code?

a)

A

b)

B

c)

C

d)

AB

27.

In C++, what is the purpose of the if-else if ladder?

a)

To create a sequence of if statements that execute independently.

b)

To ensure that multiple if conditions are true at the same time.

c)

To provide a series of conditions to check, executing the first true condition's block.

d)

To replace the if-else statement in complex scenarios.

28.

What will be the value of result after executing the following code?

a)

2

b)

12

c)

35

d)

42

29.

In C++, what does the else statement indicate?

a)

The end of the program.

b)

A fallback condition to execute when all if and else if conditions are false.

c)

A loop construct.

d)

The else statement is not used in C++.

30.

What is the output of the following code?

a)

A

b)

B

c)

C

d)

BC

31.

What is the value of result after executing the following code?

a)

5

b)

8

c)

13

d)

48

32.

True or False: In C++, the if-else if ladder allows multiple conditions to be true simultaneously.

a)

TRUE

b)

FALSE

33.

What is the output of the following code?

a)

Positive

b)

Negative

c)

Zero

d)

PositiveNegativeZero

34.

What is the output of the following code?

a)

X

b)

Y

c)

Z

d)

XY

35.

What is the value of x after the following code is executed?

a)

0

b)

1

c)

2

d)

3

36.

What is the output of this code? int x = 10; cout << x * 2 << endl;

a)

10

b)

20

c)

2

d)

102

37.

What is wrong with this code? int num; cin >> num cout << num << endl;

a)

Missing semicolon after cin statement

b)

Missing << operator in cin

c)

num is not initialized

d)

cout cannot output integers

38.

What will this code output? double price = 15.99; cout << fixed << setprecision(2) << price << endl;

a)

15

b)

15.99

c)

16.00

d)

15.9900

39.

Which code snippet correctly declares a constant variable for a tax rate?

a)

const double TAX = 0.10;

b)

double const TAX = 0.10;

c)

const TAX = 0.10;

d)

double TAX = const 0.10;

40.

What is the output of this code? int a = 5, b = 8; if (a + b > 10) { cout << "Sum is greater than 10" << endl; } else { cout << "Sum is 10 or less" << endl; }

a)

Sum is greater than 10

b)

Sum is 10 or less

c)

Nothing is printed

d)

Error

41.

Which header is needed for this code to work? cout << fixed << setprecision(2) << 10.555 << endl;

a)

<iostream>

b)

<iomanip>

c)

<string>

d)

<math>

42.

What will this code output? string status = "yes"; if (status == "yes") { cout << "Approved" << endl; } else { cout << "Denied" << endl; }

a)

Approved

b)

Denied

c)

Nothing

d)

Error

43.

Which code correctly reads a user's age and outputs it?

a)

int age; cin >> age; cout << age << endl;

b)

int age; cout >> age; cin << age << endl;

c)

age int; cin >> age; cout << age;

d)

int age; cin << age; cout >> age << endl;

44.

What is the output of this code? int score = 75; if (score >= 80) { cout << "Pass"; } else if (score >= 60) { cout << "Conditional Pass"; } else { cout << "Fail"; }

a)

Pass

b)

Conditional Pass

c)

Fail

d)

Nothing

45.

What is wrong with this code? int x = 10 cout << x << endl;

a)

Missing semicolon after variable declaration

b)

cout cannot output integers

c)

Variable x is not initialized

d)

Missing << operator

46.

What is the output of this code? double total = 100.50; double discount = total * 0.1; cout << fixed << setprecision(2) << total - discount << endl;

a)

90.45

b)

100.50

c)

10.05

d)

90.50

47.

Which code correctly checks if a number is positive?

a)

int num; cin >> num; if (num > 0) { cout << "Positive" << endl; }

b)

int num; cin >> num; if (num = 0) { cout << "Positive" << endl; }

c)

int num; cin >> num; if (num >= 0) { cout << "Positive" << endl; }

d)

int num; cin >> num; if (num < 0) { cout << "Positive" << endl; }

48.

What is the output of this code? int x = 10, y = 20; int sum = x + y; cout << sum << endl;

a)

10

b)

20

c)

30

d)

1020

49.

Which code snippet correctly declares and initializes a string variable?

a)

string name = "Alice";

b)

string name = Alice;

c)

string name = 'Alice';

d)

name string = "Alice";

50.

What is the output of this code? int temp = 25; if (temp <= 20) { cout << "Cold"; } else if (temp <= 30) { cout << "Warm"; } else { cout << "Hot"; }

a)

Cold

b)

Warm

c)

Hot

d)

Nothing

51.

What is the value of tax after this code executes? double income = 300000; double tax; if (income <= 250000) { tax = 0; } else { tax = (income - 250000) * 0.2; }

a)

0

b)

10000

c)

60000

d)

50000

52.

Which code correctly outputs a formatted price with two decimal places?

a)

double price = 19.999; cout << fixed << setprecision(2) << price << endl;

b)

double price = 19.999; cout << setprecision(2) << price << endl;

c)

double price = 19.999; cout << fixed << price << endl;

d)

double price = 19.999; cout << setprecision << price << endl;

53.

What is wrong with this code? int x = 5; if (x = 10) { cout << "x is 10" << endl; }

a)

Uses = instead of == in the condition

b)

Missing semicolon after cout

c)

x is not declared

d)

if statement is invalid

54.

This code will compile without errors: int x = 10; cout << x << endl;

a)

True

b)

False

55.

The following code uses the correct operator to read input: int num; cin << num;

a)

True

b)

False

56.

The const keyword prevents a variable's value from being changed after initialization.

a)

True

b)

False

57.

This code will output "Fail" for a score of 85: int score = 85; if (score < 60) { cout << "Fail" << endl; }

a)

True

b)

False

58.

The << operator is used with cout to display output to the console.

a)

True

b)

False

59.

This code will cause an error because num is not initialized: int num; cout << num << endl;

a)

True

b)

False

60.

The if statement can have multiple else if blocks but only one else block.

a)

True

b)

False

61.

This code correctly formats a number to two decimal places: double num = 10.555; cout << num << endl;

a)

True

b)

False

62.

This code will always execute the else block: int x = 5; if (x > 10) { cout << "Greater"; } else { cout << "Not greater"; }

a)

True

b)

False

63.

The (a)   object is used to output data to the console in C++.

64.

To read user input into a variable, use the (a)   operator with cin.

65.

To declare a variable that cannot be modified, use the (a)   keyword.

66.

The (a)   header is required to use fixed and setprecision for output formatting.

67.

The (a)   statement executes a block of code if a condition is true.

68.

To add a newline and flush the output buffer, use the (a)   manipulator.

69.

The (a)   operator checks if two values are equal in a conditional statement.

70.

The (a)   data type is used to store text values in C++.

71.

The (a)   header must be included to use cin and cout.

72.

This code will output "Eligible" if age is 18: int age = 18; if (age >= 18) { cout << "Eligible" << endl; }

a)

True

b)

False

73.

This code will cause a compilation error due to incorrect syntax: int x = 5; if x > 0 { cout << "Positive" << endl; }

a)

True

b)

False

74.

This code will always output "Fail": int score = 85; if (score < 60) { cout << "Fail" << endl; } else { cout << "Pass" << endl; }

a)

True

b)

False

75.

This code correctly uses the equality operator to compare two variables: int a = 10, b = 10; if (a == b) { cout << "Equal" << endl; }

a)

True

b)

False

76.

This code will output "Invalid" for any input value: int num = 100; if (num > 0) { cout << "Positive"; } else if (num < 0) { cout << "Negative"; } else { cout << "Invalid"; }

a)

True

b)

False

77.

This code will compile without errors: double price = 50.0; if (price <= 100.0) { cout << "Affordable" << endl; }

a)

True

b)

False

78.

This code incorrectly uses the assignment operator instead of comparison: int x = 5; if (x = 10) { cout << "x is 10" << endl; }

a)

True

b)

False

79.

This code will output "High" for value = 75: int value = 75; if (value >= 80) { cout << "High"; } else if (value >= 60) { cout << "Medium"; } else { cout << "Low"; }

a)

True

b)

False

80.

The else block in this code will execute if temperature is 25: int temperature = 25; if (temperature < 20) { cout << "Cold" << endl; } else { cout << "Warm" << endl; }

a)

True

b)

False

81.

This code will output nothing if grade is 50: int grade = 50; if (grade >= 75) { cout << "Pass" << endl; }

a)

True

b)

False

82.

This code will cause a compilation error due to missing parentheses: int x = 10; if x > 5 { cout << "Greater" << endl; }

a)

True

b)

False

83.

This code correctly handles string comparison: string status = "yes"; if (status == "yes") { cout << "Approved" << endl; }

a)

True

b)

False

84.

This code will output "No Discount" for total = 200: double total = 200; if (total > 500) { cout << "Discount Applied" << endl; } else { cout << "No Discount" << endl; }

a)

True

b)

False

85.

This code will output "Category A" for score = 90: int score = 90; if (score >= 85) { cout << "Category A"; } else if (score >= 70) { cout << "Category B"; } else { cout << "Category C"; }

a)

True

b)

False

86.

This code will cause an error because else is used without an if: int x = 5; else { cout << "Error" << endl; }

a)

True

b)

False

87.

This code will output "Valid" for input = 0: int input = 0; if (input > 0) { cout << "Positive"; } else if (input < 0) { cout << "Negative"; } else { cout << "Valid"; }

a)

True

b)

False

88.

This code will output "Low" for speed = 50: int speed = 50; if (speed > 60) { cout << "High"; } else if (speed > 30) { cout << "Medium"; } else { cout << "Low"; }

a)

True

b)

False

89.

This code uses an incorrect comparison operator: int x = 10; if (x = 5) { cout << "Five" << endl; }

a)

True

b)

False

90.

This code will output "Approved" for balance = 1000: double balance = 1000; if (balance >= 500) { cout << "Approved" << endl; } else { cout << "Rejected" << endl; }

a)

True

b)

False

91.

This code will output nothing if value is negative: int value = -5; if (value >= 0) { cout << "Non-negative" << endl; }

a)

True

b)

False

92.

Complete this code to output "Pass" if grade is 75 or higher: int grade = 80; if (grade ____ 75) { cout << "Pass" << endl; }

a)
>=
b)

=

c)

==

d)

<=

93.

Fill in the blank to compare if x equals 10: int x = 10; if (x ____ 10) { cout << "Equal" << endl; }

a)
==
b)

!=

c)

=

d)

<=

94.

Complete this code to output "Negative" if num is less than 0: int num = -5; if (num ____ 0) { cout << "Negative" << endl; }

a)
<
b)

==

c)

>

d)

=

95.

Fill in the blank to make this code output "Discount" for total over 1000: double total = 1200; if (total ____ 1000) { cout << "Discount" << endl; }

a)
>
b)

==

c)

<>

d)

<

96.

Complete this code to output "Approved" if status is "yes": string status = "yes"; if (status ____ "yes") { cout << "Approved" << endl; }

a)
==
b)

<

c)

!=

d)

=

97.

Fill in the blank to output "High" if score is at least 90: int score = 95; if (score ____ 90) { cout << "High" << endl; }

a)
>=
b)

<

c)

==

d)

=

98.

Complete this code to output "Valid" if input is not negative: int input = 5; if (input ____ 0) { cout << "Valid" << endl; }

a)
>=
b)

<=

c)

<

d)

==

99.

A traffic light system checks the current signal color to display instructions. The code should print "Stop" for red, "Prepare" for yellow, and "Go" for green. It’s not working correctly

a)

The if condition uses = instead of ==.

b)

else if is not valid in C++; use elif.

c)

The string type is incorrect for signal.

d)

The cout statements are missing endl.

100.

Complete this code to output "Fail" if grade is below 60: int grade = 55; if (grade ____ 60) { cout << "Fail" << endl; }

a)
<
b)

>

c)

=

d)

==

101.

Fill in the blank to compare x and y for inequality: int x = 5, y = 10; if (x ____ y) { cout << "Not equal" << endl; }

a)

!= (Not Equal)

b)

>=

c)

==

d)

=

102.

Complete this code to output "Warm" if temp is between 20 and 30 (inclusive): int temp = 25; if (temp >= 20 && temp ____ 30) { cout << "Warm" << endl; }

a)
<=
b)

>=

c)

==

d)

!=

103.

Fill in the blank to output "Cold" if temp is below 15: int temp = 10; if (temp ____ 15) { cout << "Cold" << endl; }

a)
<
b)

>

c)

=

d)

==

104.

Complete this code to output "Reject" if balance is below 500: double balance = 400; if (balance ____ 500) { cout << "Reject" << endl; }

a)
<
b)

>

c)

! =

d)

==

105.

Fill in the blank to output "Valid" if code equals 123: int code = 123; if (code ____ 123) { cout << "Valid" << endl; }

a)
==
b)

!=

c)

=

d)

>=

106.

Complete this code to output "High" if speed exceeds 70: int speed = 80; if (speed ____ 70) { cout << "High" << endl; }

a)
>
b)

=

c)

<

d)

!=

107.

Fill in the blank to output "Approved" if status is not "no": string status = "yes"; if (status ____ "no") { cout << "Approved" << endl; }

a)
!=
b)

==

c)

>=

d)

<=

108.

Complete this code to output "Category A" if score is at least 85: int score = 90; if (score ____ 85) { cout << "Category A" << endl; }

a)
>=
b)

==

c)

!=

d)

<

109.

Fill in the blank to output "Low" if value is less than or equal to 10: int value = 5; if (value ____ 10) { cout << "Low" << endl; }

a)
<=
b)

>=

c)

==

d)

!=

110.

Complete this code to output "Pass" if grade is 70 or higher: int grade = 75; if (grade ____ 70) { cout << "Pass" << endl; }

a)
>=
b)

<=

c)

==

d)

=

111.

Complete this code to output "Negative" if num is not positive: int num = -10; if (num ____ 0) { cout << "Negative" << endl; }

a)
<
b)

=

c)

>

d)

!=