A student writes the following method to count elements equal to a target value in a 2D array:
public static int count(int[][] arr, int target) {
int cnt = 0;
for (int r = 0; r < arr.length; r++) {
for (int c = 0; c < arr[0].length; c++) {
if (arr[r][c] == target) cnt++;
}
}
return cnt;
}
For which input would this method produce incorrect results?
Question 2
What is the output of the following code?
int[][] g = new int[2][3];
for (int r = 0; r < g.length; r++) {
for (int c = 0; c < g[r].length; c++) {
g[r][c] = r + c;
}
}
System.out.println(g[1][2]);
Question 3
Given the following classes:
public class Animal {
public String speak() { return "..."; }
}
public class Dog extends Animal {
public String speak() { return "Woof"; }
}
What does the following print?
Animal a = new Dog();
System.out.println(a.speak());
Question 4
What is the output of the following code?
int n = 100;
int count = 0;
while (n > 1) {
n /= 2;
count++;
}
System.out.println(count);
Question 5
What is printed by the following code?
double d = 1.0 / 3.0;
int i = (int)(d * 10);
System.out.println(i);
In-content ad
Question 6
Consider the following hierarchy:
public class Vehicle { public void move() { System.out.println("Vehicle moves"); } }
public class Boat extends Vehicle { public void move() { System.out.println("Boat sails"); } }
public class AmphibiousVehicle extends Boat { }
Which of the following correctly demonstrates the use of equals() from the Object class, and when would you override it?
public class Point {
int x, y;
public Point(int x, int y) { this.x = x; this.y = y; }
}
Point p1 = new Point(2, 3);
Point p2 = new Point(2, 3);
System.out.println(p1.equals(p2));
Question 9
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 10
Consider the following class hierarchy:
public class Vehicle {
private String brand;
public Vehicle(String brand) { this.brand = brand; }
public String getBrand() { return brand; }
}
public class Car extends Vehicle {
private int doors;
public Car(String brand, int doors) {
super(brand);
this.doors = doors;
}
}
Which statement about Car's constructor is true?
In-content ad
Question 11
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>();
list.add(100);
list.add(200);
list.set(0, 999);
System.out.println(list.get(0));
System.out.println(list.size());
Question 12
What does the following method return when called with mystery(5)?
public static int mystery(int n) {
if (n == 1) return 0;
return 1 + mystery(n / 2);
}
Question 13
What is the output of the following code?
String s = "Java";
s = s.toUpperCase();
s = s + " rocks";
System.out.println(s);
Question 14
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 15
What is printed by the following code?
public class Temp {
private double celsius;
public Temp(double c) { celsius = c; }
public double toFahrenheit() {
return celsius * 9 / 5 + 32;
}
public String toString() {
return celsius + "C";
}
}
// In main:
Temp t = new Temp(100);
System.out.println(t);
System.out.println(t.toFahrenheit());
In-content ad
Question 16
What is the result of the following expression in Java?
int result = 3 + 4 * 2 - 10 / 4;
Question 17
What happens when the following code executes?
int[] arr = new int[3];
System.out.println(arr[0]);
arr[5] = 10;
Question 18
What is the output of the following code?
int[] nums = {3, 1, 4, 1, 5};
int max = nums[0];
for (int n : nums) {
if (n > max) {
max = n;
}
}
System.out.println(max);
Question 19
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
list.add(i);
}
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
Question 20
What is the output of the following code?
String[] words = {"cat", "bird", "ant", "dog"};
String first = words[0];
for (String w : words) {
if (w.compareTo(first) < 0) {
first = w;
}
}
System.out.println(first);
In-content ad
Question 21
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 22
What is the output of the following code?
int x = 4;
if (x % 2 == 0)
System.out.println("even");
System.out.println("positive");
Question 23
What is the output of the following code?
int[][] m = {{2, 4}, {6, 8}};
for (int r = 0; r < m.length; r++) {
for (int c = 0; c < m[r].length; c++) {
System.out.print(m[r][c] + " ");
}
}
Question 24
What is the output of the following code?
int a = 2;
System.out.println(a++);
System.out.println(a);
Question 25
What is the output of the following code?
public static void mystery(int n) {
if (n <= 0) return;
mystery(n - 1);
System.out.println(n);
}
// Called with:
mystery(3);
In-content ad
Question 26
What is the output of the following code?
ArrayList<Integer> a = new ArrayList<>();
a.add(1); a.add(2); a.add(3);
ArrayList<Integer> b = new ArrayList<>(a);
b.add(4);
System.out.println(a.size());
System.out.println(b.size());
Question 27
What is the output of the following code?
import java.util.ArrayList;
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list.size());
System.out.println(list.get(1));
Question 28
What is the output of the following code?
public class Student {
private String name;
private int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public void setGrade(int grade) {
if (grade >= 0 && grade <= 100) {
this.grade = grade;
}
}
public int getGrade() { return grade; }
}
// In main:
Student s = new Student("Ali", 85);
s.setGrade(110);
System.out.println(s.getGrade());
Question 29
What is the output of the following code?
public class Counter {
private int count = 0;
public void increment() {
count++;
}
public int getCount() {
return count;
}
}
// In main:
Counter c = new Counter();
c.increment();
c.increment();
c.increment();
System.out.println(c.getCount());
Question 30
What value does the following method return when called with sum(4)?
public static int sum(int n) {
if (n == 0) return 0;
return n + sum(n - 1);
}
In-content ad
Question 31
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list.add(i * 2);
}
list.remove(Integer.valueOf(4));
System.out.println(list);
Question 32
What is the output of the following code?
int[] arr = {5, 3, 8, 1, 9};
int target = 8;
int index = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
index = i;
}
}
System.out.println(index);
Question 33
Which of the following correctly compares two String objects for equal content in Java?
String a = "hello";
String b = "hello";
Question 34
What is the output of the following code?
for (int i = 0; i < 5; i++) {
if (i % 2 == 0) continue;
System.out.print(i + " ");
}
Question 35
Which boolean expression correctly checks whether x is between 1 and 10 (inclusive)?
In-content ad
Question 36
Which of the following recursive methods contains a flaw that would cause infinite recursion for the call badRecurse(5)?
// Option A
public static int badRecurse(int n) {
if (n == 0) return 0;
return n + badRecurse(n + 1);
}
Question 37
What is the output of the following code?
public class Thing {
private int x;
public Thing() {
x = 10;
}
public Thing(int x) {
this.x = x;
}
public int getX() { return x; }
}
// In main:
Thing a = new Thing();
Thing b = new Thing(7);
System.out.println(a.getX() + b.getX());
Question 38
Which declaration correctly creates a 2D array of integers with 4 rows and 3 columns?
Question 39
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0) {
list.remove(i);
}
}
System.out.println(list);
Question 40
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);
}
AP Computer Science A Full-length practice exam 2 — Free with Answer Explanations | Test Practice Hub