Core Java APIs Flashcards Preview

Java OCA > Core Java APIs > Flashcards

Flashcards in Core Java APIs Deck (77)
Loading flashcards...
1
Q

What does 3 + “c” result in?

A

“3c” (a String)

2
Q

Is String mutable or immutable?

A

immutable

Soooo you can concat it with + but there aren’t any String methods to change them.

HOWEVER, you can set it equal to something else to make it point to that instead. It won’t change the original object though, just create a new one

3
Q

string pool

A

An area in the JVM where string literals are stored. The JVM can optimize the use of string literals by allowing only one instance of a string in the pool.

4
Q

What is the difference between
String name = “Fluffy”;
and
String name = new String(“Fluffy”);

A

The first puts “Fluffy” in the string pool, and the second does not. The second one is less efficient, so do the first.

5
Q

mystring.length()

A

Returns the length of mystring

6
Q

mystring.charAt(i)

A

Returns the character at the i-th index of mystring

7
Q

mystring.indexOf(c)

A

Finds the first index of mystring that matches the char or String c

8
Q

mystring.indexOf(c, i)

A

Finds the first index of mystring that matches c starting from index i

9
Q

What does indexOf() return if it can’t find a match?

A

-1

10
Q

mystring.substring(x)

A

Returns the substring of mystring starting at index x until the end

11
Q

mystring.substring(x,y)

A

Returns the substring of mystring starting at index x and ending at index y (meaning don’t actually include the character at index y)

String s = “funtimes”;
return s.substring(3,7);

will return “time”

12
Q

mystring.toLowerCase()

A

Returns mystring as all lowercase

does not actually change the String

13
Q

mystring.toUpperCase()

A

Returns mystring as all uppercase

does not actually change the String

14
Q

mystring.equals(yourString)

A

Returns true if mystring and yourString contain exactly the same characters in the same order. Otherwise returns false.

15
Q

mystring.equalsIgnoreCase(yourString)

A

Like mystring.equals() but ignores case

16
Q

mystring.startsWith(str)

A

Returns true if mystring starts with str, otherwise returns false

17
Q

mystring.endsWith(str)

A

Returns true if mystring ends with str, otherwise returns false

18
Q

mystring.contains(str)

A

Returns true if mystring contains str, otherwise returns false

19
Q

mystring.replace(str1, str2)

A

Returns mystring with str1 replaced by str2

Note: Can use char or String parameters

20
Q

mystring.trim()

A

Returns mystring with white space removed from the beginning and end

21
Q

StringBuilder

A

Basically the mutable version of String!

22
Q

myStringBuilder.append(c)

A

Appends the character c to myStringBuilder

23
Q
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder("animal");
StringBuilder sb3 = new StringBuilder(10);
A

Create empty StringBuilder object

Create StringBuilder object with “animal” as it’s value

Create StringBuilder object with space for ten characters

24
Q

myStringBuilder.charAt()
myStringBuilder.indexOf()
myStringBuilder.length()
myStringBuilder.substring()

A

All the same as in the String class

25
Q

myStringBuilder.insert(n, str)

A

Inserts str at the n-th index of myStringBuilder

26
Q

myStringBuilder.delete(i, j)

A

Deletes characters from myStringBuilder starting at index i and stopping before index j

27
Q

myStringBuilder.deleteCharAt(i)

A

Deletes the character of myStringBuilder at the i-th index

28
Q

myStringBuilder.reverse()

A

Reverses myStringBuilder

29
Q

myStringBuilder.toString()

A

Converts myStringBuilder to a String

30
Q

How to declare an array of 3 ints

A

int[] nums = new int[3]

31
Q

How to delcare an array of 3 specific ints

A

int[] nums = new int[] {1, 2, 3}

or shorter

int[] nums = {1, 2, 3}

32
Q

4 ways to put the [] when declaring an array

A

int[] myArray
int [] myArray
int myArray[]
int myArray []

33
Q

myArray.length

A

Returns the number of elements in myArray

34
Q

What must you import to use the Arrays class?

A

import java.util.*
or
import java.util.Arrays

35
Q

int[] nums = {6, 9, 1};

Arrays.sort(nums);

A

Sorts nums in ascending order

36
Q

What are the rules of numbers and letters when sorting an array?

A

Numbers before letters, and uppercase before lowercase

37
Q

int[] numbers = {2,4,6,8};
System.out.println(Arrays.binarySearch(numbers, 2));
System.out.println(Arrays.binarySearch(numbers, 4));
System.out.println(Arrays.binarySearch(numbers, 1));
System.out.println(Arrays.binarySearch(numbers, 3));
System.out.println(Arrays.binarySearch(numbers, 9));

A
0
 1
-1
-2
-5

Array must be sorted for this to work!

38
Q

How to import ArrayList?

A

import java.util.ArrayList;

39
Q

3 ways to declare an ArrayList (old way)

A
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList(10);
ArrayList list3 = new ArrayList(list2);
40
Q
ArrayList list = new ArrayList();
list.add("word");
A

Adds “word” to the ArrayList

41
Q

How to add an item at a specific index in an ArrayList called list

A

list.add(idx, item)

42
Q

How to remove an item from an ArrayList called list

A

list.remove(item) //returns true if item is in the list, and false if not
or
list.remove(index) //returns the item at that index

43
Q

How to set a specific index with an element in an ArrayList called list

A

E set(int index, E newElement)

list.set(i, item)

returns the element that got replaced. Keeps the ArrayList the same size

44
Q

ArrayList list = new ArrayList();

list. isEmpty();
list. size();

A

Checks if list is empty (returns a boolean)

Then determines number of elements in list (returns an int)

45
Q
ArrayList list = new ArrayList();
list.clear()
A

