wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

APCSA Semester Review

Total questions: 61

Worksheet time: 31mins

Name
Class
Date
1.

> public class MyClass

> {

> public static void main(String[] args)

> {

> String.out.Println("Hi there!")

> }

> }

a.

Change String to System

b.

Change Println to println

c.

Remove the ! exclamation mark.

d.

Add a semi-colon to the end of the Println statement.

e.

Remove the quotations around Hi there!

a)

a

b)

b

c)

c

d)

d

e)

e

2.

What is the output of the following code?

> System.out.println("Hello World!");

> System.out.println("I ");

> System.out.print("like");

> System.out.print("pie");

a.

Hello World!

I

like

pie

b.

Hello World!

I

likepie

c.

Hello

World!

I like pie

d.

Hello World!

I like

pie

e.

Compiler Error

a)

a

b)

b

c)

c

d)

d

e)

e

3.

Consider the following code segment.

> System.out.print("AP");

> System.out.println();

> System.out.print("CS");

> System.out.print("A");

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

a.

APCSA

b.

APCS

A

c.

AP

C

SA

d.

AP

CS

A

e.

AP

CSA

a)

a

b)

b

c)

c

d)

d

e)

e

4.

Consider the following code segment.

> System.out.print("Java is ");

> System.out.println("fun ");

> System.out.println("and awesome!");

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

a.

Java is fun and awesome!

b.

Java isfun

and awesome!

c.

Java is

fun

and awesome!

d.

Java is fun

and

awesome!

e.

Java is fun

and awesome!

a)

a

b)

b

c)

c

d)

d

e)

e

5.

> System.out.print("*");

> System.out.print("**");

> System.out.println("***");

> System.out.print("****");

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

a.

******

****

b.

*

**

*******

c.

*

*****

****

d.

*

**

***

****

e.

***

*******

a)

a

b)

b

c)

c

d)

d

e)

e

6.

What is output by the code below?

> System.out.println("What's up ");

> System.out.println("doc!");

> System.out.print("Howdy");

a.

What's up

doc! Howdy

b.

What's up doc!

Howdy

c.

What's up

doc!

Howdy

d.

What's

up

doc! Howdy

e.

Compiler Error.

a)

a

b)

b

c)

c

d)

d

e)

e

7.

> System.out.println("Roses are red, "); // Line 1

> System.out.println("Violets are blue, "); // Line 2

> System.out.print("You got this, "); // Line 3

> System.out.print("I know you do!"); // Line 4

The code segment is intended to produce the following output but may not work as intended.

> Roses are red,

> Violets are blue,

> You got this, I know you do!

Which change, if any, can be made so that the code segment produces the intended output?

a.

Replacing print with println on lines 3 and 4.

b.

Replacing println with print on lines 1 and 2.

c.

Putting a space before the I on line 4.

d.

Changing the println on line 1 to a print and changing the print on line 3 and a println.

e.

The code segment works as intended.

a)

a

b)

b

c)

c

d)

d

e)

e

8.

What is the output of the following code?

System.out.println(hello!);

a.

hello!

b.

hello

c.

helo

d.

!olleh

e.

Compiler Error

a)

a

b)

b

c)

c

d)

d

e)

e

9.

Consider the following code segment.

> System.out.print("*"); // Line 1

> System.out.print("*"); // Line 2

> System.out.println(); // Line 3

> System.out.println("*"); // Line 4

The code segment is intended to produce the following output, but may not work as intended.

**

*

Which line of code, if any, causes an error?

a.

Line 1

b.

Line 2

c.

Line 3

d.

Line 4

e.

The code segment works as intended.

a)

a

b)

b

c)

c

d)

d

e)

e

10.

What is the output of the following code?

> System.out.println("Hi there.");

a.

Hi there.

b.

Hithere

c.

Hithere.

d.

Hi

there.

e.

Compiler Error

a)

a

b)

b

c)

c

d)

d

e)

e

11.

Which of the following code segments are valid? Pick all that apply.

a)

final int i = 5; i = 7;

b)

double hi = 7; int i = 2; hi = i;

