Friday, 10 March 2017


"Collection is the super most Interface in the collections framework and present into the "java.util" package.Interviewer will ask you like super most inteface is Iterable in JCF as collection interface extends the Iterable inteface then you should say as Iterable Inteface is present into java.lang package not in java.util.All interfaces(Data Structures) like List,Queue,set and Map commonly extending the Collection interface."
Note: Map is not a part of java collections framework but still it is extending the Collection interface.

Q 1. What is a Collection? 
Ans :- "A collection is a object in java,which contains multiple objects may be same type or different type to group into a single unit". 
Sometimes ,collection is also called a container which contains other objects called elements.

Q 2. What does stand for JCF in java?
Ans :- "JCF stands for 'Java collections framework' is a unified architecture for representing and manipulating collections
(eg. ArrayList, LinkedList) those are commonly implementing the Collection interface."

Q 3. What are the benefits of JCF and why do we need JCF in java ?
Ans:
1)Reduce the programming efforts
2)Increase the program speed and quality
3)Reducing the efforts in  designing the new API's

Q 4.What does mean by collections ?
Ans :-"Collections is a class in java which implements the data structures like List,Set,Queue and used to store ,
retrieve ,delete ,searching, sorting data"

Q 5. How will you insert different different kinds of objects into ArrayList:-
Inserting different different kinds of objects into ArrayList and verifying that the object is present or not.
package AllCollections;
import java.lang.*;
import java.util.*;

interface Fruit{

}
///simple a interface creating
class Apple implements Fruit{

//Creating a class which is implementing the above interface (rule :class ->
implements interface)
public class DemoOfArrayList 
{
public static void main(String[] args) 
{
// Inserting different-2 object type into AL
ArrayList list = new ArrayList();
list.add(10);//adding a integer object
list.add(true);//adding a boolean object
list.add("hello");//adding a string object
list.add(new String("Bangalore"));
Apple ap =new Apple();
list.add(ap);
//Checking a object into a AL
System.out.println(list.contains(10));
System.out.println(list.contains("Viren"));
System.out.println(list.contains("Bangalore"));
System.out.println(list.contains(ap));
}
}

Q 6. Write a program to convert a ArrayList into Array:

package AllCollections;
import java.util.ArrayList;
import java.util.*;
public class ListToArrayConversion
{
static ArrayList list; //Defining a ArrayList and making as generic
static Object obj[]; //Defining a Array type object

//Declare a static method
static ArrayList getList()
{
System.out.println("Enter the list objects...");
list =new ArrayList();
list.add(10);
list.add(20);
list.add("Hello");
list.add(new String ("viren"));
System.out.println("Printing the List.."+"\n"+list);
return list;
}

static void convertListToArray()
{
ArrayList l=getList();
obj=l.toArray();
System.out.println("Printing the Array values .."+"\n");
for (Object o:obj)
{
System.out.println(o);    
}
}
public static void main(String arg[])
{
convertListToArray();
}
}

 Q 7 . Write a program to sort an array
import java.util.*;
public class DemoOfArraySorting
{
public static void main(String[] args)
{
int a[]={10,25,56,23,58,30};
System.out.println("Printing the arraya before sorting ..");
for (int x:a)
{
System.out.println(x);
}
Arrays.sort(a);
System.out.println("Printing the array after sorting..");
for (int y:a)
{
System.out.println(y);
}
}

