• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/67

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

67 Cards in this Set

  • Front
  • Back

Why are String objects immutable?

Because the value is stored as private final array of characters.

When is a new objects created for a String (describe 2 scenarios)

When a String is created using keyword new and when a String is created by other method and the passed value is not present in String Constant Pool

What are the 3 ways to create a String

1) Using keyword new; 2) using = sign; 3) using double quote marks around the value, for example passed to a method or in value comparison statements.

What is String Constant Pool and when is it populated

String Constant Pool (or String Pool) helps save memory resources as it stores all String objects created using assignment symbol and double quotes, without repeating them. So when a new String is created using those methods Java first checks whether an object with that value exists in String Pool already, and if it doesn't , the object is created and variable refers to it, otherwise the variable will point to an existing object with passed value.

Name at least 4 possible argument types for String() constructor

1) Text in double quotes 2) array of characters 3) StringBuffer object, 4) StringBuilder object 5) No arguments (which creates an empty string)



What String methods modify the String value?

None. String methods always return a new object with modified value of original String.

What type of argument does charAt() take in String class and what's a possible exception thrown?

charAt() takes any integer values (or values resolvable to int, e.g. char) and throws run-time exception if the index passed is greater than length of String minus 1

What type of argument does indexOf() in String class take and what's the output when the value is not found

indexOf() takes char and String values as arguments, and returns negative 1 if not found

What versions of substring() method exist and what's the possible exception thrown in String class?

substring() can take 1 integer for starting point and returns the rest of the string starting from that point; substring() can take 2 integers for starting and ending positions, and will return the characters not including the end position. Remember the length of returned element is end integer minus start integer. Run time exception will occur if start or end is out of bounds.

What does trim() method do in String class?

Trim() returns a String without leading and trailing spaces, including new lines and tabs

What versions of replace() method exist in String class?

replace() method takes 2 character arguments, or 2 String arguments, but not a mix of those two.

What arguments do startsWith() and endsWith() methods take in String class?

startsWith() and endsWith() take String arguments. Remember String startsWith() a String, and endsWith() a String. The value returned is Boolean.

How does method chaining work for Strings?

String methods chained are read from left to write.

Can you call methods on String constructor used with keyword new? If so, can that constructor have no arguments?

Yes to both. You can call new String() with or without arguments and call methods on those objects. If the argument is not passed to constructor, the code will still compile and no error will be thrown during runtime.

How does concatenation work for Strings with plus sign?

If at least one operand is a String, plus sign concatenates the values into a String. The evaluation of the statement starts on the left, so if the first operand is a String, all the other values will be concatenated together.

When does equals() method return true for comparison of Strings?

When a) it's the same object or b) when it's a String object and the sequence of characters is the same.

When does == (double equals) operator return true for comparison of Strings?

When the variables and/or literals compared refer to the same object. For example they were initialized using assignment operator or double quotes and the value is on String pool

What's the recommended way to compare String objects?

Use equals() method. the double = sign is not recommended due to its limitations.

What is the benefit of using StringBuilder objects instead of String?

StringBuilder objects are mutable. The char array is not final, so it can be modified, which saves on the amount of created objects.

Describe 4 StringBuilder constructors and objects they create

1) No arguments - empty array with default 16 capacity. 2) StringBuilder argument - same array and capacity as passed object. 3) Integer - sets initial capacity to empty array of characters. 4) String or String literal - sets value to value of String and capacity equals length of string + 16 of buffer.

What argument does StringBuilder indexOf() method take?

Only string and optionally a second argument - integer. Char throws compilation error.

What arguments does StringBuilder append() method take

All 8 primitive types, array of characters, String, StringBuffer and any Java API object

What is the default implementation of toString() method in Java objects

Name of the class followed by @ (at) sign followed by hexadecimal representation of the hash of the object.

What arguments does StringBuilder insert() method take?

The first argument is always the offset integer, the second argument could be a primitive value to insert, a String, StringBuffer, array of Characters, CharSequence object, or any other Java object. For array of Characters the optional following arguments are startPosition and length of subarray.

What methods delete characters from StringBuilder objects and how do they work?

delete() method takes 2 integers and works like substring() method, not removing the endPosition character. deleteCharAt() method takes a single integer and removes the character at that position.