c)

boolean blah = false; blah = true;

d)

final int i; i = -7;

e)

int static = 92; static = 42;

12.

The code segment below is intended to calculate the perimeter p of a rectangle with a side length l of 4.2 and another side length w of 2. The perimeter of a rectangle is equal to 2*(l+w).

> /* missing declarations*/ > p = 2*(l+w);

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

a)

final int l = 4.2; final int w = 2; int p;

b)

int l = 4.2; final int w = 2; final int p;

c)

final double l = 4.2; final w = 2; int p;

d)

final double l = 4.2; final int w = 2; double p;

e)

ANSWER NOT HERE

13.

Which of the following line correctly defines an integer variable?

a)

int x = 123

b)

x = 123

c)

integer x = 123;

d)

int x = 123;

e)

integer x = 123;

14.

What is output by the code below? > boolean truth = false; > System.out.println("Yes or No: " + truth);

a)

truth

b)

Yes or No:

c)

Yes or No: false

d)

There is no output due to a syntax error.

e)

There is no output due to a runtime error.

15.

What is output by the code below? > double num = 10.2; > System.out.println("Number: " + num);

a)

10.2

b)

Number:

c)

Number: 10.2

d)

There is no output due to a syntax error.

e)

There is no output due to a runtime error.

16.

What is the output by the code? > double x = 5.0; > int y = x; > System.out.println(y);

a)

5

b)

5.0

c)

0

d)

There is no output due to a syntax error.

e)

There is no output due to a runtime error.

17.

What is the output by the code? > double x = 5; > int y = x; > System.out.println(y);

a)

5

b)

5.0

c)

0

d)

There is no output due to a syntax error.

e)

There is no output due to a runtime error.

18.

What is the output by the code? > int x = 5; > double y = x; > System.out.println(y);

a)

5

b)

5.0

c)

0

d)

There is no output due to a syntax error.

e)

There is no output due to a runtime error.

19.

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. double z; III. boolean z = false;

a)

I only

b)

II only

c)

I and II only

d)

II and III only

e)

I, II, and III

20.

A code segment (not shown) is intended to determine the number of players whose average score in a game exceeds 2.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;

e)

int avgScore; int count;

21.

What is the output? > int a = 5; > int b = 2; > double c = 7.0; > System.out.println(3 + a / b * c - 7);

a)

5.0

b)

2.0

c)

7.0

d)

10.0

e)

Answer Not Here

22.

What is the output? > int a = 3; > int b = 2; > double c = 4.0; > System.out.println((3 + a) / b * (c - 7));

a)

0.0

b)

5.0

c)

-9.0

d)

-2.0

e)

Answer Not Here

23.

What is the output? > int numGrades = 4; > int numPeople = 7; > System.out.println(numGrades numPeople - 5 / 2 + 4.0 3 / 2);

a)

0.0

b)

32

c)

-9.0

d)

87.0

e)

Answer Not Here

24.

What is the output? > System.out.println(3 / 0);

a)

0

b)

-1

c)

3

d)

3.0

e)

answer not here

25.

What is the output? > int a = 3; > int b = 2; > double c = 4.0; > System.out.println((3 + a) / (b * (c - 4)));

a)

0.0

b)

5.0

c)

-9.0

d)

-2.0

e)

Answer not here

26.

What is the output? If a decimal, round to two places. > double x = 10/3.0; > System.out.println(x);

a)

0

b)

3

c)

3.33

d)

3.0

e)

Answer Not here

27.

What is the output? > int a = 7; > int b = 2; > System.out.println(a % b);

a)

0

b)

1

c)

2

d)

3

e)

Answer Not Here

28.

What is the output? > int x = 22/3; > System.out.println(x);

a)

0

b)

7

c)

7.4

d)

8

e)

Answer Not Here

29.

What is output by the code below? > int x = 7, y = 5; > int z = x + y; > double b = z; > System.out.print(b);

a)

5

b)

7

c)

12

d)

12.0

e)

Answer not here

30.

What is the output? > double x = 6.4; > x = x % 4; > System.out.println(x);

