Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

c++ test

Total questions: 120

Worksheet time: 3600secs

Name
Class
Date
1.

Which characteristic does a good algorithm have?

a)

It must be written in a programming language

b)

It is clear and unambiguous

c)

It must be long and complex

d)

It should have infinite steps

2.

What is the purpose of a flowchart in algorithm design?

a)

To visually represent the steps of an algorithm

b)

To execute an algorithm

c)

To write pseudocode

d)

To debug a program

3.

What is pseudocode?

a)

A low-level machine code

b)

A type of programming language

c)

A high-level representation of an algorithm

d)

A compiled version of an algorithm

4.

What is the main goal of debugging in programming?

a)

To make the code run faster

b)

To compile the program

c)

To add more features to the program

d)

To find and fix errors in a program

5.

What is a computer program?

a)

A hardware component of a computer

b)

A sequence or set of instructions in a programming language for a computer to execute

c)

A physical storage device for programs

d)

A mathematical formula used in software

6.

Why does source code need another program to execute?

a)

Because source code does not contain executable instructions

b)

Because computers can only execute their native machine instructions

c)

Because source code is already in machine language

d)

Because source code cannot be written by humans

7.

What is the purpose of a compiler?

a)

To convert machine instructions back to source code

b)

To store software documentation

c)

To directly execute source code without translation

d)

To translate source code into machine instructions

8.

What was the main reason for the intentionally limited syntax of BASIC (1964)?

a)

To improve execution speed

b)

To make the language easy to learn

c)

To support complex algorithms

d)

To make it compatible with all operating systems

9.

What is a programming language?

a)

A tool used only for web development

b)

A type of hardware that runs computer programs

c)

A collection of pre-written software applications

d)

A set of keywords, symbols, identifiers, and rules for communication with a computer

10.

What is the purpose of syntax in a programming language?

a)

To store large amounts of data

b)

To define rules for structuring code

c)

To improve execution speed

d)

To make programming languages harder to learn

11.

Which of the following is NOT a component of programming language syntax?

a)

Identifiers

b)

Keywords

c)

Symbols

d)

Data compression methods

12.

How are syntax rules typically defined?

a)

Through random trial and error

b)

Using the Backus–Naur form

c)

By copying rules from natural languages

d)

Without any predefined structure

13.

What is an algorithm?

a)

A random set of commands without structure

b)

A method of compressing programming language syntax

c)

A sequence of simple instructions that solve a problem

d)

A type of computer hardware

14.

Which of the following is a correct variable declaration in C++?

a)

variable num;

b)

int num;

c)

declare num;

d)

num int;

15.

Which data type should be used to store a single character in C++?

a)

string

b)

char

c)

character

d)

text

16.

What is the correct way to declare multiple variables of the same type in C++?

a)

variable int a, b, c;

b)

declare int a, b, c;

c)

variable a; b; c;

d)

int a, b, c;

17.

Which operator is used to assign a value to a variable in C++?

a)

=В

b)

==B

c)

:=B

d)

=>B

18.

What is the correct syntax to declare an integer variable in C++?

a)

int=x;

b)

int x;

c)

variable x;

d)

declare inr x;

19.

Which of the following is used to output text in C++?

a)

output()

b)

print()

c)

cout<<

d)

console.log()

20.

What is the correct file extension for a C++ source file?

a)

.cpp

b)

.t

c)

.k++

d)

.kp

21.

How do you correctly start the main function in C++?

a)

main()

b)

int main()

c)

main++

d)

start main()

22.

Which data type is used to store a single character in C++?

a)

string

b)

text

c)

char

d)

character

23.

Which data type is used to store whole numbers without decimals in C++?

a)

string

b)

char

c)

double

d)

int

24.

Which data type is used to store floating-point numbers with decimals in C++?

a)

double

b)

int

c)

char

d)

bool

25.

Which data type is used to store text in C++?

a)

char

b)

string

c)

text

d)

word

26.

Which data type is used to store values with two states (true or false) in C++?

a)

char

b)

string

c)

int

d)

bool

