wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

19W Final Exam - PLTW

Total questions: 15

Worksheet time: 8mins

Name
Class
Date
1.
a)

I Only

b)

II Only

c)

III Only

d)

I and II Only

2.

Consider the following code segment.

int a = 5;

int b = 8;

int c = 3;

System.out.println(a + b / c * 2);

What is printed as a result of executing this code?

a)

9

b)

8

c)

6

d)

2

3.

Consider the following static method:

public static int calculate(int x)

{

  x = x + x;

  x = x + x;

  x = x + x;

 

  return x;

}

 

Which of the following can be used to replace the body of calculate so that the modified version of calculate will return the same result as the original version for all x ?

a)

return 4 * x;

b)

return 2 + x;

c)

return 3 * x;

d)

return 8 * x;

4.

Which of the following expressions evaluate to 3.5 ?

I. (double) 2 / 4 + 3

II. (double) (2 / 4) + 3

III. (double) (2 / 4 + 3)

a)

I Only

b)

II Only

c)

I and II Only

d)

I and III Only

5.

The code segment below is intended to calculate the circumference c of a circle with the diameter d of 1.5. The circumference of a circle is equal to its diameter times pi.

/* missing declarations */

c = pi * d;

Which of the following variable declarations are most appropriate to replace /* missing declarations */ in this code segment?

a)

int pi = 3.14159;

int d = 1.5;

final int c;

b)

final int pi = 3.14159;

int d = 1.5;

int c;

c)

final double pi = 3.14159;

double d = 1.5;

double c;

d)

double pi = 3.14159;

double d = 1.5;

final double c = 0.0;

6.

Consider the following code segment.

int x = 5;

int y = 6;

/* missing code */

z = (x + y) / 2;

Which of the following can be used to replace /* missing code */ so that the code segment will compile?

I. int z = 0;

II. int z;

III. boolean z = false;

a)

I Only

b)

II Only

c)

I and II Only

d)

II and III Only

7.

A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 0.5. A player’s average score is stored in avgScore, and the number of players who meet the criterion is stored in the variable count.

Which of the following pairs of declarations is most appropriate for the code segment described?

a)

double avgScore;

boolean count;

b)

double avgScore;

double count;

c)

double avgScore;

int count;

d)

int avgScore;

boolean count;

8.

Consider the following code segment.

int a = 5;

int b = 2;

double c = 3.0;

System.out.println(5 + a / b * c - 1);

What is printed when the code segment is executed?

a)

1.1

b)

9.0

c)

10.0

d)

12.5555

9.

Assume that x and y are variables of type int. Which of the following Java expressions never results in a division by zero?

a)

(y / x) == 0

b)

((y / x) == 0) && (x != 0)

c)

((y / x) == 0) || (x != 0)

d)

(x != 0) && ((y / x) == 0)

10.

Consider the following code segment.

System.out.print("Hello System.out.println");

System.out.print("!!!");

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

a)

Hello!!!

b)

Hello System.out.println!!!

c)

Hello

!!!

11.

Consider the following code segment.

double firstDouble = 2.5;

int firstInt = 30;

int secondInt = 5;

double secondDouble = firstInt - secondInt / firstDouble + 2.5;

What value will be assigned to secondDouble when the code segment is executed?

a)

5

b)

13

c)

15.25

d)

30.5

12.

The following code segment is intended to interchange the values of the int variables x and y. Assume that x and y have been properly declared and initialized.

int temp = x;

/* missing code */

Which of the following can be used to replace /* missing code */ so that the code segment works as intended?

a)

x = y;

x = temp;

b)

x = y;

y = temp;

c)

y = x;

x = temp;

d)

y = x;

temp = y;

13.

Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized.

/* missing code */

System.out.print(onesDigit);

System.out.print(tensDigit);

Which of the following can be used to replace /* missing code */ so that the code segment works as intended?

a)
b)
c)
d)
14.

Consider the following code segment.

int a = 3 + 2 * 3;

int b = 4 + 3 / 2;

int c = 7 % 4 + 3;

double d = a + b + c;

What is the value of d after the code segment is executed?

a)

14.0

b)

16.5

c)

20.0

d)

20.5

15.

Which of the following expressions evaluate to 7 ?

I. 9 + 10 % 12

II. (9 + 10) % 12

III. 9 – 2 % 12

a)

I

b)

II Only

c)

II and III

d)

I and III Only