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. |
Write a program in java to join two arraylists into one arraylist. |
|
Answer» We use the addAll() METHOD of the ArrayList class to add the contents of both the given arraylists into a new arraylist. //importing the required header filesimport JAVA.util.ArrayList;import java.util.Collections; public class Join_Lists { public static void main(STRING[] args) { //creating the first array list ArrayList<String> list_1 = new ArrayList<String>(); list_1.add("Monday"); list_1.add("TUESDAY"); list_1.add("Wednesday"); list_1.add("Thursday"); //printing the first array list System.out.println("The elements of the first array list is as follows : " + list_1); //creating the second array list ArrayList<String> list_2 = new ArrayList<String>(); list_2.add("Friday"); list_2.add("Saturday"); list_2.add("Sunday"); //printing the second array list System.out.println("The elements of the second array list is as follows : " + list_2); //creating the third array list ArrayList<String> joined_list = new ArrayList<String>(); joined_list.addAll(list_1);//adding the elements of the first array list joined_list.addAll(list_2);//adding the elements of the second array list System.out.println("The elements of the joined array list is as follows : " + joined_list); }}Output: The elements of the first array list is as follows : [Monday, Tuesday, Wednesday, Thursday]The elements of the second array list is as follows : [Friday, Saturday, Sunday]The elements of the joined array list is as follows : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]Useful Resources:Basics of Java Java Developer Skills Online Java Compiler Java MCQ |
|
| 2. |
Write a program in java to get the collection view of the values present in a HashMap. |
|
Answer» We use the values() function of the HashMap to GET the collection view. //importing the required header filesimport java.util.*; public class Collection_View { public static void main(String args[]){ HashMap<String,String> hash_map = new HashMap<String,String>();//creating an empty hash map //adding key values to the hash map hash_map.put("1","Monday"); hash_map.put("2","TUESDAY"); hash_map.put("3","Wednesday"); hash_map.put("4","Thursday"); hash_map.put("5","Friday"); hash_map.put("6","SATURDAY"); hash_map.put("7","Sunday"); //printing the original hash map System.out.println("The original hash map is as follows : " + hash_map); //printing the collection view of the hash map System.out.println("The collection view is as follows : " + hash_map.values()); }}The original hash map is as follows: {1=Monday, 2=Tuesday, 3=Wednesday, 4=Thursday, 5=Friday, 6=Saturday, 7=Sunday}The collection view is as follows : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] |
|
| 3. |
Write a program in Java to clone a Treeset to another Treeset. |
|
Answer» We use the clone() method of the TreeSet class to clone one TreeSet into another. //importing the required header filesimport java.util.TreeSet;IMPORT java.util.Iterator; public class Clone_Tree_Set { public STATIC void main(String[] ARGS) { TreeSet<String> tree_set = NEW TreeSet<String>();//creating an empty tree set //adding VALUES in the tree set tree_set.add("Monday"); tree_set.add("Tuesday"); tree_set.add("Wednesday"); tree_set.add("Thursday"); tree_set.add("Friday"); tree_set.add("Saturday"); tree_set.add("Sunday"); //printing the original tree set System.out.println("The original tree set is as follows : " + tree_set); //cloning the tree set TreeSet<String> cloned_tree_set = (TreeSet<String>)tree_set.clone(); //printing the cloned tree set System.out.println("The cloned tree set is as follows : " + cloned_tree_set); }}Output: The original tree set is as follows : [Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday]The cloned tree set is as follows : [Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday] |
|
| 4. |
Write a program to shuffle all the elements of a collection in Java. |
|
Answer» We use the shuffle() method of the Collections class. //importing the required header filesimport java.util.ArrayList;IMPORT java.util.Collections;import java.util.LIST;public class Shuffle_collection { public static void main(String[] argv) THROWS EXCEPTION { ArrayList<String> array_list = new ArrayList<String>();//creating an arraylist of strings array_list.add("Monday"); array_list.add("Tuesday"); array_list.add("Wednesday"); array_list.add("Thursday"); array_list.add("Friday"); array_list.add("Saturday"); array_list.add("Sunday"); Collections.shuffle(array_list);//shuffling the arraylist System.out.println("The shuffled ARRAY list is as follows : " + array_list);//printing the shuffled array list } }Output: The shuffled array list is as follows : [Thursday, Friday, Saturday, Wednesday, Tuesday, Sunday, Monday] |
|
| 5. |
Write a program in Java to display the contents of a HashTable using enumeration. |
|
Answer» We use the hasMoreElements and the nextElement methods of the Enumeration CLASS to ITERATE through the HashMap. //including the necessary header filesimport java.util.Enumeration;import java.util.Hashtable;public class Iterate_HashTable { public static void main(String[] args) { Hashtable hash_table = new Hashtable();//creating a hash table hash_table.put("1", "Monday"); hash_table.put("2", "Tuesday"); hash_table.put("3", "Wednesday"); hash_table.put("4", "Thursday"); hash_table.put("5", "Friday"); hash_table.put("6", "Saturday"); hash_table.put("7", "Sunday"); Enumeration enumeration_hash_table = hash_table.ELEMENTS();//creating an enumeration object //while loop runs until the hashtable has more entries in it while(enumeration_hash_table.hasMoreElements()) { System.out.println(enumeration_hash_table.nextElement()); } }}Output: SaturdayFridayThursdayWednesdayTuesdayMondaySundayWe notice that the order of the values is not the same as that of the order in which we inserted the key-value pair in the hashtable. This is because the elements of a Hashtable are not guaranteed to be in any particular sequence. The hashtable's implementation divides values into multiple buckets based on their Hashcode and internal implementation, which means that the same values may appear in a different order on different machines, runs, or VERSIONS of the framework. This is because Hashtables are designed to retrieve data by key rather than by order. |
|
| 6. |
Given an array in Java, convert it to a collection. |
|
Answer» We can convert an array to a COLLECTION using the asList() METHOD of the Arrays CLASS in Java. //including the required header filesimport java.util.*;public class Convert_Array_To_Collection { public static void main(String args[]) { //creating a sample array String sample_array[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; int length_array = sample_array.length; System.out.println("The INPUT elements are as follows : "); for(int i = 0; i < length_array; i ++) { System.out.print(sample_array[i] + " "); } System.out.println();// setting the print cursor to the next line List converted_list = Arrays.asList(sample_array);// converting the array to a list // print CONVERTED elements System.out.println("The converted list is as follows : " + converted_list); }}Output: The input elements are as follows : Monday Tuesday Wednesday Thursday Friday Saturday Sunday The converted list is as follows : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] |
|