27.

What is the correct syntax for declaring a variable in C++?

a)

type = variableName value;

b)

type variableName = value;

c)

variableName = value;

d)

declare variableName = value;

28.

Which keyword is used to take user input in C++?

a)

input

b)

scanf

c)

cout

d)

cin

29.

Which keyword is used to display output in C++?

a)

cout

b)

cin

c)

print

d)

display

30.

What is the purpose of the assignment operator (=) in C++?

a)

It multiplies two numbers

b)

It swaps two variables

c)

It assigns a value to a variable

d)

It compares two values

31.

Which of the following is a correct use of the assignment operator?

a)

int x < 10;

b)

10 = x;

c)

int x == 10;

d)

int x = 10;

32.

What will be the value of x after the following statement? int x = 5; x = 10;

a)

10

b)

5

c)

15

d)

Undefined

33.

Which statement about the assignment operator is true?

a)

It checks for equality between two variables

b)

It swaps the values of two variables

c)

It declares a variable without assigning a value

d)

It updates the value stored in a variable

34.

What is the difference between = and == in C++?

a)

= assigns a value, while == compares two values

b)

= compares two values, while == assigns a value

c)

Both perform assignment

d)

Both perform comparison

35.

What is the purpose of comments in C++?

a)

To execute additional lines of code

b)

To speed up program execution

c)

To store variable values

d)

To add explanations or notes in the code that are ignored by the compiler

36.

Which of the following is a single-line comment in C++?

a)

/* This is a comment */

b)

# This is a comment

c)

// This is a comment

d)

-- This is a comment

37.

Which of the following is a multi-line comment in C++?

a)

# This is a comment

b)

// This is a comment

c)

/* This is a comment */

d)

# This is a comment

38.

What happens to comments when a C++ program is compiled?

a)

They slow down the execution of the program

b)

They cause an error if too many are used

c)

They are ignored by the compiler

d)

They are included in the final executable file

39.

What is the correct way to comment out multiple lines in C++?

a)

// This is a multi-line comment //

b)

# This is a multi-line comment #

c)

/* This is a multi-line comment */

d)

-- This is a multi-line comment --

40.

Which of the following statements about comments is true?

a)

Comments are required for the program to run correctly

b)

Comments improve code readability but do not affect execution

c)

Comments execute before the main function

d)

Comments can store variable values

41.

What is the main difference between // and /* ... */ comments?

a)

// is for multi-line comments, while /* ... */ is for single-line comments

b)

// is for single-line comments, while /* ... */ is for multi-line comments

c)

Both can be used interchangeably

d)

Neither is a valid comment syntax in C++

42.

Which of the following is not a valid comment in C++?

a)

// This is a comment

b)

/* This is a comment */

c)

!! This is a comment !!

d)

// Another comment

43.

What is the purpose of an if statement in C++?

a)

To execute a block of code in all cases

b)

To execute a block of code only if a specified condition is true

c)

To declare a variable

d)

To define a function

44.

What will happen if the condition inside an if statement is false?

a)

The compiler will throw an error

b)

The condition will be ignored, and the block will execute

c)

The program will terminate

d)

The block of code inside {} will be skipped

45.

Which of the following is a correct syntax for an if statement in C++?

a)

if x > 5 then { cout << "x is greater than 5"; }

b)

if (x > 5) { cout << "x is greater than 5"; }

c)

if {x > 5} cout << "x is greater than 5";

d)

if x > 5 { cout << "x is greater than 5"; }

46.

What type of value does the condition inside an if statement evaluate to?

a)

Integer only

b)

String

c)

Boolean (true or false)

d)

Character

47.

What is the output of the following code snippet? if (10 < 5) { cout << "x is less than 5"; } cout << "End of program";

a)

End of program

b)

x is less than 5

c)

x is less than 5 End of programВ

d)

Compilation error

48.

Use ________ to specify a block of code to be executed, if a specified condition is true

a)

else

b)

if

c)

switch

d)

loop

49.

Use ________ to specify a block of code to be executed, if the same condition is false

