wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Programming Quiz

Total questions: 10

Worksheet time: 20mins

Name
Class
Date
1.

What will be the output of the following code?

int i = 0;

while (i < 3)

{

cout << "Number: " << i << endl;

i++;

}

a)

Number: 1

Number: 2

Number: 3

b)

Number: 0

Number: 1

Number: 2

c)

Number: 0

Number: 1

Number: 2

Number: 3

d)

None of the above

2.

What will be the output of the following code using `switch case`?

char ch = 'b';

switch(ch)

{

case 'a': cout << "Apple";

case 'b': cout << "Banana";

case 'c': cout << "Cherry";

default: cout << "None";break;

}

a)

Apple

Banana

Cherry

None

b)

Banana

Cherry

None

c)

Cherry

None

d)

None

3.

What is the output of this program with a `while` loop?

int num = 6;

while (num >= 1)

{

cout << num << " ";

--num ; }

a)

6 5 4 3 2 1

b)

1 2 3 4 5 6

c)

6 5 4 3 2

d)

Infinite loop

4.

What will be the output of this C++ code using a `do while` loop?

int num = 1;

do

{

cout << num << " ";

num++;

} while (num <= 5);

a)

1 2 3 4 5

b)

1 2 3 4

c)

0 1 2 3 4

d)

None of the above

5.

What will the following code output?

int i = 1, sum = 0;

for (; i <= 5; i++)

{

sum += i;

} cout << sum;

a)

15

b)

10

c)

5

d)

None of the above

6.

What will be the output of the following code with a `continue` statement?

for (int i = 1; i <= 5; i++)

{

if (i == 3)

continue;

cout << i << " ";

}

a)

`1 2 3 4 5`

b)

`1 2 4 5`

c)

`1 2 4`

d)

None of the above

7.

What will be the output of this program with nested loops?

for (int i = 1; i <= 3; i++)

{

for (int j = 1; j <= 3; j++)

{

cout << i << j << " ";

}

cout << endl;

}

a)

11 12 13

21 22 23

31 32 33

b)

11 21 31

12 22 32

13 23 33

c)

11 22 33

11 22 33

11 22 33

d)

1 2 3

1 2 3

1 2 3

8.

Which of the following is a correct syntax for a nested for loop in C++?

a)

for (initialization; condition)

{

}

b)

for (initialization; condition; update)

{

for (initialization; condition; update)

{}

}

c)

for () { while () {} }

d)

while () { do {} while(); }

9.

Which of these control statements is used to exit from a loop?

a)

continue

b)

return

c)

break

d)

goto

10.

In a 2D array, how are elements stored?

a)

Rows and columns

b)

Sequentially in memory

c)

Both

d)

None