1. Which of the following are false about Collections and Collection?
Ans: Collection is a special type of collection which holds set of collections
2. The following method can be used to add elements to a Map
Ans: put
3.______class is synchronized
Ans: Vector
4. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not?
Ans: java.util.ArrayList
5. Select all checked exceptions. a) ClassCastException b) IndexOutOfBoundException c) FileNotFoundException d) IOException
Ans: c,d
6. Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
Ans: java.util.Hashtable
7. What is the output of below java program?
import java.util.*;
import java.lang.*;
class MainClass
{
public static void main(String args[])
{
int[] arr={1,2,3,4,5,6,7,8,9,10};
System.out.println("array element at 0 is.."+arr[0]);
}
}
Ans: array element at 0 is..1
**8. Find the right syntax in the below Array instantiation statements
Ans: list1=new Object[5]
9. Select one or more true statements: A. Set does not store duplicate values B. All Set implementations are sorted. C. HashMap and Hashtble both store key value pairs, Hashtable we would use in case of synchronization requirements. D. List implementations dynamically grow in size, allow duplicates and are not sorted
Ans: A,C,D
10. 1. import java.util.*;
2. public class Example{
3. public static void main(String[] args){
4. //insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8.}
9.}
Which code, inserted at line 4. guarantees that this program will output[1,2]?
Ans: Set set=new TreeSet();
11. Which method gives the total number of elements in an ArrayList?
Ans: ArrayList.size()
12.
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test
{
public static void main(String args[])
{
List list=new ArrayList();
list.add(10); list.add(40);
list.add(20); list.add(15);
list.add(80);
Collections.reverse(list);
System.out.println(list);
}
}
Ans: [80,15,20,40,10]
13.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Employee implements Comparator
{
private String name;
private int age;
Employee(){}
Employee(String n,int a)
{
name=n;
age=a;
}
public String getEmployeeName()
{
return name;
}
public int getEmployeeAge()
{
return age;
}
public int compare(Employee d, Employee d1)
{
return d1.age-d.age;
}
}
What is the output of the above program
public class Example{
public static void main(String args[])
{
List list=new ArrayList();
list.add(new Employee("Anu",29));
list.add(new Employee("Binu",25));
list.add(new Employee("Janu",22));
list.add(new Employee("Renu",27));
list.add(new Employee("Tinu",23));
Collections.sort(list,newEmployee());
for(Employee a:list)
System.out.println(a.getEmployeeName()+":");
}
}
Ans: Anu:Renu:Binu:Tinu:Janu:
14. public class Foo{
public static void main(String args[]){
try{return;}
finally{System.out.println("Finally");}}}
What would be the output of the code mentioned above?
Ans: Finally
15. Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
Ans: java.util.Hashtable
16. Map<String,String>obj=new HashMap<String,String>();obj.put("City","Goa");
obj.put("City","Simla"); obj.put("City","Pune"); obj.put("CityName","Simla"); Select one or more true statements for above code
Ans: Following values will be stored in HashMap{CityName=Simla,City=Pune}
17. public class ThrowsDemo
{
static void throwMethod()
{
System.out.println("Inside throwMethod");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try{throwMethod();
catch(IllegalAccessException e)
{
System.out.println("Caught"+e);
}
}
}
What would be the output of the code mentioned above. Assume the code is present in the main method
Ans: Compilation error
18.
import java.util.List;
public class Test
{
public static void main(String args[])
{
List myList=new ArrayList();
for(int i=0;i<10;i++)
{
myList.add("String"+i);
}
int count=0;
Iterator iter=myList.iterator();
while(iter.hasNext())
{
count++;
if(count%2==0)
myList.remove(count);
System.out.println(iter.next()+"");
}
}
}
Ans: Exception occurs at runtime because the list is being ready and modified simultaneously
19. What will be the output of the following program?
import java.util.*;
public class Test
{
public static void main(String args[])
{
List arrayList=new ArrayList();
arrayList.add(1);
arrayList.add(2);
arrayList(5);
for(int i:arrayList)
{
if(i==5)
{
arrayList.remove(2);
}
}
System.out.println(arrayList);
}
}
Ans: run time exception
20. What is the output of this code?
public class GetSetViewOfKeysFromHashMapExample
{
public static void main(String args[])
{
HashMap hMap=new HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Set st=hMap.keySet();
Iterator itr=st.iterator();
while(itr.hasNext())
System.out.println(itr.next());
st.remove("2");
boolean blnExists=hMap.containsKey("2");
System.out.println(nlnExists);
}
}
Ans: 1 2 3 True
21. What is the output of below java program?
import java.util.*;
import java.lang.*;
class MainClass
{
public static void main(String args[])
{
int[] arr={1,2,3,4,5,6,7,8,9,10};
System.out.println("array element at 0 is.."+arr[-1]);
}
}
Ans: run time error, array index out of bounds exception
22. What is the output of this program?
import java.util.*;
class Test
{
public static void main(String args[])
{
TreeSet ts=new TreeSet();
ts.add("3");
ts.add("9");
ts.add("1");
ts.add("4");
ts.add("8");
System.out.println(ts);
}
}
Ans: [1,3,4,8,9]
23. What is the output of the following program?
public class Emp
{
private int age;
public Emp(int age)
{
super();
this.age=age;
}
public int hashCode()
{
return age;
}
public boolean equals(Object obj)
{
boolean flag=false;
Emp emp=(Emp)obj;
if(emp.age==age)
flag=true;
return flag;
}
}
public class TestEmp
{
public static void main(String args[])
{
Emp emp1=new Emp(23);
Emp emp2=new Emp(24);
Emp emp3=new Emp(25);
Emp emp4=new Emp(26);
Emp emp5=new Emp(27);
HashSet hs=new HashSet();
hs.add(emp1);
hs.add(emp2);
hs.add(emp3);
hs.add(emp4);
hs.add(emp5);
System.out.println(hs.size());
System.out.println(hs.contains(new Emp(25)));
System.out.println(hs.remove(new Emp(24)));
System.out.println(hs.size());
}
}
Ans: 5 true true 4
**23. Which statement is true about set variable in the following program
import java.util.*;
public class TestSet
{
enum Example
{
ONE,TWO,THREE
};
public static void main(String a[])
{
Collection col1=new ArrayList();
col1.add(Example.THREE);
col1.add(Example.THREE);
col1.add(Example.THREE);
col1.add(Example.TWO);
col1.add(Example.ONE);
Set set=new HashSet(col1);
}
}
Ans: The set variable contains all six elements from col1 collection and the ordering of the elements is NOT preserved
24. Which interface does java.util.Hashtable implement?
Ans: java.util.Collection
25. Which of them are the correct syntax for instantiating arrays
Ans: int[] arr=new int[3]
26. What will be the output of the following program?
class Gen
{
T obj;
Gen(T o)
{
obj=o;
}
T get()
{
return obj;
}}
public class GenericsDemo
{
public static void main(String args[])
{
Gen i1=new Gen(new Integer(10));
int i=i1.get();
System.out.println(i);
}
}
Ans: 10.0
27. Which collection class allows you to access its elements by associating a key with a key with an element's value, and provides synchronization?
Ans: java.util.Hashtable
28. What is the output of the following program?
public class Test
{
public static void main(String args[])
{
String str1="Strings are immutable";
String str2="Strings are immutable";
String str3="Integers are not immutable";
int result=str1.compareTo(str2);
if(result==0)
{
System.out.println("It's zero!");
}
result=str2.compareTo(str3);
if(result<0)
{
System.out.print("It's less than zero");
}
result=str3.compareTo(str1);
if(result>0)
{
System.out.print("It's greater than zero");
}
}
}
Ans: It's zero!
29. What will be the output of the below program?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test
{
public static void main(String args[])
{
List myList=new ArrayList();
for(int i=0;i<10;i++)
{
myList.add("String"+i);
}
int count=0;
Iterator iter=myList.iterator();
while(iter.hasNext())
{
count++;
if(count%2==0)
myList.remove(count);
System.out.println(iter.next()+"");
}
}
}
Ans: Exception occurs at runtime because the list is being ready and modified simultaneously
30.
import java.util.*;
public class LetterASort
{
public static void main(String[] args)
{
ArrayList strings=new ArrayList();
strings.add("aAaA");
strings.add("AaA");
strings.add("aAa");
strings.add("AAaa");
Collections.sort(strings);
for(String s:strings)
{
System.out.print(s+"");
}
}
}
What is the result?
Ans: AAaa AaA aAa aAaA
31. How to find the length of arrayList ArrayList arr=new ArrayList()
Ans: arr.size()
32. Which of these interface is not a part of Java's collection framework?
Ans: SortedList
33. Given:
TreeSet map=new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it=map.iterator();
while(it.hasNext())
{
System.out.print(it.next()+"");
}
What is the result?
Ans: four one three two
Ans: Collection is a special type of collection which holds set of collections
2. The following method can be used to add elements to a Map
Ans: put
3.______class is synchronized
Ans: Vector
4. Which collection class allows you to grow or shrink its size and provides indexed access to its elements, but whose methods are not?
Ans: java.util.ArrayList
5. Select all checked exceptions. a) ClassCastException b) IndexOutOfBoundException c) FileNotFoundException d) IOException
Ans: c,d
6. Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
Ans: java.util.Hashtable
7. What is the output of below java program?
import java.util.*;
import java.lang.*;
class MainClass
{
public static void main(String args[])
{
int[] arr={1,2,3,4,5,6,7,8,9,10};
System.out.println("array element at 0 is.."+arr[0]);
}
}
Ans: array element at 0 is..1
**8. Find the right syntax in the below Array instantiation statements
Ans: list1=new Object[5]
9. Select one or more true statements: A. Set does not store duplicate values B. All Set implementations are sorted. C. HashMap and Hashtble both store key value pairs, Hashtable we would use in case of synchronization requirements. D. List implementations dynamically grow in size, allow duplicates and are not sorted
Ans: A,C,D
10. 1. import java.util.*;
2. public class Example{
3. public static void main(String[] args){
4. //insert code here
5. set.add(new Integer(2));
6. set.add(new Integer(1));
7. System.out.println(set);
8.}
9.}
Which code, inserted at line 4. guarantees that this program will output[1,2]?
Ans: Set set=new TreeSet();
11. Which method gives the total number of elements in an ArrayList?
Ans: ArrayList.size()
12.
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test
{
public static void main(String args[])
{
List list=new ArrayList();
list.add(10); list.add(40);
list.add(20); list.add(15);
list.add(80);
Collections.reverse(list);
System.out.println(list);
}
}
Ans: [80,15,20,40,10]
13.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class Employee implements Comparator
{
private String name;
private int age;
Employee(){}
Employee(String n,int a)
{
name=n;
age=a;
}
public String getEmployeeName()
{
return name;
}
public int getEmployeeAge()
{
return age;
}
public int compare(Employee d, Employee d1)
{
return d1.age-d.age;
}
}
What is the output of the above program
public class Example{
public static void main(String args[])
{
List list=new ArrayList();
list.add(new Employee("Anu",29));
list.add(new Employee("Binu",25));
list.add(new Employee("Janu",22));
list.add(new Employee("Renu",27));
list.add(new Employee("Tinu",23));
Collections.sort(list,newEmployee());
for(Employee a:list)
System.out.println(a.getEmployeeName()+":");
}
}
Ans: Anu:Renu:Binu:Tinu:Janu:
14. public class Foo{
public static void main(String args[]){
try{return;}
finally{System.out.println("Finally");}}}
What would be the output of the code mentioned above?
Ans: Finally
15. Which collection class allows you to access its elements by associating a key with an element's value, and provides synchronization?
Ans: java.util.Hashtable
16. Map<String,String>obj=new HashMap<String,String>();obj.put("City","Goa");
obj.put("City","Simla"); obj.put("City","Pune"); obj.put("CityName","Simla"); Select one or more true statements for above code
Ans: Following values will be stored in HashMap{CityName=Simla,City=Pune}
17. public class ThrowsDemo
{
static void throwMethod()
{
System.out.println("Inside throwMethod");
throw new IllegalAccessException("demo");
}
public static void main(String args[])
{
try{throwMethod();
catch(IllegalAccessException e)
{
System.out.println("Caught"+e);
}
}
}
What would be the output of the code mentioned above. Assume the code is present in the main method
Ans: Compilation error
18.
import java.util.List;
public class Test
{
public static void main(String args[])
{
List myList=new ArrayList();
for(int i=0;i<10;i++)
{
myList.add("String"+i);
}
int count=0;
Iterator iter=myList.iterator();
while(iter.hasNext())
{
count++;
if(count%2==0)
myList.remove(count);
System.out.println(iter.next()+"");
}
}
}
Ans: Exception occurs at runtime because the list is being ready and modified simultaneously
19. What will be the output of the following program?
import java.util.*;
public class Test
{
public static void main(String args[])
{
List arrayList=new ArrayList();
arrayList.add(1);
arrayList.add(2);
arrayList(5);
for(int i:arrayList)
{
if(i==5)
{
arrayList.remove(2);
}
}
System.out.println(arrayList);
}
}
Ans: run time exception
20. What is the output of this code?
public class GetSetViewOfKeysFromHashMapExample
{
public static void main(String args[])
{
HashMap hMap=new HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Set st=hMap.keySet();
Iterator itr=st.iterator();
while(itr.hasNext())
System.out.println(itr.next());
st.remove("2");
boolean blnExists=hMap.containsKey("2");
System.out.println(nlnExists);
}
}
Ans: 1 2 3 True
21. What is the output of below java program?
import java.util.*;
import java.lang.*;
class MainClass
{
public static void main(String args[])
{
int[] arr={1,2,3,4,5,6,7,8,9,10};
System.out.println("array element at 0 is.."+arr[-1]);
}
}
Ans: run time error, array index out of bounds exception
22. What is the output of this program?
import java.util.*;
class Test
{
public static void main(String args[])
{
TreeSet ts=new TreeSet();
ts.add("3");
ts.add("9");
ts.add("1");
ts.add("4");
ts.add("8");
System.out.println(ts);
}
}
Ans: [1,3,4,8,9]
23. What is the output of the following program?
public class Emp
{
private int age;
public Emp(int age)
{
super();
this.age=age;
}
public int hashCode()
{
return age;
}
public boolean equals(Object obj)
{
boolean flag=false;
Emp emp=(Emp)obj;
if(emp.age==age)
flag=true;
return flag;
}
}
public class TestEmp
{
public static void main(String args[])
{
Emp emp1=new Emp(23);
Emp emp2=new Emp(24);
Emp emp3=new Emp(25);
Emp emp4=new Emp(26);
Emp emp5=new Emp(27);
HashSet hs=new HashSet();
hs.add(emp1);
hs.add(emp2);
hs.add(emp3);
hs.add(emp4);
hs.add(emp5);
System.out.println(hs.size());
System.out.println(hs.contains(new Emp(25)));
System.out.println(hs.remove(new Emp(24)));
System.out.println(hs.size());
}
}
Ans: 5 true true 4
**23. Which statement is true about set variable in the following program
import java.util.*;
public class TestSet
{
enum Example
{
ONE,TWO,THREE
};
public static void main(String a[])
{
Collection col1=new ArrayList();
col1.add(Example.THREE);
col1.add(Example.THREE);
col1.add(Example.THREE);
col1.add(Example.TWO);
col1.add(Example.ONE);
Set set=new HashSet(col1);
}
}
Ans: The set variable contains all six elements from col1 collection and the ordering of the elements is NOT preserved
24. Which interface does java.util.Hashtable implement?
Ans: java.util.Collection
25. Which of them are the correct syntax for instantiating arrays
Ans: int[] arr=new int[3]
26. What will be the output of the following program?
class Gen
{
T obj;
Gen(T o)
{
obj=o;
}
T get()
{
return obj;
}}
public class GenericsDemo
{
public static void main(String args[])
{
Gen i1=new Gen(new Integer(10));
int i=i1.get();
System.out.println(i);
}
}
Ans: 10.0
27. Which collection class allows you to access its elements by associating a key with a key with an element's value, and provides synchronization?
Ans: java.util.Hashtable
28. What is the output of the following program?
public class Test
{
public static void main(String args[])
{
String str1="Strings are immutable";
String str2="Strings are immutable";
String str3="Integers are not immutable";
int result=str1.compareTo(str2);
if(result==0)
{
System.out.println("It's zero!");
}
result=str2.compareTo(str3);
if(result<0)
{
System.out.print("It's less than zero");
}
result=str3.compareTo(str1);
if(result>0)
{
System.out.print("It's greater than zero");
}
}
}
Ans: It's zero!
29. What will be the output of the below program?
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test
{
public static void main(String args[])
{
List myList=new ArrayList();
for(int i=0;i<10;i++)
{
myList.add("String"+i);
}
int count=0;
Iterator iter=myList.iterator();
while(iter.hasNext())
{
count++;
if(count%2==0)
myList.remove(count);
System.out.println(iter.next()+"");
}
}
}
Ans: Exception occurs at runtime because the list is being ready and modified simultaneously
30.
import java.util.*;
public class LetterASort
{
public static void main(String[] args)
{
ArrayList strings=new ArrayList();
strings.add("aAaA");
strings.add("AaA");
strings.add("aAa");
strings.add("AAaa");
Collections.sort(strings);
for(String s:strings)
{
System.out.print(s+"");
}
}
}
What is the result?
Ans: AAaa AaA aAa aAaA
31. How to find the length of arrayList ArrayList arr=new ArrayList()
Ans: arr.size()
32. Which of these interface is not a part of Java's collection framework?
Ans: SortedList
33. Given:
TreeSet map=new TreeSet();
map.add("one");
map.add("two");
map.add("three");
map.add("four");
map.add("one");
Iterator it=map.iterator();
while(it.hasNext())
{
System.out.print(it.next()+"");
}
What is the result?
Ans: four one three two
Thank you for sharing this powerful article, your explanation is clear and very easy to understand. Please kindly visit our site to get more information about IT solution.SEO Services in Pakistan
ReplyDelete