Search Header Logo

AP CS A Test #2

Computers

9th - 12th Grade

Used 1+ times

AP CS A Test #2
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

12 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Consider the following code segment:

int x = 3;

double y = Math.pow(x, 3);

double z = Math.sqrt(y - Math.abs(-2));

What is the value of z after this code segment has executed?

0.0

3.0

5.0

9.0

10.0

2.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

What does the expression "Nomnomnom yummy turkey".indexOf("Thank") evaluate to?

Thank

Nomnomnom

turkey

-1

0

3.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Consider the following code segment.

int x = /* some integer value */ ;

int y = /* some integer value */ ;

boolean result = (x < y);

result = ( (x >= y) && !result );

Which of the following best describes the conditions under which the value of result will be true after the code segment is executed?

Only when x < y

Only when x >= y

Only when x and y are equal

The value will always be true .

The value will always be false .

4.

MULTIPLE CHOICE QUESTION

5 mins • 1 pt

Consider the following instance variables and incomplete method that are part of a class that represents an item. The variables years and months are used to represent the age of the item, and the value for months is always between 0 and 11, inclusive. Method updateAge is used to update these variables based on the parameter extraMonths that represents the number of months to be added to the age.

private int years;

private int months; // 0 <= months <= 11

public void updateAge(int extraMonths)

{

/* body of updateAge */

}

Which of the following code segments shown below could be used to replace /* body of updateAge */ so that the method will work as intended?

I int yrs = extraMonths % 12;

int mos = extraMonths / 12;

years = years + yrs;

months = months + mos;

II int totalMonths = years * 12 + months + extraMonths;

years = totalMonths / 12;

months = totalMonths % 12;

III int totalMonths = months + extraMonths;

years = years + totalMonths / 12;

months = totalMonths % 12;

I only

II only

III only

I and II only

II and III

5.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Consider the following method.

public static void conditionalTest(int a, int b)

{

if ((a > 0) && (b > 0))

{

if (a > b)

System.out.println("A");

else

System.out.println("B");

}

else if ((b < 0) || (a < 0))

System.out.println("C");

else

System.out.println("D");

}

What is printed as a result of the call conditionalTest(3, -2) ?

A

B

C

D

Nothing is printed

6.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

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 possible values of x ?

return 3 + x;

return 3 * x;

return 4 * x;

return 6 * x;

return 8 * x;

7.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Assume that a and b are boolean variables and have been properly initialized.

(a && b) || !(a && b)

The result of evaluating the expression above is best described as:

Always true

Always false

true only when a is true and b is true

true only when a and b have the same value

true only when a and b have different values

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?