















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
















◉ What package is the ArrayList class part of? Answer: The ArrayList class is part of the java.util package. ◉ What is the syntax to create a new ArrayList? Answer: ArrayList list = new ArrayList(); ◉ What does the 'E' in ArrayList represent? Answer: 'E' is a type parameter that indicates the type of elements stored in the ArrayList. ◉ What is the purpose of the 'Diamond Operator' in ArrayList? Answer: The Diamond Operator allows for shorter syntax by omitting the element type on the right side of the declaration. ◉ What does the size method in ArrayList do? Answer: The size method returns the current number of elements in the ArrayList.
◉ How does the add method work in ArrayList? Answer: The add method appends the specified element to the end of the list. ◉ What happens when you call the remove method in ArrayList? Answer: The remove method deletes the element at the specified index and shifts subsequent elements left. ◉ What does the get method do in ArrayList? Answer: The get method retrieves the element at the specified index. ◉ What is the purpose of the clear method in ArrayList? Answer: The clear method removes all elements from the ArrayList. ◉ What does the contains method check in ArrayList? Answer: The contains method checks if a specific value exists in the ArrayList and returns a boolean. ◉ What does the indexOf method return? Answer: The indexOf method returns the index of the first occurrence of a specified value, or - 1 if not found.
◉ How does the remove method preserve order in ArrayList? Answer: The remove method shifts elements left to fill the gap left by the removed element. ◉ What does the size method return if the ArrayList is empty? Answer: The size method returns 0 if the ArrayList is empty. ◉ What happens if you try to add an element of a different type to an ArrayList? Answer: It generates a compiler error because ArrayLists are type- specific. ◉ What is the purpose of the overloaded version of the add method? Answer: It allows inserting an element at a specific index in the ArrayList. ◉ What does the ArrayList remove method require as a parameter? Answer: The remove method requires an integer index as a parameter. ◉ What is the return type of the indexOf method if the value is not found? Answer: The indexOf method returns - 1 if the value is not found.
◉ What is the main advantage of using ArrayList over regular arrays? Answer: ArrayLists can dynamically resize, allowing for flexible storage of elements. ◉ What is the purpose of the add method in an ArrayList? Answer: To add an element to the ArrayList. ◉ What does the example code 'words.add("four")' do? Answer: It adds the string 'four' to the ArrayList named words. ◉ What is a common pitfall when inserting elements in an ArrayList? Answer: Inserting elements without considering the shifting of existing elements can lead to infinite loops or incorrect placements. ◉ What happens when you insert an element at index 0 in an ArrayList? Answer: All existing elements shift one position to the right. ◉ What is the output of the code 'System.out.println("words = " + words);'
Answer: Use increments of 1 in the loop to ensure you are removing the correct elements. ◉ Why does the ArrayList shift elements dynamically? Answer: Because it is designed to maintain the order of elements as items are added or removed. ◉ What is the significance of using 'i += 2' in the insertion loop? Answer: It prevents the loop from getting stuck in an infinite insertion cycle by accounting for the shifting of elements. ◉ What does the term 'dynamic structure' refer to in the context of an ArrayList? Answer: It refers to the ability of the ArrayList to grow and shrink as elements are added or removed. ◉ How does inserting an element at index 0 affect subsequent elements? Answer: It shifts all subsequent elements one position to the right. ◉ What is the problem with removing elements at even indexes in a dynamic ArrayList? Answer: Removing an element shifts the remaining elements left, causing the next element to be skipped.
◉ What is the output of the ArrayList after removing elements correctly? Answer: The output will show only the elements that were not removed, maintaining their original order. ◉ What is the syntax to create an Array in Java? Answer: [] = new []; ◉ How do you set an element in an Array? Answer: [] = ; ◉ What is the syntax to create an ArrayList in Java? Answer: ArrayList<> = new ArrayList<>(); ◉ How do you add an item to an ArrayList? Answer: .add(""); ◉ What is a quicker way to initialize an array from a list of elements? Answer: [] = {, , ...};
Answer: A class that 'wraps' (stores) primitive data as an object. ◉ What is the purpose of boxing in Java? Answer: Automatic conversion from primitive data to a wrapped object. ◉ What is unboxing in Java? Answer: Automatic conversion from a wrapped object to its corresponding primitive data. ◉ What is the wrapper class for the primitive type int? Answer: Integer ◉ How do you retrieve the primitive int value from an Integer object? Answer: Call the method intValue() on the Integer object. ◉ What happens when you try to modify a list while using a for-each loop? Answer: It throws a ConcurrentModificationException.
◉ What is the output of the following code? ArrayList list = new ArrayList<>(); list.add(13); list.add(47); int sum = 0; for (int n: list) { sum += n; } Answer: list = [13, 47], sum = 60 ◉ What is the wrapper class for the primitive type double? Answer: Double ◉ What is the wrapper class for the primitive type char? Answer: Character ◉ What is the wrapper class for the primitive type boolean? Answer: Boolean ◉ What is the wrapper class for the primitive type long? Answer: Long ◉ What method is used to sort an ArrayList in Java? Answer: Collections.sort ◉ Why can a list of String objects be sorted but not a list of Point objects?
◉ What is the syntax for the Comparable interface? Answer: public interface Comparable { public int compareTo(T other); } ◉ How does the compareTo() method compare two Strings? Answer: It compares them alphabetically based on their character values. ◉ What is the output of x.compareTo(y) if x is 'hello' and y is 'world'? Answer: - 15, indicating 'hello' comes before 'world' alphabetically. ◉ Can relational operators be used to compare objects in Java? Answer: No, relational operators cannot be used on objects. ◉ What is the correct way to compare two Strings in Java? Answer: Using the compareTo() method, e.g., s1.compareTo(s2) < 0. ◉ What is the relationship between primitive data comparison and object comparison in Java? Answer: Primitive data uses relational operators, while objects use the compareTo() method.
◉ What does the Java Language Specification guarantee about boxing and unboxing? Answer: It guarantees that boxing and unboxing will work for values of n between - 128 and 127. ◉ What is the significance of the values returned by the compareTo() method? Answer: They indicate the relative order of the objects being compared. ◉ What happens if you try to compare objects using relational operators? Answer: The code will not compile. ◉ What is the output of compareTo() if two Strings are equal? Answer: 0, indicating that they are equal. ◉ What is the main purpose of the Comparable interface? Answer: To allow objects to be compared and sorted based on their natural ordering. ◉ What does it mean if a type does not have a natural ordering? Answer: It means there is no agreed-upon way to compare or sort instances of that type.
Answer: You need to include the interface name after the class name, e.g., 'class CalendarDate implements Comparable'. ◉ What method must be included when implementing the Comparable interface? Answer: The method 'public int compareTo(T other)'. ◉ What canonical values does compareTo return? Answer: - 1, 0, or 1, corresponding to the signum function. ◉ Why doesn't Java require that compareTo returns only - 1, 0, or 1? Answer: Java lacks a convenient ternary type, so any negative number can indicate less than, and any positive number can indicate greater than. ◉ What is the advantage of allowing flexible return values in compareTo? Answer: It reduces the need for error-checking code and allows for faster comparison methods. ◉ How does the String class compare two strings? Answer: It uses lexicographic order, comparing characters until a difference is found.
◉ What happens if no differing characters are found when comparing strings? Answer: The method returns the difference between the lengths of the two strings. ◉ What is the pseudocode behavior of compareTo for the String class? Answer: Search for differing character pairs; return the difference between them or the lengths if none differ. ◉ Why is efficiency a reason for not specifying strict return values in compareTo? Answer: Less strict rules allow for faster implementations of compareTo methods. ◉ What is a common use case for the compareTo method? Answer: String comparison, which is one of the most frequently called methods. ◉ What does the compareTo method return when two strings are of the same length and identical? Answer: It returns 0. ◉ What is the significance of the Comparable interface in Java?
◉ How does the Comparable interface impact the design of custom classes? Answer: It allows custom classes to be sorted and compared like standard Java classes. ◉ What is an example of a class that commonly implements Comparable? Answer: The String class. ◉ What does the ArrayList class in Java represent? Answer: A growable list of objects implemented using an array. ◉ How are elements in an ArrayList indexed? Answer: Elements have a zero-based index. ◉ What is a generic class in Java? Answer: A class that accepts a data type as a parameter when created, like ArrayList. ◉ What operations can be performed on an ArrayList? Answer: Operations include add, remove, get, set, clear, and toString. ◉ How can you search for elements in an ArrayList?
Answer: Using methods named contains, indexOf, and lastIndexOf. ◉ Can you modify an ArrayList during a for-each loop? Answer: No, the list cannot be modified during the execution of the for-each loop. ◉ What wrapper types must be used for primitive values in an ArrayList? Answer: Wrapper types such as Integer for int and Double for double. ◉ What does the Comparable interface do? Answer: It defines a natural ordering for the objects of a class. ◉ How can you sort objects in an ArrayList? Answer: By implementing the Comparable interface and writing a method compareTo. ◉ Which common types implement the Comparable interface? Answer: Common types include String and Integer. ◉ What is the purpose of the ArrayList's size management?