a)

else

b)

switch

c)

continue

d)

break

50.

>Use ________ to specify a new condition to test, if the first condition is false

a)

while

b)

switch

c)

else if

d)

vreak

51.

Use ________ to specify many alternative blocks of code to be executed

a)

return

b)

break

c)

else

d)

switch

52.

Which operator is used to check if one value is less than another in C++?

a)

>=

b)

>

c)

<

d)

<=

53.

Which operator is used to check if one value is less than or equal to another in C++?

a)

<

b)

<=

c)

>=

d)

>

54.

Which operator is used to check if one value is greater than another in C++?

a)

<

b)

>

c)

<=

d)

!=

55.

Which operator is used to check if one value is greater than or equal to another in C++?

a)

<

b)

==

c)

>

d)

>=

56.

Which operator is used to check if two values are equal in C++?

a)

=

b)

==

c)

!=

d)

<=

57.

Which operator is used to check if two values are not equal in C++?

a)

>

b)

==

c)

!=

d)

<

58.

Which operator is used to increase a variable's value by 1 in C++?

a)

+=

b)

++

c)

-=

d)

--

59.

What does the ++ operator do in C++?

a)

Multiplies a variable by 2

b)

Decreases a variable's value by 1

c)

Increases a variable's value by 1

d)

Divides a variable by 2

60.

Which operator is used to decrease a variable's value by 1 in C++?

a)

-=

b)

+=

c)

--

d)

++

61.

What does the -- operator do in C++?

a)

Increases a variable's value by 1

b)

Decreases a variable's value by 1

c)

Multiplies a variable by 2

d)

Divides a variable by 2

62.

What is an array in C++?

a)

A single variable that holds multiple values of different types

b)

A collection of elements of the same type stored in contiguous memory locations

c)

A function that performs calculations

d)

A keyword for declaring variables

63.

How do you declare an integer array of size 5 in C++?

a)

int array[5];

b)

int array[];

c)

array <int> arr(5);

d)

int array = {5};

64.

What is the index of the first element in an array?

a)

1

b)

0

c)

-1

d)

Depends on the array size

65.

What will happen if you access an array index that is out of bounds?

a)

It will return 0 by default

b)

Compiler error

c)

The program will crash

d)

Undefined behavior

66.

Which of the following is NOT a valid array declaration?

a)

float numbers[10];

b)

int arr[5];

c)

double data[] = new double[5];

d)

char str[20];

67.

Which statement correctly initializes an array?

a)

int arr[3]; arr = {1, 2, 3};

b)

int arr = {1, 2, 3};

c)

int arr[3] = {1, 2, 3};

d)

arr[3] = {1, 2, 3};

68.

What will be the output of the following code? int arr[] = {10, 20, 30, 40}; cout << arr[2];

a)

20

b)

30

c)

40

d)

10

69.

How can you find the size of an array in C++?

a)

sizeof(arr) / sizeof(arr[0])

b)

length(arr)

c)

size(arr)

d)

arr.length()

70.

Which loop is commonly used to traverse an array?

a)

if statement

b)

switch statement

c)

do-while loop

d)

for loop

71.

What does the following code output? int arr[3] = {1, 2}; cout << arr[2];

a)

Undefined behavior

b)

0

c)

1

d)

2

72.

How are arrays passed to functions in C++?

a)

They cannot be passed

b)

By reference

c)

By value

d)

As a pointer to the first element

73.

Which function prototype correctly accepts an integer array?

a)

void func(int arr[]);

b)

void func(int* arr);

c)

void func(int arr[10]);

d)

All of the above

74.

What happens when an array is passed to a function?

a)

The entire array is copied

b)

The function receives a pointer to the first element

c)

The function receives only the last element

d)

The function cannot access the array

75.

What is the correct way to modify an array inside a function?

a)

Return the modified array

b)

Pass it by reference using &

c)

Directly modify the passed array

d)

Use a global variable

76.

How do you ensure a function does not modify an array?

a)

Use #define

b)

Copy the array manually

c)

Use static inside the function

