Chapter 3 Core Java APIs Flashcards

1
Q

What method length() does ?

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

A

The method length() returns the number of characters in the String. The method signature is as follows: int length()

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

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

What method chartAt() does ?

String string = “animals”;
System.out.println(string.charAt(0));

A

The method charAt() lets you query the string to find out what character is at a specific index. The method signature is as follows: char charAt(int index)

String string = “animals”;
System.out.println(string.charAt(0)); // a

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

What method indexOf() does ?

String string = “animals”;
System.out.println(string.indexOf(‘a’));
System.out.println(string.indexOf(“al”));
System.out.println(string.indexOf(“al”, 5));

A

The method indexOf()looks at the characters in the string and fi nds the fi rst index that matches the desired value. indexOf can work with an individual character or a whole String as input. It can also start from a requested position. indexOf() returns –1 when no match is found.

String string = “animals”;
System.out.println(string.indexOf(‘a’)); // 0
System.out.println(string.indexOf(“al”)); // 4
System.out.println(string.indexOf(“al”, 5)); // -1

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

What method substring() does ?

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));

A

It returns parts of the string. The first parameter is the index to start with for the returned string. There is an optional second parameter, which is the end index you want to stop at.

String string = “animals”;
System.out.println(string.substring(3)); // mals
System.out.println(string.substring(string.indexOf(‘m’))); // mals
System.out.println(string.substring(3, 4)); // m
System.out.println(string.substring(3, 7)); // mals

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

What toLowerCase() and toUpperCase() do ?

String string = “animals”;
System.out.println(string.toUpperCase());
System.out.println(“Abc123”.toLowerCase());

A

toUpperCase() converts any lowercase characters to uppercase in the returned string. toLowerCase() converts any uppercase characters to lowercase in the returned string. These methods leave alone any characters other than letters.

String string = “animals”;
System.out.println(string.toUpperCase()); // ANIMALS
System.out.println(“Abc123”.toLowerCase()); // abc123

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

What equals() and equalsIgnoreCase() do ?

System.out.println(“abc”.equals(“ABC”));
System.out.println(“ABC”.equals(“ABC”));
System.out.println(“abc”.equalsIgnoreCase(“ABC”));

A

The equals() method checks whether two String objects contain exactly the same characters in the same order. The equalsIgnoreCase() method checks whether two String objects contain the same characters with the exception that it will convert the characters’ case if needed.

System.out.println(“abc”.equals(“ABC”)); // false
System.out.println(“ABC”.equals(“ABC”)); // true
System.out.println(“abc”.equalsIgnoreCase(“ABC”)); // true

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

What startsWith() and endsWith() do ?

System.out.println(“abc”.startsWith(“a”));
System.out.println(“abc”.startsWith(“A”));
System.out.println(“abc”.endsWith(“c”));
System.out.println(“abc”.endsWith(“a”));

A

The startsWith() and endsWith() methods look at whether the provided value matches part of the String.

System.out.println(“abc”.startsWith(“a”)); // true
System.out.println(“abc”.startsWith(“A”)); // false
System.out.println(“abc”.endsWith(“c”)); // true
System.out.println(“abc”.endsWith(“a”)); // false

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

What method contains() does ?

System.out.println(“abc”.contains(“b”));
System.out.println(“abc”.contains(“B”));

A
The contains() method also looks for matches in the String. It isn’t as particular as
startsWith() and endsWith()—the match can be anywhere in the String.

System.out.println(“abc”.contains(“b”)); // true
System.out.println(“abc”.contains(“B”)); // false

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

What method replace() does ?

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

A

The replace() method does a simple search and replace on the string. There’s a version that takes char parameters as well as a version that takes CharSequence parameters.

System.out.println(“abcabc”.replace(‘a’, ‘A’)); // AbcAbc
System.out.println(“abcabc”.replace(“a”, “A”)); // AbcAbc

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

What method trim() does ?

System.out.println(“abc”.trim());
System.out.println(“\t a b c\n”.trim());

A
The trim() method removes whitespace from the beginning and end
of a String. In terms of the exam, whitespace consists of spaces along with the \t (tab) and \n (newline) characters. Other characters, such as \r (carriage return), are also included in what gets trimmed.

System.out.println(“abc”.trim()); // abc
System.out.println(“\t a b c\n”.trim()); // a b c

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

What is the main difference between StringBuilder and StringBuffer ?

A

When writing new code that concatenates a lot of String objects together, you should use StringBuilder. StringBuilder was added to Java in Java 5. If you come across older code, you will see StringBuffer used for this purpose. StringBuffer does the same thing but more slowly because it is thread safe.

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

What equals() and ‘==’ do with String / StringBuilder ?

A

Calling == on String objects will check whether they point to the same object in the
pool. Calling == on StringBuilder references will check whether they are pointing to the same StringBuilder object. Calling equals() on String objects will check whether the sequence of characters is the same. Calling equals() on StringBuilder objects will check whether they are pointing to the same object rather than looking at the values inside.

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

What Arrays.binarySearch() does ?

A

Arrays.binarySearch() searches a sorted array and returns the
index of a match. If no match is found, it negates the position where the element would need to be inserted and subtracts 1.

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

What is Autoboxing ?

A

Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
LocalDate d = new LocalDate();
 System.out.println(d);
A

LocalDate d = new LocalDate(); // DOES NOT COMPILE

The date and time classes have private constructors to force you to use the static methods. Don’t fall for this. You are not allowed to construct a date or time object directly.

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

What will be the result:

LocalDate date = LocalDate.of(2020, Month.JANUARY, 20);
date.plusDays(10);
System.out.println(date);

A

It prints January 20, 2020. Adding 10 days was useless because we ignored the result. The date and time classes are all immutable, which means the return value must be used.

17
Q

Which one is immutable: String, StringBuilder, StringBuffer ?

A

Only String is immutable.

StringBuilder / StringBuffer can be mutated with methods like append().

18
Q

How you’ll get the length of an Array and ArrayList ?

A

Array:

int length = c.length;

ListArray:

int length = c.size();

19
Q

Which is true (when equals() is used):
1.) An arrays with the same content are equal.

2.) Two ArrayLists with the same content are equal.

A
  1. ) False: An array does not override equals() and so uses object equality.
  2. ) True: ArrayList does override equals() and defines it as the same elements in the same order.
20
Q

Can we change dates classes (are they immutable): LocalTime, LocalTime, LocalDateTime ?

A

The date and time classes are all immutable, which means the return value must be used.

21
Q

What is wrong with the following code:

Period p = Period.ofDays(1).ofYears(2);

A

Period does not allow chaining. Only the last Period method called counts, so only
the two years are subtracted.