Unit 1 — Using Objects and Methods
Use the topic links below to study. After that, take the Unit 1 practice quiz: 15 multiple-choice (AP-style) + 1 short FRQ.
Topic pages
Go in order (1.1 → 1.15).
Introduction to Algorithms, Programming, and Compilers1.1
Variables and Data Types1.2
Expressions and Output1.3
Assignment Statements and Input1.4
Casting and Range of Variables1.5
Compound Assignment Operators1.6
Application Program Interface (API) and Libraries1.7
Documentation with Comments1.8
Method Signatures1.9
Calling Class Methods1.10
Math Class1.11
Objects: Instances of Classes1.12
Object Creation and Storage (Instantiation)1.13
Calling Instance Methods1.14
String Manipulation1.15
How to use the quiz
- Answer the 15 MCQs first (don’t peek).
- Click Check Answers to see score + explanations.
- Try the FRQ, then reveal the sample solution.
Unit 1 Quiz — Multiple Choice (15)
Choose the best answer. (One correct option each.)
1) Which statement best describes an algorithm?
Answer: C. Algorithms are language-independent step-by-step procedures that solve a problem.
2) Consider the code segment below. What happens when it is compiled?
int x = 4
int y = 5;
Answer: B. Missing semicolon after
int x = 4 causes a compile-time (syntax) error.3) What is the value of y after the code executes?
int x = 3;
double y = x / 2;
Answer: A.
x / 2 is int / int → integer division → 1, then stored as 1.0.4) Which line correctly reads an int from the keyboard using Scanner named in?
Answer: D.
nextInt() reads an integer token from input.5) What is printed by the code segment?
System.out.println((double) 5 / 2);
Answer: B. Casting makes the division
double / int → 2.5.6) What is the value of a after the code executes?
int a = 5;
a += 3 * 2;
Answer: C. Multiply first:
3*2=6, then a=a+6 → 11. (Choices include a duplicate; AP won’t, but the intended value is 11.)7) Which import statement allows a program to use the Scanner class?
Answer: A.
Scanner is in java.util.8) Which comment best documents a precondition for a method?
Answer: D. A precondition states what must be true before the method is called.
9) Which part of the method header below is the return type?
public static int add(int a, int b)
Answer: B. The return type is the type immediately before the method name.
10) Which call correctly invokes a class method from the Math class?
Answer: A.
Math methods are static (class) methods called using the class name.11) Which statement compiles correctly?
Answer: C.
Math.pow returns a double and takes two arguments.12) In the code below, what best describes s?
String s = "hello";
Answer: B. Object variables store references to objects (like
String objects).13) Which line demonstrates instantiation (creating a new object)?
Answer: D. The keyword
new creates (instantiates) a new object.14) What is the value of t after the code executes?
String word = "Java";
String t = word.substring(1, 3);
Answer: A.
substring(start, end) includes start and excludes end: indices 1–2 → "av".15) What is printed by the code segment?
String s = "ab";
s = s + "cd";
System.out.println(s.length());
Answer: C. After concatenation,
s is "abcd", which has length 4.Unit 1 Quiz — FRQ (1)
This is a short “Unit 1 style” FRQ: strings + method signature + simple expressions (no loops/conditionals needed).
FRQ 1) makeUsername
Write a static method makeUsername that creates a username from a first and last name.
- The method header must be:
public static String makeUsername(String first, String last) - Return a String in the format: lowercase first initial + lowercase last name.
- Assume
firstandlasteach contain at least 1 character. - Examples:
makeUsername("Jake", "Sully")returns"jsully"makeUsername("Ada", "Lovelace")returns"alovelace"
Write your solution here:
Sample solution
public static String makeUsername(String first, String last) {
String initial = first.substring(0, 1).toLowerCase();
String lname = last.toLowerCase();
return initial + lname;
}
Why this works: substring(0,1) gets the first character, toLowerCase() normalizes case,
and + concatenates Strings.