AP Computer Science A · Topic 2.5

Calling a Non-void Method Practice

Part of Using Objects.

Practice questions

4

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

Sample questions

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

  1. Sample 1difficulty 3/5

    For <code>String s = "hi";</code>, calling <code>s.length()</code> is

    • A

      Instance method call (on s)

      check_circle
    • B

      Constructor

    • C

      Static method call

    • D

      Compile error

    Why

    Instance methods called on object via dot notation.

  2. Sample 2difficulty 3/5

    A method declared with return type <code>void</code>

    • A

      Returns 0

    • B

      Returns null

    • C

      Cannot return at all

    • D

      Returns nothing (no value)

      check_circle

    Why

    <code>void</code> methods return no value; <code>return;</code> with no value is allowed.

  3. Sample 3difficulty 4/5

    <code>" hello ".trim().toUpperCase()</code> returns

    • A

      HELLO

      check_circle
    • B

      HELLO

    • C

      hello

    • D

      hello

    Why

    <code>trim()</code> removes whitespace; <code>toUpperCase()</code> then capitalizes → "HELLO".

  4. Sample 4difficulty 4/5

    A method declared as <code>public Dog clone()</code> returns

    • A

      An int

    • B

      void

    • C

      A Dog object reference

      check_circle
    • D

      Compile error

    Why

    Return type Dog; method must return a Dog (or null).