AtlasEntrar
Black FridayHalf off Premium, through Sunday, Nov 29.See plans
AP Computer Science A · Unit test

Using objects and methods practice test

Up to 15 AP Computer Science A questions on using objects and methods, timed at the exam's pace and scored, new every time. Try 3 below first.

Take the using objects and methods unit test →Try 3 questions

Using objects and methods is 15%–25% of the AP Computer Science A exam's multiple choice. Learn it first in the using objects and methods chapter.

3 free using objects and methods practice questions

Real questions from the Atlas AP Computer Science A exam pool, with the answer and the working.

  1. Consider the following code segment.

    double d = 2.7;
    int a = (int) d * 3;
    int b = (int) (d * 3);
    System.out.println(a + " " + b);

    What is printed as a result of executing the code segment?

    1. A8 6
    2. B8 8
    3. C6 8
    4. D6 6
    Show the answer

    C.

    A cast binds more tightly than *, so (int) d * 3 is 2 × 3 = 6. In (int) (d * 3) the product 8.1 is computed first and then truncated to 8.

    8 6: swaps the two: the cast binds to d alone in the first line, and to the product in the second. 8 8: casts after multiplying in both lines: without parentheses the cast applies to d alone. 6 6: truncates d before multiplying in both lines: the parentheses make the second line cast the product.

  2. Consider the following code segment.

    String a = "12";
    int n = Integer.parseInt(a) + 3;
    String s = a + 3;
    System.out.println(n + 1 + " " + s + 1);

    What is printed as a result of executing the code segment?

    1. A151 1231
    2. B16 124
    3. C16 16
    4. D16 1231
    Show the answer

    D.

    Integer.parseInt(a) is the int 12, so n = 15. a + 3 joins a String and an int, so s = "123". The print expression is evaluated left to right: n + 1 is 16 (two ints), then " ", then "123", then "1" are joined.

    151 1231: joins the 1 to n as text: n + 1 is evaluated first, between two ints, before any String appears. 16 124: adds 1 to s as a number: s is the String "123", so + joins. 16 16: treats a + 3 as addition: a is a String, so 3 is joined to it.

  3. Consider the following method.

    public static int combine(int x, int y)
    {
      return x * 2 - y;
    }

    What is printed as a result of executing the following code segment?

    int a = 5;
    int b = 3;
    System.out.println(combine(combine(a, b), a));
    1. A-3
    2. B3
    3. C9
    4. D15
    Show the answer

    C.

    The inner call runs first: combine(5, 3) = 5 × 2 − 3 = 7. Then combine(7, 5) = 7 × 2 − 5 = 9.

    3: swaps the outer arguments: the inner call's value, 7, is the first argument, x. -3: swaps the inner arguments: combine(a, b) passes a as x. 15: subtracts before multiplying: * is done before -.

Take the full unit test →AP Computer Science A practice exam

Questions people ask

What is in the AP Computer Science A Using objects and methods practice test?
Up to 15 multiple-choice questions from this unit, plus a free-response question when the unit has one, timed at the real exam's pace and scored as a percent. The questions are new every time, drawn from a pool of checked questions.
How much of the AP Computer Science A exam is Using objects and methods?
15% to 25% of the multiple-choice questions, by the College Board’s course and exam description.
Is it free?
The 3 questions on this page need no account. With a free Atlas account your first unit test is free; Atlas Premium has no limit.

Other AP Computer Science A units