Chapter 3 : Core Java APIs Flashcards

1
Q

What will be the output of the following source?

String name1 = "Fluffy";
String name2 = new String("Fluffy");
String name3 = "Fluffy";
boolean x = name1 == name2;
boolean y = name1 == name3;
System.out.println(x + " " + y);

a) true true
b) true false
c) false false
d) false true

A

d) false true

boolean x = name1 == name2; is false because the new String() creates a new object

boolean y = name1 == name3; is true because the literal “Fluffy” has one single immutable object in the String pool and therefore both references x and y point to it.

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

What will be the output of the following source?

int three = 3;
String four = “4”;
System.out.println(1 + 2 + three + four);

a) 1234
b) 64
c) 334
d) 10

A

b) 64

When doing String concatenation using the + operator, the statement is processed in pairs left-to-right

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

What will be the output of the following source?

String s = “1”;
s += “2”;
s += 3;
System.out.println(s);

a) 15
b) 123
c) 1223
d) 11223

A

b) 123

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

What method will give the expected result? Choose all that apply.

String string = “animals”;
System.out.println(string.____()); // Expected 7

a) size
b) charAt
c) indexOf
d) length

A

d) length

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

What method will give the expected result? Choose all that apply.

String string = “animals”;
System.out.println(string.____); // Expected s

a) indexOf(6)
b) indexOf(7)
c) charAt(6)
d) charAt(7)

A

c) charAt(6)
a) and b) are wrong because indexOf() returns an int
d) will throw an exception since it is out of bounds

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

What method will give the expected result? Choose all that apply.

String string = “animals”;
System.out.println(string.____); // Expected 4

a) indexOf(“al”)
b) indexOf(“al”, 4)
c) contains(“al”)
d) contains(“al”, 4)

A

a) indexOf(“al”) & b) indexOf(“al”, 4)
b) indexOf(“al”, 4) searches for the string “al” starting at position 4. Since “al” occurs starting at 4, it returns immediately.
c) and d) are wrong because contains() returns a boolean

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

What method will give the expected result? Choose all that apply.

String string = “animals”;
System.out.println(string.____); // Expected mals

a) substring(3)
b) substring(string.indexOf(‘m’)));
c) substring(3,6);
d) substring(3,7);

A

a) , b), d)
c) substring(3,6); would result in “mal”

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

What will be the output of the following source?

String string = “animals”;
System.out.println(string.substring(3,2));

a) i
b) mals
c) Does not compile
d) Exception thrown

A

d) Exception thrown

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

What method will give the expected result? Choose all that apply.

String string = “animals”;
System.out.println(string.____(“animals”)); // Expected true

a) equals
b) compare
c) equalsIgnoreCase
d) compareTo

A

a) equals & c) equalsIgnoreCase
b) compare, is not a valid method
d) compareTo is a method that does lexicographical comparison and it returns an integer. 0 means the strings are equals.

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

What will be the output of the following source?

String string = “animals”;
System.out.println(string.equals(null));

a) false
b) true
c) Exception is thrown
c) Does not compile

A

a) false

equals does not throw an exception when parameter is null

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

What will be the output of the following source? String string = “animals”; System.out.println(string.compareTo(null)); a) false b) -1 c) Exception is thrown c) Does not compile

A

c) Exception is thrown compareTo throws an exception when parameter is null

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

What method will give the expected result? Choose all that apply. String string = “animals”; System.out.println(string.____)); // Expected AnimAls a) toUpperCase(“a”) b) toUpperCase(‘a’) c) replace(“a”, “A”) d) replace(‘a’, ‘A’)

A

c) and d) a) and b) are wrong since those methods act on the whole string and do not take a String or char parameter

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

What will be the output of the following source? (Note that \t is a invisible tab) String string = “\tA\tBC\t”; System.out.print(string.trim()); System.out.println(“C”); a) \tA\tBC b) A\tBC c) ABC c) Does not compile

A

b) A\tBC

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

What will be the output of the following source? 4 StringBuilder a = new StringBuilder(“abc”); 5 StringBuilder b = a.append(“de”); 6 b = b.append(“f”).append(“g”); 7 System.out.println(“a=” + a); 8 System.out.println(“b=” + b); a) a=abc , b=abcdefg b) a=abc , b=fg c) a=abc , b=g d) a=abcdefg, b=abcdefg

A

d) a=abcdefg, b=abcdefg Both a and b are pointing to the same object. append() returns a reference to the object itself.

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

How many strings are created? StringBuilder alpha = “”; for (char current=’a’; current <= ‘z’; current++) { alpha.append(current); } System.out.println(alpha); a) 1 b) 27 c) Does not compile d) Exception thrown

A

c) StringBuilder cannot be assigned a String literal. The following would work: StringBuilder alpha = new StringBuilder();

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