a)

0

b)

2

c)

2.4

d)

3

e)

Answer not here

31.

What is the output? > int x = 27; > x = x / 8; > System.out.println(x);

a)

0

b)

3

c)

3.375

d)

4

e)

Answer not here

32.

What are the values of the variables after the following code executes?

int x = 4; int y = 1; int z = 3; x--; y++; z+=y;

a)

x = 0; y = 1; z = 1;

b)

x = 2; y = 4; z = 3;

c)

x = 3; y = 2; z = 5;

d)

x = 1; y = 1; z = 3;

e)

x = 2; y = 2; z = 4;

33.

What are the values of the variables after the following code executes?

int x = 0; int y = 3; int z = 6; x++; y -= 3; z = x + z; x = y * z; y %= 2; z--;

a)

x = 0; y = 1; z = 1;

b)

x = 5; y = 1; z = 1;

c)

x = 3; y = 1; z = 2;

d)

x = 3; y = 2; z = 1;

e)

x = 0; y = 0; z = 6;

34.

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 5; d++; double num = c; num /= d;

Which of the following best describes the behavior of the code segment?

a)

The code segment stores the value of (a + 5) / b in the variable num.

b)

The code segment stores the value of (a+5) / (b+1) in the variable num.

c)

The code segment stores the value of (a+5) / (b-1) in the variable num.

d)

The code segment stores the value of (a + 5) / (1 - b) in the variable num.

e)

The code segment causes a runtime error in the last line of code because num is type double and d is type int.

35.

Consider the following code segment.

int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c;

System.out.print(b);

What is printed when the code segment is executed?

a)

9

b)

11

c)

12

d)

13

e)

1

36.

Consider the following code segment. num /= 2.0; num *= num; 

Assume that num has been previously declared and initialized to contain a double value. Which of the following best describes the behavior of the code segment?

a)

The value of num is two times its original value.

b)

The value of num is the square of its original value.

c)

The value of num is two times the square of its original value.

d)

The value of num is the square of half its original value.

e)

It cannot be determined without knowing the initial value of num.

37.

What are the values of the variables after the following code executes?

int x = 4; int y = 5; int z = 1; x = z * 3; y = y / 2; z++;

a)

x = 3; y = 1; z = 1;

b)

x = 3; y = 2; z = 2;

c)

x = 2; y = 3; z = 1;

d)

x = 3; y = 1; z = 2;

e)

x = 2; y = 1; z = 2;

38.

Consider the following code segment. int x = 3; x += 5 * 3; x -= 3 / 2;

What value is stored in x after the code segment executes?

a)

12

b)

13

c)

15

d)

16

e)

17

39.

Consider the following code segment, where k and count are properly declared and initialized int variables.

k++; k++; count++; k--; count++;

Which of the following best describes the behavior of the code segment?

a)

The code segment leaves both k and count unchanged.

b)

The code segment increases both k and count by 2.

c)

The code segment increases k by 4 and count by 2.

d)

The code segment increases k by 1 and increases count by 2.

e)

The code segment increases k by 2 and leaves count unchanged.

40.

Consider the following code segment. int a = 3; int b = 5; a++; b++; int c = a + b; a -= 2; System.out.println(a + c);

What is printed when the code segment is executed?

a)

10

b)

11

c)

12

d)

2

e)

0

41.

Consider the following code segment. int a = 2; int b = 5; int c = a + b; a -= 1; c /= b; System.out.println(a + c);

What is printed when the code segment is executed?

a)

1

b)

2

c)

0

d)

2.5

e)

3

42.

Consider the following code segment, which is intended to find the average of two positive integers, x and y.

int x; int y; int sum = x + y; double average = (double) (sum / 2.0);

Which of the following best describes the error, if any, in the code segment?

a)

There is no error, and the code works as intended.

b)

In the expression (double) (sum / 2.0), the cast to double is applied too late, so the average will be less than the expected result for even values of sum.

c)

In the expression (double) (sum / 2.0), the cast to double is applied too late, so the average will be greater than the expected result for even values of sum.

