Ch5 Core Java APIs Flashcards

1
Q

What interfaces does String implement?

A

Serializable, CharSequence, Comparable

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

Name as many important String methods as you can (answer contains only methods relevant for OCP exam)

A
  • length()
  • charAt()
  • indexOf()
  • substring()
  • toLowerCase() & toUpperCase()
  • equals() & equalsIgnoreCase()
  • startsWith() & endsWith()
  • replace()
  • contains()
  • trim() & strip() & stripLeading(), and stripTailing()
  • intern()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the options to construct a StringBuilder?

A

Stringbuilder b1 = new StringBuilder();
Stringbuilder b2 = new StringBuilder(“animal”);
Stringbuilder b3 = new StringBuilder(10);

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

What is an easy way to reverse a String?

A

Convert it to a StringBuilder and call the reverse() method.

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

How do we convert a StringBuilder object to a String object?

A

By calling the toString() method.

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

Does StringBuilder implement the method equals()?

A

No.

If you call equals() on two StringBuilder instances, it will check for reference equality. To check for equality, convert it to String first.

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

Why does the following code not compile?

String string = “a”;
StringBuilder builder = new StringBuilder(“a”);
System.out.println(string == builder);

A

The compiler is smart enough to know that two references to two different type of objects can never return true.

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

What will happen when this code is executed?

String x = “Hello world”;
String y = “ Hello world”.trim();
System.out.println(x == y);

A

It prints false.

Even though x and y evaluate to the same string, it is computed at run-time. Since it isn’t the same at compile-time, a new String object is created.

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

What is the following type of array called?

int[] numbers = {42, 55, 99};

A

Anonymous array

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

How do we print the contents of an array in a human-readable way?

A

Arrays.toString(arrayHere);

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

Arrays is a class provided by Java that requires an import. What package does it reside in?

A

java.util

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

How can we sort an array? Class & method

A

Using the Arrays.sort() method.

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

How can we search an array?

A

Using Arrays.binarySearch().

Requires the array to be sorted.

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

What does Arrays.binarySearch() return when an element is not found?

A

It returns the negative value showing one smaller than the negative of the index where a match needs to be inserted to preserve sorted order.

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

What does Arrays.binarySearch() return when the argument array is not sorted?

A

The answer will be unpredictable.

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

How can we compare an array?

A

Using the Arrays.equals(), Arrays.compare() or the Arrays.mismatch() method.

17
Q

What are the return value rules for Arrays.compare()?

A

A negative number means the first array is smaller than the second.
A zero means the arrays are equal.
A positive number means the first array is larger than the second.

18
Q

Does this compile?

Arrays.compare(new int[] {1}, new String[] {“a”});

A

No. They must be of the same type.

19
Q

What does Arrays.mismatch() do?

A

Arrays.mismatch() is familiar to Arrays.compare(). If the arrays are equal, mismatch() returns -1. Otherwise it returns the first index where they differ.

20
Q

What does the following code return?

Arrays.mismatch(new int[] {1, 2}, new int[] {1});

A

It returns 1.

The element at index 1 of the second array is not equal the first array.

21
Q

What are three ways to instantiate an ArrayList?

A

new ArrayList();
new ArrayList(10);
new ArrayList(otherList); //shallow copy other list

22
Q

Why does

var list = new ArrayList<>();

compile? and what type is it?

A

The type of the var is ArrayList. Since there isn’t a type specified for the generic, Java assumed the ultimate superclass (Object).

23
Q

What is the advantage of calling the Wrapper class method valueOf() over using the constructor?

ex: Long.valueOf(1)

A

the valueOf() method allows object caching (like the String pool)

24
Q

Given the following String “123”

Which Wrapper class method can we use to make it into an int and which method can we use to make it into an Integer?

A

Integer.parseInt(“123”) returns a primitive

Integer.valueOf(“123”) returns a Wrapper object

25
Q

How do we convert a list to an array? (2 options)

A

list. toArray(); //returns Object[] array

list. toArray(new String[0]); //returns String[] array

26
Q

How do we convert an array to a list?

A

Arrays.asList(array); // returns fixed sized list

List.of(array); //returns immutable list

27
Q

What happens when you want to change a value in an immutable list?

A

It throws a UnsupportedOperationException

28
Q

How do we create a list using varargs? (two options)

A

Arrays.asList(“one”, “two”);

List.of(“one”, “two”);

29
Q

What is the difference between Arrays.asList() and List.of()?

A

With Arrays.asList() it is allowed to change values in the created object.

With Arrays.asList() changing values in the created object affects the original or vice versa.

30
Q

What happens when trying to add an item to a set that already contains that item?

A

It returns false.

31
Q

What are two common implementations of Set?

A

HashSet and TreeSet

32
Q

What are common Math methods?

A
  • min() and max()
  • round()
  • pow()
  • random()
33
Q

What is the difference between the equals method and the == operator?

A

== checks object equality.

equals() depends on the implementation of the object it is being called on. For Strings, equals() checks the characters inside of it.

34
Q

What is the result of the following?

List hex = Arrays.asList(“30”, “8”, “3A”, “FF”);
Collections.sort(hex);

int x = Collections.binarySearch(hex, “8”);
int y = Collections.binarySearch(hex, “3A”);
int z = Collections.binarySearch(hex, “4F”);

System.out.println(x + “ “ + y + “ “ + z);

A. 0 1 -2
B. 0 1 -3
C. 2 1 -2
D. 2 1 -3
E. None of the above.
F. The code doesn’t compile.

A

D.

After sorting, hex contains [30, 3A, 8, FF].
Remember that numbers sort before letters and strings sort alphabetically.
This makes 30 come before 8.
A binary search correctly finds 8 at index 2 and 3A at index 1.
It cannot find 4F but notices it should be at index 2.
The rule when an item isn’t found is to negate that index and subtract 1.
Therefore, we get -2-1, which is -3.

35
Q

What is the result of the following code?

12: int count = 0;
13: String s1 = “java”;
14: String s2 = ”java”;
15: StringBuilder s3 = new StringBuilder(“java”);
16: if (s1 == s2) count++;
17: if (s1.equals(s2)) count++;
18: if (s1 == s3) count++;
19: if (s1.equals(s3)) count++;
20: System.out.println(count);

A. 0
B. 1
C. 2
D. 3
E. 4
F. An exception is thrown
G. The code does not compile

A

G.

The question is trying to distract you into paying attention to logical equality versus object reference equality. The exam creators are hoping you will miss the fact that line 18 does not compile. Java does not allow you to compare String and StringBuilder using ==.

36
Q

Which of the following return the number 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

A, B, F.

Remember that indexes are zero-based, which means that 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.

37
Q

Which of the following method calls can be applied to a String object?

repeat(int )
equalsIgnoreCase(String)
prune()
append()
intern()
compareTo(String )

A
  • repeat (Returns a string whose value is the concatenation of this string repeated count times.)
  • equalsIgnoreCase(String)
  • intern()
  • compareTo(String )