Ch 3 - Core Java APIs Flashcards

1
Q

What would the output of the following code be?
String string = “animals”;
System.out.println(string.substring(3));
System.out.println(string.substring(string.indexOf(‘m’)));
System.out.println(string.substring(3,4));
System.out.println(string.substring(3,7));

A

mals
mals
m
mals

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

When the indexOf() method can’t find a match, what does it do?

A

it returns -1 when no match is found.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
What would the output of the following code be?
String string = "animals";
System.out.println(string.charAt(0));
System.out.println(string.charAt(6));
System.out.println(string.charAt(7));
A

a
s
StringIndexOutOfBoundsException

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What would the output of the following code be?
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));

A

true
false
true
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What is the difference between the following two statements:
String name = "Fluffy";
String name = new String("Fluffy");
A

String name = “Fluffy”;
uses the String pool

String name = new String("Fluffy");
creates a new object, even though it is less efficient than using the String Pool.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the output of the following lines of code?
System.out.println(1 + 2);
System.out.println("a" + "b");
System.out.println("a" + "b" + 3);
System.out.println(1 + 2 + "c");
A

3
ab
ab3
3c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

true/false: a String is an example of a reference type.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the output of the following code:
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);

A

64

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q
What would the output of the following code be?
String string = "animals";
System.out.println(string.charAt(0));
System.out.println(string.charAt(6));
System.out.println(string.charAt(7));
A

a
s
Index out of bounds Exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What would the output of the following code be?
System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”));
System.out.println(“abc”.equalsIgnoreCase(“ABC”));

A

false
true
true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What would the output of the following code be?
System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));

A

AbcAbc

AbcAbc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is aka the intern pool, is the location in the Java virtual machine (JVM) that collects all strings?

A

The String Pool

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

s += “2” means the same thing as

A

s = s + 2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

A String is mutable/immutable?

A

immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What would the output of the following code be?
String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(‘a’, 4));
System.out.println(string.indexOf(“al”, 5));

A

0
4
4
-1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What would the output of the following code be?
System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));

A

true
false
true
false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What would the output of the following code be?
System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());

A

abc

a b c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What would the output of the following code be?
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));

A

true

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q
What is the output of the following lines of code?
System.out.println(1 + 2);
System.out.println("a" + "b");
System.out.println("a" + "b" + 3);
System.out.println(1 + 2 + "c");
A

3
ab
ab3
3c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the rules for using the + character in string concatenation?

A
  1. If both operands are numeric, + means numeric addition.
  2. If either operand is a String, + means concatenation.
  3. The expression is evaluated left to right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

true/false: The String class is special and does not need to be instantiated with the keyword “new”.

A

true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What would the output of the following code be?
String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toUpperCase());

A

ANIMALS

ABC123

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What would the output of the following code be?
String string = “animals”;
System.out.println(string.substring(3,3));
System.out.println(string.substring(3,2));
System.out.println(string.substring(3,8));

A

empty string
throws exception
throws exception

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How is StringBuffer different than StringBuilder?

A