What will be the output of the following source? StringBuilder alpha = new StringBuilder(10); alpha.append(12345); System.out.println(alpha.length()); a) 5 b) 10 c) Does not compile d) Exception thrown

A

a) The constructor takes in 10 which means the object can hold up to 10 characters. However capacity != length

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

What will be the output of the following source? StringBuilder alpha = new StringBuilder().append(1).append(‘c’); alpha.append(“-“).append(true); System.out.println(alpha); a) 1c b) 1c-true c) Does not compile d) Exception thrown

A

b) 1c-true

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

What is the difference between StringBuilder and StringBuffer?

A

They do the same thing except StringBuffer is thread safe.

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

What will be the output of the following source? public class Junk { String name; public static void main(String[] args) { Junk a = new Junk(); Junk b = new Junk(); Junk c = a; System.out.print((a == c) + “ “); System.out.print((a == b) + “ “); System.out.print(a.equals(b)); } } a) false false false b) true false true c) true false false d) true true true

A

c) true false false a.equals(b) is false because, when equals() is not implemented, then the default behavior is to compare references.

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

What statements will compile? Choose all that apply. 1 int[] a; 2 int [] b; 3 int c[]; 4 int d []; 5 int[] e, f; 6 int g[], h; 7 int i[]; a) 1, 3 b) 1, 3, 5, 6 c) 1, 2, 5 d) 1, 2, 5, 7 e) All of them compile

A

e) All of them compile

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

How many arrays are declared? Choose all that apply. int[] a, b; int c[], d; a) 1 b) 2 c) 3 d) 4

A

c) 3 a, b, c are arrays d is just an int

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

Which of the following is true? Select all that apply. 3 String[] strings = { “apples” }; 4 Object[] objects = strings; 5 String[] moreStrings = (String[]) objects; 6 moreStrings[0] = new StringBuilder(); 7 objects[0] = new StringBuilder(); a) 5 does not compile b) 6 does not compile c) 6 throws exception d) 7 does not compile e) 7 throws exception

A

b) 6 does not compile & e) 7 throws an exception 7 throws a ArrayStoreException

23
Q

What options will give the expected result? Choose all that apply. int[] numbers = new int[10]; for (int i = 0; i == 4; i++) { numbers[i] = i; } System.out.println(numbers.____); // Expected 5 a) size() b) length() c) length d) None of the above

A

d) None of the above The array is initialized to be of size 10, therefore lenght is 10 regardless of how many values are in the array. numbers.length will print 5

24
Q

What is the output of the following? int[] numbers = new int[10]; for (int i = 0; i == numbers.length; i++) { numbers[i] = i; } System.out.println(numbers); a) [I@4d7e1886 b) 0123456789 c) Does not compile d) None of the above

A

a) [I@4d7e1886 The above [ means it is an array. The letter “I” means its an int. The string after the “@” is a hash value for the memory address

25
Q

What is the output of the following? String[] strings = { “10”, “9”, “100” }; Arrays.sort(strings); for (String string : strings) System.out.print(string + “ “); a) 10 100 9 b) 9 10 100 c) Does not compile d) Exception thrown

A

a) 10 100 9 Strings are sorted in alphabetic order. “100” comes before “9”

26
Q

What is the output of the following? int[] numbers = {2, 4, 6, 8}; System.out.print(Arrays.binarySearch(numbers, 2) + “ “); System.out.print(Arrays.binarySearch(numbers, 4) + “ “); System.out.print(Arrays.binarySearch(numbers, 1) + “ “); System.out.print(Arrays.binarySearch(numbers, 3) + “ “); System.out.print(Arrays.binarySearch(numbers, 9) + “ “); a) 0 1 0 1 4 b) 0 1 -1 -2 4 c) 0 1 0 1 5 d) 0 1 -1 -2 -5 e) None of the above

A

d) 0 1 -1 -2 -5 If binarySearch cannot find a value then it returns the index at which the missing value should be inserted in such a way that it will maintain the ascending sorted order. Then, it negates that values and substracts 1. For example, 9 : -[4] - 1 = -5

27
Q

What is the output of the following? int[] numbers = {4, 2, 6, 8}; System.out.print(Arrays.binarySearch(numbers, 2) + “ “); System.out.print(Arrays.binarySearch(numbers, 4) + “ “); System.out.print(Arrays.binarySearch(numbers, 1) + “ “); System.out.print(Arrays.binarySearch(numbers, 3) + “ “); System.out.print(Arrays.binarySearch(numbers, 9) + “ “); a) 0 1 0 1 4 b) 0 1 -1 -2 4 c) 0 1 0 1 5 d) 0 1 -1 -2 -5 e) None of the above

A

e) None of the above The array is not sorted and therefore binarySearch is unpredictable.

28
Q

