Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Variables in C++

Total questions: 10

Worksheet time: 8mins

Name
Class
Date
1.

What is the final value of x in the following code segment?


int x = 7;

x += 2*x;

a)

63

b)

21

c)

81

d)

This code segment will not compile.

2.

What is the final value of y in the following code segment?


int y = 3 * 7 % 8 + 1 * 4;

a)

9

b)

24

c)

25

d)

6

e)

This code segment will not compile.

3.

What is the final value of x in the following code segment?


double x = 7/2;

a)

3 1/2

b)

3.5

c)

3.0

d)

This code segment will not compile.

4.

What are the final values of y and z in the following code segment?


int z =4;

int y = 3 + z--;

a)

y = 7, z = 3

b)

y = 7, z = 4

c)

y = 6, z = 3

d)

y = 6, z = 4

e)

This code segment will not compile.

5.

What is the final value of y in the following code segment?


double y = 2;

int z = 3;

y /= z + 1;

a)

y = 1.0

b)

y = 2.0

c)

y = 0.5

d)

y = 0.0

e)

This code segment will not compile.

6.

What is the final value of w in the following code segment?


int w = int(3 / 2) + 5 % 10 - 3.5;

a)

-2

b)

3

c)

-3

d)

2

e)

This code segment will not compile.

7.

What is the final value of x in the following code segment?

(Assume the header file <cmath> has been included.)


double x = pow(2, 3) + 1 / 2;

a)

9.5

b)

8.5

c)

8.0

d)

9.0

e)

This code segment will not compile.

8.

What is the final value of x in the following code segment?

(Assume the header file <cmath> has been included.)


double x = 15 / sqrt(4.0) - 10 % 3;

a)

6.5

b)

6.0

c)

0.0

d)

-0.5

e)

This code segment will not compile.

9.

What are the final values of x and y in the following code segment?


int x = 3;

int y = ++x * 5 / 2;

a)

x = 3, y = 10

b)

x = 4, y =7

c)

x = 4, y = 10

d)

x = 3, y = 7

e)

This code segment will not compile.

10.

What is the final value of y in the following code segment?


int x = 1234;

int y = x % 10 + x / 10 % 10;

a)

y = 34

b)

y = 7

c)

y = 38

d)

y = 10

e)

This code will cause an error due to division by zero.