AP Computer Science A · Topic 2.1
Objects: Instances of Classes Practice
Part of Using Objects.
Practice questions
9
Sample questions
5 of 9 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
Which is a reference (object) type in Java?
- A
boolean
- Bcheck_circle
String
- C
int
- D
double
Why
<code>String</code> is a class. Objects are stored by reference.
- A
Sample 2difficulty 3/5
public class Holder { private int n; public Holder(int n) { this.n = n; } public int getN() { return n; } public void setN(int n) { this.n = n; } } // ... Holder a = new Holder(1); Holder b = a; b.setN(42); System.out.println(a.getN());What is printed?
- A
0
- B
1
- Ccheck_circle
42
- D
Compile-time error
Why
a and b refer to the same object; modifying via b is visible via a.
- A
Sample 3difficulty 3/5
String[] names = new String[3]; System.out.println(names[0].length());What happens when this code runs?
- Acheck_circle
NullPointerException because names[0] is null
- B
Prints 0
- C
Prints empty string
- D
Compile error
Why
A new String[] is filled with null references. Calling .length() on null throws NullPointerException.
- A
Sample 4difficulty 3/5
Calling a method on a <code>null</code> reference causes
- A
Returns null
- B
Compile error
- C
Returns 0
- Dcheck_circle
Throws NullPointerException at runtime
Why
<code>String s = null; s.length();</code> → NPE at runtime.
- A
Sample 5difficulty 3/5
What is the default value for an uninitialized object reference (instance field)?
- A
Empty string
- Bcheck_circle
null
- C
false
- D
0
Why
Reference types default to <code>null</code>.
- A