Practice Dojo

AP Computer Science A › Unit 1

AP Computer Science A Unit 1: Using Objects and Methods

15 to 25% of the exam · 180 practice questions · 5 free-response questions

Unit 1 introduces Java and the building blocks of every program. You will work with variables, primitive types, expressions, casting, and assignment, then call methods from classes like Math and String and create objects with constructors. Multiple-choice questions often ask you to trace code and predict its output. Integer division trips up many students: 7 / 2 evaluates to 3, not 3.5. Others forget that String methods such as substring return a new String and leave the original unchanged, or that indexes start at 0.

Practice this unit

White belt · 60 questionsRegular · 60 questionsBlack belt · 60 questionsFree response

White belt covers core terms and foundational ideas, Regular is set at exam level, and Black belt has harder multi-step questions.

Topics in this unit

Free-response questions

Sample questions

These are taken from the Regular set, with the answer and explanation shown.

Topic 1.3 Expressions and Output

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

  1. A12345
  2. B1239
  3. C3345
  4. D339

Answer: C. Evaluation is left to right. 1 + 2 is 3 (addition), then 3 + "3" gives the text "33". After that every + concatenates, so "334" and then "3345".

Common trap: Choosing D, "339". Once the value is a String, + 4 + 5 joins each number. It does not add 4 and 5 first.

Topic 1.9 Method Signatures

2. A class contains the following method.

  1. Apublic static int calc(double a, int b) { return 1; }
  2. Bpublic void calc(int a, double b) { }
  3. Cpublic static int calc(int b, double a) { return 2; }
  4. Dpublic static double calc(int x, double y) { return 1; }

Answer: A. Overloaded methods need different parameter lists, meaning different types or a different order of types. The other choices repeat the same name and parameter types, which is not allowed no matter what the return type or parameter names are.

Common trap: Choosing public static double calc(int x, double y). Changing the return type to double and renaming the parameters does not change the signature calc(int, double).

Topic 1.14 Calling Instance Methods

3. A class Rect has the methods public int area() and public void scale(int k), and r is a Rect variable that refers to an object. Which of the following statements compiles without error?

  1. Aint a = r.area(5);
  2. Br.scale(2);
  3. Cint a = Rect.area();
  4. Dint b = r.scale(2);

Answer: B. scale takes one int and returns nothing, so it can be called as a statement but its result cannot be stored. area takes no arguments and is an instance method, so it must be called on an object.

Common trap: Choosing int b = r.scale(2);. scale is void, so it returns nothing that b could store.

Start practicing Unit 1 →