AP Computer Science A study materials
AP Computer Science A study resources

AP Computer Science A practice questions

One question at a time. Build your AP Computer Science A understanding.

First 50 of 200 questions. Each one lists the choices, the correct answer, and an explanation. The checker at the top of this page saves progress in this browser when it is available.

Continue in the practice tool above to load more questions, 50 at a time.

Question 1Using Objects and Methods

Consider the declaration `String s = "hello";`. Which method returns the number of characters in `s`?

  1. s.length() Correct answer.
  2. s.length
  3. s.size()
  4. s.count()

Explanation

For a String object, the method `length()` returns the number of characters it contains, so `s.length()` evaluates to 5. Strings are objects, so the method must be called with parentheses. Arrays use a `length` field, but String does not.

Question 2Using Objects and Methods

A programmer writes `int n = 7 / 2;`. What value is stored in `n`?

  1. 4
  2. 3 Correct answer.
  3. 3.5
  4. 3.0

Explanation

Both operands are ints, so Java performs integer division and truncates the fractional part, giving 3. No rounding occurs, so the result is not 4. The result is an int, so a value like 3.5 cannot be stored.

Question 3Using Objects and Methods

Given `String word = "Computer";`, what does `word.substring(2, 5)` return?

  1. "mpu" Correct answer.
  2. "mput"
  3. "omp"
  4. "put"

Explanation

`substring(a, b)` returns the characters from index a up to but not including index b. Index 2 is 'm', index 3 is 'p', and index 4 is 'u', so the result is "mpu". The character at the ending index is excluded.

Question 4Using Objects and Methods

Which line correctly declares and constructs a Scanner object that reads from standard input?

  1. Scanner sc = new Scanner();
  2. sc = new Scanner(System.in);
  3. Scanner sc = new Scanner(System.in); Correct answer.
  4. Scanner sc = Scanner(System.in);

Explanation

Creating an object requires the `new` keyword followed by a constructor call, and the type must be declared for a new variable. `new Scanner(System.in)` passes the input stream to the constructor. Omitting `new` or the argument causes a compile error.

Question 5Using Objects and Methods

What is the value of `Math.abs(-4.7)`?

  1. 4.7 Correct answer.
  2. 5
  3. 4
  4. -4.7

Explanation

`Math.abs` returns the absolute value of its argument. Because the argument is a double, the result is also a double, so the value is 4.7. No rounding or truncation takes place.

Question 6Using Objects and Methods

What is printed by the following code? Integer a = 100; Integer b = 100; System.out.println(a == b);

  1. false
  2. A compile error occurs.
  3. 100
  4. true Correct answer.

Explanation

For Integer values in the range -128 to 127, Java reuses cached objects, so `a` and `b` refer to the same object and `==` compares references that are equal. The expression prints true. Comparing wrapper values with `==` is risky outside the cached range.

Question 7Using Objects and Methods

Which expression correctly compares the contents of two String objects `s1` and `s2`?

  1. s1 == s2
  2. s1.equals(s2) Correct answer.
  3. s1.compare(s2)
  4. s1 = s2

Explanation

The `equals` method compares the characters stored in two String objects. The `==` operator compares references, so it may be false even when the text matches. Assignment with `=` does not produce a comparison.

Question 8Using Objects and Methods

A method is declared as `public static int mystery(int x)`. Which call is valid?

  1. mystery(3.7)
  2. mystery()
  3. mystery("3")
  4. mystery(3) Correct answer.

Explanation

The parameter type is int, so the argument must be assignable to int without narrowing. The literal 3 is an int and matches the single parameter. A double or String argument does not match, and the method requires one argument.

Question 9Using Objects and Methods

What is the value of `(int) 9.99`?

  1. 10
  2. 9 Correct answer.
  3. 9.99
  4. 0

Explanation

Casting a double to an int truncates toward zero, discarding the fractional part. The result is 9, not a rounded value. The cast produces an int, so no decimal portion remains.

Question 10Using Objects and Methods

Given `String s = "banana";`, what does `s.indexOf("na")` return?

  1. 1
  2. 2 Correct answer.
  3. 4
  4. 3

Explanation