d)

In the expression (double) (sum / 2.0), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.

e)

In the expression (double) (sum / 2.0), the cast to double is applied too late, so the average will be greater than the expected result for odd values of sum.

43.

Consider the following code segment.

double num = 14 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num);

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

a)

3 3

b)

3.0 3

c)

3.0 3.5

d)

3.5 3

e)

3.5 3.0

44.

Which of the following expressions evaluate to 4.25 ?

(double) 5 / 4 + 3 (double) (5 / 4) + 3 (double) (5 / 4 + 3)

a)

I only

b)

III only

c)

I and II only

d)

II and III only

e)

I, II, and III

45.

Consider the following code segment.

double x = (int) (8.5 - 2.5); double y = (int) 8.5 - 2.5; System.out.println(x - y);

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

a)

-1.0

b)

-0.5

c)

0.0

d)

0.5

e)

1.0

46.

Consider the following code segment.

int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y);

Which of the following best describes the results of compiling the code segment?

a)

The code segment does not compile, because the int variable x cannot be assigned the result of the operation w / 2.

b)

The code segment does not compile, because the integer value 3 cannot be assigned to the double variable y.

c)

The code segment does not compile, because the operands of the addition operator cannot be of different types int and double.

d)

The code segment does not compile because the result of the addition operation is of type double and cannot be cast to an int.

e)

The code segment compiles without error.

47.

Consider the following code segment.

double x = 4.5; double y = (int) x * 2; System.out.print(y);

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

a)

8

b)

8.0

c)

9

d)

9.0

e)

10

48.

The following code segment is intended to round val to the nearest integer and print the result.

double val = 2.1; int roundedVal = (int) (val + 0.5); System.out.println(roundedVal);

Which of the following best describes the behavior of the code segment?

a)

The code segment works as intended.

b)

The code segment does not work as intended because val and roundedVal should be declared as the same data type.

c)

The code segment does not work as intended because the expression (val + 0.5) should be cast to a double instead of an int.

d)

The code segment does not work as intended because val should be cast to an int before 0.5 is added to it.

e)

The code segment does not work as intended because the expression (int) (val + 0.5) rounds to the nearest integer only when val is positive.

49.

What will print out?

double num = 9 / 3; int val = num / 2; num = (double)val * val; System.out.println(num);

a)

1

b)

1.0

c)

0

d)

0.0

e)

Answer not here

50.

Consider the following code segment.

double a = 3; int y = (int)(a /2 ); System.out.print(y);

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

a)

0

b)

1

c)

2

d)

2.0

e)

Answer Not Here

51.

Consider the following code segment.

double a = 10.2483; System.out.println((int)a);

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

a)

10

b)

10.0

c)

10.2483

d)

11

e)

Answer not here

52.

A student has created a Dog class. The class contains variables to represent the following.

A String variable called breed to represent the breed of the dog An int variable called age to represent the age of the dog A String variable called name to represent the name of the dog

The object pet is declared as type Dog. Which of the following descriptions is accurate?

a)

An attribute of the name object is String.

b)

An attribute of the pet object is name.

c)

An instance of the pet class is Dog.

d)

An attribute of the Dog instance is pet.

e)

An instance of the Dog object is pet.

53.

A student has created a Movie class. The class contains variables to represent the following.

A String variable called title to represent the title of the movie A String variable called director to represent the director of the movie A double variable called rating to represent the rating of the movie

The object scaryMovie will be declared as type Movie. Which of the following descriptions is accurate?

a)

An attribute of the scaryMovie class is title.

b)

scaryMovie is an instance of the Movie class.

c)

Title, director, and rating are instances of the scaryMovie object.

d)

An attribute of the Movie instance is scaryMovie.

e)

Movie is an instance of scaryMovie.

54.

A student has created a Song class. The class contains the following variables.

A String variable called artist to represent the artist name. A String variable called title to represent the song title. A String variable called album to represent the album title.

The object happyBirthday will be declared as type Song.

Which of the following statements is true?

a)

artist, title, and album are instances of the Song class.