d)

Use const before the parameter

77.

Which is the correct declaration of a 2D array?

a)

int matrix[3][3];

b)

int matrix[][];

c)

int matrix{3,3};

d)

matrix arr(3,3);

78.

How do you access the third row, second column of a 2D array?

a)

arr(3,2)

b)

arr[3][2]

c)

arr[2][1]

d)

arr{2,1}

79.

What will be the output of this code? int mat[2][2] = { {1, 2}, {3, 4} }; cout << mat[1][1];

a)

1

b)

2

c)

3

d)

4

80.

Which loop is best suited for iterating over a 2D array?

a)

Single for loop

b)

do-while loop

c)

switch statement

d)

Nested for loop

81.

t does the following code do? int arr[2][3] = { {1, 2, 3}, {4, 5, 6} }; cout << arr[0][2];

a)

Outputs 1

b)

Outputs 3

c)

Outputs 4

d)

Outputs 6

82.

Which algorithm is the most efficient for sorting an array?

a)

Bubble Sort

b)

Quick Sort

c)

Selection Sort

d)

Linear Sort

83.

Which sorting algorithm repeatedly swaps adjacent elements?

a)

Merge Sort

b)

Insertion Sort

c)

Radix Sort

d)

Bubble Sort

84.

Which of the following is a disadvantage of linear search?

a)

Works only on sorted arrays

b)

Inefficient for large datasets

c)

Requires recursion

d)

Needs additional memory

85.

Which sorting algorithm divides an array into smaller sub-arrays?

a)

Heap Sort

b)

Selection Sort

c)

Bubble Sort

d)

Merge Sort

86.

What is a loop in C++?

a)

A control structure that repeats a block of code until a condition is met

b)

A variable used for iteration

c)

A function that executes once

d)

A data structure for storing numbers

87.

Which of the following is a type of loop in C++?

a)

Recursion

b)

Conditional loop

c)

While loop

d)

Switch loop

88.

Which loop is best when the number of iterations is known in advance?

a)

While loop

b)

For loop

c)

Do-while loop

d)

If-else statement

89.

What is the correct syntax for a for loop?

a)

for (condition; initialization; update) { // code }

b)

for { initialization; condition; update }

c)

for(initialization; condition; update) { // code }

d)

for condition (initialization; update) { // code }

90.

Which loop always executes at least once?

a)

While loop

b)

Do-while loop

c)

While loop

d)

Nested loop

91.

What is the output of this code? int i = 5; while (i > 0) {cout << i << " ";В В В i--;}

a)

4 3 2 1

b)

5 4 3 2 1

c)

5 4 3 2

d)

Infinite loop

92.

Which of the following is true about while loops?

a)

It executes only if the condition is true

b)

It executes at least once

c)

It is used when the number of iterations is known

d)

It cannot have a break statement

93.

Which loop structure is preferred when a condition must be checked before execution?

a)

Do-while llop

b)

For loop

c)

Recursive function

d)

While loop

94.

What is the correct way to use a break statement in a loop?

a)

loop.break();

b)

if (condition) break;

c)

break = true;

d)

stop loop;

95.

What does the continue statement do in a loop?

a)

Jumps to the end of the program

b)

Executes the loop indefinitely

c)

Stops the loop execution

d)

Skips the current iteration and proceeds to the next one

96.

>How many times will this loop execute? for (int i = 0; i < 5; i++) {cout << i << " ";}

a)

6 times

b)

5 times

c)

4 times

d)

Infinite times

97.

Which keyword is used to exit a loop immediately?

a)

Continue

b)

Return

c)

Break

d)

Exit

98.

Which of the following creates an infinite loop?

a)

do { cout << "Hello"; } while (false);

b)

while (i > 10) { cout << "Hello"; i++; }

c)

for (int i = 0; i < 10; i++) { cout << i; }

d)

while (true) { cout << "Hello"; }

99.

What happens when a loop condition is never false?

a)

The loop executes once

b)

Infinite loop

c)

The program crashes

d)

Compilation error

100.