What is the output of the following? public static void main(String…args) { System.out.print(args[0] + “ “); System.out.println(args.length); } a) Prints the first argument plus the length of args b) Prints the first argument c) Does not compile d) Exception thrown

A

a) Prints the first argument plus the length of args Varargs are treated like arrays

29
Q

What statements will compile? Choose all that apply. 1 int[][] vars1; 2 int[] vars2[]; 3 int[] vars3[][]; 4 int[][] vars4 = {{1,4},{3},{3,6,9}}; a) 1, 4 b) 1, 3 c) 1, 2, 4 d) All statements compile

A

d) All statements compile

30
Q

What method will give the expected result? Choose all that apply. ArrayList list = new ArrayList(); list.add(“hawk”); // [“hawk”] list.add(“owl”); // [“hawk”,”owl”] list.________; // [“pigeon”, “owl”] a) add(0, “pigeon”) b) add(1, “pigeon”) c) set(0, “pigeon”) d) set(1, “pigeon”)

A

c) set(0, “pigeon”)

31
Q

What method will give the expected result? Choose all that apply. ArrayList list = new ArrayList(); list.add(“hawk”); // [“hawk”] list.add(“owl”); // [“hawk”,”owl”] list.________; // [“hawk”, “robin”, “owl”] a) add(0, “robin”) b) add(1, “robin”) c) set(0, “robin”) d) set(1, “robin”)

A

b) add(1, “robin”)

32
Q

What method will give the expected result? Choose all that apply. ArrayList list = new ArrayList(); list.add(“hawk”); // [“hawk”] list.add(“owl”); // [“hawk”,”owl”] list.________; // [] a) removeAll() b) remove() c) empty() d) clear()

A

d) clear()

33
Q

What method will give the expected result? Choose all that apply. ArrayList list = new ArrayList(); list.add(“hawk”); // [“hawk”] list.add(“owl”); // [“hawk”,”owl”] int x = list._____; // x = 2 a) length() b) size() c) count() d) max()

A

b) size()

34
Q

What method will give the expected result? Choose all that apply. ArrayList list = new ArrayList(); list.add(“hawk”); // [“hawk”] list.add(“owl”); // [“hawk”,”owl”] list.________; // [“owl”] a) remove(0) b) set(0, null) c) clear(0) d) remove(“hawk”)

A

a) remove(0) and d) remove(“hawk”)

35
Q

What is the output of the following? List one = new ArrayList<>(); List two = new ArrayList<>(); System.out.print(one.equals(two) + “ “); one.add(“a”); System.out.print(one.equals(two) + “ “); two.add(“a”); System.out.print(one.equals(two) + “ “); one.add(“b”); two.add(0, “b”); System.out.print(one.equals(two)); a) false false false false b) true false false false c) true false true false c) true false false true

A

c) true false true false

36
Q

What method will give the expected result? Choose all that apply. int a = Integer._______; // a = 12 a) valueOf(12) b) parseInt(“12.0”) c) parseInt(“12”) d) valueOf(“12”)

A

a), c), d) The most efficient is c) because a) and d) use autoboxing

37
Q

What method will give the expected result? Choose all that apply. double a = Float._______; // a = 12 a) valueOf(12) b) parseFloat(“12.0”) c) parseDouble(“12”) d) valueOf(“12”) e) Does not compile

A

a), b), d)

38
Q

What is the output of the following? List heights = new ArrayList<>(); heights.add(null); int h = heights.get(0); System.out.println(h); a) Exception thrown b) Does not compile c) 0 d) null

A

a) Exception thrown int h = heights.get(0); The line above will result in autoboxing of the element returned from the get() method. But that object is null therefore a NullPointerException is thrown

39
Q

What method will give the expected result? Choose all that apply. List< String > list = new ArrayList<>(); list.add(“hawk”); list.add(“robin”); String[] stringArray = list._______; a) toArray() b) array() c) toArray(new String[0]) d) asArray()

A

c) a) does not compile b) and d) are not valid methods

40
Q

What method will give the expected result? Choose all that apply. String[] array = { “hawk”, “robin” }; List list = Arrays.asList(array); list.set(1,”test”); for (String b : array) System.out.print(b + “ “); list.remove(1); for (String b : array) System.out.print(b + “ “); a) hawk robin b) hawk test c) hawk test hawk d) Exception thrown

A

b) hawk test and d) Exception thrown When creating an ArrayList from an array, the array backs the ArrayList, which means: a) any modifications in the array are reflected in the ArrayList (and viceversa) b) the ArrayList size cannot be changed list.remove(1); The above threw an exception

41
Q

What method can be used to get the current date, time, or both in Java 8?

A

Date - LocalDate.now() Time - LocalTime.now() Date + Time - LocalDateTime.now()

42
Q

What is the output of the following? LocalDate d = new LocalDate(); d.set(10, 1, 2015); System.out.println(d); a) January 10th, 2015 b) February 10th, 2015 c) October 1st, 2015 d) Does not compile

