Chapter 3 Core Java APIs Flashcards

Using operators and decision constructs Creating and using arrays Working with selected classes from the Java API Working with Java Data Types

1
Q

API stands for

A

application programming interface.

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

string is basically a sequence of [Blank]

A

characters

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

reference types are created using the [Blank] keyword

A

new

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

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

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

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

A

123

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

Can a String be changed when it is created?

A

No, String is immutable

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

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

A

Mutable

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

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

A

final

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

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

A

12

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

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

A

String Pool, also known as the intern pool

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

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

Java counts from [Blank] when indexed

A

0

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

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