Chapter V Flashcards

Working witj Java primitive data types and string APIs Working with Java arrays Programming abstractly through interfaces

1
Q

What are two ways to create a String object?

A
String name = "Fluffy";
String name = new String("Fluffy");
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
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
3
Q

What package is String part of?

A

java.lang

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

What are the rules for 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 from left to right.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
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
6
Q

What does the String method length() do?

A

returns the length of the string, where the first number is 1.

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

What does the String method charAt() do?

A

The method charAt() lets you query the string to find out what character is at a specific index.

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

What does the String method indexOf() do?

A

The method indexOf() looks at the characters in the string and finds the first index that matches the value.

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

What does String.indexOf() return?

ex: “test”.indexOf(“z”);

A

The method returns -1.

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

What does String.indexOf() return?

ex: “test”.indexOf(“es”);

A

1

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

What does String.indexOf() return?

ex: “animals”.indexOf(‘a’);

A

0.

note: characters can be passed as an argument.

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

What can be passed as a second argument of the String method indexOf()?

A

An index that indicates where to start the search.

ex: “animals”.indexOf(‘a’, 4); // returns 4

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

What will happen when this code is executed?

“test”.charAt(4);

A

It throws a StringIndexOutOfBoundsException.

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

What will happen when this code is executed?

“animals”.indexOf(“al”, 5);

A

It returns -1 (cannot find a match)

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

What will happen when this code is executed?

“animals”.indexOf(“al”, 5);

A

It returns -1 (cannot find a match)

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

What does the String method substring() do?

A

The method substring() looks for characters in a string and returns part of the string. The first parameter is the index to start with for the returned string.

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

The String method substring takes two arguments. Is the second argument inclusive in the search?

A

No.

ex: “animals”.substring(3, 4); // returns m

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

What will happen when this code is executed?

“animals”.substring(3, 3);

A

Returns an empty string

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

What will happen when this code is executed?

“animals”.substring(3, 2);
or
“animals”.substring(3, 8);

A

Throws an exception.

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

is the String method equals() case sensitive?

A

yes

To use equals without case sensitivity, use the method equalsIgnoreCase().

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

What will happen when this code is executed?

“abc”.equals(“ABC”);

A

It returns false. Equals is case sensitive.

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

What will happen when this code is executed?

“abcabc”.replace(‘a’, ‘A’);

A

It returns “AbcAbc”

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

What do the strip() and trim() string methods do?

A

They remove whitespace from the beginning and end of a string.

Whitespace includes \t (tab, \n (newline) and \r (carriage return).

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

What do the string methods stripLeading() and stripTrailing() do?

A

stripLeading() removes whitespace from the beginning and stripTrailing() removes whitespace at the end of a string.

Whitespace includes \t (tab, \n (newline) and \r (carriage return).

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

What does the string method intern() do?

A

The intern() method returns the value from the string pool if it is there. Otherwise, it adds the value to the string pool.

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

What is the following technique called?
(bonus: how many objects are created?)

String result = “AniMal “.trim().toLowerCase().replace(‘a’, ‘A’);

A

The technique is called method chaining.

Bonus answer: 4 objects

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

Why should we use Stringbuilder instead of String?

A

Stringbuilder is mutable, whereas Strings are immutable. If we need to work on a piece of String, it might be better to use a StringBuilder to avoid the creation of many objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
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
28
Q

What is the difference between StringBuilder and StringBuffer?

A

StringBuffer is thread-safe. StringBuilder is faster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
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
30
Q

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

A

By calling the toString() method.

31
Q

Does StringBuilder implement the method equals()?

32
Q

Why does the following code not compile?

String string = "a";
StringBuilder builder = new StringBuilder("a");
System.out.println(string == builder);
33
Q

What is the string pool (also known as intern pool)?

34
Q

What kinds of strings are stored in the string pool?

35
Q

What will happen when this code is executed?

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

36
Q

What will happen when this code is executed?

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

37
Q

How do we force to use a string from a string pool, if it exists?

38
Q

What are valid ways to declare an array?

39
Q

What is the following type of array called?

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

40
Q

What type of reference variable does the following code create?

int[] ids, types;

41
Q

What type of reference variable does the following code create?

int ids[], types;

42
Q

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

43
Q

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

44
Q

How can we sort an array? Class & method

45
Q

How can we search an array?

46
Q

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

47
Q

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

48
Q

How can we compare an array?

49
Q

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

50
Q

Does this compile?

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

51
Q

What does Arrays.mismatch() do?

52
Q

What does the following code return?

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

53
Q

What is the benefit of an Arraylist over an array?

54
Q

What package does ArrayList reside in?

55
Q

What are three ways to instantiate an ArrayList?

56
Q

Why does

var list = new ArrayList<>();

compile? and what type is it?

57
Q

What is a wrapper class?

58
Q

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

ex: Long.valueOf(1)

59
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?

60
Q

Explain what autoboxing and unboxing is

61
Q

What happens when you try to unbox a null value?

62
Q

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

63
Q

How do we convert an array to a list?

64
Q

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

65
Q

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

66
Q

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

67
Q

What is a Set?

68
Q

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

69
Q

What are two common implementations of Set?

70
Q

When is TreeSet more important than Hashset?

71
Q

What is a Map?

72
Q

What is the most common implementation of Map?

73
Q

What are common Math methods?

74
Q

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