Home › Free questions › AP Computer Science A
Free · no account, no card

Free AP Computer Science A Practice Questions

Ten multiple-choice questions from our AP Computer Science A practice exams, many of them reading Java code and predicting what it does.

Take these as an interactive quiz The full AP Computer Science A exams

Question 1

Consider the following code segment.
String s = "Computer";
System.out.println(s.substring(3, 6));
What is printed?
  1. A. pute
  2. B. mpu
  3. C. put
  4. D. mput
Show the answer

The answer is C. substring(3, 6) starts at index 3 and stops before index 6. In "Computer" the characters at 3, 4 and 5 are p, u and t, so put is printed.

  • A is wrong: This is the result of treating the second argument as inclusive, giving indexes 3 through 6. The end index is exclusive.
  • B is wrong: This starts at index 2 rather than 3, which is what happens when the character at index 0 is not counted as the first.
  • D is wrong: This combines both errors, starting one position early and running one position too far.

Question 2

Consider the following code segment.
int a = 7;
int b = 2;
double c = a / b;
System.out.println(c);
What is printed?
  1. A. 3
  2. B. 3.5
  3. C. 3.0
  4. D. 4.0
Show the answer

The answer is C. Both a and b are int, so a / b is integer division and evaluates to 3. Only then is 3 widened to a double for the assignment, so 3.0 is printed.

  • A is wrong: The value is stored in a double, so it prints with a decimal point. This is what would print if c were declared as an int.
  • B is wrong: This assumes the division happens in floating point. The type of the variable being assigned to does not change how the expression itself is evaluated.
  • D is wrong: Integer division truncates toward zero rather than rounding, so 3.5 becomes 3 and never 4.

Question 3

Consider the following method.
public static int mystery(int n)
{
    n = n * 2;
    return n;
}
Consider the code segment below.
int x = 5;
mystery(x);
System.out.println(x);
What is printed?
  1. A. 0
  2. B. Nothing is printed because the return value is not used
  3. C. 10
  4. D. 5
Show the answer

The answer is D. Java passes primitives by value. The method receives a copy of x and doubles the copy, so x in the caller is unchanged and 5 is printed.

  • A is wrong: The variable was initialized to 5 and nothing sets it to zero at any point.
  • B is wrong: Ignoring a returned value is legal in Java. The println statement runs normally.
  • C is wrong: This assumes the parameter is a reference back to the caller's variable. For a primitive it is a separate copy.

Question 4

Consider the following code segment.
int total = 0;
for (int i = 1; i <= 5; i++)
{
    total += i;
}
System.out.println(total);
What is printed?
  1. A. 5
  2. B. 10
  3. C. 14
  4. D. 15
Show the answer

The answer is D. The loop runs with i equal to 1, 2, 3, 4 and 5, adding each to total. The sum is 15.

  • A is wrong: This is the final value of i rather than the accumulated total.
  • B is wrong: This is the sum 1 through 4, which is what happens if the condition is read as strictly less than 5.
  • C is wrong: This is the sum with the first iteration omitted, as if i started at 2.

Question 5

Consider the following code segment.
int n = 0;
while (n < 10)
{
    n += 3;
}
System.out.println(n);
What is printed?
  1. A. 12
  2. B. The loop never terminates
  3. C. 9
  4. D. 10
Show the answer

The answer is A. n takes the values 3, 6, 9 and then 12. The test is made before each pass, so the loop exits once n is 12, and 12 is printed.

  • B is wrong: The value increases every pass and eventually exceeds 10, so the condition does become false.
  • C is wrong: This is the last value for which the condition was still true. The body runs once more after that test.
  • D is wrong: The variable increases in steps of three from zero, so it never takes the value 10 exactly.

Question 6

Consider the following code segment.
int x = 8;
if (x > 10)
    System.out.print("A");
else if (x > 5)
    System.out.print("B");
else if (x > 3)
    System.out.print("C");
else
    System.out.print("D");
What is printed?
  1. A. BCD
  2. B. BC
  3. C. C
  4. D. B
Show the answer

The answer is D. In an if / else if chain, at most one branch runs. The first true condition is x > 5, so B is printed and the rest are skipped.

  • A is wrong: The else branch is only reached when every preceding condition is false.
  • B is wrong: This would happen if each test were a separate if statement rather than a chain joined by else.
  • C is wrong: The condition x > 3 is true, but the chain never reaches it because an earlier condition already matched.

Question 7

Consider the following class.
public class Counter
{
    private int count;

    public Counter()
    {
        count = 0;
    }

    public void increment()
    {
        count++;
    }

    public int getCount()
    {
        return count;
    }
}
Consider the code segment below.
Counter c = new Counter();
c.increment();
c.increment();
c.increment();
System.out.println(c.getCount());
What is printed?
  1. A. 0
  2. B. A run-time error occurs because count is private
  3. C. 3
  4. D. 1
Show the answer

The answer is C. The constructor sets count to 0 and each call to increment adds one. Three calls leave it at 3.

  • A is wrong: The constructor sets count to zero, but the three increments run afterward and change it.
  • B is wrong: A private field is freely accessible from methods inside its own class, which is exactly what increment and getCount do.
  • D is wrong: This is the value after a single call. Each of the three calls to increment adds one.

Question 8

A class declares an instance variable as private rather than public. The main reason is that
  1. A. private variables are shared by every object of the class
  2. B. code outside the class cannot change the variable directly, so the class controls its own state
  3. C. private variables use less memory than public ones
  4. D. only private variables can be used inside constructors
Show the answer

The answer is B. This is encapsulation. Keeping data private forces other code to go through the class's own methods, so the class can enforce whatever rules its state has to obey.

  • A is wrong: Sharing one copy across every object is what static does. Each object has its own copy of an instance variable regardless of access.
  • C is wrong: Access modifiers control visibility at compile time and have no effect on how much memory a field occupies.
  • D is wrong: Constructors can assign to public fields just as easily as to private ones.

Question 9

Consider the following code segment.
int[] a = {4, 8, 15, 16, 21};
System.out.println(a[2] + a.length);
What is printed?
  1. A. 20
  2. B. 19
  3. C. 21
  4. D. 13
Show the answer

The answer is A. a[2] is the third element, 15, and a.length is 5. Their sum is 20.

  • B is wrong: This adds the last index, 4, instead of the length, which is 5.
  • C is wrong: This uses a[3] rather than a[2], which is one position too far right.
  • D is wrong: This uses a[1] rather than a[2], which is one position too far left.

Question 10

Consider the following code segment.
int[] nums = {3, 1, 4, 1, 5};
int m = nums[0];
for (int i = 1; i < nums.length; i++)
{
    if (nums[i] > m)
    {
        m = nums[i];
    }
}
System.out.println(m);
What is printed?
  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 1
Show the answer

The answer is A. The loop keeps the largest value seen so far, so it finds the maximum, which is 5.

  • B is wrong: This is the largest value in the first three elements, but the loop continues to the end of the array.
  • C is wrong: This is the starting value taken from the first element, before the loop replaced it.
  • D is wrong: This would be the result if the comparison looked for smaller values instead of larger ones.