Objects, Instantiation, and Strings
Free AP Computer Science A multiple-choice practice for topics 1.12–1.15, Unit 1 (Using Objects and Methods). Answer five random questions, check your work, and review the essential knowledge behind each one.
Unit 1: Using Objects and Methods. Topics 1.12–1.15. This set has 55 questions. Each time the page loads you get five of them, chosen at random. Pick an answer, press Check answer, and read the explanation for every choice. Reload the page or use New questions for a fresh five.
Topics assessed
- 1.12 Objects: Instances of Classes
- 1.13 Object Creation and Storage (Instantiation)
- 1.14 Calling Instance Methods
- 1.15 String Manipulation
Practice questions
Loading questions…
Essential knowledge for this set
These are the essential knowledge (EKS) statements from the AP Computer Science A Course and Exam Description that the 55 questions in this set assess, grouped by topic and learning objective. The table under each question links here.
Topic 1.12: Objects: Instances of Classes
1.12.A — Explain the relationship between a class and an object.
- 1.12.A.1 An object is a specific instance of a class with defined attributes. A class is the formal implementation, or blueprint, of the attributes and behaviors of an object.
- 1.12.A.2 A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass. Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without replacing these in the code. This creates an inheritance relationship from the subclasses to the superclass.
- 1.12.A.3 All classes in Java are subclasses of the Object class.
1.12.B — Develop code to declare variables to store reference types.
- 1.12.B.1 A variable of a reference type holds an object reference, which can be thought of as the memory address of that object.
Topic 1.13: Object Creation and Storage (Instantiation)
1.13.A — Identify, using its signature, the correct constructor being called.
- 1.13.A.1 A class contains constructors that are called to create objects. They have the same name as the class.
- 1.13.A.2 A constructor signature consists of the constructor’s name, which is the same as the class name, and the ordered list of parameter types. The parameter list, in the header of a constructor, lists the types of the values that are passed and their variable names.
- 1.13.A.3 Constructors are said to be overloaded when there are multiple constructors with different signatures.
1.13.B — Develop code to declare variables of the correct types to hold object references.
- 1.13.B.1 A variable of a reference type holds an object reference or, if there is no object, null.
1.13.C — Develop code to create an object by calling a constructor.
- 1.13.C.1 An object is typically created using the keyword new followed by a call to one of the class’s constructors.
- 1.13.C.2 Parameters allow constructors to accept values to establish the initial values of the attributes of the object.
- 1.13.C.3 A constructor argument is a value that is passed into a constructor when the constructor is called. The arguments passed to a constructor must be compatible in order and number with the types identified in the parameter list in the constructor signature. When calling constructors, arguments are passed using call by value. Call by value initializes the parameters with copies of the arguments.
- 1.13.C.4 A constructor call interrupts the sequential execution of statements, causing the program to first execute the statements in the constructor before continuing. Once the last statement in the constructor has been executed, the flow of control is returned to the point immediately following where the constructor was called.
Topic 1.14: Calling Instance Methods
1.14.A — Develop code to call instance methods and determine the result of these calls.
- 1.14.A.1 Instance methods are called on objects of the class. The dot operator is used along with the object name to call instance methods.
- 1.14.A.2 A method call on a null reference will result in a NullPointerException.
Topic 1.15: String Manipulation
1.15.A — Develop code to create string objects and determine the result of creating and combining strings.
- 1.15.A.1 A String object represents a sequence of characters and can be created by using a string literal or by calling the String class constructor.
- 1.15.A.2 The String class is part of the java.lang package. Classes in the java.lang package are available by default.
- 1.15.A.3 A String object is immutable, meaning once a String object is created, its attributes cannot be changed. Methods called on a String object do not change the content of the String object.
- 1.15.A.4 Two String objects can be concatenated together or combined using the + or += operator, resulting in a new String object. A primitive value can be concatenated with a String object. This causes the implicit conversion of the primitive value to a String object.
- 1.15.A.5 A String object can be concatenated with any object, which implicitly calls the object’s toString method (a behavior that is guaranteed to exist by the inheritance relationship every class has with the Object class). An object’s toString method returns a string value representing the object. Subclasses of Object often override the toString method with class-specific implementation. Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass, but the behavior of the method is specific to the subclass.
1.15.B — Develop code to call methods on string objects and determine the result of calling these methods.
- 1.15.B.1 A String object has index values from 0 to one less than the length of the string. Attempting to access indices outside this range will result in a StringIndexOutOfBoundsException.
-
1.15.B.2 The following String methods—including what they do and when they are used—are part of the Java Quick Reference:
- int length() returns the number of characters in a String object.
- String substring(int from, int to) returns the substring beginning at index from and ending at index to - 1.
- String substring(int from) returns substring(from, length()).
- int indexOf(String str) returns the index of the first occurrence of str; returns -1 if not found.
- boolean equals(Object other) returns true if this corresponds to the same sequence of characters as other; returns false otherwise.
- int compareTo(String other) returns a value < 0 if this is less than other; returns zero if this is equal to other; returns a value > 0 if this is greater than other. Strings are ordered based upon the alphabet.
- 1.15.B.3 A string identical to the single element substring at position index can be created by calling substring(index, index + 1).