b)

happyBirthday is an instance of three String objects.

c)

happyBirthday is an instance of the Song class.

d)

Song is an instance of the happyBirthday object.

e)

Song is an instance of three String objects.

55.

A student has created a Liquid class. The class contains variables to represent the following.

A double variable called boilingPoint to represent the boiling point of the liquid. A double variable called freezingPoint to represent at what temperature the liquid will freeze. A boolean variable called hasFrozen to indicate whether the liquid is now frozen.

The object liquid1 will be declared as type Liquid. Which of the following statements is accurate?

a)

An instance of a Liquid object is hasFrozen.

b)

An attribute of boilingPoint object is double.

c)

An attribute of the liquid1 object is freezingPoint.

d)

An attribute of freezingPoint is liquid1

e)

An instance of the liquid1 class is Liquid.

56.

A student has created an OrderedPair class to represent points on an xy-plane. The class contains the following.

An int variable called x to represent an x-coordinate. An int variable called y to represent a y-coordinate. A method called printXY that will print the values of x and y.

The object origin will be declared as type OrderedPair.

Which of the following descriptions is accurate?

a)

origin is an instance of the printXY method.

b)

origin is an instance of the OrderedPair class.

c)

origin is an instance of two int objects.

d)

OrderedPair is an instance of the origin object.

e)

printXY is an instance of the OrderedPair class.

57.

A student has created an Artwork class. The class contains the following variables.

A String variable called artist to represent the artist’s name A String variable called title to represent the artwork’s title A String variable called gallery to represent the gallery title

The object painting1 will be declared as type Artwork. Which of the following statements is true?

a)

Artwork is an instance of three String objects.

b)

Artwork is an instance of the painting1 object.

c)

painting1 is an instance of three String objects.

d)

painting1 is an instance of the Artwork class.

e)

artist, title, and gallery are instances of the Artwork class.

58.

[QUESTION] A car dealership needs a program to store information about the cars for sale.For each car, they want to keep track of the following information: the number of doors (2 or 4),its average number of miles per gallon, and whether the car has air conditioning. Which of the following is the best design?

a)

Use one class, Car, which has three attributes: int numDoors, double mpg, and boolean hasAir.

b)

Use four unrelated classes: Car, Doors, MilesPerGallon, and AirConditioning

c)

Use a class, Car, which has three subclasses: Doors, MilesPerGallon, and AirConditioning

d)

Use a class Car, which has a subclass Doors, with a subclass AC, with a subclass MPG.

e)

Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.

59.

A student has created a Car class. The class contains variables to represent the following.

A String variable called color to represent the color of the car An int variable called year to represent the year the car was made A String variable called make to represent the manufacturer of the car A String variable called model to represent the model of the car

The object vehicle will be declared as type Car.

Which of the following descriptions is accurate?

a)

An instance of the vehicle class is Car.

b)

An instance of the Car object is vehicle.

c)

An attribute of the year object is int.

d)

An attribute of the vehicle object is color.

e)

An attribute of the Car instance is vehicle.

60.

A student has created a Cat class. The class contains variables to represent the following.

A String variable called color to represent the color of the cat A String variable called breed to represent the breed of the cat An int variable called age to represent the age of the cat

The object myCat will be declared as type Cat. Which of the following descriptions is accurate?

a)

An attribute of breed is String.

b)

color, breed, and age are instances of the Cat class.

c)

Cat is an instance of the myCat class.

d)

age is an attribute of the myCat object.

e)

An attribute of Cat is myCat.

61.

A student has created a Party class. The class contains variables to represent the following.

An int variable called numOfPeople to represent the number of people at the party. A boolean variable called discoLightsOn to represent whether the disco ball is on. A boolean variable called partyStarted to represent whether the party has started.

The object myParty is declared as type Party. Which of the following descriptions is accurate?

a)

boolean is an attribute of the myParty object.

b)

myParty is an attribute of the Party class.

c)

myParty is an instance of the Party class.

d)

myParty is an attribute of the Party instance.

e)

numOfPeople is an instance of the Party object.