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.
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.
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?
- A8 6
- B8 8
- C6 8
- D6 6
Show the answer
C.
A cast binds more tightly than
*, so(int) d * 3is 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 todalone in the first line, and to the product in the second.8 8: casts after multiplying in both lines: without parentheses the cast applies todalone.6 6: truncatesdbefore multiplying in both lines: the parentheses make the second line cast the product.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?
- A151 1231
- B16 124
- C16 16
- D16 1231
Show the answer
D.
Integer.parseInt(a)is the int 12, so n = 15.a + 3joins a String and an int, so s = "123". The print expression is evaluated left to right:n + 1is 16 (two ints), then " ", then "123", then "1" are joined.151 1231: joins the 1 to n as text:n + 1is 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: treatsa + 3as addition: a is a String, so 3 is joined to it.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));- A-3
- B3
- C9
- D15
Show the answer
C.
The inner call runs first:
combine(5, 3)= 5 × 2 − 3 = 7. Thencombine(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-.
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.