 Q 8. The difference between Iterator and ListIterator:-
The difference between Iterator and ListIterator :
Using Iterator,we can traverse the List objects in forward direction only with hasNext() and next() methods where using ListIterator we can traverse the List Objects into both direction as in forward using hasNext() and next() methods and reverse directions with hasPrevious() and previous() methods.

Q 9.Write a program to show the difference between Iterator and ListIterator interface ?
package AllCollections;
import java.util.*;
public class DemoOfIteratorAndListIterator
{
static ArrayList list;
static Iterator itr;
static ListIteratorlsitr;
static ArrayList getList()
{
System.out.println("Adding elements into list..");
list= new ArrayList();
list.add(10);
list.add("Hello");
list.add(20);
System.out.println(list);
return list;
}
//demo of Iterator
static void demoOfIterator()
{
ArrayList list1=getList();
itr=list1.iterator();
while (itr.hasNext())
{
System.out.println(itr.next());   }
}
//demo of ListIterator
static void demoOfListIterator()
{
ArrayList list1=getList();
lsitr =list1.listIterator(); //List Iterator
while (lsitr.hasNext())
{
System.out.println(lsitr.next());
System.out.println("Printing the next index number" +lsitr.nextIndex());
}
while (lsitr.hasPrevious()) {
System.out.println(lsitr.previous());
System.out.println("Printing the previous index number" +lsitr.previousIndex());
// -1 0 1 2
}
}
public static void main(String arg[])
{
demoOfIterator();
demoOfListIterator();
}
}

Q.10 How will you compare two lists in java ?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

public class Test01 {


 public static void compareLists(ArrayList<Integer> a, ArrayList<Integer> b)
 {
  boolean result = true;
  if(a.size()==b.size())
  {
  if(a.equals(b))
  {
   System.out.println("Equal");
  }
  else
  {
   System.out.println("not Equal");
  }
 }
}
 public static void main(String arg[])
 {
 
  ArrayList<Integer> al1= new ArrayList<Integer>(Arrays.asList(10,20,50));
  ArrayList<Integer> al2= new ArrayList<Integer>(Arrays.asList(10,20,50));
  compareLists(al1,al2);
  System.out.println(al1.toString());
}
}

Q 11. What is a Map in java?
Ans :- "Map is an Interface in java that maps keys to corresponding values and key-value pairs called Entry." It is not child interface of the Collection interface.

Q 12. When should we use the Map ?

Ans:"Suppose we have a group of individual objects and we want to represent to them as key-value pairs then we should go for Map."

Eg. Suppose, I have 100 students and I want to store them according to their roll number then I will use Map to store so that searching will easy to access the particular student.


Roll No      Name

---------      --------
101             Ravi
203             Ram
                 
Here, both key(Roll No) and value(Name) are objects and duplicate keys are not allowed.

import java.util.HashMap;

import java.util.Map;
public class MapDemo {
 public static void main(String[] args) {
 
 Map<Integer,String> hash = new HashMap<Integer, String>();
 hash.put(101, "Ravi");
 hash.put(302, "Ram");
 System.out.println("Map elemnets :"+hash);

 }

}
O/P
Map elemnets :{101=Ravi, 302=Ram}

Q.13 What is HashMap in java ?

Ans: "HashMap is a non-synchronized class in java which allows us to store object as key value pair and implements the Map Interface."
Q.14 Please explain properties of the HashMap ?
Ans:
1. It is not thread safe because it is unsynchronized.
2. It allows one null key and any number of null values.
3. It does not follow any order which means it does not return the keys and values in the same order in which they have been inserted into the HashMap.
4.Hash Map objects are iterate through Iterator.
5. Hash Map is much faster and uses less memory.
Q. 15 Why should we use HashMap ?
Ans : The purpose of  a map is to store items based on a key that can be used to retrieve the item at a later point. It is a data structure, based on hashing principle.

import java.util.HashMap;

import java.util.Map;
public class MapDemo {

 public static void main(String[] args) {

  // TODO Auto-generated method stub
 
 HashMap<Integer,String> hash = new HashMap<Integer, String>();
 hash.put(101, "Ravi");
 hash.put(302, "Ram");
 System.out.println("Map elemnets :"+hash);
    }
}
O/P

Map elemnets :{101=Ravi, 302=Ram}
Go Back

No comments:

Post a Comment

Download Android SDK

Download Android SDK tools Follow me on Instagram - https://www.instagram.com/virenautomationtesting/ Follow me on Twitter- https://twitt...