int a = 2;
System.out.println(a++);
System.out.println(a);
In-content ad
Question 16
Which of the following correctly compares two String objects for equal content in Java?
String a = "hello";
String b = "hello";
Question 17
Which of the following best demonstrates short-circuit evaluation of &&?
String s = null;
if (s != null && s.length() > 0) {
System.out.println("Non-empty");
} else {
System.out.println("Null or empty");
}
Question 18
A programmer writes:
public class Printer {
public void print(String s) { System.out.println(s); }
public void print(int n) { System.out.println(n * 2); }
}
Printer p = new Printer();
p.print(5);
What is the output, and what concept does this demonstrate?
Question 19
What is the output of the following code?
int[][] t = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
for (int c = 0; c < t[0].length; c++) {
for (int r = 0; r < t.length; r++) {
System.out.print(t[r][c] + " ");
}
}
Question 20
What are the values of x and y after the following code executes?
double x = 5 / 2;
double y = 5.0 / 2;
In-content ad
Question 21
What is the output of the following code?
public class Box {
private int value;
public Box(int v) {
value = v;
}
public void doubleValue() {
value = value * 2;
}
public int getValue() { return value; }
}
// In main:
Box b1 = new Box(5);
Box b2 = b1;
b2.doubleValue();
System.out.println(b1.getValue());
Question 22
Given the following class, which statement correctly accesses the name variable from OUTSIDE the class?
public class Person {
private String name;
public Person(String n) {
name = n;
}
public String getName() {
return name;
}
}
Question 23
What is the output of the following code?
String s = "abcdef";
System.out.println(s.indexOf("cd"));
System.out.println(s.indexOf("xy"));
Question 24
How many times does the following loop print "hello"?
for (int i = 0; i < 5; i++) {
System.out.println("hello");
}
Question 25
What is the output of the following code?
int x = 4;
if (x % 2 == 0)
System.out.println("even");
System.out.println("positive");
In-content ad
Question 26
What is the output of the following code?
int[] arr = {4, 2, 7, 1, 5};
for (int i = 0; i < arr.length - 1; i++) {
if (arr[i] > arr[i + 1]) {
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
System.out.println(arr[0] + " " + arr[1]);
Question 27
What is the output of the following code?
ArrayList<Double> list = new ArrayList<>();
list.add(1.5);
list.add(2.5);
list.add(3.5);
double sum = 0;
for (double d : list) {
sum += d;
}
System.out.println(sum);
Question 28
A class declares public String toString(). Which statement is true about this method?
Question 29
What is the output of the following code?
public static void countdown(int n) {
if (n == 0) {
System.out.println("Go!");
return;
}
System.out.println(n);
countdown(n - 1);
}
// Called with:
countdown(3);
Question 30
The Fibonacci sequence is defined as fib(0)=0, fib(1)=1, fib(n)=fib(n-1)+fib(n-2) for n≥2. What does the following method return for fib(5)?
public static int fib(int n) {
if (n == 0) return 0;
if (n == 1) return 1;
return fib(n - 1) + fib(n - 2);
}
In-content ad
Question 31
A method receives a 2D array and should return the largest value in the array. Which implementation is correct?
public static int findMax(int[][] arr) {
int max = arr[0][0];
for (int[] row : arr) {
for (int val : row) {
if (val > max) max = val;
}
}
return max;
}
Question 32
Which of the following correctly defines a constructor for a Dog class?
public class Dog {
private String name;
// Which option is a valid constructor?
}
Question 33
The following code attempts to print the sum of the main diagonal of a 3×3 array:
int[][] m = {{1, 0, 0}, {0, 5, 0}, {0, 0, 9}};
int diag = 0;
for (int i = 0; i < m.length; i++) {
diag += m[i][i];
}
System.out.println(diag);
What does it print?
Question 34
What is printed by the following code?
double d = 1.0 / 3.0;
int i = (int)(d * 10);
System.out.println(i);
Question 35
Given the following code:
Animal a = new Dog();
if (a instanceof Dog) {
System.out.println("Dog");
} else {
System.out.println("Not Dog");
}
What is printed, assuming Dog extends Animal?
In-content ad
Question 36
What is the output of the following code?
int x = 0;
if (x = 5) {
System.out.println("hello");
}
Question 37
What is the output of the following code?
int x = 5;
if (x > 3) {
System.out.println("A");
} else if (x > 4) {
System.out.println("B");
} else {
System.out.println("C");
}
Question 38
After the following code executes, what is the value of n?
double d = 7.9;
int n = (int) d % 3;
Question 39
What is the output of the following code?
public class Counter {
private int count;
public Counter(int start) {
count = start;
}
public Counter add(int n) {
count += n;
return this;
}
public int getCount() { return count; }
}
// In main:
Counter c = new Counter(0);
System.out.println(c.add(3).add(4).getCount());
Question 40
Consider a 2D array representing a grid where each row may have a different length (a jagged array). Which expression safely gives the number of columns in row i?
AP Computer Science A Full-length practice exam 1 — Free with Answer Explanations | Test Practice Hub