`indexOf` returns the index of the first occurrence of the substring. In "banana", the characters are b(0) a(1) n(2) a(3) n(4) a(5), so "na" first begins at index 2. It does not return the last occurrence.

Question 11Using Objects and Methods

Which statement about the parameter of a method is correct when an int argument is passed?

  1. The argument must be a constant.
  2. The method can change the caller's variable directly.
  3. The method receives a copy of the argument's value. Correct answer.
  4. The method receives a reference to the caller's variable.

Explanation

Java passes arguments by value, so the parameter is initialized with a copy of the argument. Reassigning the parameter inside the method has no effect on the caller's variable. The argument can be any expression of the correct type.

Question 12Using Objects and Methods

What is the result of `Math.pow(2, 3)`?

  1. 8
  2. 6.0
  3. 9.0
  4. 8.0 Correct answer.

Explanation

`Math.pow` computes the first argument raised to the power of the second, so 2 cubed is 8. The method returns a double, so the value is 8.0. It does not multiply the arguments.

Question 13Using Objects and Methods

Given `String s = "program";`, what does `s.substring(3)` return?

  1. "gra"
  2. "ogra"
  3. "pro"
  4. "gram" Correct answer.

Explanation

A single-argument `substring` returns characters from the given index through the end of the String. Index 3 is 'g', so the result is "gram". The starting index is included.

Question 14Using Objects and Methods

What is printed by the following code? int x = 5; System.out.println(x + 3 + "7");

  1. 8 7
  2. 87 Correct answer.
  3. 15
  4. 537

Explanation

Operators of equal precedence are evaluated left to right, so `x + 3` is computed first as the int 8. Then 8 is concatenated with "7" to form the String "87". The addition happens before string concatenation.

Question 15Using Objects and Methods

Which of the following correctly creates an ArrayList that stores String objects?

  1. ArrayList list = new ArrayList<String>();
  2. ArrayList<String> list = new ArrayList<String>(); Correct answer.
  3. ArrayList<String> list = new ArrayList();
  4. ArrayList list = new ArrayList<String>;

Explanation

An ArrayList variable must specify its element type in angle brackets, and the constructor call needs `new` plus parentheses. `ArrayList<String> list = new ArrayList<String>();` satisfies both requirements. Omitting the type parameter or the parentheses causes errors.

Question 16Using Objects and Methods

Given `ArrayList<Integer> nums = new ArrayList<Integer>();` with elements [10, 20, 30], what does `nums.get(1)` return?

  1. 10
  2. 1
  3. 20 Correct answer.
  4. 30

Explanation

ArrayList indexing is zero-based, so index 1 refers to the second element, which is 20. `get` returns the element at the given index. The index itself is not returned.

Question 17Using Objects and Methods

What is printed by the following code? String a = "cat"; String b = a; b = b + "s"; System.out.println(a);

  1. cats
  2. cat cats
  3. cat Correct answer.
  4. s

Explanation

Strings are immutable, so `b + "s"` creates a new String and assigns it to `b`. The variable `a` still refers to the original "cat". Reassignment of `b` does not affect `a`.

Question 18Using Objects and Methods

What is the value of the expression `Math.sqrt(16)`?

  1. 8.0
  2. 2.0
  3. 4
  4. 4.0 Correct answer.

Explanation

`Math.sqrt` returns the square root of its argument as a double, so the value is 4.0. It does not return an int even when the result is a whole number. The square root of 16 is 4, not 8 or 2.

Question 19Using Objects and Methods

A method is declared as `public static void greet(String name)`. What does `void` indicate?

  1. The method takes no parameters.
  2. The method returns nothing. Correct answer.
  3. The method cannot be called.
  4. The method returns a String.

Explanation

The keyword `void` in a method header means the method does not return a value. A method that returns nothing cannot be used where a value is required. The parameter list is separate from the return type.

Question 20Using Objects and Methods

What is the result of `Math.round(2.5)`?

  1. 2
  2. 3.0
  3. 2.5
  4. 3 Correct answer.

Explanation

`Math.round(double)` returns the closest long, rounding half up, so 2.5 becomes 3. The return type for a double argument is long, printed as 3. It does not truncate.

Question 21Using Objects and Methods

