Chapter 4: Core APIs Flashcards

1
Q

What is the output of the following code?

public class Fish {
public static void main(String[] args) {
int numFish = 4;
String fishtype = “tuna”;
String anotherFish = numfish + 1;

A

no, it’s a String. You can’t add 2 ints and call it a string, it won’t work

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

Which of the array declarations are not legal?

A. int[][] scores = new int[5][]
B. String beans[] = new beans[6]
C. java.util.Date[] dates[] = new java.util.Date[2][];
D. int[][] types = new int[];
E. int[][] java = new int[][];

A

String beans[] = new beans[6]
- uses the variable name as a type, clearly
illegal

int[][] types = new int[];
int[][] java = new int[][];
- both leave out the size of the outer array. Illegal. You can leave the later array sizes for later, but for multi-dimensional arrays you must have the first size in there.

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

Output?

3: var s = “Hello”
4: var t = new String(s);
5: if (“Hello”.equals(s)) print(“one”);
6: if ( t == s) print( “two” );
7: if (t.intern() == s) print (“three”);
8: if (“Hello” == s) print (“four”);
9: if(“Hello”.intern() == t) print(“five”);

*Key-takeways
A. Strings in Java are immutable and can be stored in a String Pool to save memory.
Using the new keyword creates a new string object, not referencing the String Pool.
B. The == operator checks for reference equality, while .equals() checks for content equality.
C. The intern() method returns a reference to the string object in the String Pool with the same content, ensuring that equal strings have the same reference in memory.

A

one, three, four

line 3 points to a String in the string pool
line 4 calls to the String constructor explicitly and is therefore a different object than s
line 5 checks for object equality, which is true, so prints “one”
line 6: checks for object reference equality which is not true since we have different objects
line 7: calls intern(), which returns the value from the string pool and is therefore the same reference as s.
line 8: also compares references, but is true because both references point to the object from the string pool.
line 9: a trick, “Hello” is already in the string pool, so calling intern() does not change anything. The reference t is a different object so the result is false

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

Result of the following code?

var sb = new StringBuilder();
sb.append(“aaa”).insert(1, “bb”).insert(4, “ccc”);
System.out.println(sb);

A

abbaccca

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

How many of these lines contain a compiler error?

double one = Math.pow(1, 2);
int two = Math.round(1.0);
float three = Math.random();
var doubles = new double[] { one, two, three };

A

2
watch return types w/ maths operations

round() returns an int when called with a float. however we are calling it with a double, so it returns a long

random() returns a double

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

Which statements is true of the 2 values?

? Check these values
2022-08-028T05:00 GMT-04:00
2022-08-028T05:00 GMT-06:00
?

The first date/time is earlier
The date/times are 6 hours apart

A

when dealing with time zones, it is best to convert GMT first by subtracting the time zone. Remember that subtracting a negative is like adding.
The first date/time is 9:00 GMT, the 2nd is 15:00 GMT. Thus the first is earlier by 6 hours

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

Which are true?

First element is index 0
Arrays are fixed size
Arrays are immutable
Calling equals() on 2 different arrays containing the same primitive values always return false

A

A,C,F
Arrays are zero-indexed
They are not able to change size
They are NOT immutable = values can be changed
An array does not override equals(), so it uses object equality. There are 2 different objects, = not equal.

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

how many cause a compiler error?

int one = Math.min(5, 3);
long two = Math.round(5.5);
double three = Math.floor(6.6);
var doubles = new double[] {one, two, three};

A

all compile
min() and floor() methods return the same type passed in, int and double, respectively. The round() method returns a long when called with a double.

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

var date = LocalDate.of(2022, 4, 3);
date.plusDays(2);
date.plusHours(3);
System.out.println(date.getYear() + “ “ + date.getMonth() + “ “ + date.getDayOfMonth());

A

won’t compile
a LocalDate does not have a time element. Therefore there is no method to add hours.

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

What is the output of the following code?

var numbers = “012345678”.indent(1);
numbers = numbers.stripLeading();
print ( numbers.substring(1,3));
print ( numbers.substring(7,7));
print ( numbers.substring(7));

A

12, 78, a blank line
indent() call adds a blank space to the beginning of numbers, and stripLeading() immediately removes it.
Therefore they cancel out and have no effect.

substring() has 2 forms -
one takes an index to start with (inclusive) and index to stop before (exclusive)
the other just takes one to start with and goes -> end of string

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

public class Lion {
public void roar(String roar1, StringBuilder roar2) {
roar1.concat(“!!!”);
roar2.concat(“!!!”);
}
main method {
var roar1 = “roar”;
var roar2 = new StringBuilder(“roar”);
new Lion().roar(roar1, roar2);
print (roar1 + “ “ + roar2)

A

roar!!! roar

a String is immutable. Calling concat() returns a new String but does not change the original.
a StringBuilder is mutable. Calling append() adds characters to the existing character sequence along with returning a reference to the same object.

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

var dare = LocalDate.now();
var time = LocalTime.now();
var dateTime = LocalDateTime.now();
var zoneID = ZoneId.systemDefault();
var zonedDateTime = ZoneDateTime.of(dateTime, zoneID);

Instant instant = _______________;

A

Instant.now()
- correctly creates the current instant.

zonedDateTime.toInstant()
- is also a proper conversion

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

What is the output of the following
var arr = new String[] { “PIG”, “pig”, “123”};
Arrays.sort(arr);
print(Arrays.toString(arr));
print(Arrays.binarySearch(arr, “Pippa”));

A

C = [123, PIG, pig]
E = -3
Numbers sort before letters and uppercase sorts before lowercase.
binarySearch() looks at where a value would be inserted, which is before the second element for Pippa.
It then negates it and subtracts one, which is option E

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

A, B, G
there are 11 characters in base because there are 2 escape characters. \n counts as one character representing a new line.
\ counts as one character representing a backslash
indent() adds two characters to the beginning of each of the two lines of base.

this gives us 4 characters - however the method also adds a new line to the end if it is missing.

the extra character means we add 5 characters to the existing 11.

finally
translateEscapes() method turns any text escape characters into actual escape characters, making \t and \t. This gets rid of one character, leaving 10 characters

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

compare() method returns a positive integer when the arrays are different and the first is larger. This is the case for option A since the element at index 1 comes first alphabetically.

mismatch() returns a positive integer when the arrays are different in a position index 1 or greater.

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