Ch 3 - Core Java APIs Flashcards
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));
mals
mals
m
mals
When the indexOf() method can’t find a match, what does it do?
it returns -1 when no match is found.
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
s
StringIndexOutOfBoundsException
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”));
true
false
true
false
What is the difference between the following two statements: String name = "Fluffy"; String name = new String("Fluffy");
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.
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");
3
ab
ab3
3c
true/false: a String is an example of a reference type.
true
What is the output of the following code:
int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);
64
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
s
Index out of bounds Exception
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”));
false
true
true
What would the output of the following code be?
System.out.println(“abcabc”.replace(‘a’, ‘A’));
System.out.println(“abcabc”.replace(“a”, “A”));
AbcAbc
AbcAbc
What is aka the intern pool, is the location in the Java virtual machine (JVM) that collects all strings?
The String Pool
s += “2” means the same thing as
s = s + 2;
A String is mutable/immutable?
immutable
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));
0
4
4
-1
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”));
true
false
true
false
What would the output of the following code be?
System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());
abc
a b c
What would the output of the following code be?
System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));
true
false
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");
3
ab
ab3
3c
What are the rules for using the + character in string concatenation?
- If both operands are numeric, + means numeric addition.
- If either operand is a String, + means concatenation.
- The expression is evaluated left to right.
true/false: The String class is special and does not need to be instantiated with the keyword “new”.
true
What would the output of the following code be?
String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toUpperCase());
ANIMALS
ABC123
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));
empty string
throws exception
throws exception
How is StringBuffer different than StringBuilder?
StringBuffer does the same thing as StringBuilder but more slowly because it is thread safe.
What are the three ways to construct a StringBuilder?
StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder("animal"); StringBuilder sb3 = new StringBuilder(10);
What is the output of the following code?
String result = “AniMal “.trim().toLowerCase().replace(‘a’, ‘A’);
System.out.println(result);
AnimAl
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
What datatype does the .substring() method return?
String
What is the output of the following code?
StringBuilder sb = new StringBuilder(“ABC”);
sb.reverse();
System.out.println(sb);
CBA
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
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
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);
true/false: StringBuilder is immutable
false
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
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
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-
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
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
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
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.
What is the output of the following code?
String x = “Hello World”;
String y = “ Hello World”.trim();
System.out.println(x.equals(y));
true
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.
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
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
true/false, the following code creates two arrays of int values:
int[] ids, types;
true
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
What would the output of the following code be?
String[] names;
System.out.println(names);
null
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
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;
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};
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
true/false, the equals method on arrays looks at the elements of the array.
false
int is what data type and int[] is what data type?
int is a primitive, and int[] is an object
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
true/false: An array is an ordered list.
true
true/false, the following code creates two arrays of int values:
int ids[], types;
false
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
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
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
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();
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
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
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;
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.
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]
What would the output of the following code be?
List birds = new ArrayList();
birds. add(“hawk”);
birds. remove(1);
IndexOutOfBoundsException
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
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
true/false, the following compiles:
ArrayList list7 = new List<>();
false
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.
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]
<> is called the what type of operator?
The diamond operator
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<>();
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
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
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
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.
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”.
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
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
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
Convert the string “1” to a float primitive.
float f = Float.parseFloat(“1”);
Convert the string “2” to a Long wrapper object.
Long l2 = Long.valueOf(“2”);
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);
Convert the string “1” to a short primitive.
short s = Short.parseShort(“1”);
Convert the string “true” to a Boolean wrapper object.
Boolean b2 = Boolean.valueOf(“true”);
Convert the string “2” to a Double wrapper object.
Double d2 = Double.valueOf(“2”);
Convert the string “2” to a Byte wrapper object.
Byte b = Byte.valueOf(“2”);
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
Convert the string “2” to a Short wrapper object.
Short s2 = Short.valueOf(“2”);
Convert the string “1” to an int primitive.
int i = Integer.parseInt(“1”);
Convert the string “true” to a boolean primitive.
boolean b = Boolean.parseBoolean(“true”);
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.
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
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[]
Convert the string “1” to a double primitive.
double d = Double.parseDouble(“1”);
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
Convert the string “1” to a byte primitive.
byte b0 = Byte.parseByte(“1”);
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
Convert the string “2” to an Integer wrapper object.
Integer i2 = Integer.valueOf(“2”);
What is meant by “Autoboxing”?
You can just type the primitive value and Java will convert it to the relevant wrapper class for you.
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));
Convert the string “2” to a Float wrapper object.
Float f2 = Float.valueOf(“2”);
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
Convert the string “1” to a long primitive.
long l = Long.parseLong(“1”);
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]
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
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.
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
What is the package containing the java date and time classes?
java.time.*
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
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
Calling equals() on String objects will check what?
Whether the sequence of characters is the same.
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
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
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.
What method in the StringBuilder class does not modify the value of the object?
substring()
What is the method implemented by the Java 8 date/time classes to display the current date and time?
now()
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);
What is the output of the following code?
LocalDate d = LocalDate.of(2020, Month.JANUARY, 32);
DateTimeException
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
Calling == on String objects will check what?
Whether they point to the same object in the pool.
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);
What character does java use to separate the date and time when produced by the LocalDateTime class?
T
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
Calling == on StringBuilder references will check what?
Whether they are pointing to the same StringBuilder object.
true/false, the LocalDate class provides date, time, and timezone information.
false
it only provides date
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
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
Arrays.binarySearch() searches a sorted array and returns what?
The index of a match.
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);
true/false, the Local date and time classes are immutable.
true
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
true/false, the following code compiles:
LocalDate d = new LocalDate();
false
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
Calling equals() on StringBuilder objects will check what?
Whether they are pointing to the same object rather than looking at the values inside.
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();
What is the output of the following code?
StringBuilder sb2 = new StringBuilder("animals"); sb2.insert(8, "-"); System.out.println(sb2);
StringIndexOutOfBoundsException
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
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
Which method do you use to convert an array to a List?
Arrays.asList();
Example:
String[] stephen = {“Stephen”, “Spalding”};
List list = Arrays.asList(stephen);
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();
Which of the following lines of code compile?
- int[][] ints = new int[][];
- int[][] ints = new int[1][];
- int[][] ints = new int[][1];
- int[1][] ints = new int[][];
- int[][] ints = new int[1][1];
2 and 5
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
true/false, an array is mutable.
true
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
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.
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
true/false, an ArrayList is mutable.
true
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.
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
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
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
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