Given `String s = "abcdef";`, what does `s.indexOf('c')` return?

  1. 4
  2. 1
  3. 2 Correct answer.
  4. 3

Explanation

String indices start at 0, so 'a' is at 0, 'b' is at 1, and 'c' is at 2. `indexOf` returns the position of the first occurrence. It does not use one-based counting.

Question 22Using Objects and Methods

Consider the code: int a = 3; int b = 4; double avg = (a + b) / 2; System.out.println(avg); What is printed?

  1. 7.0
  2. 4.0
  3. 3.0 Correct answer.
  4. 3.5

Explanation

Both operands of the division are ints, so `(a + b) / 2` performs integer division and yields 3. That int is then widened to 3.0 when stored in the double variable. To get 3.5, one operand would need to be a double.

Question 23Using Objects and Methods

What is printed by the following code? String s = "racecar"; System.out.println(s.substring(0, 3).equals(s.substring(4, 7)));

  1. false Correct answer.
  2. racecar
  3. A runtime error occurs.
  4. true

Explanation

Java substring excludes the ending index. The two substrings are "rac" and "car", which have different character sequences. Their equals comparison therefore returns and prints false.

Question 24Using Objects and Methods

Given `ArrayList<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c");`, what is the result of `list.set(1, "z")` followed by `list.get(1)`?

  1. "a"
  2. "z" Correct answer.
  3. "b"
  4. "c"

Explanation

`set(index, value)` replaces the element at the given index and returns the old element. After replacing index 1 with "z", `get(1)` returns "z". The size of the list is unchanged.

Question 25Using Objects and Methods

Consider the method: public static void change(int[] arr) { arr[0] = 99; } If called as `change(nums);`, what happens to `nums[0]` in the caller?

  1. It becomes 99 because the reference is copied and points to the same array. Correct answer.
  2. A compile error occurs because arrays cannot be parameters.
  3. It becomes 99 only if nums has length 1.
  4. It remains unchanged because arguments are passed by value.

Explanation

The reference to the array is passed by value, so the parameter refers to the same array object as the caller's variable. Modifying an element through that reference changes the shared array. Reassigning the parameter itself would not affect the caller.

Question 26Using Objects and Methods

A class declares a private instance variable `count` and provides `public int getCount()` and `public void setCount(int c)`. What is the primary purpose of these two methods?

  1. To allow the field to be declared static instead of private
  2. To control access to the private field through a public interface Correct answer.
  3. To convert the field into a constant that cannot change
  4. To let other classes modify the field directly without restrictions

Explanation

Accessor and mutator methods expose a controlled public interface while the field itself stays private, which is encapsulation. Other classes interact with the field only through these methods.

Question 27Using Objects and Methods

Consider the declaration `String s = new String("hello");`. Which statement about `s` is accurate?

  1. `s` holds the integer value of the characters summed together
  2. `s` is a primitive char array named hello
  3. `s` stores the characters directly in a primitive variable
  4. `s` holds a reference to a String object created by the constructor call Correct answer.

Explanation

Declaring a variable of a class type creates a reference, and `new` constructs an object that the reference points to. String is a class type, not a primitive.

Question 28Using Objects and Methods

Given `String word = "COMPUTER";`, what does `word.length()` return?

  1. 0
  2. 9
  3. 8 Correct answer.
  4. 7

Explanation

The `length()` method returns the number of characters in the string. "COMPUTER" has eight characters, so the result is 8.

Question 29Using Objects and Methods

A method is declared as `public static double half(int n)`. Which statement about calling it is correct?

  1. The method cannot be called from a main method
  2. The call must be preceded by the keyword new
  3. The argument must be a double, since the return type is double
  4. The argument may be an int, and the returned value can be assigned to a double variable Correct answer.

Explanation

The parameter type is int, so an int argument is appropriate, and the returned double can be stored in a double variable. The return type does not restrict the argument type.

Question 30Using Objects and Methods

What is printed by the following code? ``` String s = "abcdef"; System.out.println(s.substring(1, 4)); ```

  1. bcde
  2. abcd
  3. cde
  4. bcd Correct answer.

Explanation

The two-argument substring takes characters from the first index up to but not including the second index. Indices 1, 2, and 3 give "bcd".

