Chapter 4 Core APIs Flashcards
Review Questions
1. What is output by the following code? (Choose all that apply.)
1: public class Fish { 2: public static void main(String[] args) { 3: int numFish = 4; 4: String fishType = "tuna"; 5: String anotherFish = numFish + 1; 6: System.out.println(anotherFish + " " + fishType); 7: System.out.println(numFish + " " + 1); 8: } }
A. 4 1
B. 5
C. 5 tuna
D. 5tuna
E. 51tuna
F. The code does not compile.
F
F.
- Line 5 does not compile. This question is checking to see whether you are paying attention to the types. numFish is an int, and 1 is an int. Therefore, we use numeric addition and get 5. The problem is that we can’t store an int in a String variable.
- Suppose line 5 said String anotherFish = numFish + 1 + “”;.
- In that case, the answers would be option A and option C. The variable defined on line 5 would be the string “5”, and both output statements would use concatenation.
2. Which of these array declarations are not legal? (Choose all that apply.)
A. int[][] scores = new int[5][];
B. Object[][][] cubbies = new Object[3][0][5];
C. String beans[] = new beans[6];
D. java.util.Date[] dates[] = new java.util.Date[2][];
E. int[][] types = new int[];
F. int[][] java = new int[][];
C, E
C, E, F.
- Option C uses the variable name as if it were a type, which is clearly illegal.
- Options E and F don’t specify any size.
- Although it is legal to leave out the size for later dimensions of a multidimensional array, the first one is required.
- Option A declares a legal 2D array.
- Option B declares a legal 3D array.
- Option D declares a legal 2D array.
- Remember that it is normal to see classes on the exam you might not have learned. You aren’t expected to know anything about them.
3. Note that March 13, 2022 is the weekend when we spring forward, and November 6, 2022 is when we fall back for daylight saving time. Which of the following can fill in the blank without the code throwing an exception? (Choose all that apply.)
var zone = ZoneId.of("US/Eastern"); var date = \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_; var time = LocalTime.of(2, 15); var z = ZonedDateTime.of(date, time, zone);
A. LocalDate.of(2022, 3, 13)
B. LocalDate.of(2022, 3, 40)
C. LocalDate.of(2022, 11, 6)
D. LocalDate.of(2022, 11, 7)
E. LocalDate.of(2023, 2, 29)
F. LocalDate.of(2022, MonthEnum.MARCH, 13);
A, C, D, F
A, C, D.
- Option B throws an exception because there is no March 40.
- Option E also throws an exception because 2023 isn’t a leap year and therefore has no February 29.
- Option F doesn’t compile because the enum should be named
Month
, rather than MonthEnum. - Option D is correct because it is just a regular date and has nothing to do with daylight saving time.
- Options A and C are correct because Java is smart enough to adjust for daylight saving time.
4. Which of the following are output by this code? (Choose all that apply.)
3: var s = "Hello"; 4: var t = new String(s); 5: if ("Hello".equals(s)) System.out.println("one"); 6: if (t == s) System.out.println("two"); 7: if (t.intern() == s) System.out.println("three"); 8: if ("Hello" == s) System.out.println("four"); 9: if ("Hello".intern() == t) System.out.println("five");
A. one
B. two
C. three
D. four
E. five
F. The code does not compile.
G. None of the above
A, C, D, E
A, C, D.
- The code compiles fine. Line 3 points to the String in the string pool.
- Line 4 calls the String constructor explicitly and is therefore a different object than s.
- Line 5 checks for object equality, which is true, and so it prints one.
- Line 6 uses object reference equality, which is not true since we have different objects.
- Line 7 calls intern(), which returns the value from the string pool and is therefore the same reference as s.
- Line 8 also compares references but is true since both references point to the object from the string pool.
- Finally, line 9 is a trick. The string Hello is already in the string pool, so calling intern() does not change anything. The reference t is a different object, so the result is still false.
5. What is the result of the following code?
7: var sb = new StringBuilder(); 8: sb.append("aaa").insert(1, "bb").insert(4, "ccc"); 9: System.out.println(sb);
A. abbaaccc
B. abbaccca
C. bbaaaccc
D. bbaaccca
E. An empty line
F. The code does not compile.
B
B.
- This example uses method chaining.
- After the call to append(), sb contains “aaa”.
- That result is passed to the first insert() call, which inserts at index 1.
- At this point, sb contains abbaa.
- That result is passed to the final insert(), which inserts at index 4, resulting in abbaccca.
6. How many of these lines contain a compiler error? (Choose all that apply.)
23: double one = Math.pow(1, 2); 24: int two = Math.round(1.0); 25: float three = Math.random(); 26: var doubles = new double[] {one, two, three};
A. 0
B. 1
C. 2
D. 3
E. 4
C
C.
- Remember to watch return types on math operations.
- One of the tricks is line 24.
- The
round()
methodreturns an int
when called with afloat
. - However, we are calling it with a
double
, so it returns along
.
- The
- The other trick is line 25. The
random()
methodreturns a double
. - Since two lines have a compiler error, option C is the answer.
7. Which of these statements is true of the two values? (Choose all that apply.)
2022–08–28T05:00 GMT-04:00 2022–08–28T09:00 GMT-06:00
A. The first date/time is earlier.
B. The second date/time is earlier.
C. Both date/times are the same.
D. The date/times are two hours apart.
E. The date/times are six hours apart.
F. The date/times are 10 hours apart.
A, E
A, E.
- When dealing with time zones, it is best to convert to GMT first by subtracting the time zone.
- Remember that subtracting a negative is like adding.
- The first date/time is 9:00 GMT,
- and the second is 15:00 GMT.
- Therefore, the first one is earlier by six hours.
8. Which of the following return 5 when run independently? (Choose all that apply.)
var string = "12345"; var builder = new StringBuilder("12345");
A. builder.charAt(4)
B. builder.replace(2, 4, “6”).charAt(3)
C. builder.replace(2, 5, “6”).charAt(2)
D. string.charAt(5)
E. string.length
F. string.replace(“123”, “1”).charAt(2)
G. None of the above
A, B, F
A, B, F.
- Remember that indexes are zero-based, which means index 4 corresponds to 5, and option A is correct.
- For option B, the replace() method starts the replacement at index 2 and ends before index 4. This means two characters are replaced, and charAt(3) is called on the intermediate value of 1265. The character at index 3 is 5, making option B correct.
- Option C is similar, making the intermediate value 126 and returning 6.
- Option D results in an exception since there is no character at index 5.
- Option E is incorrect. It does not compile because the parentheses for the length() method are missing.
- Finally, option F’s replace results in the intermediate value 145. The character at index 2 is 5, so option F is correct.
9. Which of the following are true about arrays? (Choose all that apply.)
A. The first element is index 0.
B. The first element is index 1.
C. Arrays are fixed size.
D. Arrays are immutable.
E. Calling equals() on two different arrays containing the same primitive values always returns true.
F. Calling equals() on two different arrays containing the same primitive values always returns false.
G. Calling equals() on two different arrays containing the same primitive values can return true or false.
A, C, ()
A, C, F.
- Arrays are zero-indexed, making option A correct and option B incorrect.
- They are not able to change size, which is option C.
- The values can be changed, making option D incorrect.
-
An array does not override
equals()
, so it uses object equality. Since two different objects are not equal, option F is correct, and options E and G are incorrect.
10. How many of these lines contain a compiler error? (Choose all that apply.)
23: int one = Math.min(5, 3); 24: long two = Math.round(5.5); 25: double three = Math.floor(6.6); 26: var doubles = new double[] {one, two, three};
A. 0
B. 1
C. 2
D. 3
E. 4
A
A.
- All of these lines compile.
- The
min()
andfloor()
methods return thesame type passed in
: int and double, respectively. - The
round()
method returns a long when called with adouble
. - Option A is correct since the code compiles.
11. What is the output of the following code?
var date = LocalDate.of(2022, 4, 3); date.plusDays(2); date.plusHours(3); System.out.println(date.getYear() + " " + date.getMonth() \+ " " + date.getDayOfMonth());
A. 2022 MARCH 4
B. 2022 MARCH 6
C. 2022 APRIL 3
D. 2022 APRIL 5
E. The code does not compile.
F. A runtime exception is thrown.
E
E.
- A LocalDate does not have a time element. Therefore, there is no method to add hours, making option E the answer.
12. What is output by the following code? (Choose all that apply.)
var numbers = "012345678".indent(1); numbers = numbers.stripLeading(); System.out.println(numbers.substring(1, 3)); System.out.println(numbers.substring(7, 7)); System.out.print(numbers.substring(7));
A. 12
B. 123
C. 7
D. 78
E. A blank line
F. The code does not compile.
G. An exception is thrown.
A, D, E
A, D, E.
- First, notice that the indent() call adds a blank space to the beginning of numbers, and stripLeading() immediately removes it. Therefore, these methods cancel each other out and have no effect.
- The substring() method has two forms.
- The first takes the index to start with and the index to stop immediately before.
- The second takes just the index to start with and goes to the end of the String.
- Remember that indexes are zero-based.
- The first call starts at index 1 and ends with index 2 since it needs to stop before index 3. This gives us option A.
- The second call starts at index 7 and ends in the same place, resulting in an empty String which is option E. This prints out a blank line.
- The final call starts at index 7 and goes to the end of the String finishing up with option D.
13. What is the result of the following code?
public class Lion { public void roar(String roar1, StringBuilder roar2) { roar1.concat("!!!"); roar2.append("!!!"); } public static void main(String[] args) { var roar1 = "roar"; var roar2 = new StringBuilder("roar"); new Lion().roar(roar1, roar2); System.out.println(roar1 + " " + roar2); } }
A. roar roar
B. roar roar!!!
C. roar!!! roar
D. roar!!! roar!!!
E. An exception is thrown.
F. The code does not compile.
B
B.
- A String is immutable. Calling concat() returns a new String but does not change the original.
- A StringBuilder is mutable. Calling append() adds characters to the existing character sequence along with returning a reference to the same object. Therefore, option B is correct.
14. Given the following, which can correctly fill in the blank? (Choose all that apply.)
var date = LocalDate.now(); var time = LocalTime.now(); var dateTime = LocalDateTime.now(); var zoneId = ZoneId.systemDefault(); var zonedDateTime = ZonedDateTime.of(dateTime, zoneId); Instant instant = \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_;
A. Instant.now()
B. new Instant()
C. date.toInstant()
D. dateTime.toInstant()
E. time.toInstant()
F. zonedDateTime.toInstant()
A, F
A, F.
- Option A correctly creates the current instant.
- Option F is also a proper conversion.
- Option B is incorrect because Instant, like many other date/time classes, does not have a public constructor and is instantiated via methods.
- Options C, D, and E are incorrect because the source object does not represent a point in time.
- Without a time zone, Java doesn’t know what moment in time to use for the Instant.
15. What is the output of the following? (Choose all that apply.)
var arr = new String[] { "PIG", "pig", "123"}; Arrays.sort(arr); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.binarySearch(arr, "Pippa"));
A. [pig, PIG, 123]
B. [PIG, pig, 123]
C. [123, PIG, pig]
D. [123, pig, PIG]
E. -3
F. -2
G. The results of binarySearch() are undefined in this example.
C, F
C, E.
- Numbers sort before letters and uppercase sorts before lowercase. This makes option C one of the answers.
- The
binarySearch()
method looks at where a value would be inserted, which is before the second element for Pippa. It then negates it and subtracts one, which is option E.
2 (second element) -> -2 (negates) -> -2-1 (subtracts one) = -3