Sail E0 Webinar

MCQs

Total Questions : 105 | Page 5 of 11 pages
Question 41.


What is the output of this program?


import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
obj.remove(new String("A"));
System.out.print(obj);
}
}
  1.    {C=8, B=2}
  2.    [C=8, B=2]
  3.    {A=3, C=8, B=2}
  4.    [A=3, C=8, B=2]
 Discuss Question
Answer: Option B. -> [C=8, B=2]

None.
Output:
$ javac hashtable.java
$ java hashtable
{C=8, B=2}


Question 42.


What is the output of this program?


import java.util.*;
class Bitset {
public static void main(String args[]) {
BitSet obj = new BitSet(5);
for (int i = 0; i < 5; ++i)
obj.set(i);
System.out.print(obj.get(3));
}
}
  1.    2
  2.    3
  3.    4
  4.    5
 Discuss Question
Answer: Option A. -> 2

None.
Output:
$ javac Bitset.java
$ java Bitset
2


Question 43.


What is the output of this program?


import java.util.*;
class Arraylist {
public static void main(String args[]) {
ArrayList obj = new ArrayList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.add(1, "D");
System.out.println(obj);
}
}
  1.    [A, B, C, D]
  2.    [A, D, B, C]
  3.    [A, D, C]
  4.    [A, B, C]
 Discuss Question
Answer: Option B. -> [A, D, B, C]

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
$ java Arraylist
[A, D, B, C]


Question 44.


What is the output of this program?


class access{
public int x;
static int y;
void cal(int a, int b){
x += a ;
y += b;
}
}
class static_specifier {
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.y = 0;
obj1.cal(1, 2);
obj2.x = 0;
obj2.cal(2, 3);
System.out.println(obj1.x + " " + obj2.y);
}
}
  1.    1 2
  2.    2 3
  3.    3 2
  4.    1 5
 Discuss Question
Answer: Option D. -> 1 5

None.
output:
$ javac static_specifier.java
$ java static_specifier
1 5


Question 45.


What is the output of this program?


import java.util.*;
class Linkedlist {
public static void main(String args[]) {
LinkedList obj = new LinkedList();
obj.add("A");
obj.add("B");
obj.add("C");
obj.addFirst("D");
System.out.println(obj);
}
}
  1.    [A, B, C]
  2.    [D, B, C]
  3.    [A, B, C, D]
  4.    [D, A, B, C]
 Discuss Question
Answer: Option D. -> [D, A, B, C]

obj.addFirst("D") method is used to add 'D' to the start of a LinkedList object obj.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[D, A, B, C]



Question 46.


What is the output of this program?


class A {
int x;
int y;
void display() {
System.out.print(x + " " + y);
}
}
class Output {
public static void main(String args[]) {
A obj1 = new A();
A obj2 = new A();
obj1.x = 1;
obj1.y = 2;
obj2 = obj1.clone();
obj1.display();
obj2.display();
}
}
  1.    1 2 0 0
  2.    1 2 1 2
  3.    0 0 0 0
  4.    System Dependent
 Discuss Question
Answer: Option B. -> 1 2 1 2

clone() method of object class is used to generate duplicate copy of the object on which it 

is called. Copy of obj1 is generated and stored in obj2.
Output:
$ javac Output.java
$ java Output
1 2 1 2


Question 47.


What is the output of this program?


import java.util.*;
class Maps {
public static void main(String args[]) {
HashMap obj = new HashMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj);
}
}
  1.    {A 1, B 1, C 1}
  2.    {A, B, C}
  3.    {A-1, B-1, C-1}
  4.    {A=1, B=2, C=3}
 Discuss Question
Answer: Option D. -> {A=1, B=2, C=3}

None.
Output:
$ javac Maps.java
$ java Maps
{A=1, B=2, C=3}


Question 48.


What is the output of this program?


import java.util.*;
class vector {
public static void main(String args[]) {
Vector obj = new Vector(4,2);
obj.addElement(new Integer(3));
obj.addElement(new Integer(2));
obj.addElement(new Integer(5));
System.out.println(obj.capacity());
}
}
  1.    2
  2.    3
  3.    4
  4.    6
 Discuss Question
Answer: Option C. -> 4

None.
Output:
$ javac vector.java
$ java vector
4


Question 49.


What is the output of this program?


import java.util.*;
class Arraylist {
public static void main(String args[]) {
ArrayList obj1 = new ArrayList();
ArrayList obj2 = new ArrayList();
obj1.add("A");
obj1.add("B");
obj2.add("A");
obj2.add(1, "B");
System.out.println(obj1.equals(obj2));
}
}
  1.    0
  2.    1
  3.    true
  4.    false
 Discuss Question
Answer: Option C. -> true

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
$ java Arraylist
true


Question 50.


What is the output of this program?


import java.util.*;
class hashtable {
public static void main(String args[]) {
Hashtable obj = new Hashtable();
obj.put("A", new Integer(3));
obj.put("B", new Integer(2));
obj.put("C", new Integer(8));
obj.clear();
System.out.print(obj.size());
}
}
  1.    0
  2.    1
  3.    2
  4.    3
 Discuss Question
Answer: Option A. -> 0

None.
Output:
$ javac hashtable.java
$ java hashtable
0


Latest Videos

Latest Test Papers