Question 31Using Objects and Methods

What is printed by the following code? ``` String a = "Java"; String b = "Java"; System.out.println(a.equals(b)); ```

  1. 0
  2. true Correct answer.
  3. A compile-time error occurs
  4. false

Explanation

`equals` compares the character contents of two String objects, and both strings contain the same characters. The result is the boolean value true.

Question 32Using Objects and Methods

What is printed by the following code? ``` Integer x = 5; int y = x; System.out.println(y + 1); ```

  1. A NullPointerException is thrown
  2. 51
  3. 6 Correct answer.
  4. 5

Explanation

Assigning an Integer to an int triggers automatic unboxing, so y holds the int value 5. Adding 1 gives 6.

Question 33Using Objects and Methods

What is printed by the following code? ``` ArrayList<Integer> list = new ArrayList<Integer>(); list.add(4); list.add(7); list.add(9); list.set(1, 5); System.out.println(list); ```

  1. [5, 7, 9]
  2. [4, 7, 9, 5]
  3. [4, 5, 9] Correct answer.
  4. [4, 7, 5]

Explanation

`set(index, value)` replaces the element at the given index without changing the size. Index 1 changes from 7 to 5.

Question 34Using Objects and Methods

What is printed by the following code? ``` String s = "keyboard"; System.out.println(s.indexOf("board")); ```

  1. 3 Correct answer.
  2. 0
  3. -1
  4. 4

Explanation

`indexOf` returns the starting index of the first occurrence of the substring. "board" begins at index 3 of "keyboard".

Question 35Using Objects and Methods

What is printed by the following code? ``` public static int f(int n) { n = n * 2; return n; } int value = 6; int result = f(value); System.out.println(value + " " + result); ```

  1. 6 6
  2. 6 12 Correct answer.
  3. 12 6
  4. 12 12

Explanation

Java passes primitive arguments by value, so the method receives a copy of 6. Reassigning the parameter does not change the caller's variable, which stays 6 while the returned value is 12.

Question 36Using Objects and Methods

What is printed by the following code? ``` ArrayList<String> words = new ArrayList<String>(); words.add("sun"); words.add("moon"); words.add("star"); words.remove(0); System.out.println(words.get(0)); ```

  1. star
  2. An IndexOutOfBoundsException is thrown
  3. moon Correct answer.
  4. sun

Explanation

Removing index 0 shifts the remaining elements left, so "moon" moves to index 0. The list then holds [moon, star].

Question 37Using Objects and Methods

What is printed by the following code? ``` public static void addOne(int n) { n = n + 1; } Integer boxed = 10; addOne(boxed); System.out.println(boxed); ```

  1. 10 Correct answer.
  2. A compile-time error occurs
  3. 0
  4. 11

Explanation

The Integer argument is unboxed to an int and passed by value, so the method modifies only its local copy. The original Integer object is unchanged.

Question 38Using Objects and Methods

What is printed by the following code? ``` String s = "a,b,c"; String[] parts = s.split(","); System.out.println(parts.length + " " + parts[2]); ```

  1. 3 b
  2. 4 c
  3. 3 c Correct answer.
  4. 2 c

Explanation

`split` divides the string at each comma, producing three pieces: "a", "b", and "c". The array length is 3 and index 2 holds "c".

Question 39Using Objects and Methods

Consider the method `public static int sum(int a, int b)`. Which call causes a compile-time error?

  1. `sum('a', 3)`
  2. `sum(2.5, 3)` Correct answer.
  3. `sum(2 + 3, 4)`
  4. `sum(2, 3)`

Explanation

A double argument cannot be passed to an int parameter without an explicit cast, so `sum(2.5, 3)` does not compile. An int literal, a char (widened to int), and an int expression are all acceptable.

Question 40Using Objects and Methods

A class has a private int field score and a method public void addPoints(int amount) { score = score + amount; }. An object of this class is created and addPoints(5) is called twice. Which statement best describes what happens to the object's score field?

  1. It holds only the value from the first call because the second call has no effect on an existing field.
  2. It holds the combined value from both calls because the field persists across method calls on the same object. Correct answer.
  3. It is reset to its default value of zero after each call because amount is a parameter.
  4. It holds only the value from the second call because the field resets at the start of each method call.

