WIPRO Few Logic Building
WIPRO Few Logic Building
import java.io.*;
import java.util.*;
class DecreasingSequence {
public class Result{
public final int output1;
public final int output2;
dcrCount++;
longestLen = dcrCount > longestLen ? dcrCount : longestLen;
} else {
if (flag == true) {
flag = false;
dcrCount = 0;
}
}
}
import java.io.*;
import java.util.*;
class EncodingThreeStrings {
public class Result{
public final String output1;
public final String output2;
public final String output3;
ip1parts = getParts(input1);
ip2parts = getParts(input2);
ip3parts = getParts(input3);
if (len % 3 == 0) {
parts[0] = str.substring(0, partLen);
parts[1] = str.substring(partLen, 2 * partLen);
parts[2] = str.substring(2 * partLen, len);
} else if (len % 3 == 1) {
parts[0] = str.substring(0, partLen);
parts[1] = str.substring(partLen, 2 * partLen + 1);
parts[2] = str.substring(2 * partLen + 1, len);
} else if (len % 3 == 2) {
parts[0] = str.substring(0, partLen + 1);
parts[1] = str.substring(partLen + 1, 2 * partLen + 1);
parts[2] = str.substring(2 * partLen + 1, len);
}
return parts;
}
}
PASS WORD STABLE OR UNSTABLE
import java.io.*;
import java.util.*;
public class FindPasswordStableUnstable {
public int findPassword(int input1,int input2,int input3,int input4,int input5){
int[] nums = {input1, input2, input3, input4, input5};
int stable = 0, unstable = 0;
for (int num: nums){
if (isStable(num)) stable += num;
else unstable += num;
}
return stable - unstable;
}
import java.io.*;
import java.util.*;
class GetCodeThroughStrings {
public int getCodeThroughStrings(String input1){
String[] words = input1.split(" ");
int pin = 0;
int pin2 = 0;
String pinStr = String.valueOf(pin);
for (int i = 0; i < pinStr.length(); i++) {
pin2 += Integer.parseInt(String.valueOf(pinStr.charAt(i)));
}
return pin2;
}
}
IDENTIFY POSSIBLE WORDS
import java.io.*;
import java.util.*;
class IdentifyPossibleWords {
public String identifyPossibleWords(String input1,String input2){
input1 = input1.toUpperCase();
StringBuffer output = new StringBuffer();
String[] words = input2.split(":");
int underscoreIndex = input1.indexOf('_');
import java.util.Arrays;
public int getMostFrequestDigit(int input1, int input2, int input3, int input4) {
Arrays.fill(freq,0);
freq[digit]++;
num /= 10;
int maxFreq = 0;
return maxFreq;
}
MOST FREQUENTLY OCUURING DIGIT
import java.io.*;
import java.util.*;
class MostFrequentlyOccurringDigit {
public int mostFrequentlyOccurringDigit(int[] input1,int input2){
StringBuilder input = new StringBuilder();
for (int ip : input1) input.append(ip);
int[] freq = new int[10];
for (int j = 0; j < input.length(); j++) {
freq[Integer.parseInt(String.valueOf(input.charAt(j)))]++;
}
int maxFreqIndex = 0;
int maxFreq = 0;
for (int i = 9; i >= 0; i--) {
if (freq[i] > maxFreq) {
maxFreqIndex = i;
maxFreq = freq[i];
}
}
return maxFreqIndex;
}
}
MSG CONTROLLED ROBOT
import java.io.*;
import java.util.*;
public class MsgControlledRobot {
public String moveRobot(int input1,int input2,String input3,String input4){
int X = input1, Y = input2;
String currentPos = input3, msg = input4;
if (currD.equals("E")) currX++;
else if (currD.equals("W")) currX--;
else if (currD.equals("N")) currY++;
else if (currD.equals("S")) currY--;
} else {
if (currD.equals("E") && instructions[i].equals("L"))
currD = "N";
else if (currD.equals("E") && instructions[i].equals("R"))
currD = "S";
else if (currD.equals("W") && instructions[i].equals("L"))
currD = "S";
else if (currD.equals("W") && instructions[i].equals("R"))
currD = "N";
else if (currD.equals("N") && instructions[i].equals("L"))
currD = "W";
else if (currD.equals("N") && instructions[i].equals("R"))
currD = "E";
else if (currD.equals("S") && instructions[i].equals("L"))
currD = "E";
else if (currD.equals("S") && instructions[i].equals("R"))
currD = "W";
}
output.delete(0, output.length());
output.append(currX + "-" + currY + "-" + currD);
}
return output.toString();
}
}
NAMBER DIGIT CREATING
import java.io.*;
import java.util.*;
public class NambiarNumberGenerator {
public int nnGenerator(String input1){
String mobileNo = input1;
StringBuilder numbiarNo = new StringBuilder();
if (j == mobileNo.length()) {
numbiarNo.append(digit);
break;
}
while (true) {
sum += Integer.parseInt(String.valueOf(mobileNo.charAt(j++)));
import java.util.ArrayList;
import java.util.Collections;
public class NonRepeatedDigitsCount {
public static int nonRepeatedDigitsCount(int input1) {
int count = 0;
ArrayList<Integer> nums = new ArrayList<Integer>();
while(input1 != 0){
int digit = input1 % 10;
nums.add(digit);
input1 /= 10;
}
return count;
}
}
PinGenerator
input1 /= 10;
input2 /= 10;
input3 /= 10;
ip1 = input1 % 10;
ip2 = input2 % 10;
ip3 = input3 % 10;
min = Math.min(ip1, ip2);
min = Math.min(min, ip3);
max = Math.max(max, ip1);
max = Math.max(max, ip2);
max = Math.max(max, ip3);
pin = min * 10 + pin;
input1 /= 10;
input2 /= 10;
input3 /= 10;
ip1 = input1 % 10;
ip2 = input2 % 10;
ip3 = input3 % 10;
min = Math.min(ip1, ip2);
min = Math.min(min, ip3);
max = Math.max(max, ip1);
max = Math.max(max, ip2);
max = Math.max(max, ip3);
pin = min * 100 + pin;
pin = max * 1000 + pin;
return pin;
}
}
REMOVE 1 DIGITFORPALINDROME
import java.io.*;
import java.util.*;
public class Remove1DigitForPalindrome {
public int digitRemove_Palin(int input1){
StringBuilder num = new StringBuilder(String.valueOf(input1));
if (isPalindrome(newNum)) {
return Integer.parseInt(String.valueOf(removed));
} else {
num.insert(i, removed);
}
}
return -1;
}
if (len % 2 == 0) range--;
try {
int startIndex = sb.indexOf(" ");
if (startIndex == -1) return "LESS";
try {
int endIndex = sb.indexOf(" ", startIndex+1) + 1;
return sb.substring(startIndex+1, endIndex).toUpperCase().trim();
} catch (StringIndexOutOfBoundsException e) {
return sb.substring(startIndex+1).toUpperCase().trim();
}
} catch (StringIndexOutOfBoundsException e) {
return "LESS";
}
}
}
SIMPLE ENCODED ARRAY
import java.io.*;
import java.util.*;
class SimpleEncodedArray {
public class Result{
public final int output1;
public final int output2;
int sum = 0;
for (int item : out)
sum += item;
import java.io.*;
import java.util.*;
class SumOfPowersOfDigits {
public int sumOfPowerOfDigits(int input1){
if (input1 <= 9) return 0;
import java.io.*;
import java.util.*;
public class UserIDGeneration {
public String userIdGeneration(String input1,String input2,int input3,int input4){
String firstName = input1,lastName = input2, longerName, smallerName;
int pin = input3,N = input4;
StringBuilder userId = new StringBuilder();
userId.append(smallerName.charAt(smallerName.length() - 1));
userId.append(longerName);
for (int i = 0; i < userId.length(); i++) {
if (Character.isUpperCase(userId.charAt(i)))
userId.setCharAt(i, Character.toLowerCase(userId.charAt(i)));
else
userId.setCharAt(i, Character.toUpperCase(userId.charAt(i)));
}
userId.append(String.valueOf(pin).charAt(N - 1));
userId.append(String.valueOf(pin).charAt(String.valueOf(pin).length() - N));
return userId.toString();
}
}
Which exception will the following
throw?
public class Test {
A. ArrayIndexOutOfBoundsException
B. ClassCastException
C. IllegalArgumentException
D. NumberFormatException
E. None of the above.
Answer:
B. ClassCastException
Explanation:
The second line tries to cast an Integer to a String. Since String does not extend Integer, this is not allowed and
a ClassCastException is thrown.
9. Which of the following exceptions are thrown by the
JVM? (Choose all that apply)
A. ArrayIndexOutOfBoundsException
B. ExceptionInInitializerError
C. java.io.IOException
D. NullPointerException
E. NumberFormatException
Answer:
A, B, D
Explanation:
java.io.IOException is thrown by many methods in the java.io package, but it is always thrown programmatically.
The same is true for NumberFormatException; it is thrown programmatically by the wrapper classes of java.lang. The
other three exceptions are all thrown by the JVM when the corresponding problem arises.
Which of the following statements correctly describes the behavior of this program?
a) This program will result in a compiler error.
b) This program will throw a NullPointerException.
c) This program will print 10 in the console.
d) This program will print null in the console.
Answer:
c) This program will print 10 in the console.
Explanation:
The valueOf(int) method is a static method in String that returns the String representation of the integer value that
is passed as its argument. Since calling a static method does not require dereferencing the reference variable on
which it is called, this program does not throw a NullPointerException.
a) 11511
b) 1105110
c) 115110
d) 27
Answer:
c) 115110
Explanation:
The string concatenation operator works as follows: if both the operands are numbers, it performs the addition;
otherwise, it concatenates the arguments by calling the toString() method if needed. It evaluates from left to right.
Hence, the expression in the program results in the string 115110.
Q4. Consider the following program and predict its
output:
public class Test {
public static void main(String[] args) {
String str = null;
switch (str) { // #1
case "null":
System.out.println("null string"); // #2
break;
}
}
}
Answer:
c) This program results in throwing a NullPointerException.
a) null null
b) null NullPointerException
c) NullPointerException NullPointerException
d) None
Answer:
b) null NullPointerException
Explanation:
Line 2 will print null because the println() method has a null check like below.
if (s == null) {
s = "null";
}
Q6. Select all the classes that extend the
String class
a) StringBuffer
b) StringBuilder
c) StringWriter
d) None
Click to View Answer and Explanation
Answer:
d) None
Explanation:
The String is a final class, so you can't extend it.
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
a) false
b) true
c) none
Click to View Answer and Explanation
Answer:
b) true
Explanation:
We know that the intern() method will return the String object reference from the string pool since we assign it back
to s2 and now both s1 and s2 are having the same reference. It means that s1 and s2 references point to the same
object.
Q8. What will be the output of the below
statements?
public class Test {
a) false
b) true
c) ClassCastException at runtime
d) Compile-time error
Click to View Answer and Explanation
Answer:
a) false
Explanation:
It will print false because s2 is not of type String. If you will look at the equals method implementation in the String
class, you will find a check using instanceof operator to check if the type of passed object is String? If not, then
return false.
Answer:
C. true is printed out exactly three times.
Explanation:
String literals are used from the string pool. This means that s1 and s2 refer to the same object and are equal.
Therefore, the first two print statements print true. The third print statement prints false because toString() uses a
method to compute the value and it is not from the string pool. The final print statement again prints true
because equals() looks at the values of String objects.
Answer:
b) StringBuilder is not a thread-safe, while StringBuffer is a thread-safe.
Answer:
a) A new String object is created.
a) Java
b) Java Programming
c) Programming
d) Compile-time error
Click to View Answer and Explanation
Answer:
a) Java
Explanation:
The concat() method returns a new string resulting from concatenating the specified string to the original string.
However, in this code, the result of concat() is not assigned back to the str variable. Therefore, the original string
"Java" remains unchanged, and the output is "Java".
Answer:
a) equals()
Explanation:
The equals() method is used to compare two strings for equality in Java. It returns true if the two strings have the
same characters in the same order, and false otherwise.
Which method is used to copy one array
into another in Java?
a) copy()
b) clone()
c) System.arraycopy()
d) Arrays.copy()
Answer:
Explanation:
The System.arraycopy() method is used to copy one array into another in Java, allowing you to efficiently copy
elements between arrays.
b) ...
c) --
d) ---
Answer:
b) ...
Explanation:
Three dots (...) are the syntax for a method parameter of type varargs. It is treated as an array.
a) buses.length
b) buses.length()
c) buses.size
d) buses.size()
Answer:
a) buses.length
Explanation:
Arrays use the length variable to determine the number of elements, making Option A correct.
a) [1, 9, 10]
b) [1, 10, 9]
c) [10, 1, 9]
Answer:
b) [1, 10, 9]
Explanation:
The elements of the array are of type String rather than int. Therefore, we use alphabetical order when sorting.
Character 1 sorts before character 9, alphabetically making Option A incorrect. Shorter strings sort before longer
strings when all the other characters are the same, making Option B the answer.
a) None
b) One
c) Two
d) Three
Answer:
c) Two
Explanation:
When using an array initializer, you are not allowed to specify the size separately. The size is inferred from the number
of elements listed. Therefore, tiger and cat are incorrect. When you’re not using an array initializer, the size is
required. An empty array initializer is allowed. Option C is correct because lion and bear are legal.
b) [,]
c) [Ljava.lang.String;@74a14482
Answer:
c) [Ljava.lang.String;@74a14482
Explanation:
Calling toString() on an array doesn’t output the contents of the array, making Option C correct. If you wanted
Option A to be the answer, you’d have to call Arrays.toString(strings).
2. Which of these is not a method defined in the Map interface?
a) put()
b) get()
c) remove()
d) add()
Answer:
d) add()
Explanation:
The add() method is not defined in the Map interface. Map interface includes methods like put(), get(), and remove()
for manipulating key-value pairs.
Answer:
c) TreeSet maintains elements in a sorted order, while HashSet does not
Explanation:
The main difference between HashSet and TreeSet is that TreeSet maintains its elements in a sorted order (either
natural ordering or according to a Comparator) while HashSet does not guarantee any order.
b) Set
c) List
d) Map
Click to View Answer and Explanation
Answer:
c) List
Explanation:
The Stack class extends Vector and represents a last-in-first-out (LIFO) stack of objects. Despite its name, it is a List
implementation.
b) SortedMap
c) SortedSet
d) Sortable
Answer:
d) Sortable
Explanation:
Sortable is not an interface in the Java Collections Framework. List, SortedMap, and SortedSet are all interfaces within
the framework.
b) Collections.synchronizedMap()
c) Hashtable
d) SyncMap
Answer:
b) Collections.synchronizedMap()
Explanation:
Collections.synchronizedMap() method is used to return a synchronized (thread-safe) map backed by the specified
map.
7. What is the purpose of the Comparable interface in Java Collections?
a) To define a natural ordering for objects of each class that implements it
Answer:
a) To define a natural ordering for objects of each class that implements it
Explanation:
The Comparable interface is used to define a natural ordering for objects of each class that implements it. It has a
compareTo method used for sorting objects.
b) 12
c) 16
d) 20
Answer:
c) 16
Explanation:
The default initial capacity of a HashMap is 16. This is the number of buckets in the hash table initially.
b) 12
c) 16
d) 20
Explanation:
The default initial capacity of a HashMap is 16. This is the number of buckets in the hash table initially.
b) It is not thread-safe
Answer:
c) It allows the modification of the collection while iterating
Explanation:
ConcurrentHashMap allows concurrent modification of the Map from several threads without the need to block them.
Answer:
c) When keys can be garbage collected
Explanation:
WeakHashMap is a hashtable-based implementation where the keys are weak references, allowing the key-value pairs
to be garbage-collected when the key is no longer in ordinary use.
15. What is the purpose of the Collections.unmodifiableCollection()
method?
a) To create a new collection that is a clone of the current collection
Answer:
b) To create a read-only view of a collection
Explanation:
Collections.unmodifiableCollection() method is used to return an unmodifiable view of the specified collection. This
means that the collection cannot be modified through this view.
16. Which class should be used for a resizable array that provides the ability
to grow or shrink its size dynamically?
a) LinkedList
b) Vector
c) ArrayList
d) ArrayDeque
Answer:
c) ArrayList
Explanation:
ArrayList is an implementation of the List interface that uses a resizable array, which is expanded or contracted as
needed.
17. What is the primary difference between the offer() and add() methods in
a Queue?
a) offer() throws an exception if the element cannot be added, while add() returns false
c) offer() returns false if the element cannot be added, while add() throws an exception
d) offer() is used in priority queues, while add() is used in normal queues
Answer:
c) offer() returns false if the element cannot be added, while add() throws an exception
Explanation:
In Queue, the offer() method is typically preferred over add() as it returns false if the element cannot be added due to
capacity restrictions, while add() throws an IllegalStateException.
b) ConcurrentHashMap
c) ArrayList
d) ConcurrentLinkedQueue
Answer:
c) ArrayList
Explanation:
ArrayList provides a fail-fast iterator, which means it will throw a ConcurrentModificationException if the collection is
modified after the iterator is created, except through the iterator's own remove method.
9. What is the behavior of the poll() method in a Queue when the Queue is
empty?
a) Throws an exception
b) Returns null
Answer:
b) Returns null
Explanation:
The poll() method retrieves and removes the head of the queue, or returns null if the queue is empty.
20. What is the key characteristic of a Set in Java Collections?
a) Allows duplicate elements
Answer:
d) Does not allow duplicate elements
Explanation:
A Set is a collection that does not allow duplicate elements. It models the mathematical set abstraction.
21. Which Java collection class is thread-safe and does not allow null
elements?
a) ConcurrentHashMap
b) CopyOnWriteArrayList
c) Vector
d) Hashtable
Answer:
d) Hashtable
Explanation:
Hashtable is a legacy class from the original version of Java. It is synchronized (thread-safe) and does not allow null
keys or null values.
Answer:
a) set(int index, E element)
Explanation:
The set(int index, E element) method replaces the element at the specified position in the list with the specified
element.
c) A sorted collection
Answer:
b) A synchronized (thread-safe) collection
Explanation:
Collections.synchronizedList() wraps a given List and returns a synchronized (thread-safe) version of it.
Answer:
a) ConcurrentSkipListMap is synchronized, whereas TreeMap is not
Explanation:
ConcurrentSkipListMap is a concurrent, thread-safe version of a skip list, while TreeMap is a non-synchronized, sorted
map implementation.
25. What does the iterator of a CopyOnWriteArrayList return when the list is
modified after the iterator is created?
a) The updated list
b) The list at the state it was when the iterator was created
c) A ConcurrentModificationException
d) An UnsupportedOperationException
Answer:
b) The list at the state it was when the iterator was created
Explanation:
The iterator of a CopyOnWriteArrayList returns a "snapshot" of the list at the point the iterator was created, regardless
of subsequent modifications.
b) LinkedList
c) ArrayDeque
d) PriorityQueue
Answer:
c) ArrayDeque
Explanation:
ArrayDeque is commonly used for stack operations and is more efficient than Stack. It provides a resizable array and
allows null elements, making it suitable for LIFO operations.
d) ListIterator can only be used with lists, whereas Iterator can be used with any collection
Answer:
b) ListIterator allows traversing in both directions, whereas Iterator only allows forward traversal
Explanation:
ListIterator extends Iterator with additional methods that allow bidirectional traversal of a list and the modification of
elements.
b) It allows operations that wait for the queue to become non-empty when retrieving an element and wait for space
to become available in the queue when storing an element
Answer:
b) It allows operations that wait for the queue to become non-empty when retrieving an element and wait for space
to become available in the queue when storing an element
Explanation:
BlockingQueue is designed for cases where producers and consumers are different threads. Consumers can wait for
producers to insert items and vice-versa.
c) Is not synchronized
Explanation:
LinkedHashMap consumes more memory than HashMap because it maintains a linked list to keep track of insertion
order.
b) It retains only those elements that are also contained in the specified collection
d) It compares two collections and returns true if they have the same elements
Answer:
b) It retains only those elements that are also contained in the specified collection
Explanation:
The retainAll() method is used to retain only the elements in the collection that are contained in the specified
collection.
31. Which of these interfaces is typically used for a collection that does not
allow duplicate elements and for which order is not an issue?
a) List
b) Set
c) Map
d) Queue
Answer:
b) Set
Explanation:
The Set interface models the mathematical set abstraction and is typically used for collections that do not allow
duplicate elements and do not need to maintain any order.
32. What is the difference between the add and offer methods in the Queue
interface?
a) There is no difference, both insert an element into the queue
b) add throws an exception if the element can't be added, while offer returns false
c) add returns false if the element can't be added, while offer throws an exception
Answer:
b) add throws an exception if the element can't be added, while offer returns false
Explanation:
In the Queue interface, the add method throws an IllegalStateException if the element cannot be added due to
capacity restrictions, while the offer method returns false in such cases.
33. Which method would you use to obtain the first element of a Set?
a) getFirst()
b) first()
c) It's not possible to get the first element of a Set as it doesn't maintain any order
d) iterator().next()
Answer:
d) iterator().next()
Explanation:
Sets do not have a method to directly retrieve an element by its position because Sets typically do not guarantee an
order. The iterator().next() method can be used to get the first element if iterating over the Set.
Explanation:
Collections.synchronizedSet() method is used to return a synchronized (thread-safe) set backed by the specified set.
35. Which class in Java Collections Framework is designed for cases where
high-throughput and thread-safe implementation is required?
a) Vector
b) ArrayList
c) CopyOnWriteArrayList
d) LinkedList
Answer:
c) CopyOnWriteArrayList
Explanation:
CopyOnWriteArrayList is designed for environments where high-throughput is needed, and the array rarely changes
but is frequently iterated. It provides a thread-safe variant of ArrayList.
Answer:
a) To implement a producer-consumer scenario where elements are processed based on priority
Explanation:
c) HashMap allows null keys and values, but Hashtable does not
Answer:
c) HashMap allows null keys and values, but Hashtable does not
Explanation:
HashMap allows null keys and null values, whereas Hashtable does not allow null keys or values and is synchronized.
8. Which interface should be used when you need a map and you wish to
preserve the order of elements?
a) SortedMap
b) LinkedHashMap
c) TreeMap
d) ConcurrentHashMap
Answer:
b) LinkedHashMap
Explanation:
LinkedHashMap maintains a linked list of the entries in the map, in the order in which they were inserted. This linked
list defines the iteration ordering, which is normally the order in which keys were inserted into the map (insertion-
order).
b) It uses a linked list that allows insertion and removal at both ends
Answer:
c) It allows concurrent modification from multiple threads without locking
Explanation:
ConcurrentLinkedQueue is a thread-safe queue based on linked nodes and supports concurrent access from multiple
threads without locking.
b) A null pointer
Answer:
d) An immutable empty set
Explanation:
Collections.emptySet() returns a special immutable empty set. This set is serializable and no elements can be added to
it.
Answer:
b) poll() returns null if the queue is empty, remove() throws an exception
Explanation:
In Queue interface, poll() retrieves and removes the head of the queue, returning null if the queue is empty, whereas
remove() retrieves and removes the head of the queue but throws an exception if the queue is empty.
42. What will happen if you try to sort a List that contains a null element?
a) NullPointerException
b) The list will be sorted and the null element will be placed at the beginning
d) The list will be sorted and the null element will be placed at the end
Answer:
a) NullPointerException
Explanation:
Attempting to sort a list containing null elements will result in a NullPointerException because null cannot be
compared with non-null elements.
b) PriorityQueue
c) ArrayDeque
d) Stack
Answer:
c) ArrayDeque
Explanation:
ArrayDeque is more efficient than LinkedList for new standard FIFO (first-in-first-out) queue operations.
45. Which method in the Map interface is used to retrieve all keys contained
in the map?
a) values()
b) keySet()
c) entrySet()
d) getAllKeys()
Answer:
b) keySet()
Explanation:
The keySet() method in the Map interface is used to return a Set view of the keys contained in the map.
46. What is the difference between the size and capacity of an ArrayList?
a) Size and capacity are the same in an ArrayList
b) Size is the number of elements in the ArrayList, and capacity is the size of the array buffer
c) Size is the size of the array buffer, and capacity is the number of elements it can hold
Answer:
b) Size is the number of elements in the ArrayList, and capacity is the size of the array buffer
Explanation:
In an ArrayList, the size is the number of elements currently in the list, while the capacity is the size of the array buffer
inside the ArrayList, which can potentially hold more elements than the current size.
b) HashMap
c) ConcurrentHashMap
d) LinkedList
Answer:
c) ConcurrentHashMap
Explanation:
ConcurrentHashMap is a concurrent collection class that allows multiple threads to read and write concurrently and is
designed to be used in multithreaded contexts.
Answer:
c) The string that comes last in lexicographical order
Explanation:
Collections.max() returns the maximum element of the given collection, according to the natural ordering of its
elements. For a string, it would be the one that comes last in lexicographical order.
50. Which method do you need to implement when creating a class that can
be used as a key in a HashMap?
a) compareTo()
c) clone()
d) toString()
Answer:
b) equals() and hashCode()
Explanation:
When creating a class that can be used as a key in a HashMap, it is crucial to properly override both equals() and
hashCode() methods to ensure that duplicate keys are not allowed and the hashcode contract is maintained.