Docsity

BUILDING JAVA PROGRAMS CERTIFICATION EVALUATION EXAMS SET FULL QUESTIONS AND COMPLETE SOLU, Exams of Java Programming

Toggle sidebar
bg1
BUILDING JAVA PROGRAMS
CERTIFICATION EVALUATION EXAMS SET
FULL QUESTIONS AND COMPLETE
SOLUTION
●● 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<E> list = new ArrayList<E>();
●● What does the 'E' in ArrayList<E> 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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download BUILDING JAVA PROGRAMS CERTIFICATION EVALUATION EXAMS SET FULL QUESTIONS AND COMPLETE SOLU and more Exams Java Programming in PDF only on Docsity!

BUILDING JAVA PROGRAMS

CERTIFICATION EVALUATION EXAMS SET

FULL QUESTIONS AND COMPLETE

SOLUTION

●● 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: It prints the current contents of the ArrayList named words.

●● 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: [] = {, , ...}; ●● Does ArrayList support the { } syntax for initialization? Answer: No, ArrayList does not support the { } syntax. ●● What static method can be used to quickly initialize an ArrayList?

●● 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? Answer: The String class implements the Comparable interface, while the Point class does not. ●● What is boxing in Java? Answer: Boxing is the process of converting a primitive type into its corresponding wrapper class object. ●● What is the controversy surrounding boxing and unboxing in Java? Answer: Some developers find it confusing and believe it hides important details about object equality.

●● 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. ●● What is the output of compareTo() if the first String is greater than the second? Answer: A positive number. ●● What is the output of compareTo() if the first String is less than the second? Answer: A negative number. ●● What can implement the Comparable interface? Answer: Many standard Java classes and user-defined classes.

●● 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? Answer: It allows objects to be compared, enabling sorting and searching functionalities. ●● What is the relationship between Comparable and generics in Java? Answer: Comparable uses generics to ensure type safety, e.g., Comparable. ●● What is the role of the compareTo method in sorting algorithms? Answer: It determines the order of objects based on their natural ordering. ●● What does a return value of - 1 indicate in compareTo? Answer: It indicates that the current object is less than the other object. ●● What does a return value of 1 indicate in compareTo?

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? Answer: ArrayList maintains its own size, allowing dynamic addition and removal of elements. ●● What is the significance of the ArrayList's sequential order? Answer: It allows storage and retrieval of objects in a specific sequence. ●● What is a List? Answer: An ordered collection of items or elements. ●● Is a List just an Array with more functionality? Answer: No, because arrays have a fixed length, while a List's size is dynamic and adjusts as items are added or removed. ●● Do unordered Lists truly exist? Answer: No, Lists have 'order' even if said to be unordered; they are in the order in which they are built. ●● What must Java do when an entry is added to an ArrayList?