What is the purpose of nested loops?

a)

To iterate over multi-dimensional data

b)

To execute only one iteration

c)

To stop execution at a certain point

d)

To prevent infinite loops

101.

Which loop is best when the number of iterations is known in advance?

a)

While loop

b)

Do-while llop

c)

Infinite loop

d)

For loop

102.

Which of the following loops executes at least once, even if the condition is false?

a)

While loop

b)

Do-while loop

c)

Nested loop

d)

For loop

103.

What is the syntax of a for loop in C++?

a)

for { initialization; condition; update }

b)

for (condition; initialization; update) { // code }

c)

for(initialization; condition; update) { // code }

d)

for condition (initialization; update) { // code }

104.

What does a while loop check before executing its body?

a)

The condition specified in parentheses

b)

The type of variable being used

c)

The number of iterations

d)

Whether there is a break statement

105.

Which of the following is an infinite loop?

a)

for (int i = 0; i < 5; i++) { cout << i; }

b)

while (i > 10) { cout << "Loop"; i++; }

c)

while (true) { cout << "Looping"; }

d)

do { cout << "Hello"; } while (false);

106.

How many times will the following loop execute? int i = 0; while (i < 3) {cout << i << " ";В В В i++;}

a)

Infinite times

b)

2 times

c)

3 times

d)

4 times

107.

Which statement is used to exit a loop immediately?

a)

Break

b)

Continue

c)

Return

d)

Exit

108.

What does the continue statement do inside a loop?

a)

Exits the program

b)

Stops the loop execution

c)

Restarts the loop from the beginning

d)

Skips the current iteration and moves to the next one

109.

Which loop is best suited for iterating through an array of fixed size?

a)

Recursive function

b)

For loop

c)

While loop

d)

Do-while loop

110.

What will be the output of this for loop? for (int i = 0; i < 4; i++) {cout << i << " ";}

a)

0 1 2 3 4

b)

0 1 2 3

c)

1 2 3 4

d)

4 3 2 1

111.

What is the purpose of nested loops?

a)

To execute only one iteration

b)

To stop execution at a certain point

c)

To iterate over multi-dimensional data

d)

To prevent infinite loops

112.

Which loop is preferred when the number of iterations is unknown?

a)

For loop

b)

While loop

c)

Do-while loop

d)

Switch case

113.

What happens if the condition in a while loop is always true?

a)

The loop executes only once

b)

The loop stops automatically

c)

The program gives a syntax error

d)

The loop runs indefinitely

114.

Which of the following is a valid way to initialize a for loop?

a)

for (int i = 0; i < 10; i++)

b)

for (i = 0; i < 10; i++)

c)

for { int i = 0; i < 10; i++ }

d)

for (int i = 0; i++)

115.

Which of the following correctly represents a do-while loop?

a)

while (condition) { // code }

b)

do { // code } while (condition);

c)

for (initialization; condition; update) { // code }

d)

loop (condition) { // code }

116.

How many times does this loop execute? int i = 10; while (i > 5) {cout << i << " "; i--;}

a)

6 times

b)

4 times

c)

5 times

d)

infinite times

117.

What does the following for loop do? for (int i = 5; i > 0; i--) {cout << i << " ";}

a)

Prints numbers in decreasing order

b)

Prints numbers in increasing order

c)

Prints only 0

d)

Causes an infinite loop

118.

How can you create an infinite for loop?

a)

for (i = 1; i > 0; i++) { cout << "Looping"; }

b)

for (i = 0; i++) { cout << "Looping"; }

c)

for(;;) { cout << "Looping"; }

d)

for (i = 0; i < 100; i--) { cout << "Looping"; }

119.

Which of the following is an example of an off-by-one error in a loop?

a)

Using an incorrect data type

b)

Using i <= size instead of i < size

c)

Using break when not needed

d)

Forgetting to increment the loop variable

120.

What will be the output of this loop? int i = 2; do {cout << i << " "; i++;} while (i < 5);

a)

2 3

b)

2 3 4 5

c)

Infinite loop

d)

2 3 4