Ch3: Core Java APIs Flashcards

1
Q

What does API stand for?

A

Application Programming Interface

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

What is the value of s1 and s2, and why?

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

A

s1 = “1”
s2 = “12”
The String object is immutable, so the .concat method doesn’t modify the object.

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

What’s another name for the string pool?

A

The intern pool

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

What’s another name for the intern pool?

A

The string pool

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

What goes into the string pool?

A

String literals (not String objects created with “new”)

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

How do you get the number of characters in a String?

A

.length()

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

How do you find out what is at a specific index in a string?

A

.charAt(int index)

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

How do you find the index of a specific item in a string?

A

indexOf(char ch) or indexOf(String str)

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

How do you find the index of a specific item in a string starting from a specific position?

A

indexOf(char ch, index int)

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

What happens when you call indexOf for a value that does not exist?

A

The value -1 is returned

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

How would you return the value “123” from the object:

String str = “012345”;

A

str.substring(1,4) // value at the ending index will not be included

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

What is the method signature to replace characters?

A

str.replace(orig, new)

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

How do you remove trailing and leading whitespace from a string and what is removed?

A

.trim() removes space, new line (\n), tab (\t), and carriage return (\r)

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

What does the .append() method on StringBuilder return?

A

A reference to itself

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

List all StringBuilder constructor signatures.

A
new StringBuilder(); // empty object
new StringBuilder("foo"); // contains val "foo"
new StringBuilder(10); // Empty, but capacity of 10
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the two StringBuilder methods for adding text to the string in the object?

A

.append(String str)

.insert(int index, String str)

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

What are the two StringBuilder methods for removing text from the string in the object?

A

.delete(int startIndex, int stopIndex) // stopIndex is not removed, same as substring
.deleteChatAt(int index)

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

Which StringBuilder method is used to reorder the text backwards?

A

.reverse()

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

What is StringBuffer?

A

The same as StringBuilder, but slower because it’s thread safe.

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

What does the StringBuilder.equals(StringBuilder obj) test?

A

Checks for reference equality (not value equality)

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

for
char[] letters;
what kind of variable is letters?

A

A reference variable pointing to an array of primitive char types

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

What kind of data structure is an array?

A

Ordered list

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

How do you create an empty array of ints?

A

int[] numbers = new int[3];

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

How do you create an array of ints with values? (2 ways)

A

int[] numbers = new int[] {11, 20, 45};

int[] numbers = {11, 20, 45};

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

What are acceptable variable definitions for an array?

A

int[] name;
int [] name;
int name[];
int name [];

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

What type are primitive arrays: primitives or object references?

A

Object references

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

How do you get the number of elements in an array?

A

arr.length;

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

How do you get a string representation of an array?

A

java.util.Arrays.toString(myArray);

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

How do you sort an array?

A

java.util.Arrays.sort(myArray);

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

What are the rules for binary search?

A
  1. Sorted means “smallest to largest”
  2. Target element found in a sorted array = index of match
  3. Target element not found in sorted array = Tells us where to insert to keep the array sorted. Does: neg. desired index minus 1. eg, if the target is index 4, would be -4 - 1 = -5.
  4. If unsorted (must be smallest to largest), result will be unpredictable.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

How does binary search work?

A
  1. Divides the data set into two equal parts
  2. Determines which half the data should exist in
  3. repeats until data is found
  4. Only works on sorted data sets (for obvious reasons)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is the syntax for varargs?

A

String… args;

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

List all ways to declare a 2D array

A

int[][] coords;
int coords[][];
int[] coords[];

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

How do you define an empty asymmetric multidimensional array?

A

Only define the first (top level) dimension, then go through each element and define each dimension separately:
int[][] args = new int[2][];
args[0] = new int[5];
args[1] = new int[3];

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

How do you import ArrayList?

A

import java.util.ArrayList;

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

What are the signatures of the three ArrayList constructors?

A

ArrayList()
ArrayList(int capacity)
ArrayList(ArrayList listToCopy)

37
Q

How do you instantiate a new ArrayList?

A

ArrayList strList = new ArrayList<>();

38
Q

Which interface does ArrayList implement?

A

List

39
Q

What is the signature for the method that appends elements to an ArrayList?

A
boolean add(E element)
void add(int index, E element)

where E means any element the array can hold

40
Q

What elements cannot be added to an ArrayList?

A

Primitives. They must use the object wrapper.

41
Q

What happens if you add an element to an ArrayList and specify an index that already holds a value?

A

The new element will be inserted in front of the existing value

42
Q

What are the method signatures for deleting an element from an ArrayList?

A
boolean remove(Object object)
E remove(int index)
43
Q

What happens if you try to remove an object from an ArrayList that does not exist?

A

The method returns false

44
Q

What happens if you try to remove an object at an index that does not exist?

A

IndexOutOfBoundsException is thrown

45
Q

What is the signature for replacing an object in an ArrayList?

A

E set(int index, E newElement) // where the return value is the object that was replaced

46
Q

What’s the method for finding out if an ArrayList has any elements?

A

.isEmpty()

47
Q

What’s the method for finding out how many elements are in an ArrayList?

A

.size()

48
Q

What is the method for removing all elements from an ArrayList?

A

.clear()

49
Q

What’s the signature of the method that determines if an object is in an ArrayList?

A

boolean contains(Object object)

50
Q

How do you compare two ArrayLists?

A

.equals(myList) compares contents and order

51
Q

List all primitive wrapper classes