StringBuffer does the same thing as StringBuilder but more slowly because it is thread safe.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
What are the three ways to construct a StringBuilder?
``` StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("animal"); StringBuilder sb3 = new StringBuilder(10); ```
26
What is the output of the following code? String result = "AniMal ".trim().toLowerCase().replace('a', 'A'); System.out.println(result);
AnimAl
27
``` What would the output of the following code be? StringBuilder sb = new StringBuilder("animals"); String sub = sb.substring(sb.indexOf("a"), sb.indexOf("al")); int len = sb.length(); char ch = sb.charAt(6); System.out.println(sub + " " + len + " " + ch); ```
anim 7 s
28
What datatype does the .substring() method return?
String
29
What is the output of the following code? StringBuilder sb = new StringBuilder("ABC"); sb.reverse(); System.out.println(sb);
CBA
30
How many objects do you think this piece of code creates? String alpha = ""; for (char current = 'a'; current <= 'z'; current++) alpha += current; System.out.println(alpha);
27
31
``` What is the output of the following code? StringBuilder a = new StringBuilder("abc"); StringBuilder b = a.append("de"); b = b.append("f").append("g"); System.out.println("a=" + a); System.out.println("b=" + b); ```
a=abcdef | b=abcdef
32
``` What is the output of the following code? StringBuilder sb = new StringBuilder("abcdef"); sb.delete(1,3); sb.deleteCharAt(5); ```
index out of bounds exception | sb.deleteCharAt(5);
33
true/false: StringBuilder is immutable
false
34
``` What is the output of the following code? StringBuilder sb = new StringBuilder().append(1).append('c'); sb.append("-").append(true); System.out.println(sb); ```
1c-true
35
What is the output of the following code? String a = "abc"; String b = a.toUpperCase(); b = b.replace("B", "2").replace('C', '3'); System.out.println("a=" + a); System.out.println("b=" + b);
a=abc | b=A23
36
What is the output of the following code? StringBuilder sb = new StringBuilder("animals"); sb.insert(7, "-"); sb.insert(0, "-"); sb.insert(4, "-"); System.out.println(sb);
-ani-mals-
37
What would the output of the following code be? StringBuilder sb = new StringBuilder("start"); sb.append("+middle"); StringBuilder name = sb.append("+end");
sb and name are pointing to the same object, thus the value assigned to each is: start+middle+end
38
What is the output of the following code? ``` StringBuilder puzzle = new StringBuilder("Java"); System.out.println(puzzle); System.out.println(puzzle.reverse()); System.out.println(puzzle); ```
Java avaJ avaJ
39
What is the output of the following code? ``` StringBuilder puzzle = new StringBuilder("Stephen"); System.out.println(puzzle); puzzle.substring(1); System.out.println(puzzle); ```
Stephen | Stephen
40
What is the output of the following code? ``` String x = new String("Hello World"); String y = "Hello World"; System.out.println(x == y); System.out.println(x.equals(y)); ```
false true The first line forces the creation of a new object, and the second line uses the string pool, so the first comparison is false.
41
What is the output of the following code? String x = "Hello World"; String y = " Hello World".trim(); System.out.println(x.equals(y));
true
42
During an equality check, if an object does not implement an equals() method, Java determines...
... if the references point to the same object, which is exactly what == does.
43
What is the output of the following code? ``` StringBuilder one = new StringBuilder(); StringBuilder two = new StringBuilder(); StringBuilder three = one.append("a"); System.out.println(one == two); System.out.println(one == three); ```
false | true
44
What would the output of the following code be? int[] numbers = new int[3]; for (int i : numbers ) { System.out.println(i); }
0 0 0
45
true/false, the following code creates two arrays of int values: int[] ids, types;
true
46
What would the output of the following code be? String [] bugs = {"cricket", "beetle", "ladybug"}; String[] bugs2 = {"one", "two"}; System.out.println(bugs.equals(bugs2));
false
47
What would the output of the following code be? String[] names; System.out.println(names);
null
48
What would the output of the following code be? public class ArrayType { public static void main(String args[]) { String [] bugs = {"cricket", "beetle", "ladybug"}; String [] alias = bugs; System.out.println(bugs.equals(alias)); System.out.println(bugs.toString()); } }
true | [Ljava.lang.String;@160bc7c0
49
What would the output of the following code be? ``` int[] newVals = {}; newVals[0] = 1; newVals[1] = 2; if (newVals == null) { System.out.println("newVals is null"); ``` } else { System.out.println("newVals is not null"); }
Index out of bounds exception on this line: | newVals[0] = 1;
50
Instantiate a new array called "numbers" of int values of 25, 30, 35.
int[] numbers = new int[] {25, 30, 35}; - or - int[] numbers = {25, 30, 35};
51
What would the output of the following code be? String [] bugs = {"cricket", "beetle", "ladybug"}; for (String b : bugs) { System.out.println(b); }
cricket beetle ladybug
52
true/false, the equals method on arrays looks at the elements of the array.
false
53
int is what data type and int[] is what data type?
int is a primitive, and int[] is an object
54
What does the following code output? ``` public class Tiger { String name; public static void main(String[] args) { Tiger t1 = new Tiger(); Tiger t2 = new Tiger(); Tiger t3 = t1; ``` ``` System.out.println(t1 == t3); System.out.println(t1 == t2); System.out.println(t1.equals(t2)); } } ```
true false false
55
true/false: An array is an ordered list.
true
56
true/false, the following code creates two arrays of int values: int ids[], types;
false
57
What would the output of the following code be? String[] numbers = {"10", "9", "100"}; Arrays.sort(numbers); for (String string : numbers) System.out.println(numbers[string] + " ");
10 100 9
58
What dimensions are the arrays being declared in the code below? int[][] vars1; int vars2[][]; int[] vars3[]; int[] vars4[], space[][];
two two two two and three
59
What would the output of the following code be? int[] numbers = {2, 4, 6, 8}; System.out.println(Arrays.binarySearch(numbers, 2)); System.out.println(Arrays.binarySearch(numbers, 4)); System.out.println(Arrays.binarySearch(numbers, 1)); System.out.println(Arrays.binarySearch(numbers, 3)); System.out.println(Arrays.binarySearch(numbers, 9));
``` 0 1 -1 -2 -5 ```
60
Will the following code compile? ``` String[] strings = { "stringvalue" }; Object[] objects = strings; String[] againStrings = (String[]) objects; againStrings[0] = new StringBuilder(); object[0] = new StringBuilder(); ```
``` no. The following line produces a compiler error: againStrings[0] = new StringBuilder(); ``` ``` The following line doesn't create compiler error, but it throws and ArrayStoreException when you try to run it. object[0] = new StringBuilder(); ```
61
What would the output of the following code be? int[] numbers = {6, 9, 1}; Arrays.sort(numbers); for (int i=0; i< numbers.length; i++) System.out.println(numbers[i] + " ");
1 6 9
62
true/false, the following lines of code will compile: public static void main(String[] args) public static void main(String args[]) public static void main(String... args)
true
63
Considering the following code: int[] numbers = new int[10]; for (int i=0; i < numbers.length; i++) numbers[i] = i + 5; Will the following lines of code compile? numbers[10] = 5; numbers[numbers.length] = 5; for (int i=0; i =< numbers.length; i++)
numbers[10] = 5;
64
What kind of results would you get when trying to do a binary search in the following array? int[] numbers = {3, 2, 1, 5};
unpredictable, because the array is unsorted.
65
What would the output of the following code be? ``` List birds = new ArrayList(); birds.add("hawk"); birds.add(1, "robin"); birds.add(0, "blue jay"); birds.add(1, "cardinal"); birds.add("sparrow"); System.out.println(birds); ```
[blue jay, cardinal, hawk, robin, sparrow]
66
What would the output of the following code be? List birds = new ArrayList(); birds. add("hawk"); birds. remove(1);
IndexOutOfBoundsException
67
What would the output of the following code be? ``` List birds = new ArrayList(); System.out.println(birds.isEmpty()); System.out.println(birds.size()); birds.add("hawk"); birds.add("hawk"); System.out.println(birds.isEmpty()); System.out.println(birds.size()); ```
true 0 false 2
68
What would the output of the following code be? ``` List one = new ArrayList(); List two = new ArrayList(); System.out.println(one.equals(two)); one.add("a"); System.out.println(one.equals(two)); two.add("a"); System.out.println(one.equals(two)); one.add("b"); two.add(0, "b"); System.out.println(one.equals(two)); ```
true false true false
69
true/false, the following compiles: ArrayList list7 = new List<>();
false
70
What would the output of the following code be? ArrayList list = new ArrayList(); list.add("hawk"); list.add(Boolean.TRUE); System.out.println(list);
[hawk, true] * it is ok to add objects of different types to this ArrayList because we didn't declare the type when initializing the ArrayList.
71
What would the output of the following code be? ``` List birds = new ArrayList(); birds.add("hawk"); birds.add("hawk"); birds.add("hawk"); System.out.println(birds.remove("hawk")); System.out.println(birds); ```
true | [hawk, hawk]
72
<> is called the what type of operator?
The diamond operator
73
Name five ways to create an ArrayList
``` ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(10); ArrayList list3 = new ArrayList(list2); ArrayList list4 = new ArrayList(); ArrayList list5 = new ArrayList<>(); ```
74
What would the output of the following code be? ``` ArrayList list = new ArrayList(); System.out.println(list.add("hawk")); System.out.println(list.add(Boolean.TRUE)); ```
true | true
75
What would the output of the following code be? ``` List birds = new ArrayList(); birds.add("hawk"); System.out.println(birds.contains("hawk")); System.out.println(birds.contains("robin")); ```
true | false
76
What would the output of the following code be? ``` List birds = new ArrayList(); birds.add("hawk"); System.out.println(birds.size()); birds.set(0, "robin"); System.out.println(birds.size()); birds.set(1, "robin"); ```
1 1 IndexOutOfBoundsException
77
What would the output of the following code be? ArrayList list = new ArrayList(); list. add("hawk"); list. add(Boolean.TRUE);
Compiler error on this line: list. add(Boolean.TRUE); * We've specifically declared this ArrayList to hold Strings.
78
What would the output of the following code be? ``` List birds = new ArrayList(); birds.add("hawk"); System.out.println(birds.remove("hawk")); System.out.println(birds.remove("hawk")); ```
true false * the second line returns false because there is no more elements in the ArrayList of "hawk".
79
What would the output of the following code be? ``` List birds = new ArrayList(); birds.add("hawk"); birds.add("hawk"); System.out.println(birds.isEmpty()); System.out.println(birds.size()); birds.clear(); System.out.println(birds.isEmpty()); System.out.println(birds.size()); ```
false 2 true 0
80
What would the output of the following code be? ArrayList one1 = new ArrayList(); one1.add("one"); ArrayList one2 = new ArrayList(); one2.add("one"); ``` if (one1.equals(one2)) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } ``` ``` if (one1 == one2) { System.out.println("They are equal"); } else { System.out.println("They are not equal"); } ```
They are equal | They are not equal
81
``` Which of the following code will compile: Integer i0 = new Integer(); Integer i1 = new Integer(1); Integer i2 = new Integer((int) 1); Long l0 = new Long(); Long l1 = new Long(1); Long l2 = new Long((long) 1); ```
``` Integer i0 = new Integer(); // no Integer i1 = new Integer(1); // yes Integer i2 = new Integer((int) 1); // yes Long l0 = new Long(); // no Long l1 = new Long(1); // yes Long l2 = new Long((long) 1); // yes ```
82
Convert the string "1" to a float primitive.
float f = Float.parseFloat("1");
83
Convert the string "2" to a Long wrapper object.
Long l2 = Long.valueOf("2");
84
What is the output of the following code? ``` List numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.remove(1); System.out.println(numbers); ```
[1] This line is removing the array member at index 1, not with the value 1: numbers.remove(1);
85
Convert the string "1" to a short primitive.
short s = Short.parseShort("1");
86
Convert the string "true" to a Boolean wrapper object.
Boolean b2 = Boolean.valueOf("true");
87
Convert the string "2" to a Double wrapper object.
Double d2 = Double.valueOf("2");
88
Convert the string "2" to a Byte wrapper object.
Byte b = Byte.valueOf("2");
89
Which of the following code will compile: ``` Float f0 = new Float(); Float f1 = new Float(1.0); Float f2 = new Float((float) 1.0); Double d0 = new Double(); Double d1 = new Double(1.0); Double d2 = new Double((double) 1.0); Character c0 = new Character(); Character c1 = new Character('c'); Character c2 = new Character((char) 'c'); ```
``` Float f0 = new Float(); // no Float f1 = new Float(1.0); // yes Float f2 = new Float((float) 1.0); // yes Double d0 = new Double(); // no Double d1 = new Double(1.0); // yes Double d2 = new Double((double) 1.0); // yes Character c0 = new Character(); // no Character c1 = new Character('c'); // yes Character c2 = new Character((char) 'c'); // yes ```
90
Convert the string "2" to a Short wrapper object.
Short s2 = Short.valueOf("2");
91
Convert the string "1" to an int primitive.
int i = Integer.parseInt("1");
92
Convert the string "true" to a boolean primitive.
boolean b = Boolean.parseBoolean("true");
93
What is the output of the following code? List heights = new ArrayList<>(); heights.add(null); int h = heights.get(0);
NullPointerException on this line: int h = heights.get(0); That is because it is trying to get the int value of null.
94
What is the output of the following code? ``` String[] array = {"Hawk", "Robin"}; List list = Arrays.asList(array); System.out.println(list.size()); list.set(1, "test"); array[0] = "new"; for (String b : array) System.out.print(b + " "); list.remove(1); ```
2 new test UnsupportedOperation Exception
95
What is the output of the following code? ``` List list = new ArrayList(); list.add("hawk"); list.add("robin"); Object[] objectArray = list.toArray(); System.out.println(objectArray.length); String[] stringArray = list.toArray(); System.out.println(stringArray.length); ```
Type mismatch error on this line: String[] stringArray = list.toArray(); cannot convert from Object[] to String[]
96
Convert the string "1" to a double primitive.
double d = Double.parseDouble("1");
97
Which of the following code will compile: ``` Boolean boo0 = new Boolean(); Boolean boo1 = new Boolean(true); Byte bte0 = new Byte(); Byte bte1 = new Byte(1); Byte bte2 = new Byte((byte) 1); Short sh0 = new Short(); Short sh1 = new Short(1); Short sh2 = new Short((short) 1); ```
``` Boolean boo0 = new Boolean(); // no Boolean boo1 = new Boolean(true); // yes Byte bte0 = new Byte(); // no Byte bte1 = new Byte(1); // no Byte bte2 = new Byte((byte) 1); // yes Short sh0 = new Short(); // no Short sh1 = new Short(1); // no Short sh2 = new Short((short) 1); // yes ```
98
Convert the string "1" to a byte primitive.
byte b0 = Byte.parseByte("1");
99
What is the output of the following code? ``` List list = new ArrayList(); list.add("hawk"); list.add("robin"); Object[] objectArray = list.toArray(); System.out.println(objectArray.length); String[] stringArray = list.toArray(new String[5]); System.out.println(stringArray.length); ```
2 | 5
100
Convert the string "2" to an Integer wrapper object.
Integer i2 = Integer.valueOf("2");
101
What is meant by "Autoboxing"?
You can just type the primitive value and Java will convert it to the relevant wrapper class for you.
102
What is the output of the following code? ``` List numbers = new ArrayList<>(); numbers.add(1); numbers.add(2); numbers.remove(new Integer(1)); System.out.println(numbers); ```
[2] This line is removing the array member with value of 1: numbers.remove(new Integer(1));
103
Convert the string "2" to a Float wrapper object.
Float f2 = Float.valueOf("2");
104
What is the output of the following code? ``` List list = new ArrayList(); list.add("hawk"); list.add("robin"); Object[] objectArray = list.toArray(); System.out.println(objectArray.length); String[] stringArray = list.toArray(new String[0]); System.out.println(stringArray.length); ```
2 | 2
105
Convert the string "1" to a long primitive.
long l = Long.parseLong("1");
106
What is the output of the following code? ``` List numbers = new ArrayList(); numbers.add(99); numbers.add(5); numbers.add(81); Collections.sort(numbers); System.out.println(numbers); ```
[5, 81, 99]
107
What is the output of the following code? LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); LocalTime time = LocalTime.of(11, 12, 34); DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println(shortDateTime.format(dateTime)); System.out.println(shortDateTime.format(date)); System.out.println(shortDateTime.format(time));
1/20/20 1/20/20 UnsupportedTemporalTypeException
108
When using Arrays.binarySearch, if no match is found, what happens?
it negates the position where the element would need to be inserted and subtracts 1.
109
What is the output of the following code? DateTimeFormatter f = DateTimeFormatter.ofPattern("MM dd yyyy"); LocalDate date = LocalDate.parse("01 02 2015", f); LocalTime time = LocalTime.parse("11:22"); System.out.println(date); System.out.println(time);
2015-01-02 | 11:22
110
What is the package containing the java date and time classes?
java.time.*
111
What is the output of the following code? LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); date.plusDays(10); System.out.println(date);
2020-01-20
112
What is the output of the following code? DateTimeFormatter f = DateTimeFormatter.ofPattern("MMMM dd, yyyy, hh:mm"); System.out.println(dateTime.format(f));
January 20, 2020, 11:12
113
Calling equals() on String objects will check what?
Whether the sequence of characters is the same.
114
Provide an example of using the LocalTime class to instantiate a time variable:
LocalTime time1 = LocalTime.of(6, 15); // hours, minutes or LocalTime time2 = LocalTime.of(6, 15, 30); // hours, minutes, seconds or LocalTime time3 = LocalTime.of(6, 15, 30, 200); // hours, minutes, seconds, nanoseconds
115
What is the output of the following code? ``` LocalDate date = LocalDate.of(2014, Month.JANUARY, 20); System.out.println(date); date = date.plusDays(2); System.out.println(date); date = date.plusWeeks(1); System.out.println(date); date = date.plusMonths(1); System.out.println(date); date = date.plusYears(5); System.out.println(date); ```
``` 2014-01-20 2014-01-22 2014-01-29 2014-02-28 2019-02-28 ```
116
Which of the following lines of code will throw an exception? DateTimeFormatter f = DateTimeFormatter.ofPattern("hh:mm"); f. format(dateTime); f. format(date); f. format(time);
This line: f.format(date); The reason is because we've constructed this formatter with hour (hh) and minute (mm), thus we can only use this formatter with objects containing times.
117
What method in the StringBuilder class does not modify the value of the object?
substring()
118
What is the method implemented by the Java 8 date/time classes to display the current date and time?
now()
119
Provide an example of using the LocalDate class to instantiate a date variable:
LocalDate date1 = LocalDate.of(2020, Month.JANUARY, 8); or LocalDate date2 = LocalDate.of(2020, 1, 8);
120
What is the output of the following code? LocalDate d = LocalDate.of(2020, Month.JANUARY, 32);
DateTimeException
121
What is the output of the following code? LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); LocalTime time = LocalTime.of(5, 15); LocalDateTime dateTime = LocalDateTime.of(date, time).minusDays(1).minusHours(10).minusSeconds(30); System.out.println(dateTime);
2020-01-18T19:14:30
122
Calling == on String objects will check what?
Whether they point to the same object in the pool.
123
What is the output of the following code? LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); date = date.plusMinutes(1); System.out.println(date);
Compiler error on this line: | date = date.plusMinutes(1);
124
What character does java use to separate the date and time when produced by the LocalDateTime class?
T
125
What is the output of the following code? ``` LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); LocalTime time = LocalTime.of(5, 15); LocalDateTime dateTime = LocalDateTime.of(date, time); System.out.println(dateTime); datetime = dateTime.minusDays(1); System.out.println(dateTime); dateTime = dateTime.minusHours(10); System.out.println(dateTime); dateTime = dateTime.minusSeconds(30); System.out.println(dateTime); ```
2020-01-20T05:15 2020-01-19T05:15 2020-01-18T19:15 2020-01-18T19:14:30
126
Calling == on StringBuilder references will check what?
Whether they are pointing to the same StringBuilder object.
127
true/false, the LocalDate class provides date, time, and timezone information.
false | it only provides date
128
What information is provided by the LocalTime and LocalDateTime classes?
LocalTime - provides just time, no date or timezone | LocalDateTime - provides both date and time but no timezone
129
What is the output of the following code? LocalDate date = LocalDate.of(2015, 1, 20); LocalTime time = LocalTime.of(6, 15); LocalDateTime dateTime = LocalDateTime.of(date, time); Period period = Period.ofMonths(1); System.out.println(date.plus(period)); System.out.println(dateTime.plus(period)); System.out.println(time.plus(period));
2015-02-20 2015-02-20T06:15 UnsupportedTemporalTypeException
130
Arrays.binarySearch() searches a sorted array and returns what?
The index of a match.
131
Provide an example of using the LocalDateTime class to instantiate a datetime variable:
LocalDateTime datetime1 = LocalDateTime.of(2020, Month.JANUARY, 8, 6, 15, 30); or LocalDateTime datetime1 = LocalDateTime.of(date1, time1);
132
true/false, the Local date and time classes are immutable.
true
133
What is the output of the following code? LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); LocalTime time = LocalTime.of(11, 12, 34); LocalDateTime dateTime = LocalDateTime.of(date, time); DateTimeFormatter shortF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT); DateTimeFormatter mediumF = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); System.out.println(shortF.format(dateTime)); System.out.println(mediumF.format(dateTime));
1/20/20 11:12 AM | Jan 20, 2020 11:12:34 AM
134
true/false, the following code compiles: LocalDate d = new LocalDate();
false
135
What is the output of the following code? ``` LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); System.out.println(date.getDayOfWeek()); System.out.println(date.getMonth()); System.out.println(date.getYear()); System.out.println(date.getDayOfYear()); ```
MONDAY JANUARY 2020 20
136
Calling equals() on StringBuilder objects will check what?
Whether they are pointing to the same object rather than looking at the values inside.
137
Why does the following code fail to compile and what could you do to fix it? List list = new ArrayList(); list.add("hawk"); list.add("robin"); String[] myArray = list.toArray();
This line fails: String[] myArray = list.toArray(); list.toArray() returns a type of Object[], and the way to fix it would be to change the line to this: String[] myArray = (String[]) list.toArray();
138
What is the output of the following code? ``` StringBuilder sb2 = new StringBuilder("animals"); sb2.insert(8, "-"); System.out.println(sb2); ```
StringIndexOutOfBoundsException
139
What is the output of the following code? String[] myStrings = {"one", "two", "three"}; String[] myStrings2 = {"one", "two", "three"}; ``` if (myStrings == myStrings2) { System.out.println("is =="); } else { System.out.println("not =="); } ``` ``` if (myStrings.equals(myStrings2)) { System.out.println("is equals"); } else { System.out.println("not equals"); } ```
not == | not equals
140
What is the output of the following code? ArrayList list1 = new ArrayList<>(); list1.add("a"); ArrayList list2 = new ArrayList<>(); list2.add("a"); ``` if (list1 == list2) { System.out.println("is =="); } else { System.out.println("not =="); } ``` ``` if (list1.equals(list2)) { System.out.println("is equals"); } else { System.out.println("not equals"); } ```
not == | is equals
141
Which method do you use to convert an array to a List?
Arrays.asList(); Example: String[] stephen = {"Stephen", "Spalding"}; List list = Arrays.asList(stephen);
142
Which method do you use to convert a List to an array?
``` list.toArray(); Example: List list = new ArrayList(); list.add("hawk"); list.add("robin"); Object[] objectArray = list.toArray(); ```
143
Which of the following lines of code compile? 1. int[][] ints = new int[][]; 2. int[][] ints = new int[1][]; 3. int[][] ints = new int[][1]; 4. int[1][] ints = new int[][]; 5. int[][] ints = new int[1][1];
2 and 5
144
What is the result of the following: int[] random = {6, -4, 12, 0, -10}; int x = 12; int y = Arrays.binarySearch(random, x); System.out.println(y);
2
145
true/false, an array is mutable.
true
146
What is the output of the following code? LocalDateTime d2 = LocalDateTime.of(2015, 5, 10, 11, 22, 33); Period p2 = Period.ofDays(1).ofYears(2); d = d.minus(p2); DateTimeFormatter f2 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT); System.out.println(f.format(d));
5/10/13 11:22 AM
147
What is the output of the following code? ``` List ages = new ArrayList<>(); ages.add(Integer.parseInt("5")); ages.add(Integer.valueOf("6")); ages.add(7); ages.add(null); for (int age : ages) System.out.println(age); ```
5 6 7 NullPointerException from this line: for (int age : ages) System.out.println(age); The problem has to do with autoboxing the null value when retrieving it from the array.
148
What is the output of the following code? LocalDateTime d = LocalDateTime.of(2015, 5, 10, 11, 22, 33); Period p = Period.of(1, 2, 3); d = d.minus(p); DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); System.out.println(d.format(f));
11:22 AM
149
true/false, an ArrayList is mutable.
true
150
What is the output of the following code and why? LocalDateTime d3 = LocalDateTime.now(); Period p3 = Period.ofDays(1).ofYears(2); d3 = d3.minus(p3); System.out.println(d3);
2018-03-11T13:49:43.703 | When chaining methods on the Period class, only the last is recognized. The rest are ignored.
151
``` What is the output of the following code? String s = "Hello"; String t = new String(s); if ("Hello".equals(s)) System.out.println("one"); if (t == s) System.out.println("two"); if (t.equals(s)) System.out.println("three"); if ("Hello" == s) System.out.println("four"); if ("Hello" == t) System.out.println("five"); ```
one three four
152
``` What is the output of the following code? String letters = "abcdef"; System.out.println(letters.length()); System.out.println(letters.charAt(3)); System.out.println(letters.charAt(6)); ```
6 d an exception is thrown
153
What is the output of the following code? LocalDate date = LocalDate.of(2018, Month.APRIL, 30); date.plusDays(2); date.plusYears(3); System.out.println(date.getYear() + " " + date.getMonth() + " " + date.getDayOfMonth());
2018 APRIL 30
154
What is the output of the following code? LocalDate date = LocalDate.of(2018, Month.APRIL, 30); date = date.plusDays(2); date = date.plusYears(3); System.out.println(date.getYear() + " " + date.getMonth() + " " + date.getDayOfMonth());
2021 MAY 2