Unit 2 Practice
double y = 4.7; Integer n = new Integer((int) y); System.out.println(n);
4 -----> the 4.7 will get truncated to 4
String s = "Animal"; System.out.println(s.indexOf("al")); What is the output to be printed?
4, because nima is 1 through 4 on the index
Given the following code, what would be the value stored in the Double object referenced by the variable d? 7 Nothing will be stored. This code segment will cause an error.' "seven" 7.0 8
7.0
int a = 43; int b = 17; System.out.println(a % b); What is the output printed?
9 ----> 17 long divided by 43 is 2 with a remainder of (9)
double x = -97.6; System.out.println((int) Math.abs(x));
97
Which of the following is true? A double is a primitive data type so a double variable directly stores the data for the double. A double is a primitive data type so a double variable holds a reference to a location in memory. A double is a class data type so a double variable holds a reference to a location in memory. A double is a class data type so a double variable directly stores the data for the double.
A double is a primitive data type so a double variable directly stores the data for the double.
Suppose s1 and s2 are Strings and the following code is run: s1 = s1.toLowerCase(); s2 = s1.toUpperCase(); System.out.println(s1.compareTo(s2)); What is the numerical output with the .compareTo statement?
A positive value -----> Because you are comparing a lowercase first, than an uppercase. s2 is the most recent statement, so it follows as is in the print They are unchangeable values
What is w1 doing to w2 in the following code below w1 = w2.toUpperCase(); What is the output?
ANTS is the output, because .toUpperCase capitalizes all the characters.
System.out.println("Hello, \n\tWorld"); The output
Hello, ------> World Because \n puts it on a new line, and \t spaces it out
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 Integer.MaxValue() Integer.Max() There is no limit to the value an Integer can hold.
Integer.MAX_VALUE
int s = Math.sqrt(49); What is wrong with the code? It needs a numeric cast since Math.sqrt returns a double data type. The 49 should be 49.0 since Math.sqrt takes a double type parameter. Math.sqrt cannot take a number, it must be a variable. Math.sqrt is void and does not return a value. Nothing, the code is correct as written.
It needs a numeric cast since Math.sqrt returns a double data type.
Which of the following is not a method in the Integer wrapper class? MAX_VALUE equals intValue toString
MAX_VALUE
The correct way to concatenate a string is: Using the combine keyword None of the following Use a comma between words Use the string method merge Using the concat() function between them
None of the items listed. You have to use the + or += to concatenate
int j = (int) (3 * Math.random() + 5); Which of the following could be randomly generated when this is printed? 7 6.4 5 8 6 5.2
Note: Math.random starts between values 0 and 1 by default. 7, 6, and 5 because the generator starts at 5 (+5) It is casting to an int so it cannot be any of the decimal options It cannot be 8 because it goes down by one, so the max is 7.
Which of the following describes best the relationship between an object and a class? A class is a specific variable in a program, which has behaviors defined by objects. A class is a piece of data which is contained in an object. An object is used to create classes, which are primitive data types. Objects are created using classes, which are similar to blueprints.
Objects are created using classes, which are almost like blueprints
Which of the following commands will print the output below? \/ '` System.out.print("\\/\n'\`"); System.out.print("\/\n'`"); System.out.print("\\/\n\'\`"); System.out.print("\\/\n\'`"); System.out.print("\\\/\n'\`");
Option D: System.out.print("\\/\n\'`");
Which of the following lines of code could be used to create a regular polygon with 3 sides of length 2.0? Choose all options that apply. RegularPolygon rp = new RegularPolygon(3.0) RegularPolygon rp = new RegularPolygon() RegularPolygon rp = new RegularPolygon(2.0, 3) RegularPolygon rp = new RegularPolygon(3) RegularPolygon rp = new RegularPolygon(3, 2.0) RegularPolygon rp = new RegularPolygon(2.0)
RegularPolygon rp = new RegularPolygon(3, 2.0) RegularPolygon rp = new RegularPolygon(2.0) Because 3 is the default, the second one works as well because it will display equilateral triangle
Which of the following are signatures for constructors in the Regular Polygon class? Choose all options that apply. RegularPolygon(double len, int n, String name) RegularPolygon(double len) RegularPolygon() RegularPolygon(int n) RegularPolygon(double len, int n) RegularPolygon(int n, double len, String name)
RegularPolygon(double len) RegularPolygon() RegularPolygon(int n) Why the others won't work: You can't put a string name in the Regular Polygon class. And you cannot put double first in the parameters.
Which of the following are classes? void Scanner "Java" double String int 14 7.5
Scanner and String
Which of the following would set the String variable hobby to be completely empty (i.e. not even storing a memory address)? String hobby = "null"; String hobby = " "; String hobby = ""; String hobby = 0; String hobby = null;
String hobby = null;
Which of the following code segments correctly declares a String s and gives it a value of "fundamentals"? Select all that apply. s = new String(); s = "fundamentals"; String s = "fundamentals"; s = new String(); String s = new "fundamentals"; new String s = ("fundamentals"); String s = new "fundamentals"; String s; s = "fundamentals";
String s = "fundamentals"; new String s = ("fundamentals"); String s; s = "fundamentals";
Which of the following is a class in Java? String Decimal Int Bool
String. The others are primitive data types. Strings must be created as objects of the String class.
What is the output for System.out.println("Sum=" + 4 + 5); ?
Sum=45 ---------> Because interprets it as a literal string concatenation, not int addition.
What will be printed by the following code: System.out.println("I came,\tI saw,\n\tI left.");
The last option, option E. Because the n part in \n\t creates a new line for the "I left" at the bottom \t spaces out the words \n puts it on a new line \ is just used for quotes
.compareTo: For (type: ants) what is the output under the following code with .compareTo(); System.out.println(w2.compareTo(w3));
The output is -5. Because it is comparing the w2 "ants" beginning with an A at first to the w3 "flies", beginning with an F, so Lexi graphically speaking, it would be a minus 5 (-5) because the earlier lettered word was compared first Note: If the first characters of the word are the same, it checks the next, and gives the result so on so forth. If the two whole words are the same, the result is 0
.compareTo: For (type: ants) What is the output for this use of .compareTo() method? System.out.println(w3.compareTo("bugs"));
The output is 4 because "bugs" beginning with a b and "flies" beginning with an f are 4 characters apart in the alphabet. It is a positive value because Flies (f) is higher alphabetically than bugs (b)
Which of the following is true about classes and objects? What isn't true? Classes and object are the same exact thing and the terms can be used interchangeably. It is possible to have many classes of the same object. A class is an instance of an object. Objects only contain one piece of data Object variables have a reference pointing to their location in memory.
True - Object variables have a reference pointing to their location in memory.
Consider the following code, is it true or false? System.out.println("Answer: " + (3 + 7)); Output: Answer: 10
True, because it is in parenthesis, so it does the math
Consider the following code, is it True or False?: int x = 27; int y = 13; System.out.print("Answer: " + x + y); Output: Answer: 2713
True. It is not in parenthesis so it will not do the math, just state the integers as is.
RegularPolygon shape = new RegularPolygon(5, 2.0); Which of the following changes the length to 10, and the shape to be a six sided "hexagon"? shape.addSides(); shape.setSideLength(10.0); shape.addSides(1); shape.setSideLength(8.0); shape.addSides(6); shape.setSideLength(10.0); shape.setNumSides(6); shape.setSideLength(8.0);
shape.addSides(); shape.setSideLength(10.0); the setSideLength method resets the length to 10, and the addSides method with nothing in the parameters adds 1 to the 5, making it a hexagon with (6) sides
String w1; String w2 = "ants"; String w3 = "flies"; w1 = "type: " + w2; What is the output?
type: ants -----> There is a space in the quote, hence why it is type: ants
Consider the following code: String a = "determined"; String b = "learner"; System.out.println(a + "-" + b); What is the result printed?
determined-learner
The Math.pow() method returns a value that is of the ______ data type. int boolean String double
double
Which of the following code segments contains a static method call? double a = 1.5; double b = Math.pow(a, 4); Circle c = new Circle(5.0); double a = c.getArea(); int a = Integer.MAX_VALUE; a--; Rectangle r = new Rectangle(); r.setWidth(20.5); Scanner s = new Scanner(System.in); String l = s.nextLine();
double a = 1.5; double b = Math.pow(a, 4); -------------> Math is a static call method. It belongs to a class as a whole rather than an instance within the class.
RegularPolygon p = new RegularPolygon(5, 2.5); Which code uses a non-void method, (which gives always returns a value) that correctly calls on p, and uses the correct data type? int n = getNumSides(p); p.getArea(); int s = p.addSides(); double a = p.getArea(); p.setNumSides(7);
double a = p.getArea(); This is because the double suits both numbers 5 and 2.5, plus the getArea method returns an actual math value, hence why this makes it the answer
What of the following declares a new primitive data type variable? double d; d = double; new double; d = new double(); d double;
double d;
Substrings: For (type: ants) What is stored in w1 by the following? w1 = w3.substring(2);
ies --------> it takes w1, but gets the substring for w3, 2 specifically, but there is not stop part so it keeps going to the end. Hence why it is "ies".
Which package do the wrapper classes belong to? shapes java.lang java.util No package.
java.lang
Which of the following are examples of packages you can import and use classes from in java? numbers String integer java.lang shapes
java.lang, shapes
Take the following code: String s = "elementary"; What is the output if called by this and printed? s.substring(1, 4);
lem
Substrings: For (type: ants) What is stored in w1 by the following? w1 = w3.substring(1, 3);
li --------> it gets the substring for w3, which is "flies" so it will sample out the "li" part of the sentence in the output. That's what a substring does.
According to the Java Quick reference Guide what Math function do you NOT need to know for the AP exam?
max
What is a method which returns a value called?
non-void
Special value that can be assigned to an object that has no value:
null
To define an empty keyword, what keyword can be used?
null
String major = "Computer science"; WHat is the output when you call major.substring(1, 2); ?
o -----> Because it goes to C(o)mputer science, characters 1 and 2 om and subtracts it by 1, so "o" is the output.
Which of the following is part of the signature of a method? order of parameters method name number of parameters return type type of parameters
order of parameters method name number of parameters type of parameters Return type is not a part of the signature of a method.
When two methods in a class have the same name they are said to be...
overloaded
RegularPolygon polygon1 = new Polygon(4.0); Which of the following will increase polygon1 sides by 3 polygon1.addSides(3); addSides(3); RegularPolygon.addSides(polygon1, 3); polygon1.sides = polygon1.sides + 3; RegularPolygon.addSides(3);
polygon1.addSides(3);
Java allocates (distributes for a specific purpose) a space in memory called?
reference
String w3 = "aardvark"; System.out.println(w3.substring(w3.length()-2)); What is the resulting output?
rk -------->
Consider the code below: String a = "first"; String b = a; a = "second"; System.out.println(a + " " + b); What is printed when the code runs?
second first -----> Strings are immutable (cannot change overtime/cannot be changed), meaning where a is first set to "first", b is also set to "first" as a result, where a's value is then changed to "second" resulting in "second first" being the output.
What kind of Data can hold many pieces of data at the same time?
class
Which of the following are true about class data types: can be created can be customized can hold multiple pieces of data of different types cannot be created
-> can be created -> can be customized -> can hold multiple pieces of data of different types x- cannot be created
Rectangle r1 = new Rectangle(3.0); r1.setLength(5.0); System.out.println(r1.getArea()); What's the output when the code is executed? 15.0 6.0 12.0 25.0 9.0
15.0 --------> since the r1 length is then set to 5.0, the 3.0 in the parameters is assumed to be the width. Then theprint statement asks to get the area which multiplies the length and width, hence 15.0
What does the following code segment do below? String word = new String("question"); Declares and initializes a primitive of the String type. Declares and initializes an object of the (String) class. Declares and initializes an object of the (string) class. Declares and initializes a primitive of the string type
Declares and initializes an object of the (String) class. Classes are always uppercase
Which of the following code segments show examples of autoboxing? Double d = new Double(5.0); double e = d - 1.5; Double d = 15.0; Integer i = new Integer(15); System.out.print("Answer: " + i); Double a = new Double(17.5); int compareValue = a.compareTo(20.0); String s = "a string"; Integer len = s.length();
Double d = 15.0; -----> This treats "Double" like a double Double a = new Double(17.5); int compareValue = a.compareTo(20.0); --------> This treats Double a like a regular double being compared to another double String s = "a string"; Integer len = s.length(); --------> This treats Integer len like a regular int, and gets the length to which to be printed as an int number
Which code shows an example of autoboxing being used correctly? Integer b = new Integer(14);System.out.println(b.intValue()); Double n = new Double(4.8);n += 3.5;System.out.println(n); Integer c = Integer((int)5.6); Integer k = new Integer(7); Double m = 2.5;
Double m = 2.5; --------> Because the Double m is being autoboxed to be a regular double data type.
A class ExampleClass is imported into Java. Which of the following could be a correct signature for a constructor for this class? ExampleConstructor(int param1) constructorEC() ExampleClassMaker(double param1, double param2) ExampleClass(int param1, String param2)
ExampleClass(int param1, String param2) None of the others actually use the ExampleClass part in the beginning
Consider the code below: Scanner scan = new Scanner(System.in); Which of the following sentences best describes the features in this code segment? Variable Scanner points to an object, scan is the class of this object. Variable scan points to an object, Scanner is the class of this object. Variable System.in points to an object, scan is the class of this object. Variable Scanner points to an object, System.in is the class of this object. Variable scan points to an object, System.in is the class of this object.
Variable scan points to an object, Scanner is the class of this object. scan is an object of the Scanner class
Which of the following describe situations where unboxing occurs? Choose all options that apply. When a method is called on a wrapper class object When a wrapper class object is assigned to a variable of the corresponding primitive type When a wrapper class object is initialized using a constructor with a primitive parameter 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 When a wrapper class object is passed to a method that expects a value of the corresponding primitive type
Which of the following symbols need an escape character to be output in a System.out.print statement? ! \ # $ @
\
To output the following: Nums: [31, 43, 28, 17 ] You would need to use what escape sequence in the blank: System.out.print("Nums: [31, 43, 28, 17____]"); \s \n None \t \\
\n --------> it is putting it on a new line