AP Computer Science A · Topic 2.2

Creating and Storing Objects (Instantiation) Practice

Part of Using Objects.

Practice questions

5

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 5 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. 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();

    • C

      Scanner s = new Scanner(System.in);

      check_circle
    • D

      Scanner s = System.scanner;

    Why

    Constructor takes the input stream; <code>new</code> allocates the object.

  2. 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

    • B

      10

      check_circle
    • C

      Compile-time error

    • D

      0

    Why

    Reassigning the parameter c inside replace only changes the local variable; the original reference is unchanged.

  3. Sample 3difficulty 3/5

    The keyword <code>new</code> is used to

    • A

      Allocate a new object and call its constructor

      check_circle
    • B

      Declare a variable

    • C

      Compare references

    • D

      Cast a type

    Why

    <code>new ClassName(...)</code> allocates memory and runs constructor.

  4. Sample 4difficulty 4/5

    When passing an object reference to a method, Java passes

    • A

      The reference by value (the method has its own reference variable to the same object)

      check_circle
    • 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.

  5. Sample 5difficulty 4/5

    a b Object data

    After <code>Box a = new Box(); Box b = a;</code>, modifying <code>b</code> (through its methods)

    • A

      Modifies the same object that a refers to

      check_circle
    • 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.