Explanation

An instance field belongs to the object and keeps its value between method calls, so each call adds to the value left by the previous call. Parameters like amount are local to a single call, but the field score is not reinitialized.

Question 41Selection and Iteration

What is printed by the following code? int x = 7; if (x % 2 == 0) { System.out.println("even"); } else { System.out.println("odd"); }

  1. nothing is printed
  2. 7
  3. even
  4. odd Correct answer.

Explanation

The remainder of 7 divided by 2 is 1, so the condition x % 2 == 0 is false. Execution therefore takes the else branch, which prints "odd".

Question 42Selection and Iteration

Which boolean expression is true exactly when the int variable n is a positive multiple of 5?

  1. n > 0 && n % 5 == 0 Correct answer.
  2. n > 0 || n % 5 == 0
  3. n % 5 == 0
  4. n >= 0 && n % 5 != 0

Explanation

A positive multiple of 5 must satisfy both conditions: greater than zero and divisible by 5 with no remainder. The && operator requires both operands to be true.

Question 43Selection and Iteration

How many times does the loop body execute? int count = 0; for (int i = 3; i < 10; i += 2) { count++; }

  1. 3
  2. 5
  3. 4 Correct answer.
  4. 7

Explanation

The values of i are 3, 5, 7, and 9, all less than 10. Each value runs the body once, giving four iterations.

Question 44Selection and Iteration

What is the value of total after this code runs? int total = 0; int k = 1; while (k <= 4) { total += k; k++; }

  1. 10 Correct answer.
  2. 15
  3. 4
  4. 6

Explanation

The loop adds 1, 2, 3, and 4, and the running sum is 1, 3, 6, 10. When k becomes 5 the condition fails, leaving total equal to 10.

Question 45Selection and Iteration

Which statement about a do-while loop is correct?

  1. It cannot contain a break statement
  2. Its body executes at least once even if the condition is false initially Correct answer.
  3. It can never execute its body more than once
  4. Its condition is tested before the first iteration

Explanation

A do-while loop tests its condition after running the body, so the body always runs at least one time. A while loop, by contrast, may skip its body entirely.

Question 46Selection and Iteration

What is printed? int a = 5; int b = 8; if (a > b) System.out.print("A"); System.out.print("B"); System.out.print("C");

  1. BC Correct answer.
  2. AC
  3. ABC
  4. C

Explanation

Without braces, only the first statement after if belongs to the conditional. Since 5 > 8 is false, "A" is skipped, but the indented print of "B" always runs, followed by "C".

Question 47Selection and Iteration

What is the final value of sum? int sum = 0; for (int i = 0; i < 5; i++) { if (i == 3) { continue; } sum += i; }

  1. 4
  2. 7 Correct answer.
  3. 3
  4. 10

Explanation

The continue statement skips the rest of the body only when i is 3. The added values are 0, 1, 2, and 4, which total 7.

Question 48Selection and Iteration

Which loop header iterates over exactly the values 1, 2, 3, 4, 5?

  1. for (int i = 0; i <= 5; i++)
  2. for (int i = 1; i < 5; i++)
  3. for (int i = 1; i <= 5; i++) Correct answer.
  4. for (int i = 5; i > 0; i--)

Explanation

Starting at 1 and continuing while i <= 5 includes all of 1 through 5. The increment of 1 visits each integer exactly once.

Question 49Selection and Iteration

Consider the code: int n = 0; while (n < 3) { System.out.print(n); n = n + 1; } System.out.print(n); What is printed?

  1. 012
  2. 0123 Correct answer.
  3. 01234
  4. 123

Explanation

The loop prints 0, 1, and 2, then increments n to 3, which ends the loop. The final print outside the loop outputs the value 3.

Question 50Selection and Iteration

What is the value of result? int result = 0; for (int i = 1; i <= 4; i++) { for (int j = 1; j <= i; j++) { result++; } }

  1. 10 Correct answer.
  2. 16
  3. 4
  4. 8

Explanation

The inner loop runs i times for each outer value, so the total is 1 + 2 + 3 + 4. That sum equals 10.