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 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 2
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 3
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?
29
32
47
50
Show answer
Answer: (C)
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 4
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 5
A programmer is designing a method to determine if a package exceeds a maximum weight limit. The method receives the package's exact weight in kilograms and the maximum allowed weight in kilograms. Which method signature is most appropriate?
public boolean checkWeight(double packageWt, double maxWt)public String checkWeight(double packageWt, double maxWt)public boolean checkWeight(int packageWt, double maxWt)public boolean checkWeight(double packageWt, int maxWt)Show answer
Answer: (A)
Evaluating data types for continuous measurements and logical outcomes. Exact weights require
doubleparameters for decimal precision, and the yes/no outcome requires abooleanreturn type.Question 6
Consider the following class declaration.
</>Javapublic class Logger { public void logError(String msg, int code) { /* missing code */ } }A program calls
logError("Timeout", 404)followed bylogError("Not found", 500). The intended output is:</>TEXTError 404: Timeout Error 500: Not foundWhich of the following can replace
/* missing code */so the method works as intended?System.out.print("Error " + code + ": "); System.out.println(msg);System.out.println("Error " + code + ": "); System.out.print(msg);System.out.print("Error " + code + ": "); System.out.print(msg);System.out.println("Error " + code + ": "); System.out.println(msg);Show answer
Answer: (A)
Recognizing that
System.out.printkeeps the cursor on the same line, whileSystem.out.printlnmoves it to the next line after printing. To achieve the formatError 404: Timeouton a single line, the prefix must be printed withprint, and the message withprintlnto ensure subsequent logs start on a new line.Question 7
Consider the following class declaration.
</>Javapublic class TimeFormatter { public void printDelimiter() { System.out.print(":"); } public void formatTime(int h, int m, int s) { /* missing code */ } }The
formatTimemethod is intended to print the time in the formath:m:sand then move the cursor to a new line. Which of the following can replace/* missing code */so the method works as intended?System.out.print(h); printDelimiter(); System.out.print(m); printDelimiter(); System.out.print(s);System.out.println(h); printDelimiter(); System.out.println(m); printDelimiter(); System.out.println(s);System.out.print(h); printDelimiter(); System.out.print(m); printDelimiter(); System.out.println(s);System.out.println(h); printDelimiter(); System.out.print(m); printDelimiter(); System.out.print(s);Show answer
Answer: (C)
To print the entire time on one line and then move to the next line, the hours, minutes, and delimiters must be printed using
print. Usingprintlnfor the final valuesprints the seconds on the same line and then moves the cursor to a new line.Question 8
Consider the following code segment:
</>JavaScanner scan = new Scanner(inputString); String first = scan.next(); int id = scan.nextInt();For the variable
firstto be assigned the value"User"and the variableidto be assigned the value42, which of the following must be the initial condition ofinputString?String inputString = "User 42 ";
String inputString = "User42 ";
String inputString = "User,42 ";
String inputString = "User 4.2";
Show answer
Answer: (A)
Tracing the Scanner methods reveals that
next()reads characters until it encounters whitespace, successfully assigning "User" tofirst. The subsequentnextInt()call skips the space and reads the integer 42, assigning it toid.Question 9
Consider the following code segment:
</>JavaScanner s = new Scanner(data); double val1 = s.nextDouble(); int val2 = s.nextInt();Which initialization of the String
dataallows the code segment to execute completely without throwing anInputMismatchException?String data = "4.5 20 ";
String data = "20 4.5 ";
String data = "4.5, 20";
String data = "4.5. 20";
Show answer
Answer: (A)
Analyzing the sequence of method calls shows that
nextDouble()correctly parses "4.5" into a double. The Scanner then advances past the space, andnextInt()successfully parses "20" into an integer.Question 10
Consider the following code segment:
</>JavaString text = "100 200"; /* missing code */ int sum = scan.nextInt() + scan.nextInt();Which replacement for
/* missing code */properly establishes the initial condition needed for theScannerto read from thetextvariable?Scanner scan = new Scanner(text);
Scanner scan = new Scanner("100");
Scanner scan = new Scanner("200");
Scanner scan = new Scanner("text");
Show answer
Answer: (A)
Recognizing object instantiation requirements, this correctly passes the String variable
textinto the Scanner constructor, linking the input stream to the string containing "100 200".
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 924 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