Chapter 3 Core Java APIs Flashcards Preview

OCA Oracle Certified Associate Java SE 8 > Chapter 3 Core Java APIs > Flashcards

Flashcards in Chapter 3 Core Java APIs Deck (22)
Loading flashcards...
1
Q

API stands for

A

application programming interface.

2
Q

string is basically a sequence of [Blank]

A

characters

3
Q

reference types are created using the [Blank] keyword

A

new

4
Q

3 string concatenation rules:

A
  1. If both operands are numeric, + means numeric addition.
  2. If either operand is a String, + means concatenation.
  3. The expression is evaluated left to right.
5
Q

System.out.println(1 + 2);
System.out.println(“a” + “b”);
System.out.println(“a” + “b” + 3);
System.out.println(1 + 2 + “c”);

A

3
ab
ab3
3c

6
Q

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

A

1 + 2 = 3
3 + three = 6
6 + four = 64

7
Q

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

A

123

8
Q

Can a String be changed when it is created?

A

No, String is immutable

9
Q

[Blank] means changeable (as in StringBuffers/StringBuilders)

A

Mutable

10
Q

Immutable classes in Java are [Blank], and subclasses can’t add mutable behavior.

A

final

11
Q

String s1 = “1”;
String s2 = s1.concat(“2”);
s2.concat(“3”);
System.out.println(s2);

A

12

12
Q

What location in the JVM collects Strings to reuse and save memory?

A

String Pool, also known as the intern pool

13
Q
Which one uses the String pool?
String name = "Fluffy";
String name = new String("Fluffy");
A

First one uses String pool.

Second one creates a new object

14
Q

Java counts from [Blank] when indexed

A

0

15
Q

String class method: length()

A

The method length() returns the number of characters in the String. The method signature is as follows:
String string = “animals”;
System.out.println(string.length()); // 7

16
Q

String class method: charAt()

A

The method charAt() lets you query the string to fi nd out what character is at a specifi c
index. The method signature is as follows:

String string = “animals”;
System.out.println(string.charAt(0)); // a
System.out.println(string.charAt(6)); // s
System.out.println(string.charAt(7)); // throws exception

17
Q

String class method: indexOf()

A

The method indexOf()looks at the characters in the string and finds the first 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. The method signatures are as follows:

int indexOf(char ch)
int indexOf(char ch, index fromIndex)
int indexOf(String str)
int indexOf(String str, index fromIndex)

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

18
Q

String class method: substring()

A

The method substring() also looks for characters in a string. It returns parts of the string.
The fi rst parameter is the index to start with for the returned string. As usual, this is a
zero-based index. There is an optional second parameter, which is the end index you want
to stop at.
Notice we said “stop at” rather than “include.” This means the endIndex parameter is
allowed to be 1 past the end of the sequence if you want to stop at the end of the sequence.
That would be redundant, though, since you could omit the second parameter entirely in
that case. In your own code, you want to avoid this redundancy. Don’t be surprised if the
exam uses it though. The method signatures are as follows:
int substring(int beginIndex)
int substring(int beginIndex, int endIndex)
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

19
Q
String class method: toLowerCase() and toUpperCase()
String string = "animals";
System.out.println(string.toUpperCase()); 
System.out.println("Abc123".toLowerCase());
A

ANIMALS

abc123

20
Q
String class method: equals() and equalsIgnoreCase()
System.out.println("abc".equals("ABC")); 
System.out.println("ABC".equals("ABC")); 
System.out.println("abc".equalsIgnoreCase("ABC"));
A

false
true
true

21
Q
String class method: startsWith() and endsWith()
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

true
false
true
false

22
Q
String class method: contains()
System.out.println("abc".contains("b")); // true
System.out.println("abc".contains("B")); // false
A

true

false