wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Casting in Java

Total questions: 11

Worksheet time: 6mins

Name
Class
Date
1.

Consider the following code segment.

int a = 15;

double b = (double) a / 2.0;

System.out.println(b);

What is printed as a result of executing the code segment?

a)
7.0
b)
7.5
c)
8.0
d)
15.0
2.

Consider the following code segment.

int x = 5;

double y = (double) (x * 2);

System.out.print(x + " " + y);

What is printed as a result of executing the code segment?

a)

5 10.0

b)

5 10

c)

10 5.0

d)

10 5

3.

Consider the following code segment.

double num1 = 10.5;

int num2 = (int) num1;

double result = num1 - num2;

System.out.println(result);

What is printed as a result of executing the code segment?

a)
0.5
b)
10.5
c)
10
d)
0
4.

Consider the following code segment.

int m = 8;

double n = (double) (m + 3) / 2;

System.out.print(m + " " + n);

What is printed as a result of executing the code segment?

a)

8 5.5

b)

8 6.0

c)

8 5.0

d)

11 5.5

5.

Consider the following code segment.

double x = 7.0;

int y = (int) x;

double z = x / y;

System.out.println(z);

What is printed as a result of executing the code segment?

a)
1.0
b)
7.0
c)
0.0
d)
None of the above
6.

Consider the following code segment.

int a = 10;

double b = 3.0;

double c = (double) a / b;

System.out.print(a + " " + b + " " + c);

What is printed as a result of executing the code segment?

a)
10 3.0 3.3333333333333335
b)
10 3.0 3.0
c)
10 3.0 3.33
d)
10 3.0 3.3
7.

Consider the following code segment.

int x = 6;

double y = (double) (x / 2);

System.out.println(x + " " + y);

What is printed as a result of executing the code segment?

a)

6 3.0

b)

6 3

c)

3 3.0

d)

3 3

8.

Consider the following code segment.

double num1 = 9.7;

int num2 = (int) (num1 + 0.5);

System.out.print(num2);

What is printed as a result of executing the code segment?

a)
9
b)
10
c)
9.7
d)
10.2
9.

Consider the following code segment.

double num1 = 3.14;

int num2 = (int) num1;

double result = num1 * num2;

System.out.println(result);

What is printed as a result of executing the code segment?

a)
3.14
b)
9.42
c)
6.28
d)
0
10.

Consider the following code segment.

double x = 9.5;

int y = (int) (x / 3);

double z = (double) y / 2;

System.out.print(y);

System.out.print(" ");

System.out.print(z);

What is printed as a result of executing the code segment?

a)

3 1.5

b)

3 1.0

c)

2 1.0

d)

2 1.5

11.

Consider the following code segment.

double a = 7;

int b = (int) (a / 2);

double c = (double) b / 2;

System.out.print(b);

System.out.print(" ");

System.out.print(c);

What is printed as a result of executing the code segment?

a)

3 1.5

b)

3.5 1.75

c)

3 1

d)

4 2