Search Header Logo

APCSA Exam Review

Authored by Daniel Geiter

Computers

9th - 12th Grade

Used 4+ times

APCSA Exam Review
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

11 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

I Only

III Only

I and II Only

II and III only

I, II, and III

Answer explanation

In option I, the cast applies to the value 2, so floating-point

division is performed and the expression evaluates to 0.5 + 3, or 3.5. In option II, the cast applies to the result of the integer division 2 / 4, so the expression evaluates to 0.0 + 3, or 3.0. In option III, the cast applies to the sum of 3 and the result of the integer division 2 / 4, so the expression evaluates to (double) (0 + 3), or 3.0.

2.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

Consider the code segment to the left.

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

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

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

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

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

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

Answer explanation

In the body of the first if clause in the code segment, b1

retains the value true if num is between 0 and 100, exclusive.

In the body of the else clause, b1 retains the value true if

num is less than -100. The statement assigns true to b2 if

num is less than -100 or between 0 and 100, exclusive.

3.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

Consider the code segment to the left. Asume that a is greater than zero.

Which of the following best described the value assign to b when the code segment is executed?

a

2 * a

A random integer between 0 and a-1, inclusive

A random integer between a and 2 * a, inclusive

A random integer between a and 2 * a - 1, inclusive

Answer explanation

The random method returns a value between 0.0,

inclusive, and 1.0, exclusive. Multiplying that value by a and

casting to an int produces a result between 0 and a - 1,

inclusive. The sum of a and a value between 0 and a - 1,

inclusive, is a value between a and 2 * a - 1, inclusive.

4.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

Consider the code segment:

String[] testOne = {"first", "day", "of", "spring"};

String[] resultOne = strArrMethod(testOne);

What are the contents of resultOne when the code segment has been executed?

{"day", "first", "of", "spring"}

{"of", "day", "first", "spring"}

{"of", "day", "of", "spring"}

{"of", "of", "of", "spring"}

{"spring", "first", day", "of"}

Answer explanation

The method assigns the shortest string that occurs in any element of arr between arr[n] and arr[arr.length -

1], inclusive, to result[n]. The shortest string found between arr[0] and arr[3] is "of", so result[0] is assigned the value "of". The shortest string found between arr[1] and arr[3] is also "of", so result[1] is also assigned the value "of". The same is true for the part of the array that begins at index 2 and ends at index 3, so result[2] is also assigned the value "of". In the last iteration of the outer for loop, there are no values to consider after arr[3], so result[3] is assigned the

value "spring".

5.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

Consider the code segments below:

String[] testTwo {"last", "day", "of", "the", "school", "year"};

String[] resultTwo = strArrMethod(testTwo);

How many times is the line labeled / / Line 12 in the strArrMethod executed as a result of

executing the code segment?

4 times

5 times

6 times

15 times

30 times

Answer explanation

Line 12 is executed each time the variable sm is updated

because a new smallest value is found. When j has the value 0, sm is updated for "day" and "of". When j has the value 1, sm is updated for "of". When j has the value 4, sm is updated for "year". When j has any of the values 2, 3, or 5, sm is not updated. Line 12 is executed four times.

6.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

Consider the following two code segments. Assume that the int variables m and n have been

properly declared and initialized and are both greater than 0.

Assume that the initial values of m and n are the same in code segment I as they are in code

segment II. Which of the following correctly compares the number of times that "A" and "B"

are printed when each code segment is executed?

"A" is printed m fewer times than "B"

"A" is printed n fewer times than "B"

"A" is printed m more times than "B"

"A" is printed n more times than "B"

"A" and "B" are printed the same number of times.

Answer explanation

There are m * n iterations of the for loop in code

segment I. In code segment II, the outer loop executes m times and the inner loop executes n - 1 times for each iteration of the outer

loop. There are m * n - m iterations of the inner loop in code segment II, so "A" is printed m more times than "B" is printed.

7.

MULTIPLE CHOICE QUESTION

3 mins • 1 pt

Media Image

Consider the method to the left.

What, if anything, is returned by the method call abMethod ( "sing the song", "ng") ?

"si"

"si the so"

"si the song"

"sig the sog"

Nothing is returned because a StringindexOutOfBoundsException is thrown.

Answer explanation

The method abMethod(String a, String b)

removes all non-overlapping occurrences of string b from string a and returns the resulting String. It does this by repeatedly setting x to the index of an occurrence of b in a, then assigning a the result of the concatenation of the parts of a before and after the occurrence of b. The method call abMethod("sing the song", "ng") removes all occurrences of "ng" from "sing the song", returning "si the so".

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?