What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
list.add(15);
list.remove(1);
System.out.println(list);
Loading test…
40 questions
What is the output of the following code?
ArrayList<Integer> list = new ArrayList<>();
list.add(5);
list.add(10);
list.add(15);
list.remove(1);
System.out.println(list);
What is the state of array a after the following code executes?
int[][] a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[] temp = a[0];
a[0] = a[2];
a[2] = temp;
What is the value of count after the following code executes?
int count = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
count++;
}
}
Consider the following code. What is the value of z?
int x = 7;
int y = 3;
double z = x / y + 0.5;
What is the output of the following code?
String s = null;
System.out.println(s.length());
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) + " ");
}
What is the output of the following code?
int x = 10;
if (x++ > 10) {
System.out.println("greater");
} else {
System.out.println("not greater");
}
System.out.println(x);
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());
Which expression is equivalent to !(a && b) according to De Morgan's Law?
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?
What is the output of the following code?
String s = "coding";
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'i' || s.charAt(i) == 'o') {
count++;
}
}
System.out.println(count);
Which of the following correctly declares a boolean variable and assigns it the result of a comparison?
// Option A
boolean result = (7 > 3);
// Option B
Boolean result = 7 > 3;
// Option C
bool result = 7 > 3;
// Option D
boolean result = "7 > 3";
Consider the following method:
public static int power(int base, int exp) {
if (exp == 0) return 1;
return base * power(base, exp - 1);
}
How many times total is power called (including the initial call) when evaluating power(2, 4)?
What is the output of the following code?
int x = Integer.MAX_VALUE;
x = x + 1;
System.out.println(x > 0);
A class declares public String toString(). Which statement is true about this method?
Consider the following recursive method:
public static int sumArray(int[] arr, int index) {
if (index == arr.length) return 0;
return arr[index] + sumArray(arr, index + 1);
}
What is returned by sumArray(new int[]{3, 1, 4, 1, 5}, 0)?
What is the result of the following expression in Java?
int result = 3 + 4 * 2 - 10 / 4;
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());
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?
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());
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]);
What does the following code print?
int a = 5;
int b = 5;
System.out.println(a == b);
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));
What is the output of the following code?
public class Shape {
public String describe() { return "Shape"; }
}
public class Circle extends Shape {
public String describe() { return super.describe() + "-Circle"; }
}
// In main:
Circle c = new Circle();
System.out.println(c.describe());
What is the value of result after the following code executes?
int[] arr = {2, 5, 3, 8, 1};
int result = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 0) {
result++;
}
}
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));
Which keyword is used to indicate that one class inherits from another in Java?
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]);
What is the output of the following code?
int[] arr = {5, 10, 15, 20};
System.out.println(arr.length);
System.out.println(arr[2]);
What is the state of list after the following code executes?
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
list.add(1, "X");
What is the output of the following code?
ArrayList<String> list = new ArrayList<>();
list.add("apple");
list.add("banana");
list.add("cherry");
String result = "";
for (int i = list.size() - 1; i >= 0; i--) {
result += list.get(i).charAt(0);
}
System.out.println(result);
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);
}
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());
What is the output of the following code?
ArrayList<String> list = new ArrayList<>();
list.add("dog");
list.add("cat");
list.add("dog");
list.add("bird");
int count = 0;
for (String s : list) {
if (s.equals("dog")) count++;
}
System.out.println(count);
What is the output of the following code?
int i = 1;
while (i < 16) {
i *= 2;
}
System.out.println(i);
What is the output of the following code?
public class Accumulator {
private static int total = 0;
private int value;
public Accumulator(int v) {
value = v;
total += v;
}
public static int getTotal() { return total; }
}
// In main:
Accumulator a1 = new Accumulator(10);
Accumulator a2 = new Accumulator(20);
Accumulator a3 = new Accumulator(5);
System.out.println(Accumulator.getTotal());
An abstract class Figure is defined as:
public abstract class Figure {
public abstract double area();
public void printArea() {
System.out.println("Area: " + area());
}
}
Which of the following statements is true?
Which of the following correctly compares two String objects for equal content in Java?
String a = "hello";
String b = "hello";
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] + " ");
}
}
What is the value of result after this code executes?
double result = Math.sqrt(Math.pow(3.0, 2) + Math.pow(4.0, 2));
Which of the following correctly removes all elements equal to 0 from an ArrayList without skipping elements?
ArrayList<Integer> list = new ArrayList<>();
// list contains: [0, 5, 0, 3, 0]