Timer: 90:00
Which of the following correctly declares and initializes a String variable?
String
String s = new String;
String s = "Hello";
String("Hello") = s;
String s("Hello");
What is the value of result after the code below runs?
result
int a = 7; int b = 2; double result = (double) a / b;
3.0
3.5
2
7.2
What does the method call Math.abs(-12) return?
Math.abs(-12)
-12
12
0
Given String s = "AP CSA"; what does s.substring(3) return?
String s = "AP CSA";
s.substring(3)
"AP"
"CSA"
" CSA"
"A"
Which statement about String immutability is correct?
substring
Which expression evaluates to true?
String a = "cat"; String b = "cat";
a == b
a.equals(b)
Which of the following is not a primitive type in Java?
double
boolean
int
What is the output of the following code?
String w = "Java"; System.out.println(w.indexOf("a"));
1
-1
Which of the following is a valid method header?
public void method(int x)
public method(int x)
void method(x)
method public void(int x)
What is returned by Math.max(3, Math.min(10, 6))?
Math.max(3, Math.min(10, 6))
3
6
10
What does the following code print?
int n = 12; if (n % 3 == 0 && n % 4 == 0) System.out.println("A"); else System.out.println("B");
Which Boolean expression is equivalent to !(x > 5 || y == 10)?
!(x > 5 || y == 10)
x > 5 && y == 10
x <= 5 && y != 10
x <= 5 || y != 10
x > 5 && y != 10
How many times does the loop run?
int count = 0; for (int i = 3; i < 15; i += 3) count++; System.out.println(count);
What is printed?
int x = 5; while (x < 20) { x += 7; } System.out.println(x);
Which loop is guaranteed to run **at least once**?
for
while
do-while
What is the output?
for (int i = 10; i > 0; i -= 4) { System.out.print(i + " "); }
Which condition will cause the loop to execute exactly **7 times**?
int i = 3; while ( ______ ) { i++; }
i <= 9
i < 10
i <= 10
i < 11
What does the code below print?
int sum = 0; for (int k = 1; k <= 4; k++) { sum += k * 2; } System.out.println(sum);
int x = 0; int y = 3; if (x != 0 && y / x > 1) System.out.println("A"); else System.out.println("B");
What is printed by the following nested loop?
int c = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { c++; } } System.out.println(c);
Which statement about constructors is correct?
Which of the following best describes an instance variable?
Given the class below, what is printed?
public class Counter { private int value; public Counter(int start) { value = start; } public void increment() { value++; } public int getValue() { return value; } } Counter c = new Counter(7); c.increment(); System.out.println(c.getValue());
Which method is an example of a mutator?
public int getAge()
public String getName()
public void setAge(int a)
public boolean isEmpty()
What is true about a class with two methods that have the same name but different parameters?
When this.x is used inside a class method, what does it refer to?
this.x
Which of the following is a correct constructor header?
public void Student()
public Student(int id)
public constructor Student()
Student():public
Consider the class definition:
public class Light { private boolean on; public Light(boolean o) { on = o; } public void toggle() { on = !on; } public boolean isOn() { return on; } } Light lamp = new Light(true); lamp.toggle(); System.out.println(lamp.isOn());
Which keyword is used to prevent other classes from directly modifying instance variables?
public
private
static
protected
Which of the following is not a reason to use accessor (getter) methods?
Which declaration correctly creates an array of 5 integers?
int[5] nums;
int nums = new int[5];
int[] nums = new int[5];
int nums[] = {5};
int[] a = {2, 4, 6, 8}; System.out.println(a[2]);
Which of the following correctly declares an ArrayList of Strings?
ArrayList words = new ArrayList();
ArrayList words = new ArrayList<>();
What is printed by the following?
ArrayList list = new ArrayList<>(); list.add(3); list.add(7); list.add(1); System.out.println(list.get(1));
Which of the following replaces the value list[2] in an ArrayList?
list[2]
list[2] = 9;
list.set(2, 9);
list.update(2, 9);
set(list, 2, 9);
int[] nums = {10, 20, 30, 40}; for (int i = 0; i < nums.length; i += 2) System.out.print(nums[i] + " ");
What is the index of the last element in an array with length n?
n
n - 1
n + 1
What does list.size() return?
list.size()
int[] arr = {1, 3, 5, 7}; int sum = 0; for (int v : arr) sum += v; System.out.println(sum);
What happens when list.get(10) is called on an ArrayList with only 5 elements?
list.get(10)
null
IndexOutOfBoundsException
A linear search is being used on the array below. How many comparisons are needed to find the value 18?
18
int[] nums = {4, 9, 12, 18, 22, 25};
Consider the following binary search scenario:
int[] a = {2, 5, 7, 11, 14, 20, 25}; Searching for: 14
Which index will be checked first?