AP Computer Science A › Unit 1
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.
These are taken from the Regular set, with the answer and explanation shown.
1. What is printed as a result of executing the following code segment?
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.
2. A class contains the following method.
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).
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?
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.