A

d) Does not compile LocalDate cannot be created using a constructor

43
Q

What method will result in the expected behavior? Choose all that may apply. LocalTime time = LocalTime._______(6, 15); // 6:15am a) setTime b) getTime c) of d) set

A

c) of

44
Q

What method will result in the expected behavior? Choose all that may apply. LocalDate date = LocalDate.of(2014, Month.JANUARY, 20); date = date._____; // date is now February 20th, 2014 a) addMonth(1) b) plusMonths(1) c) set(Date.MONTH, Month.FEBRUARY) d) set(Month.FEBRUARY)

A

b) plusMonths(1)

45
Q

What is the output of the following? LocalDate date = LocalDate.of(2020, Month.JANUARY, 20); date = date.plusMonths(1).plusYears(1).plusHours(1).plusMinutes(1); a) February 20th, 2021 b) February 20th, 2021 02:01am c) Does not compile d) Exception thrown

A

c) Does not compile LocalDate does not support time-related methods (only date-related methods). Use LocalTime or LocalDateTime to modify time fields

46
Q

What option will result in the expected behavior? Choose all that may apply. LocalDate date = LocalDate.of(2014, Month.JANUARY, 20); date = date.plus(_______); // January 25th, 2015 a) 5 b) Day.DATE, 5 c) DAYS, 5 d) Period.ofDays(5)

A

d) Period.ofDays(5)

47
Q

What is the output of the following? LocalDate date = LocalDate.of(2014, Month.JANUARY, 20); Period period = Period.ofYears(1).ofMonths(1).ofDays(1); date = date.plus(period); a) February 21st, 2015 b) January 21st, 2014 c) Does not compile d) Throws an exception

A

b) January 21st, 2014 ofXXX() methods do not chain. Only the last value was applied. To give a period comprised of years, days, months, etc. then use the method of(int x, int y, int z)

48
Q

What method will result in the expected behavior? Choose all that may apply. LocalDate date = LocalDate.of(2014, Month.JANUARY, 20); date._____; System.out.println(date); // date January 20th, 2015 is printed a) addYear(1) b) addYears(1) c) plusYears(1) d) None of the above

A

d) None of the above LocalDate objects are immutable and therefore the plusYears() method returns a new object with the result of the operation (original object remains intact). date = date.plusYears(1); The above would work.

49
Q

What option will result in the expected behavior? Choose all that may apply. LocalDate date = LocalDate.of(2014, Month.MARCH, 20); System.out.println(date._________); // date printed as “3/20/2014” a) format(SimpleDateFormat.SHORT) b) format(“MM/DD/YYYY”) c) format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)) d) format(DateTimeFormatter.ISO_LOCAL_DATE)

A

c) format(DateTimeFormatter .ofLocalizedDate(FormatStyle.SHORT))

50
Q

What class can be used to format LocalDate, LocalTime, LocalDateTime objects? a) DateTimeFormatter b) LocalDateTimeFormatter c) SimpleDateFormat d) SimpleDateFormatter

A

a) DateTimeFormatter and c) SimpleDateFormat c) SimpleDateFormat is deprecated

51
Q

What import statement is needed for DateTimeFormatter? a) java.time.DateTimeFormatter; b) java.time.format.DateTimeFormatter; c) java.lang.time.DateTimeFormatter; d) java.lang.date.format.DateTimeFormatter;

A

b) java.time.format.DateTimeFormatter;

52
Q

What is the output of the following? LocalTime time = LocalTime.of(11, 22, 34); DateTimeFormatter shortDateTime = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); System.out.println(shortDateTime.format(time)); a) 11:22:34 b) 11:22 AM c) 11:22:34 AM d) Exception thrown

A

d) Exception thrown An UnsupportedTemporalTypeException is thrown because ofLocalizedDate() returns a DateTimeFormatter which is able to format dates but not times.

53
Q

What option will result in the expected behavior? Choose all that may apply. LocalDate date = LocalDate.of(2015, Month.FEBRUARY, 4); DateTimeFormatter f = DateTimeFormatter._______; System.out.println(date.format(f)); // Feb-4-15 a) ofPattern(“MMM-d-YY”) b) ofPattern(“MMM-D-YY”) c) pattern(“mmm-d-YY”) d) pattern(“MMM-D-YY”)

A

a) ofPattern(“MMM-d-YY”)

54
Q

What option will result in the expected behavior? Choose all that may apply. DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”); LocalDate date = LocalDate.________; System.out.println(date); // 2015-01-02 a) of(2015, Month.JANUARY, 2) b) of(f.parse(“01 02 2015”)) c) f.parse(“01 02 2015”) d) parse(“01 02 2015”, f)

A

a) of(2015, Months.JANUARY, 2) and c) parse(“01 02 2015”)