AP CSA Unit 2: Using Objects: Strings, Math, and Wrappers

276 multiple-choice questions on AP Computer Science A Unit 2, Using Objects: Strings, Math, and Wrappers, organized by topic. They are not tied to any one class or teacher: they follow the College Board course framework. Learn the 43 key terms with the flashcards first, then test yourself. Every answer comes with a short explanation.

Take the practice test · Flashcards · All study tools

What it covers

  1. 2.1 Objects, classes, and references
  2. 2.2 String methods
  3. 2.3 Math class and wrapper classes

Key terms

2.1 Objects, classes, and references

Class
A blueprint that defines the attributes and behaviors shared by every object of one type
Instance
One object created from a class, with its own values for the class's attributes
Instantiation
Creating a new object from a class, which in Java is done with new and a constructor call
new operator
The Java keyword that allocates a new object and runs a constructor to set its initial state
Attribute
A piece of data that every object of a class stores, such as a student's name or a lamp's wattage
Behavior
An action an object can perform, defined by one of its class's methods
Method signature
A method's name together with its ordered parameter types, such as area(double, double)
Parameter list
The variables declared in parentheses in a method or constructor header, with their types and order
Return type
The type of value a method sends back to its caller, written before the method name in the header
Non-void method
A method that returns a value, so a call to it can be used inside an expression or stored in a variable
Dot operator
The period that connects an object reference or class name to the method being called
null
The special value of a reference variable that does not refer to any object
Overloaded constructor
One of several constructors in a class that share the class's name but differ in their parameter lists
Instance method
A method that is called on a particular object and can use that object's state
Object
An instance of a class with its own state (fields) and behavior (methods)
Reference variable
A variable that stores the location of an object, not the object itself
NullPointerException
Run-time error from calling a method or accessing a field on null

2.2 String methods

String literal
A sequence of characters written between double quotes in code, such as "hello", which creates a String object
String immutability
The property that a String object's characters can never change; methods such as substring return new strings
Zero-based indexing
Numbering positions starting at 0, so the first character of a String is at index 0 and the last is at length() − 1
StringIndexOutOfBoundsException
The run-time exception thrown when a String method gets an index below 0, above length(), or a start index greater than the end index
Escape sequence
A backslash followed by a character that stands for a special character inside a String literal: \" for a quote, \\ for a backslash, \n for a new line
Empty string
The String "" that contains no characters and has length 0
length()
The String method that returns the number of characters in the string, counting spaces and punctuation
compareTo
The String method that returns a negative int, 0, or a positive int depending on whether the calling string comes before, equals, or comes after its argument
Lexicographic order
Dictionary-like ordering of strings by comparing characters one at a time, in which every uppercase letter comes before every lowercase letter
Left-to-right concatenation
The rule that + is evaluated from left to right, so numbers are added until a String appears and are concatenated after it
substring(a, b)
Returns characters from index a up to but not including b
indexOf
Returns the index of the first occurrence of a substring, or −1
equals vs ==
equals compares string contents; == compares references

2.3 Math class and wrapper classes

Math.random()
Returns a double in [0.0, 1.0)
Autoboxing
Automatic conversion from a primitive to its wrapper object (int to Integer)
Math class
A built-in Java class of static methods for common calculations, such as abs, pow, sqrt, and random
Static method
A method that belongs to a class rather than to any one object, so it is called with the class name, as in Math.sqrt(25)
Math.abs
Returns the absolute value of its argument; an int argument gives an int result and a double argument gives a double result
Math.pow
Math.pow(base, exponent) returns base raised to the exponent as a double
Math.sqrt
Returns the positive square root of its argument as a double
NaN
A special double value meaning "not a number", produced by calculations such as the square root of a negative number
Integer.parseInt
Converts a String of digits, with an optional leading sign, into an int
Double.parseDouble
Converts a String that holds a number into a double
NumberFormatException
The run-time exception thrown when a String can't be converted to a number, as in Integer.parseInt("ten")
Unboxing
Automatic conversion from a wrapper object to its primitive value, such as Integer to int
Integer.valueOf
A static method that returns an Integer object holding a given int value

Sample practice questions

  1. A class is best described as

    1. a single instance with specific values stored in its fields
    2. a primitive variable that holds one value at a time
    3. a blueprint that defines the attributes and behaviors of objects
    4. a method that returns a value to the code that called it

    Answer: C. a blueprint that defines the attributes and behaviors of objects Objects are instances created from a class, which defines their fields and methods.

  2. What is the value of "computer".substring(3)?

    1. "com"
    2. "mputer"
    3. "pute"
    4. "puter"

    Answer: D. "puter" substring(3) returns characters from index 3 to the end: c(0) o(1) m(2) p(3)...

  3. Which expression produces a random int from 1 to 6 inclusive?

    1. (int) (Math.random() * 6) + 1
    2. (int) Math.random() * 6 + 1
    3. (int) (Math.random() * 7)
    4. (int) (Math.random() * 6)

    Answer: A. (int) (Math.random() * 6) + 1 Math.random() * 6 is in [0, 6); casting gives 0 to 5; adding 1 gives 1 to 6. If the cast is applied to Math.random() alone, it happens first and always gives 0.

  4. Which statement creates a new object?

    1. int s = Integer.parseInt("5");
    2. Scanner s = null;
    3. Scanner s = new Scanner(System.in);
    4. double s = Math.sqrt(25.0);

    Answer: C. Scanner s = new Scanner(System.in); The new keyword calls a constructor to create an object. The other choices store a primitive value or null, so no new object is created.

  5. What is the value of "computer".substring(2, 5)?

    1. "mput"
    2. "mpu"
    3. "omp"
    4. "pu"

    Answer: B. "mpu" substring(a, b) includes index a but excludes index b: indices 2, 3, 4.

  6. What is the value of Math.pow(2, 3)?

    1. 8.0
    2. 8
    3. 6.0
    4. 9.0

    Answer: A. 8.0 Math.pow returns a double.

  7. A reference variable that does not refer to any object has the value

    1. 0
    2. an empty string
    3. false
    4. null

    Answer: D. null Reference variables hold null when they don't refer to an object.

  8. What is "hello".length()?

    1. 4
    2. 6
    3. 0
    4. 5

    Answer: D. 5 length() returns the number of characters.

  9. What is the value of Math.sqrt(49)?

    1. 7
    2. 49.0
    3. 7.0
    4. 24.5

    Answer: C. 7.0 Math.sqrt returns a double.

  10. Calling a method on a null reference causes

    1. a NullPointerException at run time
    2. the method to return 0
    3. a compile-time error always
    4. the program to ignore the call

    Answer: A. a NullPointerException at run time There is no object to run the method on, so a NullPointerException is thrown.

Practice all 276 questions

More AP Computer Science A

Studying for a test in your own class? Make a practice test from your class notes.