wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Chapter 3 C++

Total questions: 7

Worksheet time: 4mins

Name
Class
Date
1.

The area of a circle is calculated by multiplying PI by radius squared, or simply:

PI * radius * radius

In the above expression, radius can be r, or x, or rad, or whatever you want it to be. Assume the variable is named radius, for readability. Take a look at the following if statement. The area of the circle is calculated and displayed to the console ONLY IF what?


if (radius >= 0)

cout << radius * radius * 3.14159;

a)

The value in the radius variable is either greater than 0 or equal to 0

b)

The value in the radius variable is greater than 0

c)

The value in the radius variable is equal to 0

d)

The value in the radius is a negative value

2.

Analyze the following code.

bool even = false;

if (even)

{

cout << "It is even!";

}

a)

Nothing is displayed.

b)

It is even! is displayed

c)

false is displayed

3.

Rewrite the following if-else statement using a conditional (aka. ternary operator):

if (x > 10)

score = 3 * scale;

else

score = 4 * scale;

a)

score = (x > 10) ? 3 : 4 ;

b)

score = (x > 10) ? 3 * scale : 4 * scale;

c)

(x > 10) ? 3 * scale : 4 * scale;

d)

cout << (x > 10) ? 3 * scale : 4 * scale;

4.

The following code displays ___________.

double temperature = 50;

if (temperature >= 100)

cout << "too hot" << endl;

else if (temperature <= 40)

cout << "too cold" << endl;

else

cout << "just right" << endl;

a)

too cold

b)

too hot

c)

just right

d)

nothing is displayed

5.

You can always convert an if statement to a switch statement.

a)

True

b)

False

6.

Including a space between the relational operators >=, <=, ==, and != causes a syntax error. For example:

>= is not the same as > =

<= is not the same as < =

== is not the same as = =

a)

True

b)

False

7.

Suppose cond1 and cond2 are two Boolean expressions. When will the following if condition be true?


if (cond1 && cond2)

a)

When either cond1 or cond2 are true

b)

When both cond1 and cond2 are true

c)

When cond1 is false and cond2 is false