APCSA Chapter 5 & 6 Questions
What symbol will you use to concatenate two string values?
+ (plus sign)
How many JetPlane objects are created when the statement below are run? JetPlane ref1 = new JetPlane(); JetPlane ref2 = ref1;
1
If a JetPlane class has defined an integer property named "maxSpeed", how many unique copies of that property are made after the statements below run? JetPlane myRide = new JetPlane(); JetPlane yourRide = new JetPlane(); JetPlane theirRide = new JetPlane();
3
Given the code below, what value is stored in the result variable? String surprise = "quiz"; int result = surprise.length();
4
Given the following constructor signature, what types of data must be provided and in what order? public JetPlane(String nickname, double cost, int speed)
A String, a double, and an int, in that order
Which answer best describes the relationship between the terms "class" and "object"?
An object is a copy of a class that is created in a running program
Which of the following two lines will successfully initialize a String variable with the text "I'm Alive!"? String text1 = new String("I'm Alive!"); String text2 = "I'm Alive!";
Both will work
If you call the Scanner's nextInt() method, then the nextLine() method, what should you do in-between to ensure your program gets the expected user data?
Call nextLine() to flush the newline character left behind by nextInt()
What does the result variable contain after the following statement? String result = "Captain," + "incoming" + "message";
Captain,incomingmessage
What term is used to describe a method that runs automatically when an object is created to help initialize the object properties?
Constructor
A class is generally defined by what three main features?
Data, behavior, and relationships
What must a class do to "overload" a method?
Define multiple versions of the method with the same name but different parameters
What does the result variable contain after the following statement? String result = String.format("I'll take %5s please", "2");
I'll take 2 please
What part of the Java platform contains a large library of pre-written classes that you can use?
Java Class Library
Which of the following lines will create a new object of type "JetPlane" and store it in a reference variable?
JetPlane myRide = new JetPlane();
If the JetPlane class constructor requires an int value and String value to initialize an object, how would you provide those values?
JetPlane myRide = new JetPlane(1000, "Gulfstream");
What part of a class is used to define the behavior?
Methods
What does the term OOP mean?
Object-Oriented Programming
What term is used to describe an organized set of Java classes?
Package
Given the code below, what value is stored in the result variable? String surprise = "Quiz"; String result = surprise.toUpperCase();
QUIZ
What is a big difference between reference data types and primitive data types?
References point to more complex objects somewhere in memory, while each primitive value contains a copy of some simple data
Assuming a Scanner has been initialized in the "input" variable, which of the following statements will read an entire line of text from the user?
String text = input.nextLine();
What three things can you tell from any method signature?
The method's name, required input parameters, and return data type
What is the purpose of the ASCII and Unicode systems?
To represent individual characters with unique numbers
What does the result variable contain after the following statement? String result = String.format("%.3s", "abcdef");
abc
Given the method signature below, what is the method's return data type? boolean takeOff(int maxSpeed, double currentWindSpeed)
boolean
Given the method call below, which of the following answers is most likely method signature for the "takeOff" method? boolean isAirborne = myRide.takeoff(25.5);
boolean takeOff(double windSpeed)
Given the code below, what value is stored in the result variable? String surprise = "quiz"; boolean result = surprise.contains("Q");
false
Given the following code, what will be the value stored in the result and why? String color1 = "RED"; String color2 = "red"; boolean result = color1.equals(color2);
false, because while the letters in each value match, the capitalization is different
What does the result variable contain after the following statement? String result = String.format("humpty %2$s", "humpty", "dumpty");
humpty dumpty
What does the result variable contain after the following statement? String result = String.format("humpty dumpty", "sat", "on", "a", "wall");
humpty dumpty
Which statement can be placed at the top of your source code for easy access to the Scanner class?
import java.util.*;
If the "getMaxSpeed" method returns an int and is defined as "static" on the JetPlane class, which of the following statements would successfully call that method?
int max = JetPlane.getMaxSpeed();
If a JetPlane class has defined an integer property named "maxSpeed", given the code below, which statement would successfully assign a value to the property? JetPlane myRide = new JetPlane();
myRide.maxSpeed = 1000;
Which Scanner method would you call to read in the next user entry and automatically convert it to a "double" value?
nextDouble()
Given the code below, without knowing the exact contents of the strings, what can always be said about result1 and result2? String s1 = ... String s2 = ... boolean result1 = s1.equals(s2); boolean result2 = s2.equals(s1);
result1 and result2 will have the same value
Given the following code, what will be the value stored in the result and why? String color1 = "RED"; String COLOR2 = "red"; boolean result = color1.equalsIgnoreCase(COLOR2);
true, because the equalsIgnoreCase() method ignores capitalization and just compares the letters
Given the following code, what will be the value stored in the result and why? String color1 = "red"; String color2 = "red"; boolean result = color1.equals(color2);
true, because the two string values have exactly the same characters and capitalization
Given the code below, what value is stored in the result variable? String surprise = "quiz"; String result = surprise.substring(1,4);
uiz
Given the code below, what value is stored in the result variable? String surprise = "quiz"; char result = surprise.charAt(3);
z