AP Computer Science A · Topic 2.4

Calling a Void Method with Parameters Practice

Part of Using Objects.

Practice questions

3

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

Sample questions

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

  1. Sample 1difficulty 3/5

    public class Cell {
        public int v;
    }
    public class Helper {
        public static void bump(Cell c) {
            c.v++;
        }
    }
    // ...
    Cell c = new Cell();
    c.v = 10;
    Helper.bump(c);
    System.out.println(c.v);

    What is printed?

    • A

      11

      check_circle
    • B

      10

    • C

      0

    • D

      Compile-time error

    Why

    The reference is passed by value; bump modifies the same object's field through that reference.

  2. Sample 2difficulty 3/5

    public class Helper {
        public static void fill(int[] a) {
            a[0] = 99;
        }
    }
    // ...
    int[] arr = {1, 2, 3};
    Helper.fill(arr);
    System.out.println(arr[0]);

    What is printed?

    • A

      99

      check_circle
    • B

      1

    • C

      0

    • D

      Compile-time error

    Why

    Arrays are reference types; the method modifies the same array object, so arr[0] becomes 99.

  3. Sample 3difficulty 3/5

    public class Helper {
        public static void inc(int n) { n++; }
    }
    // ...
    int x = 5;
    Helper.inc(x);
    System.out.println(x);

    What is printed?

    • A

      5

      check_circle
    • B

      6

    • C

      0

    • D

      Compile-time error

    Why

    Java passes primitives by value; modifying the parameter n inside inc does not affect x.