What does trim() method do for StringBuilder objects?

There is no method trim() defined for StrinbBuilder class

What is the difference between functionality of most String methods and StringBuilder methods?

String methods do not modify the String object they are called on, however, calling a method on a StringBuilder object, will modify that particular object if they return StringBuilder type.

How is StringBuilder replace() method different from the same method in String class?

It takes 2 integers for start and end position of replacement with the provided String as third argument. The String can be longer than the removed characters between positions. The end position index is not touched.

How does StringBuilder subSequence() method work?

It takes start and end position integers as subString(), but returns an object of type CharSequence. It doesn't modify the original object.

What is the difference between StringBuffer and StringBuilder and when you should use either?

StringBuffer is defined with the same methods as StringBuilder, but they are synchronized, which means they can be accessed by multiple threads, but that leads to overhead. If your code needs access from multiple threads, use StringBuffer, otherwise, it's simpler to stick to StringBuilder.

What is the difference between an array of primitives and an array of objects?

An array of primitives stores the actual values of the primitives, while an array of objects stores references to those objects (their heap addresses). In both cases an array itself is an object.

How does an array store its values spacewise?

It's a big benefit of the array type, that it stores its values in continuous manner, so they are next to each other.

Explain mutlidimensional arrays

An x-dimensional array is a collection of arrays with x minus one dimensions. So a three-dimensional array is a collection of two-dimensional arrays.

What steps are involved in creating an array?

Declaration (type and variable with a single or double set of brackets at any position), Allocation of memory, and Initialization

When and how does allocation happen for arrays

Arrays cannot be allocated during declaration (it will cause compilation error). Allocation happens using the keyword new and defines the size of array which is immutable and must evaluate to an integer. If size is missing or placed on the left of assignment sign, the code will not compile. For multidimensional array at least the first pair of brackets should have array size. Once allocated arrays store default values for objects of those classes.

What's the difference between length in terms of array and String?

length() is a method for String objects, length is an instance variable for array objects.

How can an array be initialized?

1) Array values can be initialized using a loop, including nested loops for multidimensional arrays, 2) Array values can be initialized individually using indexes in square brackets.

Name two ways all three steps of array creation can be combined on one line?

1) Array type, variable name, either or both being followed by square brackets, assignment sign, array values listed between curly braces.
2) Array type, variable name, either or both being followed by square brackets, assignment sign, keyword new, array type, square brackets, array values listed between curly braces. Size is not specified for either approach, rather calculated from the provided values.


What can be said about elements of array assigned to null

Those elements have no value assigned, however, the element does exist, but it's initialized to null. On the other hand of no element is initialized at a position, no value exists at that position, so run-time exception would occur if indexed.

Describe how an array can be of type Interface, Abstract Class, or Object.

1) an array can store objects of classes implementing the specified interface, 2) an array can store objects extending abstract classes, 3) an array can be declared with type Object, and store any type of Java objects, including an array of other objects/primitives.

What methods are available for array objects?

clone() method returns an array with the same type, and all other methods of java Object class.

What is the benefit of using ArrayList instead of an array?

ArrayList stores an expendable collection of objects, so you can add, delete and modify the array, without reinventing the wheel.

How does an ArrayList get created and what happens behind the scenes?

To create an ArrayList use keyword ArrayList with type of ArrayList in angular brackets and use keyword new on the right side of the assignment sign followed by the ArrayList contructor and optionally ArrayList type in angular brackets. On the backend an array gets created, but through methods it may be reduced and increased in size, which makes it dynamic.

Do you need to specify size of ArrayList to allocate memory?

No, if the constructor with no arguments is used, by default the array gets capacity of 10, or you can optionally specify initial size as an integer during creation of ArrayList.

How does add() method work for ArrayList?

If no particular position is specified, first it checks that capacity of the underlying array has an available spot at the end. If not, it uses ensureCapacity(x) method to create a new array with more capacity and places the value at the first spot where no value exists. Adding a particular position creates a new array and shifts values if necessary to allow the new value in.

What are the 2 common ways to access elements of an ArrayList?

1) Enhanced for loop, which is build using type of array, variable referring to each element, colon sign and ArrayList variable. 2) ListIterator, created by calling listIterator() method on the ArrayList, then hasNext() and next() methods are used to access the elements in a while loop. listIterator allows removing the accessed item, while for-loop doesn't.

