AP Computer Science A Unit 1 practice questions
Try 10 multiple-choice questions from Unit 1 here. Pick an answer to see why it's right or wrong, then take the timed test above for a full AP-style set.
Question 1
Consider the following code segment.
</>Javaint a = 11; int b = 4; double x = (double) (a / b); double y = (double) a / b; System.out.println(x + " " + y);What is printed as a result of executing this code segment?
2.0 2.75
2.75 2.75
2.0 2.00
2.75 2.0
Show answer
Answer: (A)
Tracing the order of operations for casting and division. The expression
(a / b)performs integer division first, yielding2, which is then cast to2.0. The expression(double) acasts11to11.0first, resulting in floating-point division and2.75.Question 2
Consider the following code segment.
</>Javadouble num = -5.8; int a = (int) num; int b = (int) (num - 0.5); System.out.println(a + " " + b);What is printed as a result of executing this code segment?
-5 -6
-6 -5
-6 -6
-5 -5
Show answer
Answer: (A)
Evaluating the truncation behavior of casting negative floating-point numbers. Casting
-5.8directly to an integer truncates the decimal, resulting in-5. Subtracting0.5yields-6.3, which truncates to-6.Question 3
Consider the following code segment.
</>Javaint x = 10; int y = 3; x %= y; y += x; x *= y; y /= 2; System.out.println(x + " " + y);What is printed as a result of executing this code segment?
4 2
4 1
3 2
3 1
Show answer
Answer: (A)
Tracing the sequential execution of compound assignment operators requires tracking the updated state of each variable. The variable
xbecomes 1 (10 % 3), thenybecomes 4 (3 + 1). Next,xbecomes 4 (1 * 4), and finallyybecomes 2 (4 / 2), resulting in the output of 4 and 2.Question 4
Consider the following code segment.
</>JavaString s = "Val: "; int n = 5; n += 2; s += n; n *= 2; s += n; System.out.println(s);What is printed as a result of executing this code segment?
"Val: 714"
"Val: 510"
"Val: 721"
"Val: 710"
Show answer
Answer: (A)
Evaluating compound assignment operators with mixed data types requires tracking both numeric operations and string concatenations. The integer
nis updated to 7 and concatenated to form "Val: 7", thennis multiplied by 2 to become 14 and concatenated again to form "Val: 714".Question 5
Consider the following code segment.
</>Javaint val = 10; int mod = 3; val *= mod + 2; val -= mod; System.out.println(val);What is printed as a result of executing this code segment?
47
29
50
32
Show answer
Answer: (A)
Applying the correct order of operations to compound assignment statements requires evaluating the entire right-hand expression first. The expression
mod + 2evaluates to 5, soval *= 5updatesvalto 50. The final statementval -= 3reduces it to 47.Question 6
Consider the following code segment.
</>Javaint a = 2; int b = 3; String s1 = a + b + " sum"; String s2 = "sum " + a + b; System.out.println(s1 + ", " + s2);What is the result of attempting to compile and execute this code segment?
The code fails to compile due to a type mismatch.
The code compiles and prints out
5 sum, sum 5.The code compiles and prints out
5 sum, sum 23.The code compiles and prints out
23 sum, sum 23.Show answer
Answer: (C)
Applying Java's rules for operator precedence and String concatenation. In
s1, the integer additiona + bevaluates first to 5, which is then concatenated with the String. Ins2, the operation evaluates left-to-right, concatenating the String withato form"sum 2", which is then concatenated withbto form"sum 23".Question 7
Consider the following class definition and code segment.
</>Javapublic class Player { private int score; private int level; public Player(int s, int l) { score = s; level = l; } public void update(int points) { score += points; level++; score *= level; } public int getScore() { return score; } }</>Java// Code segment Player p1 = new Player(10, 1); p1.update(5); p1.update(2); System.out.println(p1.getScore());What is printed when the code segment is executed?
96
64
34
26
Show answer
Answer: (A)
Tracing the state of an object across multiple method calls involving compound assignment and increment operators. In the first call,
scoreincreases by 5 (to 15),levelincrements to 2, andscoreis multiplied by 2 (to 30). In the second call,scoreincreases by 2 (to 32),levelincrements to 3, andscoreis multiplied by 3 (to 96).Question 8
A programmer is designing a
Thermostatclass to track the current room temperature, whether the heating system is actively running, and the total number of days since the air filter was last replaced. Which of the following sets of instance variables represents the most appropriate design for this class?private double roomTemp; private boolean isHeating; private int filterDays;private int roomTemp; private boolean isHeating; private int filterDays;private double roomTemp; private String isHeating; private int filterDays;private double roomTemp; private boolean isHeating; private double filterDays;Show answer
Answer: (A)
Evaluating data requirements for physical measurements and states. Temperature requires a
doublefor decimal precision, heating state requires abooleanfor true/false status, and days require anintfor whole numbers.Question 9
A programmer is designing a method to process loan applications. The method must accept the applicant's credit score (a whole number from 300 to 850) and their exact annual income, then return a value indicating whether the loan is approved. Which method signature is most appropriate?
public boolean processLoan(int creditScore, double annualIncome)public String processLoan(int creditScore, double annualIncome)public boolean processLoan(double creditScore, double annualIncome)public boolean processLoan(int creditScore, int applicantIncome)Show answer
Answer: (A)
Analyzing the required inputs and outputs for a financial calculation. The approval status requires a
booleanreturn type, the credit score requires anint, and the exact income requires adouble.Question 10
A programmer is designing a
Bookclass for an online store. The class must store the book's title, the total number of pages, and the retail price. Which of the following sets of instance variables represents the most appropriate design?private String bookTitle; private int totalPages; private double itemPrice;private String bookTitle; private double totalPages; private double itemPrice;private String bookTitle; private int totalPages; private int itemPrice;private char bookTitle; private int totalPages; private double itemPrice;Show answer
Answer: (A)
Selecting appropriate primitive and reference types for distinct data categories. The title requires a
Stringreference type, pages require anintfor whole numbers, and price requires adoublefor decimals.
What's on the AP Computer Science A Unit 1 practice test
The standard version takes about 43 minutes and follows the timing and directions of the AP exam, sized to one unit:
- 20 Multiple Choice Questions (43 minutes)
Questions are drawn from 938 multiple-choice questions written for Unit 1, Using Objects and Methods.
Review Unit 1 before you start
- 1.1 Why Programming? Why Java?
- 1.2 Variables and Primitive Data Types
- 1.3 Expressions and Assignment Statements
- 1.4 Assignment Statements and Input
- 1.5 Casting and Ranges of Variables
- 1.6 Compound Assignment Operators
- 1.7 Application Program Interface (API) and Libraries
- 1.8 Documentation With Comments
- 1.9 Calling a Void Method With Parameters
- 1.10 Calling a Non-Void Method
- 1.11 Using the Math Class
- 1.12 Objects: Instances of Classes
- 1.13 Creating and Storing Objects
- 1.14 Calling a Void Method
- 1.15 String Methods