Chapter 3: Core Java APIs Flashcards
Covers using operators, Arrays, Strings & StringBuilder, ArrayList, classes from java.time & wrapper classes
What are the features of an ArrayList
1) its an ordered list 2) Dynamic in size 3) Allows duplicates
What must you do before using the ArrayList class?
you must import the ArrayList class using either : import java.util.*; (imports entire package) OR import java.util.ArrayList; (imports just the class)
How many ways can you create an ArrayList for the purposes of the exam?List them
You can use 3 different constructors ArrayList list = new ArrayList( ); // sets elements to their default values ArrayList list2 = new ArrayList(10); // set the initial capacity ArrayList list3 = new ArrayList(list2); // creating a copy of another list. This constructor takes a collection as a param.
What interface does List extend? How would you create a new type of this interface
Interface extends Collection. Collection collection = new ArrayList( ) OR Collection collection2 = new ArrayList<>( );
Name all the methods for the ArrayList (only the ones you need to know for the exam).
add( ) , set( ) remove ( ) , contains ( ) equals ( ) , clear ( ) IsEmpty ( ) , size ( )
Will this compile? Explain.
ArrayList list = new ArrayList <>( );
list.add(Boolean.TRUE);
No because we are using generics to specify that this list only accepts type String which Boolean.TRUE isn’t.
Will this compile? Explain.
ArrayList list = new ArrayList( );
list.add(Boolean.TRUE); list.add(“hawk”);
Yes, as we haven’t specified a type for this list, we can add any object.
What does this code do?
List <string>birds = new ArrayList<>( );</string>
birds. add(“hawk”);
birds. add(1, “robin”);
it adds “hawk” to the end of the birds list and then adds “robin” to index 1 of the list which is after “hawk”
What will be the output of this code? List<> list1 = new ArrayList( ); list1. add(“hello”)
It won’t compile as the diamond operator on the RHS is empty. it should have a type declared inside
What are the method signatures for remove( ) method of an arrayList?
boolean remove(Object) // returns a boolean when an element is passed in. AND E remove(int index) // removes an element at the set index
In arrayList, what is the method signature for set( )? what does this method do?
E set (index, newElement). set( ) replaces an element at the index provided with the new element passed in
What do the IsEmpty( ) and size( ) methods of ArrayList return when called independently?
A boolean and an int
What does ArrayLists clear( ) method do?
clear () empties all the contents of the list
what does the ArrayList contains( ) do?
it checks to see whether the list has the specified value and returns a boolean
what is the output of this code?
List<string> one = new ArrayList<>( );</string>
List<string> two = new ArrayList<>( );</string>
one. add(“a”); one.add(“b”);
two. add(“a”);
two. add(0, “b”);
System.out.println(one.equals(two));
false. because for the equals to return true, the size and the order of the list has to be the same
What does the equals() method do?
it checks whether the two lists contain the same elements and in the same order.
What is the method for converting a String into an int primitive?
parseInt( ) e.g : int primitive = Integer.parseInt(“123”)
What is the method for converting a String into an Integer wrapper class?
valueOf( ) e.g: Integer wrapper = Integer.valueOf(“123”)
How do you convert a String into a boolean primitive and a float primitive?
Boolean.parseBoolean(“true”); Float.parseFloat(“2.2”);
How do you convert a String into a Boolean and Float wrapper class?
Boolean.valueOf(“true”); Float.valueOf(“2.2”);
How do you convert a String into a Character wrapper class
No such method exists, you just call charAt normally.
What is Autoboxing?
This is when Java automatically converts a primitive into its relevant wrapper class. This is great as ArrayLists aren’t allowed to contain primitives
What is unboxing?
When you convert a wrapper into a primitive
What happens on line 1 & 2 of this code? List heights = new ArrayList<>( ); heights. add(null); // 1 int h = heights.get(0); // 2
// 1 compiles fine as you can assign null to any variable reference. // 2 will result in a NullPointerEx as you are calling a method on null
What is the output of this code? List = new ArrayList<>( ); numbers. add(1); numbers.add(2); System.out.println(numbers.remove(1));
1 is the output! As you recall the remove method also takes the index as a param thus this is invoked instead of autoboxing.
How do you convert an ArrayList into an array?
ArrayList has a method called “toArray” that you can call on a list. You can then only assign that value to an object array unless you explicitly specify the type of array.
Example of converting a list into an array and specifying the type of the array.
String[] array = list.toArray( new String [0]); Even though the code says to create an array of size 0, java will figure it out and create a new array to fit the actual size of the array.
How do you convert an Array into a List?
you can use the Arrays.asList( ) method that takes the original array as a paramater.
How do you sort an ArrayList?
Using Collections.sort( list )
What happens when you convert an Array into a list?
1)The list remains fixed meaning you can’t change its size 2)Any changes you make to the list using set() will also update the original array as both refs are pointing to the same data
What interface does ArrayList implement? How do you create a new type of this interface?
ArrayList implements List. List list = new ArrayList( ) OR List list2 = new ArrayList<>( );
Post Java 5, how else can you create an ArrayList? Give two variations of this.
Using generics which allow you to specify the “type” you want to store in the list. It uses the diamond operator e.g :
1) ArrayList list1 = new ArrayList( );
2) ArrayList<boolean> list2 = new ArrayList< >( );</boolean>
What are the string concatenation rules?
- When used with numbers it is used for addition.
- If used with Strings/booleans, + means concatenation, The expression is evaluated left to right.
3) When used with += it is added on the end of a string
What would be the output of this code? int one = 1, two = 2, four = 4; String three = “3”; System.out.println( one + two + three + four);
output is 334. this expression is evaluated from left to right, 1 & 2 are added together to get 3 then “3” & 4 are added to the end of 3.
1)System.out.println(1 + 2); 2)System.out.println(“a” + “b”); 3)System.out.println(“a” + “b” + 3); 4)System.out.println(1 + 2 + “c”);
1) 2 2) ab 3) ab3 4) 2c
What is the signature for the charAt method?
char charAt(int index) - allows you to find out what character is at the specific index in a string. Note: if you call an index that is beyond the string, you get a StringIndexOutOfBoundsException
What is the signature for the indexOf method?
int indexOf() returns the index of a string and can take various parameters ; it can work with indv characters or the whole string
What does the indexOf method do and what is the signature?
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
What is the method signature of String’s substring and what does it do?
subString(int beginIndex) Or
subString(int beginIndex, int endIndex)
This method takes one or two indexes and returns part of a string. The end index is exclusive which means that it isnt included in the output and the counting stops right before that index.
What is the output on each line of code?
String String = “Hello World!”; System.out.println(String.substring(3)); //1
System.out.println(String.substring(String.indexOf(“ W”))); //2
System.out.println(String.charAt(8)); System.out.println(String.substring(6,6)); // 3
System.out.println(String.substring(6,12)); // 4
System.out.println(String.substring(6,12)); // 5
System.out.println(String.substring(5,13)); //6
System.out.println(String.trim()); // 7
1) lo World
2) World// a space then world
3) nothing will be printed as 6 is exclusive
4) World
5) World!// eventhough there are only 11 characters, it is legal to access up 1 past the end of the sequence which in this case is 12.
6) StringIndexOutOfBoundsException
7) Hello World!// trim removes spaces before and after but not in the middle thus the string will remain unchanged
What does it mean that Strings are immutable?
It means that once created, they can’t be changed, you can add or remove things
What is the string pool?
Storage in the jvm that holds string literals e.g “hello”
What is the difference between == and Strings equals method?
== checks for the equality of references so it will only return true if the references are pointing to the same object
equals returns true if the contents inside the string are the same. The two references don’t have to be pointing to the same object.
True or False:
String x1 = “a”;
String x2 = new String(“a”);
x1.equals(x2). // 1
x1 == x2 // 2
1) True because even though x1 and x2 are pointing to different objects, the content of the objects are same
2) False as the references are pointing to different objects