wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

C++ Switch and If Else Statements

Total questions: 15

Worksheet time: 11mins

Name
Class
Date
1.

What is the syntax of a switch statement in C++?

4 lines
2.

How do you use a switch statement with integers in C++?

a)

Use the switch statement with a string variable or expression instead of an integer variable or expression.

b)

Specify the integer variable or expression inside the switch parentheses and define cases for each possible value.

c)

Use the switch statement without specifying any cases for the integer variable or expression.

d)

Specify the integer variable or expression inside the switch parentheses and define cases for each possible value and use a break statement after each case.

3.

How do you use a switch statement with characters in C++?

a)

Use the ASCII value of the character as the expression inside the switch statement.

b)

Use the character as the expression inside the switch statement and provide if-else statements for each possible character value.

c)

Use the character as the expression inside the switch statement and provide case statements for each possible character value.

d)

Use the character as the expression inside the switch statement and provide case statements for each possible integer value.

4.

What are nested switch statements in C++?

a)

Switch statements that are placed inside another switch statement.

b)

Switch statements that are placed inside a loop statement.

c)

Switch statements that are placed inside an if statement.

d)

Switch statements that are placed inside a function.

5.

What is the purpose of a switch statement in C++?

a)

To define a new variable.

b)

To repeat a code block multiple times.

c)

To perform mathematical calculations.

d)

To execute different code blocks based on the value of a variable or expression.

6.

What happens if no case matches the value in a switch statement?

a)

The code inside the last case will be executed.

b)

The code inside the default case (if it exists) will be executed.

c)

The code inside the first case will be executed.

d)

The code inside all cases will be executed.

7.

Can you use floating-point numbers in a switch statement in C++?

a)

Yes

b)

Sometimes

c)

Only in certain cases

d)

No

8.

What is the default case in a switch statement used for?

a)

The default case is used when all of the cases match the value being evaluated.

b)

The default case is used when none of the cases match the value being evaluated.

c)

The default case is used to terminate the switch statement.

d)

The default case is used to skip the switch statement and move to the next statement.

9.

How do you break out of a switch statement in C++?

a)

return

b)

break

c)

continue

d)

exit

10.

Can you have multiple case labels with the same value in a switch statement?

a)

Yes

b)

Only in certain programming languages

c)

Sometimes

d)

No

11.

Use the _________ statement to specify a block of code to be executed if the condition is false.

a)

if

b)

ladder else if

c)

else

d)

else if

12.

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

a)

else if

b)

else

c)

ladder if

d)

if

13.

Use ____________ statement to specify a new condition to test, if the first condition is false

a)

else

b)

else if

c)

if

d)

ladder else if

14.

What is Logical Precedence : Reorder the following

a)

NOT (!)

b)

AND (&&)

c)

OR (||)

1)
2)
3)
15.

isalpha belongs to the following header file:

a)

iostream

b)

math

c)

cctype

d)

iomanip