Sail E0 Webinar

MCQs

Total Questions : 105 | Page 4 of 11 pages
Question 31.


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


Question 32.


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));
System.out.print(obj.toString());
}
}
  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 C. -> {A=3, C=8, B=2}

obj.toString returns String equivalent of the hashtable, which can also be obtained by 

simply writing System.out.print(obj); as print system automatically coverts the obj to

string equivalent.
Output:
$ javac hashtable.java
$ java hashtable
{A=3, C=8, B=2}


Question 33.


What is the output of this program?


import java.util.*;
class date {
public static void main(String args[]) {
Date obj = new Date();
System.out.print(obj);
}
}
  1.    Prints Present Date
  2.    Runtime Error
  3.    Any Garbage Value
  4.    Prints Present Time & Date
 Discuss Question
Answer: Option D. -> Prints Present Time & Date

None.
Output:
$ javac date.java
$ java date
Tue Jun 11 11:29:57 PDT 2013


Question 34.


What is the output of this program?


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

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


Question 35.


What is the output of this program?


class access{
static int x;
void increment(){
x++;
}
}
class static_use {
public static void main(String args[])
{
access obj1 = new access();
access obj2 = new access();
obj1.x = 0;
obj1.increment();
obj2.increment();
System.out.println(obj1.x + " " + obj2.x);
}
}
  1.    1 2
  2.    1 1
  3.    2 2
  4.    Compilation Error
 Discuss Question
Answer: Option C. -> 2 2

All objects of class share same static variable, all the objects share same copy of static members, 

obj1.x and obj2.x refer to same element of class which has been incremented twice and its value is 2.
output:
$ javac static_use.java
$ java static_use
2 2


Question 36.


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.sort(array);
for (int i = 0; i < 5; ++i)
System.out.print(array[i]);;
}
}
  1.    12345
  2.    54321
  3.    1234
  4.    5432
 Discuss Question
Answer: Option B. -> 54321

Arrays.sort(array) method sorts the array into 1,2,3,4,5.
Output:
$ javac Array.java
$ java Array
12345


Question 37.


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.removeFirst();
System.out.println(obj);
}
}
  1.    [A, B]
  2.    [B, C]
  3.    [A, B, C, D]
  4.    [A, B, C]
 Discuss Question
Answer: Option B. -> [B, C]

None.
Output:
$ javac Linkedlist.java
$ java Linkedlist
[B, C]


Question 38.


What is the output of this program?


class Output {
public static void main(String args[]) {
int x = 3.14;
int y = (int) Math.abs(x);
System.out.print(y);
}
}
  1.    0
  2.    3
  3.    3.0
  4.    3.1
 Discuss Question
Answer: Option B. -> 3

None.
Output:
$ javac Output.java
$ java Output
3


Question 39.


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.keySet());
}
}
  1.    [A, B, C]
  2.    {A, B, C}
  3.    {1, 2, 3}
  4.    [1, 2, 3]
 Discuss Question
Answer: Option A. -> [A, B, C]

keySet() method returns a set containing all the keys used in the invoking map. Here 

keys are characters A, B & C. 1, 2, 3 are the values given to these keys.
Output:
$ javac Maps.java
$ java Maps
[A, B, C]


Question 40.


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.insertElementAt(new Integer(8), 2);
System.out.println(obj);
}
}
  1.    [3, 2, 6]
  2.    [3, 2, 8]
  3.    [3, 2, 6, 8]
  4.    [3, 2, 8, 6]
 Discuss Question
Answer: Option D. -> [3, 2, 8, 6]

None.
Output:
$ javac vector.java
$ java vector
[3, 2, 8, 6]


Latest Videos

Latest Test Papers