AP Computer Science A — Practice Examination

 

AP Computer Science A — Practice Examination

Time — 3 hours


Section I: Multiple Choice

Time: 90 minutes
Number of Questions: 40
Directions: Select the best answer for each question.


1.

What is printed?

int x = 5; int y = 2; System.out.println(x / y);

A. 2.5
B. 2
C. 3
D. Error
E. 2.0


2.

What is the value of result?

boolean result = (4 > 2) && (3 < 1);

A. true
B. false
C. 1
D. 0
E. compile error


3.

How many times will the loop execute?

for(int i = 1; i <= 10; i += 3)

A. 2
B. 3
C. 4
D. 5
E. infinite


4.

Which declaration correctly creates an array of 10 integers?

A. int arr = new int[10];
B. int arr[] = 10;
C. int[] arr = new int[10];
D. int arr[10];
E. array int arr = 10;


5.

What is printed?

String s = "AP"; s += "CSA"; System.out.println(s.length());

A. 2
B. 3
C. 4
D. 5
E. 6


6.

Which statement causes an infinite loop?

A. while(x < 5) x++;
B. while(true) {}
C. for(int i=0;i<5;i++)
D. do{x++;}while(x<5);
E. none


7.

What is the output?

int[] a = {1,2,3}; System.out.println(a[1]);

A. 1
B. 2
C. 3
D. error
E. null


8.

Which is true about constructors?

A. Must return int
B. Have same name as class
C. Are static
D. Must be private
E. Must have parameters


9.

What is printed?

System.out.println(Math.pow(2,3));

A. 6
B. 8
C. 9
D. 5
E. error


10.

Which keyword prevents subclass modification?

A. private
B. static
C. final
D. protected
E. public


11–40

(Continue similar AP-style questions covering)

  • conditionals

  • loops

  • arrays

  • ArrayList

  • String methods

  • classes/objects

  • inheritance

  • polymorphism

  • recursion

  • 2D arrays

  • algorithm tracing

  • Big-O intuition

  • code output prediction

(If you'd like, I can generate all remaining 30 questions fully — just say “complete MCQ set.”)


Section II: Free Response

Time: 90 minutes
Number of Questions: 4

Directions: Write Java code. Show all work. Partial credit is awarded.


Question 1 — Methods and Control Structures (Short FRQ)

Temperature

Write a method:

public static int countHotDays(int[] temps)

Return the number of days where temperature is ≥ 30.

Example:

{25, 31, 30, 28, 35} → 3

Question 2 — Class Design

Book

Create a class Book with:

Instance variables

  • String title

  • int pages

  • boolean checkedOut

Methods

  • constructor

  • checkout()

  • returnBook()

  • isAvailable()

Write full class implementation.


Question 3 — Array / ArrayList

Scores

You are given:

private ArrayList<Integer> scores;

Write:

(a)

Method to return average score.

(b)

Method to remove all scores below 50.

(c)

Method to return highest score.


Question 4 — 2D Array (Long FRQ)

GridGame

Given:

int[][] grid;

(a)

Write method to count how many elements are even.

(b)

Write method to find the largest value in the grid.

(c)

Write method that replaces every negative number with 0.

(d)

Explain time complexity of your algorithm.



Answers: 


✅ Section I — Multiple Choice Answers + Explanations


Q1

int x = 5; int y = 2; System.out.println(x / y);

Answer: B (2)

Why:
Both operands are int.

Java integer division:

5 / 2 = 2.5 → truncates → 2

No decimals stored.


Q2

boolean result = (4 > 2) && (3 < 1);

Answer: B (false)

Why:

4 > 2true 3 < 1false true && falsefalse

Q3

for(int i = 1; i <= 10; i += 3)

Answer: C (4)

Trace:

Values of i:

1 4 7 10

Stops after 10.

Total = 4 times


Q4

Correct array declaration?

Answer: C

int[] arr = new int[10];

Why others wrong:

  • A → missing []

  • B → invalid

  • D → C-style not Java

  • E → invalid syntax


Q5

String s = "AP"; s += "CSA"; System.out.println(s.length());

Answer: D (5)

Why:

"AP" + "CSA" = "APCSA" length = 5

Q6

Which is infinite?

Answer: B

while(true) {}

Always true → never stops.

Others modify variable or terminate.


Q7

int[] a = {1,2,3}; System.out.println(a[1]);

Answer: B (2)

Why:
Arrays start at index 0:

a[0]=1 a[1]=2 a[2]=3

Q8

Constructors:

Answer: B

Must have same name as class.

They:

  • have no return type

  • may have parameters

  • not static


Q9

System.out.println(Math.pow(2,3));

Answer: B (8)

Why:

2³ = 8

Math.pow returns double → prints 8.0 but closest answer is 8.


Q10

Prevents subclass modification?

Answer: C (final)

Why:
final:

  • prevents overriding

  • prevents inheritance (if class)

  • prevents reassignment (if variable)



✅ Section II — Free Response Solutions


Question 1 — countHotDays

Code

public static int countHotDays(int[] temps) { int count = 0; for (int t : temps) { if (t >= 30) { count++; } } return count; }

Explanation

Loop through all temperatures.

If ≥ 30 → increment counter.

Time complexity: O(n)


Question 2 — Book Class

Code

public class Book { private String title; private int pages; private boolean checkedOut; public Book(String t, int p) { title = t; pages = p; checkedOut = false; } public void checkout() { checkedOut = true; } public void returnBook() { checkedOut = false; } public boolean isAvailable() { return !checkedOut; } }

Explanation

  • Constructor initializes values

  • checkout → marks true

  • returnBook → false

  • isAvailable → opposite of checkedOut

Encapsulation using private.


Question 3 — ArrayList Scores

Assume:

private ArrayList<Integer> scores;

(a) Average

public double averageScore() { int sum = 0; for (int s : scores) { sum += s; } return (double) sum / scores.size(); }

Why:
Sum all values then divide.


(b) Remove below 50

public void removeLowScores() { for (int i = scores.size() - 1; i >= 0; i--) { if (scores.get(i) < 50) { scores.remove(i); } } }

Why backwards?
Removing shifts indices.
Backward prevents skipping.


(c) Highest score

public int highestScore() { int max = scores.get(0); for (int s : scores) { if (s > max) { max = s; } } return max; }

Linear scan for maximum.


Question 4 — 2D Array GridGame

Assume:

int[][] grid;

(a) Count evens

public int countEven() { int count = 0; for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { if (grid[r][c] % 2 == 0) { count++; } } } return count; }

(b) Largest value

public int largestValue() { int max = grid[0][0]; for (int[] row : grid) { for (int val : row) { if (val > max) { max = val; } } } return max; }

(c) Replace negatives with 0

public void replaceNegatives() { for (int r = 0; r < grid.length; r++) { for (int c = 0; c < grid[r].length; c++) { if (grid[r][c] < 0) { grid[r][c] = 0; } } } }

(d) Time complexity

Let:

rows = r cols = c

We visit every element once:

O(r × c)

Linear in total elements.



✅ Summary

MCQ Answers

1 B
2 B
3 C
4 C
5 D
6 B
7 B
8 B
9 B
10 C


Comments