Unit 2 Edhesive
String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w2.toUpperCase();
APPLE
Which of these code segments shows a constructor being used to create a new object?
Circle c = new Circle(3.8);
Which of the following statements about overloaded constructors is true?
Constructors for a class can be overloaded but they must all have a different number of parameters, different order of parameters or different types of parameters.
Select all the classes from the list
Scanner String
Consider the following code: System.out.println("Answer: " + (2 + 5)); This code outputs: Answer: 7
True
Which of the following symbols need an escape character to be output in a System.out.print statement?
\
To output: Nums: 12 13 14 15 You would need to use what escape sequence in the blank: System.out.print("Nums: 12 13 14______15");
\t
Suppose three strings are declared and initialized as shown below. String a = "lizard"; String b = "chameleon"; String c = "lizard"; Which of the following calls to the compareTo method will return a negative number?
b.compareTo(a)
Select all the primitive data types.
boolean int double
According to the Java Quick reference Guide which of the following Math functions do you NOT need to know for the AP exam?
max
______ data types hold only one piece of information at a time.
primitive
A method which returns no value is referred to as...
void
Which of the following is not a method in the Integer wrapper class?
MAX_VALUE
Where can a description of the accessible methods for a class be found?
In the API documentation for the relevant library
What is the maximum value an Integer can hold?
Integer.MAX_VALUE
Suppose s is a String which contains 5 characters. Which of the following calls will not cause an error?
None of the calls shown will cause an error
Which of the following is not a wrapper class?
String
Which of the following would set the String variable firstName to be completely empty (i.e. not even storing a memory address)?
String firstName = null;
How could you create a String variable, s, and set it equal to "Hi There" in one line?
String s = "Hi There";
What is output to the screen by the following code? System.out.println("Sum=" + 4 + 5);
Sum=45
Suppose the variable circ1 is declared and instantiated as follows: Circle circ1 = new Circle(4.0); Which of the following will change the radius of circ1 to 6.5?
circ1.setRadius(6.5);
Which of the following best describes what happens when the code segment below is run? Circle c = new Circle(); System.out.println(c);
"circle with radius 1.0" is printed
Consider the following code: String major = "Computer Science"; What is returned by the method call major.substring(1, 2)?
"o"
Which of the following is true about classes and objects?
(I don't know the answer, but it is NOT "It is possible to have many classes of the same object.")
String w1; String w2 = "apple"; String w3 = "banana"; What is the output? System.out.println(w2.compareTo(w3));
-1
String w1; String w2 = "apple"; String w3 = "banana"; What is the output? System.out.println(w3.compareTo("fruit"));
-4
Consider the following code: int x = 9; int y = 8; System.out.println(x + y); What is output?
17
Consider the following code: int a = 20; int b = 8; System.out.println(a % b); What is output?
4
Consider the following code. int j = (int) (3 * Math.random() + 5); Which of the following could be values for j after this code segment is run? Choose all options that apply.
5 7 6
Given the following code, what would be the value stored in the Double object referenced by the variable d? int x = 7; Double d = new Double(x);
7.0
Consider the following code: double x = -97.6; System.out.println((int) Math.abs(x)); What is output?
97
Which of the following best describes the relationship between a class and an object?
A class is a template or blueprint from which an object can be created.
Consider the following code: String a = "CSA"; String b = a; a = "AP"; System.out.println(a + " " + b"); What is printed when the code is run? Hint: Remember Strings are immutable.
AP CSA
What will be printed by the following code? System.out.print("All\nright\tnow");
All right now
Consider the following code segment: Scanner scan = new Scanner(System.in); What does this code segment do?
Declares and initializes an object of the Scanner class
Which of the following code segments show examples of autoboxing?
Double a = new Double(17.5); int compareValue = a.compareTo(20.0); Double d = 15; String s = "a string"; Integer len = s.length();
Which code shows an example of autoboxing being used correctly?
Double m = 2.5;
A class ExampleClass is imported into Java. Which of the following could be a correct signature for a constructor for this class?
ExampleClass(int param1, String param2)
Consider the following code: int x = 56; int y = 30; System.out.print("Answer: " + x + y); This code outputs: Answer: 86
False
What is output by the following code? System.out.println("Hello, \n\tWorld");
Hello, World
What is wrong with: int s = Math.sqrt(49);
It needs a numeric cast since Math.sqrt returns a double data type.
Which of the following creates a rectangle with length and width equal to 4.5? Choose all answers that apply.
Rectangle r = new Rectangle(4.5); Rectangle r = new Rectangle(4.5, 4.5);
Which of the following are signatures for constructors in the Rectangle class? Choose all options that apply.
Rectangle(double len, double wid) Rectangle(double len) Rectangle()
Which of the following lines of code could be used to create a regular polygon with 7 sides of length 1.0? Choose all options that apply.
RegularPolygon rp = new RegularPolygon(7) RegularPolygon rp = new RegularPolygon(7, 1.0)
Which of the following is a class in Java?
Scanner
Which code shows us creating an object of data type Scanner class and storing it with a variable named X ?
Scanner X = new Scanner(System.in);
Which of the following code segments correctly declare a String s and gives it a value of "fortran".
String s; s = "fortran"; String s = "fortran";
Which of the following correctly declares a new variable which can be used to point to an object of the String class type?
String str;
Which of the following lines of code create a String variable x and set it equal to the word Cat?
String x = "Cat";
Which of the following commands will print the output below? \/ ''
System.out.print("\\/\n\'\'")
The correct way to concatenate two Strings is ______.
Using the + symbol between them
Suppose s is a string, and that the call s.indexOf("an") returns 3. Which of the following could be the value of s?
advance
Use the following code for questions 2-7: String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w2 + " sauce";
apple sauce
______ data types can hold several pieces of data and have methods to work on that data.
class
______ data types hold the memory address of where the data is stored.
class
The Math.pow() method returns a value that is of the ______ data type.
double
A RegularPolygon p is declared and instantiated as shown: RegularPolygon p = new RegularPolygon(5, 2.5); Which code example shows a non-void method correctly being called on p and its return value being stored using a variable of the correct type?
double a = p.getArea();
How would you declare a variable x of type double and set it equal to 25.25?
double x = 25.25;
Which package do the wrapper classes belong to?
java.lang
Which of the following are examples of packages you can import and use classes from in java? Choose all options that apply.
java.util edhesive.shapes
When two methods in a class have the same name they are said to be...
overloaded
Which of the following will be printed by the following code? RegularPolygon shape = new RegularPolygon(5); System.out.println(shape);
regular pentagon with side length 1.0
What does the following code do? String w3 = "aardvark"; System.out.println(w3.substring(w3.length()-2));
rk
Which of the following are true about class data types? Click all that apply.
- has built in methods (tools) - holds more than one piece of data at a time - a programmer can create them
What will be printed by the following code? String s = "Animal"; System.out.println(s.indexOf("an"));
-1
What is the output when the following code is executed? Rectangle r1 = new Rectangle(2.0); r1.setLength(4.0); System.out.println(r1.getPerimeter());
12.0
What would be printed by the following code? double y = 4.7; Integer n = new Integer((int) y); System.out.println(n);
4
Which one of the following sentences is true?
A String is a class data type so a String variable holds a reference to a location in memory
Consider the following code example: String word = new String("answer"); Which of the following sentences best describes the features in this code segment?
Variable word points to an object, String is the class of this object
Which of the following describe situations where unboxing occurs? Choose all options that apply.
When a wrapper class object is passed to a method that expects a value of the corresponding primitive type When a wrapper class object is assigned to a variable of the corresponding primitive type
String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w3.substring(1,3);
an
Consider the following code: String a = "credulous"; String b = "differently"; System.out.println(a + b); What is output?
credulousdifferently
Which of the following code segments contains a static method call?
double a = 1.5; double b = Math.pow(a, 4);
Suppose the String s is declared and initialized as follows: String s = "elementary"; What String would be returned by the method call s.substring(2, 6);?
emen
String w1; String w2 = "apple"; String w3 = "banana"; What is stored in w1 by the following? w1 = w3.substring(2);
nana
What keyword do we use to tell Java to set aside memory to create an object?
new
What keyword is used in order to build an object in memory of any data type (String, Scanner, etc)?
new
A special value that can be assigned to an object that has no value:
null
A special value that means "no object" is called ______.
null
Which of the following is part of the signature of a method?
number of parameters method name order of parameters type of parameters
The variable r1 is declared and initialized as a Rectangle as follows. Rectangle r1 = new Rectangle(3.0, 5.0); Which of the following correctly calls the Rectangle getArea method on r1?
r1.getArea();
Suppose s1 and s2 are Strings and the following code is run: s1 = s1.toLowerCase(); s2 = s2.toLowerCase(); System.out.println(s1.compareTo(s2)); If the code causes -25 to be output, which of the following must be true
s1 appears before s2 alphabetically
Suppose the variable shape is declared and initialized as a regular polygon as follows: RegularPolygon shape = new RegularPolygon(6, 2.0); Which of the following will change shape to be a regular decagon (with 10 sides) and side-length 5.0?
shape.addSides(4); shape.setSideLength(5.0);