InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Bitset { 3. Public Static Void Main(string Args[]) { 4. Bitset Obj = New Bitset(5); 5. For (int I = 0; I < 5; ++i) 6. Obj.set(i); 7. Obj.clear(2); 8. System.out.print(obj); 9. } 10. } |
|
Answer» Output: $ javac Bitset.java Output: $ javac Bitset.java |
|
| 2. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Bitset { 3. Public Static Void Main(string Args[]) { 4. Bitset Obj1 = New Bitset(5); 5. Bitset Obj2 = New Bitset(10); 6. For (int I = 0; I < 5; ++i) 7. Obj1.set(i); 8. For (int I = 3; I < 13; ++i) 9. Obj2.set(i); 10. Obj1.and(obj2); 11. System.out.print(obj1); 12. } 13. } |
|
Answer» OBJ1.and(obj2) returns an BitSet object which CONTAINS elements common to both the object obj1 and obj2 and stores this BitSet in INVOKING object that is obj1. Hence obj1 contains 3 & 4. Output: $ javac Bitset.JAVA obj1.and(obj2) returns an BitSet object which contains elements common to both the object obj1 and obj2 and stores this BitSet in invoking object that is obj1. Hence obj1 contains 3 & 4. Output: $ javac Bitset.java |
|
| 3. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Bitset { 3. Public Static Void Main(string Args[]) { 4. Bitset Obj = New Bitset(5); 5. For (int I = 0; I < 5; ++i) 6. Obj.set(i); 7. Obj.clear(2); 8. System.out.print(obj.length() + " " + Obj.size()); 9. } 10. } |
|
Answer» obj.length() returns the length allotted to OBJECT obj at TIME of initialization and obj.SIZE() returns the size of current object obj, each BitSet element is GIVEN 16 bits therefore the size is 4 * 16 = 64, whereas length is still 5. Output: $ javac Bitset.java obj.length() returns the length allotted to object obj at time of initialization and obj.size() returns the size of current object obj, each BitSet element is given 16 bits therefore the size is 4 * 16 = 64, whereas length is still 5. Output: $ javac Bitset.java |
|
| 4. |
What Is A Method Of Class Date Which Is Used To Search Weather Object Contains A Date Before The Specified Date? |
|
Answer» before() returns true if the invoking DATE OBJECT contains a date that is earlier than one SPECIFIED by date, OTHERWISE it returns false. before() returns true if the invoking Date object contains a date that is earlier than one specified by date, otherwise it returns false. |
|
| 5. |
Which Class Object Has Architecture Similar To That Of Array? |
|
Answer» Bitset class creates a special type of ARRAY that holds bit values. This array can INCREASE in SIZE as NEEDED. Bitset class creates a special type of array that holds bit values. This array can increase in size as needed. |
|
| 6. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Properties { 3. Public Static Void Main(string Args[]) { 4. Properties Obj = New Properties(); 5. Obj.put("ab", New Integer(3)); 6. Obj.put("bc", New Integer(2)); 7. Obj.put("cd", New Integer(8)); 8. System.out.print(obj.keyset()); 9. } 10. } |
|
Answer» OBJ.keySet() returns a set containing all the keys USED in properties object, here obj contains keys AB, BC, CD therefore obj.keySet() returns [AB, BC, CD]. $ javac properties.JAVA obj.keySet() returns a set containing all the keys used in properties object, here obj contains keys AB, BC, CD therefore obj.keySet() returns [AB, BC, CD]. Output: $ javac properties.java |
|
| 7. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Hashtable { 3. Public Static Void Main(string Args[]) { 4. Hashtable Obj = New Hashtable(); 5. Obj.put("a", New Integer(3)); 6. Obj.put("b", New Integer(2)); 7. Obj.put("c", New Integer(8)); 8. System.out.print(obj.tostring()); 9. } 10. } |
|
Answer» obj.toString returns String EQUIVALENT of the HASHTABLE, which can also be obtained by SIMPLY writing System.out.PRINT(obj); as print system automatically coverts the obj to string equivalent. Output: $ JAVAC hashtable.java obj.toString returns String equivalent of the hashtable, which can also be obtained by simply writing System.out.print(obj); as print system automatically coverts the obj to string equivalent. Output: $ javac hashtable.java |
|
| 8. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Hashtable { 3. Public Static Void Main(string Args[]) { 4. Hashtable Obj = New Hashtable(); 5. Obj.put("a", New Integer(3)); 6. Obj.put("b", New Integer(2)); 7. Obj.put("c", New Integer(8)); 8. System.out.print(obj.contains(new Integer(5))); 9. } 10. } |
|
Answer» Hashtable OBJECT obj contains values 3, 2, 8 when obj.contains(new Integer(5)) is executed it searches for 5 in the hashtable since it is not present FALSE is returned. Output: $ JAVAC hashtable.JAVA Hashtable object obj contains values 3, 2, 8 when obj.contains(new Integer(5)) is executed it searches for 5 in the hashtable since it is not present false is returned. Output: $ javac hashtable.java |
|
| 9. |
Which Is The Interface Of Legacy Is Implemented By Hash-table And Dictionary Classes? |
|
Answer» DICTIONARY, Map & Hashtable all IMPLEMENT Map interface HENCE all of them uses keys to STORE value in the object. Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object. |
|
| 10. |
Which Class Object Uses Key To Store Value? |
|
Answer» Dictionary, MAP & HASHTABLE all implement Map interface hence all of them uses KEYS to store VALUE in the object. Dictionary, Map & Hashtable all implement Map interface hence all of them uses keys to store value in the object. |
|
| 11. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Stack { 3. Public Static Void Main(string Args[]) { 4. Stack Obj = New Stack(); 5. Obj.push(new Integer(3)); 6. Obj.push(new Integer(2)); 7. Obj.pop(); 8. Obj.push(new Integer(5)); 9. System.out.println(obj); 10. } 11. } |
|
Answer» push() and POP() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 HENCE stack contains ELEMENTS 3 & 5. Output: $ JAVAC stack.JAVA push() and pop() are standard functions of the class stack, push() inserts in the stack and pop removes from the stack. 3 & 2 are inserted using push() the pop() is used which removes 2 from the stack then again push is used to insert 5 hence stack contains elements 3 & 5. Output: $ javac stack.java |
|
| 12. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Vector { 3. Public Static Void Main(string Args[]) { 4. Vector Obj = New Vector(4,2); 5. Obj.addelement(new Integer(3)); 6. Obj.addelement(new Integer(2)); 7. Obj.addelement(new Integer(5)); 8. Obj.removeall(obj); 9. System.out.println(obj.isempty()); 10. } 11. } |
|
Answer» firstly ELEMENTS 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is EMPTY, hence obj.isEmpty() RETURNS true. Output: $ javac vector.java firstly elements 3, 2, 5 are entered in the vector obj, but when obj.removeAll(obj); is executed all the elements are deleted and vector is empty, hence obj.isEmpty() returns true. Output: $ javac vector.java |
|
| 13. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Vector { 3. Public Static Void Main(string Args[]) { 4. Vector Obj = New Vector(4,2); 5. Obj.addelement(new Integer(3)); 6. Obj.addelement(new Integer(2)); 7. Obj.addelement(new Integer(5)); 8. System.out.println(obj.elementat(1)); 9. } 10. } |
|
Answer» obj.elementAt(1) returns the value stored at index 1, which is 2. $ javac vector.JAVA obj.elementAt(1) returns the value stored at index 1, which is 2. Output: $ javac vector.java |
|
| 14. |
Which Methods Is Used To Add Elements In Vector At Specific Location? |
|
Answer» addElement() is USED to ADD data in the VECTOR, to obtain the data we use elementAt() and to FIRST and last element we use firstElement() and lastElement() respectively. addElement() is used to add data in the vector, to obtain the data we use elementAt() and to first and last element we use firstElement() and lastElement() respectively. |
|
| 15. |
What Are Legacy Classes? |
|
Answer» Stack, HASHTABLE, VECTOR, Properties and DICTIONARY are legacy CLASSES. Stack, Hashtable, Vector, Properties and Dictionary are legacy classes. |
|
| 16. |
Which Class Object Can Be Used To Form A Dynamic Array? |
|
Answer» Vectors are DYNAMIC arrays, it CONTAINS many LEGACY methods that are not part of COLLECTION framework, and hence these methods are not PRESENT in ArrayList. But both are used to form dynamic arrays. Vectors are dynamic arrays, it contains many legacy methods that are not part of collection framework, and hence these methods are not present in ArrayList. But both are used to form dynamic arrays. |
|
| 17. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Array { 3. Public Static Void Main(string Args[]) { 4. Int Array[] = New Int [5]; 5. For (int I = 5; I > 0; I--) 6. Array[5-i] = I; 7. Arrays.fill(array, 1, 4, 8); 8. For (int I = 0; I < 5 ; I++) 9. System.out.print(array[i]); 10. } 11. } |
|
Answer» ARRAY was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the INDEX location starting with 1 to 4 by value 8 hence array BECOMES 5,8,8,8,1. Output: $ JAVAC Array.java array was containing 5,4,3,2,1 but when method Arrays.fill(array, 1, 4, 8) is called it fills the index location starting with 1 to 4 by value 8 hence array becomes 5,8,8,8,1. Output: $ javac Array.java |
|
| 18. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Array { 3. Public Static Void Main(string Args[]) { 4. Int Array[] = New Int [5]; 5. For (int I = 5; I > 0; I--) 6. Array[5 - I] = I; 7. Arrays.sort(array); 8. For (int I = 0; I < 5; ++i) 9. System.out.print(array[i]);; 10. } 11. } |
|
Answer» Arrays.sort(array) method SORTS the array into 1,2,3,4,5. $ JAVAC Array.JAVA Arrays.sort(array) method sorts the array into 1,2,3,4,5. Output: $ javac Array.java |
|
| 19. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Arraylist { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj1 = New Arraylist(); 5. Arraylist Obj2 = New Arraylist(); 6. Obj1.add("a"); 7. Obj1.add("b"); 8. Obj2.add("a"); 9. Obj2.add(1, "b"); 10. System.out.println(obj1.equals(obj2)); 11. } 12. } |
|
Answer» obj1 and obj2 are an object of class ArrayList hence it is a DYNAMIC array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Both the objects obj1 and obj2 contain same ELEMENTS i:e A & B THUS obj1.equals(obj2) method returns true. Output: $ javac Arraylist.java obj1 and obj2 are an object of class ArrayList hence it is a dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Both the objects obj1 and obj2 contain same elements i:e A & B thus obj1.equals(obj2) method returns true. Output: $ javac Arraylist.java |
|
| 20. |
Which Methods Can Be Used To Search An Element In A List? |
|
Answer» binaryserach() method USES binary search to FIND a specified value. This method MUST be APPLIED to sorted ARRAYS. binaryserach() method uses binary search to find a specified value. This method must be applied to sorted arrays. |
|
| 21. |
Which Method Is Used To Make All Elements Of An Equal To Specified Value? |
|
Answer» FILL() METHOD assigns a value to all the elements in an array, in other WORDS it FILLS the array with specified value. fill() method assigns a value to all the elements in an array, in other words it fills the array with specified value. |
|
| 22. |
Which Classes Implements Set Interface? |
|
Answer» HASHSET and TREESET implements SET interface where as LINKEDLIST and ArrayList implements LIST interface. HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface. |
|
| 23. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Maps { 3. Public Static Void Main(string Args[]) { 4. Treemap Obj = New Treemap(); 5. Obj.put("a", New Integer(1)); 6. Obj.put("b", New Integer(2)); 7. Obj.put("c", New Integer(3)); 8. System.out.println(obj.entryset()); 9. } 10. } |
|
Answer» obj.entrySet() method is used to obtain a set that contains the ENTRIES in the MAP. This method PROVIDES set view of the invoking map. Output: $ javac MAPS.java obj.entrySet() method is used to obtain a set that contains the entries in the map. This method provides set view of the invoking map. Output: $ javac Maps.java |
|
| 24. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Maps { 3. Public Static Void Main(string Args[]) { 4. Hashmap Obj = New Hashmap(); 5. Obj.put("a", New Integer(1)); 6. Obj.put("b", New Integer(2)); 7. Obj.put("c", New Integer(3)); 8. System.out.println(obj.get("b")); 9. } 10. } |
|
Answer» obj.get(“B”) METHOD is used to obtain the VALUE associated with KEY “B”, which is 2. Output: $ JAVAC Maps.java obj.get(“B”) method is used to obtain the value associated with key “B”, which is 2. Output: $ javac Maps.java |
|
| 25. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Maps { 3. Public Static Void Main(string Args[]) { 4. Hashmap Obj = New Hashmap(); 5. Obj.put("a", New Integer(1)); 6. Obj.put("b", New Integer(2)); 7. Obj.put("c", New Integer(3)); 8. System.out.println(obj.keyset()); 9. } 10. } |
|
Answer» keySet() method returns a set containing all the KEYS USED in the invoking map. Here keys are characters A, B & C. 1, 2, 3 are the values given to these keys. Output: $ JAVAC Maps.java keySet() method returns a set containing all the keys used in the invoking map. Here keys are characters A, B & C. 1, 2, 3 are the values given to these keys. Output: $ javac Maps.java |
|
| 26. |
Which Method Is Used Add An Element And Corresponding Key To A Map? |
|
Answer» Maps revolve around TWO basic OPERATIONS – get() and PUT(). to put a VALUE into a map, USE put(), specifying the key and the value. To obtain a value, call get() , passing the key as an argument. The value is returned. Maps revolve around two basic operations – get() and put(). to put a value into a map, use put(), specifying the key and the value. To obtain a value, call get() , passing the key as an argument. The value is returned. |
|
| 27. |
Which Methods Can Be Used To Obtain Set Of All Keys In A Map? |
|
Answer» KEYSET() methods is used to GET a set containing all the KEYS used in a map. This method provides set view of the keys in the invoking map. keySet() methods is used to get a set containing all the keys used in a map. This method provides set view of the keys in the invoking map. |
|
| 28. |
Which Classes Provide Implementation Of Map Interface? |
|
Answer» AbstractMap, WeakHashMap, HASHMAP and TREEMAP PROVIDE implementation of MAP INTERFACE. AbstractMap, WeakHashMap, HashMap and TreeMap provide implementation of map interface. |
|
| 29. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Output { 3. Public Static Void Main(string Args[]) { 4. Treeset T = New Treeset(); 5. T.add("3"); 6. T.add("9"); 7. T.add("1"); 8. T.add("4"); 9. T.add("8"); 10. System.out.println(t); 11. } 12. } |
|
Answer» TreeSet CLASS uses set to store the values added by FUNCTION add in ascending ORDER using tree for STORAGE Output: $ javac Output.java TreeSet class uses set to store the values added by function add in ascending order using tree for storage Output: $ javac Output.java |
|
| 30. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Output { 3. Public Static Void Main(string Args[]) { 4. Hashset Obj = New Hashset(); 5. Obj.add("a"); 6. Obj.add("b"); 7. Obj.add("c"); 8. System.out.println(obj + " " + Obj.size()); 9. } 10. } |
|
Answer» HashSet obj creates an hash object which implements Set INTERFACE, obj.size() GIVES the number of elements stored in the object obj which in this case is 3. $ javac Output.JAVA HashSet obj creates an hash object which implements Set interface, obj.size() gives the number of elements stored in the object obj which in this case is 3. Output: $ javac Output.java |
|
| 31. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Linkedlist { 3. Public Static Void Main(string Args[]) { 4. Linkedlist Obj = New Linkedlist(); 5. Obj.add("a"); 6. Obj.add("b"); 7. Obj.add("c"); 8. Obj.addfirst("d"); 9. System.out.println(obj); 10. } 11. } |
|
Answer» OBJ.addFirst(“D”) METHOD is used to add ‘D’ to the START of a LinkedList object obj. Output: $ javac Linkedlist.JAVA obj.addFirst(“D”) method is used to add ‘D’ to the start of a LinkedList object obj. Output: $ javac Linkedlist.java |
|
| 32. |
Which Method Is Used To Change An Element In A Linkedlist Object? |
|
Answer» An ELEMENT in a LINKEDLIST object can be changed by first USING get() to obtain the index or location of that object and the passing that location to method set() along with its new value. An element in a LinkedList object can be changed by first using get() to obtain the index or location of that object and the passing that location to method set() along with its new value. |
|
| 33. |
Which Of These Methods Can Be Used To Delete The Last Element In A Linkedlist Object? |
|
Answer» removeLast() and removeFirst() METHODS are used to remove elements in END and BEGINNING of a linked LIST. removeLast() and removeFirst() methods are used to remove elements in end and beginning of a linked list. |
|
| 34. |
Which Of These Classes Implements Set Interface? |
|
Answer» HashSet and TreeSet implements Set INTERFACE where as LINKEDLIST and ARRAYLIST implements LIST interface. HashSet and TreeSet implements Set interface where as LinkedList and ArrayList implements List interface. |
|
| 35. |
What Is The Output Of This Program? 1. Class Output { 2. Public Static Void Main(string Args[]) { 3. Arraylist Obj = New Arraylist(); 4. Obj.add("a"); 5. Obj.add("d"); 6. Obj.ensurecapacity(3); 7. Obj.trimtosize(); 8. System.out.println(obj.size()); 9. } 10. } |
|
Answer» trimTosize() is USED to reduce the size of the array that underlines an ARRAYLIST OBJECT. $ javac Output.java trimTosize() is used to reduce the size of the array that underlines an ArrayList object. Output: $ javac Output.java |
|
| 36. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Output { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj = New Arraylist(); 5. Obj.add("a"); 6. Obj.ensurecapacity(3); 7. System.out.println(obj.size()); 8. } 9. } |
|
Answer» ALTHOUGH OBJ.ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj.size() returns the total number of elements stored in the obj i:e 1, it has NOTHING to do with ensureCapacity(). Output: $ javac Output.java Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the value is stored only at index 0, therefore obj.size() returns the total number of elements stored in the obj i:e 1, it has nothing to do with ensureCapacity(). Output: $ javac Output.java |
|
| 37. |
What Is The Output Of This Program? 1. Import Java.util.*; 2. Class Arraylist { 3. Public Static Void Main(string Args[]) { 4. Arraylist Obj = New Arraylist(); 5. Obj.add("a"); 6. Obj.add("b"); 7. Obj.add("c"); 8. Obj.add(1, "d"); 9. System.out.println(obj); 10. } 11. } |
|
Answer» obj is an object of class ArrayList hence it is an DYNAMIC array which can increase and DECREASE its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and shifts the previous value STORED at that position by 1. Output: $ javac Arraylist.java obj is an object of class ArrayList hence it is an dynamic array which can increase and decrease its size. obj.add(“X”) adds to the array element X and obj.add(1,”X”) adds element x at index position 1 in the list, Hence obj.add(1,”D”) stores D at index position 1 of obj and shifts the previous value stored at that position by 1. Output: $ javac Arraylist.java |
|
| 38. |
Which Method Is Used To Reduce The Capacity Of An Arraylist Object? |
|
Answer» trimTosize() is used to reduce the SIZE of the array that UNDERLINES an ArrayList OBJECT. trimTosize() is used to reduce the size of the array that underlines an ArrayList object. |
|
| 39. |
Which Method Can Be Used To Increase The Capacity Of Arraylist Object Manually? |
|
Answer» When we add an element, the capacity of ArrayList OBJECT increases automatically, but we can INCREASE it manually to SPECIFIED length X by using function ensureCapacity(x); When we add an element, the capacity of ArrayList object increases automatically, but we can increase it manually to specified length x by using function ensureCapacity(x); |
|
| 40. |
Which Standard Collection Classes Implements A Dynamic Array? |
|
Answer» ARRAYLIST class IMPLEMENTS a DYNAMIC ARRAY by extending ABSTRACTLIST class. ArrayList class implements a dynamic array by extending AbstractList class. |
|