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()?

A
32
Q

Why does the following code not compile?

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

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

A
34
Q

What kinds of strings are stored in the string pool?

A
35
Q

What will happen when this code is executed?

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

A
36
Q

What will happen when this code is executed?

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

A
37
Q

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

A
38
Q

What are valid ways to declare an array?

A
39
Q

What is the following type of array called?

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

A
40
Q

What type of reference variable does the following code create?

int[] ids, types;

A
41
Q

What type of reference variable does the following code create?

int ids[], types;

A
42
Q

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

A
43
Q

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

A
44
Q

How can we sort an array? Class & method

A
45
Q

How can we search an array?

A
46
Q

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

A
47
Q

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

A
48
Q

How can we compare an array?

A
49
Q

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

A
50
Q

Does this compile?

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

A
51
Q

What does Arrays.mismatch() do?

A
52
Q

What does the following code return?

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

A
53
Q

What is the benefit of an Arraylist over an array?

A
54
Q

What package does ArrayList reside in?

A
55
Q

What are three ways to instantiate an ArrayList?

A
56
Q

Why does

var list = new ArrayList<>();

compile? and what type is it?

A
57
Q

What is a wrapper class?

A
58
Q

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

ex: Long.valueOf(1)

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

A
60
Q

Explain what autoboxing and unboxing is

A
61
Q

What happens when you try to unbox a null value?

A
62
Q

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

A
63
Q

How do we convert an array to a list?

A
64
Q

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

A
65
Q

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

A
66
Q

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

A
67
Q

What is a Set?

A
68
Q

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

A
69
Q

What are two common implementations of Set?

A
70
Q

When is TreeSet more important than Hashset?

A
71
Q

What is a Map?

A
72
Q

What is the most common implementation of Map?

A
73
Q

What are common Math methods?

A
74
Q

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

A