Sail E0 Webinar

MCQs

Total Questions : 97 | Page 5 of 10 pages
Question 41.


What is the output of this program?


import java.util.*;
class Collection_Algos {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
Collections.reverse(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
  1.    2 8 5 1
  2.    1 5 8 2
  3.    2
  4.    2 1 8 5
 Discuss Question
Answer: Option B. -> 1 5 8 2

Collections.reverse(list) reverses the given list, the list was 2->8->5->1 after 

Question 42.


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));
obj.removeAll(obj);
System.out.println(obj.isEmpty());
}
}
  1.    0
  2.    1
  3.    true
  4.    false
 Discuss Question
Answer: Option C. -> true

firstly elements 3, 2, 5 are entered in the  vector  obj, but when obj.removeAll(obj); 

Question 43.

Which of the following statements is correct?


  1.    Public method is accessible to all other classes in the hierarchy
  2.    Public method is accessible only to subclasses of its parent class
  3.    Public method can only be called by object of its class.
  4.    Public method can be accessed by calling object of the public class.
 Discuss Question
Answer: Option A. -> Public method is accessible to all other classes in the hierarchy

None.


Question 44.
class X2
{
public X2 x;
public static void main(String [] args)
{
X2 x2 = new X2(); /* Line 6 */
X2 x3 = new X2(); /* Line 7 */
x2.x = x3;
x3.x = x2;
x2 = new X2();
x3 = x2; /* Line 11 */
doComplexStuff();
}
}


after line 11 runs, how many objects are eligible for garbage collection?


  1.    0
  2.    1
  3.    2
  4.    3
 Discuss Question
Answer: Option C. -> 2

This is an example of the islands of isolated objects. By the time line 11 has run, the objects 

instantiated in lines 6 and 7 are referring to each other, but no live thread can reach either of them.


Question 45.


What is the output of this program?


abstract class A {
int i;
abstract void display();
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class Abstract_demo {
public static void main(String args[])
{
B obj = new B();
obj.j=2;
obj.display();
}
}
  1.    0
  2.    2
  3.    Runtime Error
  4.    Compilation Error
 Discuss Question
Answer: Option B. -> 2

class A is an abstract class, it contains a abstract function display(), the full implementation of 

display() method is given in its subclass B, Both the display functions are the same. Prototype 

of display() is defined in class A and its implementation is given in class B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2


Question 46.


What is the output of this program?


class equality {
int x;
int y;
boolean isequal() {
return(x == y);
}
}
class Output {
public static void main(String args[])
{
equality obj = new equality();
obj.x = 5;
obj.y = 5;
System.out.println(obj.isequal); }
}
  1.    false
  2.    true
  3.    0
  4.    1
 Discuss Question
Answer: Option B. -> true

None.
output:
$ javac Output.java
$ java Output
true


Question 47.


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));
obj.removeAll(obj);
System.out.println(obj.isEmpty());
}
}
  1.    0
  2.    1
  3.    true
  4.    false
 Discuss Question
Answer: Option C. -> true

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 rue.
Output:
$ javac vector.java
$ java vector
true


Question 48.


What is the output of this program?


import java.util.*;
class Collection_Algos {
public static void main(String args[]) {
LinkedList list = new LinkedList();
list.add(new Integer(2));
list.add(new Integer(8));
list.add(new Integer(5));
list.add(new Integer(1));
Iterator i = list.iterator();
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
  1.    2 8 5 1
  2.    1 5 8 2
  3.    2
  4.    2 1 8 5
 Discuss Question
Answer: Option A. -> 2 8 5 1

None.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
2 8 5 1

Question 49.

Which collection class allows you to associate its elements with key values, and allows 

you to retrieve objects in FIFO (first-in, first-out) sequence?


  1.    java.util.ArrayList
  2.    java.util.LinkedHashMap
  3.    java.util.HashMap
  4.    java.util.TreeMap
 Discuss Question
Answer: Option B. -> java.util.LinkedHashMap

LinkedHashMap is the collection class used for caching purposes. FIFO is another way 

to indicate caching behavior. To retrieve LinkedHashMap elements in cached order, use 

the values() method and iterate over the resultant collection.


Question 50.


What is the output of this program?


import java.util.*;
class Array {
public static void main(String args[]) {
int array[] = new int [5];
for (int i = 5; i > 0; i--)
array[5-i] = i;
Arrays.fill(array, 1, 4, 8);
for (int i = 0; i < 5 ; i++)
System.out.print(array[i]);
}
}
  1.    12885
  2.    12845
  3.    58881
  4.    54881
 Discuss Question
Answer: Option C. -> 58881

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
$ java Array
58881


Latest Videos

Latest Test Papers