Removes all elements of list

No return

46
Q
ArrayList list = new ArrayList();
list.contains(obj);
A

Checks whether obj is in list.

Returns a boolean

47
Q
ArrayList list1 = new ArrayList();
ArrayList list2 = new ArrayList();

list1.equals(list2)

A

Checks if list1 and list2 have the same elements in the same order

48
Q

Converting String to a primitive (lets say int for this example)

A

Integer.parseInt(“10”);

49
Q

Converting String to a wrapper class (lets say Integer for this example)

A

Integer.valueOf(“10”);

50
Q

Autoboxing

A

The compiler automatically converts a primitive type into the matching wrapper class and vice versa.

51
Q

Wrapper Class

A

Integer for int,
Boolean for boolean,
etc.

52
Q

Convert from an ArrayList of Strings to an array of Strings

ArrayList list = new ArrayList;

A

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

The “0” says to pick the same size as the list

Without specifying “new String[0]” the array will be of type Object[]

53
Q

Convert from an array of Strings to a List of Strings

String[] array = {“hawk”, “robin”};

A

List list = Arrays.asList(array);

Once it is a List, you cannot change its size

54
Q

How to sort an ArrayList

ArrayList list = new ArrayList();

A

Collections.sort(list);

55
Q

What import statement must you use to work with date and time?

A

import java.time.*

56
Q

LocalDate

A

A class that deals with just the date, no time

57
Q

LocalTime

A

A class that deals with just the time, no date

58
Q

LocalDateTime

A

A class that deals with both date and time

59
Q

If the date is June 20th, 2016 at 12:30 pm, what is the output?

System.out.println(LocalDate.now());
System.out.println(LocalTime.now());
System.out.println(LocalDateTime.now());

A

2016-06-20
12:30:00.000
2016-06-20T12:30:00.000

60
Q

Create a LocalDate object for June 20th, 2016

A

LocalDate date1 = LocalDate.of(2016, Month.JUNE, 20);
or
LocalDate date2 = LocalDate.of(2016, 6, 20);

61
Q

Create a LocalTime object for 12:30:00 pm

A

LocalTime time = LocalTime.of(12, 30, 0);

Can add one more parameter to specify nanoseconds, or one parameter less to not specify seconds

62
Q

What is wrong with this?

LocalDate d = new LocalDate();

A

You can’t create a LocalDate, LocalTime, or LocalDateTime object in this fashion. You gotta use of() or now() or whateva

63
Q

How to add 3 days, 3 weeks, 3 months, or 3 years to a LocalDate object called date

A

date = date.plusDays(3);

date = date.plusWeeks(3);

date = date.plusMonths(3);

date = date.plusYears(3);

64
Q

How to go back 3 days, 3 hours, or 3 seconds to a LocalDateTime object called dateTime

A

dateTime = dateTime.minusDays(3);

dateTime = dateTime.minusHours(3);

dateTime = dateTime.minusSeconds(3);

65
Q

Period Object

A

An objet that can hold a certain period of time

Period annually = Period.ofYears(1); // every 1 year
Period quarterly = Period.ofMonths(3); // every 3 months
Period everyThreeWeeks = Period.ofWeeks(3); // every 3 weeks
Period everyOtherDay = Period.ofDays(2); // every 2 days
Period everyYearAndAWeek = Period.of(1, 0, 7); // every year and 7 days

66
Q

How to add a Period called period to a LocalDate object called date

A

date = date.plus(period);

67
Q

Can you chain Period methods?

A

no!

Period wrong = Period.ofYears(1).ofWeeks(1);

is the same as

Period wrong = Period.ofYears(1);
wrong = Period.ofWeeks(1);

68
Q

What package is the class DateTimeFormatter in?

A

java.time.format

69
Q

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)

Tell whether the following are legal and what they output

1) f.format(localDate);
2) f.format(localDateTime);
3) f.format(localTime);

A

1) Legal, shows whole object localDate
2) Legal, shows just the date of localDatetime
3) Throws runtime exception

70
Q

DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)

Tell whether the following are legal and what they output

1) f.format(localDate);
2) f.format(localDateTime);
3) f.format(localTime);

A

1) Throws runtime exception
2) Legal shows whole localDateTime object
3) Throws runtime exception

71
Q

DateTimeFormatter f = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)

Tell whether the following are legal and what they output

1) f.format(localDate);
2) f.format(localDateTime);
3) f.format(localTime);

A

1) Throws runtime exception
2) Legal shows only time part
3) Legal shows whole localTime object

72
Q

What method do you use to create your own DateTimeFormatter pattern?

A

ofPattern()

73
Q

When specifying a DateTimeFormatter pattern, what do the following mean?

1) M
2) MM
3) MMM
4) MMMM
5) d
6) dd
7) yy
8) yyyy
9) h
10) hh
11) m
12) mm

A

1) Represents month as a single digit (or two if 10-12)
2) Represents month as two digits
3) Represents the three letter abbreviation of the month
4) Represents the entire month name
5) Represents day with its default amount of digits
6) Represents day as a two digit number
7) Represents year as a two digit number
8) Represents year as a four digit number
9) Represents hour as its default amount of digits
10) Represents hour as a two digit number
11) Represents minutes as default amount of digits
12) Represents minutes as two digits

74
Q

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

A

Parses date using the formatter f

Then parses time using the default formatter for time

75
Q

Difference between calling equals() on a String and calling equals() on a StringBuilder

A
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.
76
Q

Difference between calling equals() on an array and calling equals() on an ArrayList

A

Calling equals() on an array checks object reference equality

Calling equals() on an ArrayList checks to see if the ArrayLists have the same elements in the same order

77
Q

Can you append a boolean value to a String?

A

yup