A
Boolean
Byte
Short
Integer
Long
Float
Double
Character
52
Q

How do you convert a String to an int?

A

Integer.parseInt(“123”)

53
Q

How do you convert a String to an Integer?

A

Integer.valueOf(“123”)

54
Q

How do you remove the Integer 1 from an ArrayList?

A
nums.remove(new Integer(1))
Otherwise, it removes the item at index 1 instead of the Integer object 1.
55
Q

How do convert an ArrayList of Strings to an array of Strings?

A

String[] strArray = list.toArray(new String[0]);

56
Q

If you convert an ArrayList to an array, are the two objects linked in any way?

A

No

57
Q

If you convert an array to an ArrayList, are the two objects linked in any way?

A

Yes

58
Q

What is the term for a list with an array linked to it?

A

A backed list

59
Q

How do you convert an array to a list?

A

List list = Arrays.asList(array);

60
Q

What are the properties of a list created with the method Arrays.asList(array)?

A
  • Backed by the source array (a backed list) - a change to one changes both
  • Fixed size - cannot add or remove elements
61
Q

How do you create and populate an ArrayList in a single line?

A

List list = Arrays.asList(“one”, “two”);

62
Q

How do you sort an ArrayList?

A

Collections.sort(list)

63
Q

What is the import statement for working with dates and time?

A

import java.time.*;

64
Q

What are the three objects used for storing date/time information without timezones?

A
  • LocalDate
  • LocalTime
  • LocalDateTime
65
Q

What information is stored in LocalDate?

A

Date information, no timezone or time. eg, Feb. 22nd 2002

66
Q

What information is stored in LocalTime?

A

Time information, no timezone or date. eg, 11:59PM

67
Q

What information is stored in LocalDateTime?

A

Date and time information, no timezone. eg, December 24th, 9pm.

68
Q

Which object is used for storing timezone information, and what is the recommendation for dealing with timezones?

A

ZonedDateTime, and Oracle suggests acting as if everyone is in the same timezone when you can.

69
Q

How do you get the current date and/or time from a time object?

A

.now()

70
Q

What is the format returned for LocalDateTime.now()

A

YYYY-MM-DDTHH:MM:SS.nnn

71
Q

How do you create a LocalDate object with a specific date?

A

LocalDate.of(2015, Month.JANUARY, 28), or

LocalDate.of(2015, 1, 28)

72
Q

How do you create a LocalTime object with a specific time?

A

LocalTime.of(h, mm [, ss, nnn])

73
Q

List two ways to create a LocalDateTime object

A

LocalDateTime.of(localDate, localTime)

LocalDateTime.of(yyyy, mm, dd, hh, mm)

74
Q

Can you use the new keyword on DateTime objects?

A

No, DateTime objects have private constructors

75
Q

Are DateTime methods mutable or immutable?

A

immutable

76
Q

How do you add time to a LocalDate object? (all methods)

A
date = date.plusDays(2);
date = date.plusWeeks(1);
date = date.plusMonths(1);
date = date.plusYears(5);
77
Q

How do you remove time from a LocalDateTime object?

A
dateTime = dateTime.minusDays(1);
dateTime = dateTime.minusHours(10);
dateTime = dateTime.minusSeconds(30);
78
Q

How do you compare two dates?

A

localDate1.isBefore(localDate2)

79
Q

What is java.time.Period and how do you use it?

A
* Designates a period of time, a day or longer, independent of a specific point in time (eg, "one month")
Period.ofYears(int)
.ofMonths(int)
ofWeeks(int)
ofDays(int)
of(m, d, y) // all three required
80
Q

What does this code do?

java.time.Period.ofYears(1).ofWeeks(1)

A

Creates a period of one week (Period cannot be chained, so ofWeeks overwrites ofYears)

81
Q

What is java.time.Duration?

A

Specifies a period of time, down to the nanosecond, independent of a specific point in time (eg, three minutes)

82
Q

Which objects and methods can you pass java.time.Period into?

A

LocalDate.plus()
LocalDateTime.plus()
Note - LocalTime.plus() will return an error, as Period works with larger units of time than LocalTime can handle

83
Q

Which LocalDate methods return parts of the date (day, month, etc)?

A
.getDayOfWeek // Monday
.getDayOfYear // 45
.getDayOfMonth // 14
.getMonth // February 
.getYear // 1984
84
Q

Which class allows you to define date and time formats?

A

java.time.format.DateTimeFormatter

85
Q

What are the key methods for DateTimeFormatter, and which kind of time objects does it work with?

A

.ofLocalizedDate(FormatStyle) // works with date and datetime objects
.ofLocalizedTime(FormatStyle) // works with date and datetime objects
.ofLocalizedDateTime(FormatStyle) // only works with datetime objects

86
Q

Which two Date and Time format singletons will be on the exam?

A

FormatStyle.SHORT

FormatStyle.MEDIUM

87
Q

List two ways to use the date time formatter

A

DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT).format(localizedDateObj);

localizedDateObj.format(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);

88
Q

What are the different symbols of the date formatter string?

A
M = Month
d = Day
y = Year
h = Hour
m = Minute
89
Q

How do you parse a String into a DateTime object?

A

DateTimeFormatter f = DateTimeFormatter.ofPattern(“MM dd yyyy”);
LocalDate date = LocalDate.parse(“01 02 2015”, f);
LocalTime time = LocalTime.parse(“11:22”);
System.out.println(date); // 2015-01-02
System.out.println(time); // 11:22