AP Computer Science A · Topic 2.2
Creating and Storing Objects (Instantiation) Practice
Part of Using Objects.
Practice questions
5
Sample questions
5 of 5 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 2/5
Which line creates a new <code>Scanner</code> object reading from System.in?
- A
Scanner s = Scanner.in;
- B
Scanner s = new Scanner();
- Ccheck_circle
Scanner s = new Scanner(System.in);
- D
Scanner s = System.scanner;
Why
Constructor takes the input stream; <code>new</code> allocates the object.
- A
Sample 2difficulty 3/5
public class Cell { public int v; } public class Helper { public static void replace(Cell c) { c = new Cell(); c.v = 999; } } // ... Cell c = new Cell(); c.v = 10; Helper.replace(c); System.out.println(c.v);What is printed?
- A
999
- Bcheck_circle
10
- C
Compile-time error
- D
0
Why
Reassigning the parameter c inside replace only changes the local variable; the original reference is unchanged.
- A
Sample 3difficulty 3/5
The keyword <code>new</code> is used to
- Acheck_circle
Allocate a new object and call its constructor
- B
Declare a variable
- C
Compare references
- D
Cast a type
Why
<code>new ClassName(...)</code> allocates memory and runs constructor.
- A
Sample 4difficulty 4/5
When passing an object reference to a method, Java passes
- Acheck_circle
The reference by value (the method has its own reference variable to the same object)
- B
By reference (caller's variable can be reassigned)
- C
The whole object by value
- D
Deep copy
Why
Java is always pass-by-value; for objects, it's the reference value that's copied — both refer to the same object, but reassigning inside the method doesn't affect the caller.
- A
Sample 5difficulty 4/5
After <code>Box a = new Box(); Box b = a;</code>, modifying <code>b</code> (through its methods)
- Acheck_circle
Modifies the same object that a refers to
- B
Has no effect on a
- C
Throws an error
- D
Creates a copy
Why
<code>b = a;</code> copies the reference, not the object. Both refer to the same Box.
- A