How does ArrayList remove() method work?

Remove() method takes either the index of the position or an object as arguments. In case of an object, the method uses equality to identify what to remove, for example if a variable is passed, it checks what element of the array references the same object, or in case of a constructor, it identifies whether there's an element equal to the constructed argument. This method removes and returns the the first occurrence of specified element. The method returns true if the value was present and false if the value wasn't there.

How can you modify ArrayList elements?

Access the element using for-loop or listIterator, then modify it, or use set() method with element index passed as integer. Set is used to MODIFY elements, meaning there has to be an element in the spot. (you can't use index out of bounds of ArrayList size)

What happens when an object referenced by the ArrayList is modified?

If the object modified is referenced through the array or variable referencing the same object, the modifications are visible to the ArrayList. It becomes tricky when another ArrayList references part or all of the first ArrayList.

How can multiple elements be added to an ArrayList at once?

Using another ArrayList and addAll() method, with startposition integer optional.

What methods are used to delete all elements of an ArrayList?

clear() method with no arguments.

How do contains(), indexOf() and lastIndexOf() methods work in ArrayList?

They all use equals() method to identify equality between the passed object and any element of the ArrayList. Be careful with how equals() method is defined for each class, or you can override them to work differently. These methods take type object, so will almost always compile.

How does clone() method work for ArrayList?

Clone() returns a new object of type Java Object and needs to be cast to ArrayList of whatever type to be assigned to a reference variable. The object is different from the one referenced by the original ArrayList. However, the individual elements still reference the same objects, they were not copied.

What does the toArray() method do in ArrayList class?

It creates an array, copies the elements of the ArrayList to it and returns the new array. However, the references to individual element objects are preserved in the returned array, so modifications to them will be visible to both the new array and the original ArrayList.

How do you override equals() method in a class?

You have to keep the return type, parameter type and name of the method the same to override it from default Java Object method. Otherwise, it will not be used during comparison of equality in other methods, like contains() for example. You should also use the contract for the equals method. It is recommended to override hashCode() Method along with equals()

List the rules of contract for equals() Method

The equality relationship between non-null object references is 1) reflexive (equality to self), 2) symmetric (bidirectional equality), 3) transitive (shared between 3 or more objects), 4) consistent (on multiple invocations) 5) non empty, meaning the equality to null is false.

Can an array of characters be assigned to an array of integers?

No, because arrays are objects, and an array of characters can only be assigned to a variable of the same type, which is characters.

Does specifying large capacity of ArrayList initially improve efficiency with frequently expanded ArrayLists?

Yes, that way the ArrayList won't have to keep copying arrays when you add elements to it (at least for a while)

Can you use ArrayList objects without importing any packages into the class?

No, you have to either import all classes from java.util, or just java.util.ArrayList.

If ArrayList 1 has been expanded using addAll() from ArrayList 2, what would happen if one of the elements in ArrayList 2 is modified?

If the element is modified, but the object referenced stayed the same (for example in case of StringBuilder), ArrayList 1 will also see the modification. However, if the element was modified being replaced by a new object, then ArrayList 1 will not see that change.

If array 1 was assigned an existing array 2 object, what happens when values in either are changed (they both store primitives)?

If it's an array of primitives, then changes will be visible to both, because the 2 variables now reference the same array object.

Can you use remove() method on ArrayList that hasn't been initialized?

Yes, if you use the remove (object) method, and not remove with index of position. If you attempt to use remove with index on an ArrayList that hasn't been initiated or that is out of bounds, you'll get runtime exception - indexoutofbounds.

What happens to the ArrayList elements when you remove an element in the beginning of middle of the ArrayList?

The rest of elements shift leftwards to fill in the empty spot. Indexes shift along with that.

Can a character object be assigned to a String varible?

No. Causes compilation error.

Can an array of integers be cast to an array of Strings or Objects directly?

No. This code will not compile

Can an array of integers be cast to an array of Strings or Objects indirectly (meaning throw assignment to a variable of type Object first)?

Yes, the code will compile, but it will throw ClassCastException during runtime.

Is it correct to say that "all string literals are automatically instantiated into a string object"

Yes, according to tests online.