Wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

CPA UNIT 1

Total questions: 10

Worksheet time: 5mins

Name
Class
Date
1.

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)

18.0

c)

20.0

d)

20.5

2.

{

if (num % 2 == 0)

{

System.out.println("A");

}

else

{

System.out.println("B");

}

}

Which of the following best describes the result of executing the code segment?

a)

When num is a negative odd integer, "B" is printed; otherwise, "A" is printed.

b)

When num is a negative even integer, "B" is printed; otherwise, nothing is printed.

c)

When num is a positive even integer, "A" is printed; otherwise, "B" is printed.

d)

When num is a positive even integer, "A" is printed; when num is a positive odd integer, "B" is printed; otherwise, nothing is printed.

3.

Consider the method getHours, which is intended to calculate the number of hours that a vehicle takes to travel between two mile markers on a highway if the vehicle travels at a constant speed of 60 miles per hour. A mile marker is a sign showing the number of miles along a road between some fixed location (for example, the beginning of a highway) and the current location.

The following table shows two examples of the intended behavior of getHours, based on the int parameters marker1 and marker2.

Consider the following implementation of getHours.

public static double getHours(int marker1, int marker2)

{

/* missing statement */

return hours;

}

Which of the following statements can replace /* missing statement */ so getHours works as intended?

a)

double hours = (Math.abs(marker1) - Math.abs(marker2)) / 60.0;

b)

double hours = Math.abs(marker1 - marker2 / 60.0);

c)

double hours = Math.abs(marker1 - marker2) / 60.0;

d)

double hours = Math.abs((marker1 - marker2) / 60);

4.

Consider the following method.

public static void message(int a, int b, int c)

{

if (a < 10)

{

if (b < 10)

{

System.out.print("X");

}

System.out.print("Y");

}

if (c < 10)

{

if (b > 10)

{

System.out.print("Y");

}

else

{

System.out.print("Z");

}

}

}

What is printed as a result of the call message(5, 15, 5) ?

a)

xy

b)

xyz

c)

y

d)

yy

5.

Consider the following class definition.

public class Bird

{

private String species;

private String color;

private boolean canFly;

public Bird(String str, String col, boolean cf)

{

species = str;

color = col;

canFly = cf;

}

}

Which of the following constructors, if added to the Bird class, will cause a compilation error?

a)

public Bird()

{

species = "unknown";

color = "unknown";

canFly = false;

}

b)

public Bird(boolean cf)

{

species = "unknown";

color = "unknown";

canFly = cf;

}

c)

public Bird(String col, String str)

{

species = str;

color = col;

canFly = false;

}

d)

public Bird(String col, String str, boolean cf)

{

species = str;

color = col;

canFly = cf;

}

6.

Which of the following expressions evaluate to 3.5 ?

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

a)

I only

b)

III only

c)

I only

d)

I and II only

e)

II and III only

7.

int num = /* initial value not shown */;

boolean b1 = true;

if (num > 0)

{

if (num >= 100)

{

b1 = false;

}

}

else

{

if (num >= -100)

{

b1 = false;

}

}

Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num ?

a)

boolean b2 = (num > -100) && (num < 100);

b)

boolean b2 = (num > -100) || (num < 100);

c)

boolean b2 = (num < -100) || (num > 100);

d)

boolean b2 = (num < -100) || (num > 0 && num < 100);

8.

public class Points

{

private double num1;

private double num2;

public Points(int n1, int n2) // Line 6

{

num1 = n1; // Line 8

num2 = n2; // Line 9

}

public void incrementPoints(int value) // Line 12

{

n1 += value; // Line 14

n2 += value; // Line 15

}

}

The class does not compile. Which of the following identifies the error in the class definition?

a)

In line 6, the Points constructor must have a void return type.

b)

In lines 8 and 9, int values cannot be assigned to double variables.

c)

In line 12, the incrementPoints method must have a non-void return type.

d)

n lines 14 and 15, the variables n1 and n2 are not defined.

9.

Consider the following code segment.

ArrayList<Integer> numList = new ArrayList<Integer>();

numList.add(3);

numList.add(2);

numList.add(1);

numList.add(1, 0);

numList.set(0, 2);

System.out.print(numList);

What is printed by the code segment?

a)

[1, 3, 0, 1]

b)

[2, 0, 2, 1]

c)

[2, 0, 2, 3]

d)

[2, 3, 2, 1]

10.

Consider the following method.

public static void printSome(int num1, int num2)

{

for (int i = 0; i < num1; i++)

{

if (i % num2 == 0 && i % 2 == 0)

{

System.out.print(i + " ");

}

}

}

Which of the following method calls will cause "0 10 " to be printed?

a)

printSome(0, 20)

b)

printSome(5, 10)

c)

printSome(10, 5)

d)

printSome(20, 5)