Sail E0 Webinar

MCQs

Total Questions : 105 | Page 3 of 11 pages
Question 21.


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);
System.out.print(Arrays.binarySearch(array, 4));
}
}
  1.    2
  2.    3
  3.    4
  4.    5
 Discuss Question
Answer: Option B. -> 3

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


Question 22.


What is the output of this program?


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

obj1.and(obj2) returns an BitSet object which contains elements common to both the 

object obj1 and obj2 and stores this BitSet in invoking object that is obj1. Hence obj1 

contains 3 & 4.
Output:
$ javac Bitset.java
$ java Bitset
{3, 4}


Question 23.


What is the output of this program?


import java.util.*;
class properties {
public static void main(String args[]) {
Properties obj = new Properties();
obj.put("AB", new Integer(3));
obj.put("BC", new Integer(2));
obj.put("CD", new Integer(8));
System.out.print(obj.keySet());
}
}
  1.    {AB, BC, CD}
  2.    [AB, BC, CD]
  3.    [3, 2, 8]
  4.    {3, 2, 8}
 Discuss Question
Answer: Option B. -> [AB, BC, CD]

obj.keySet() returns a set containing all the keys used in properties object, here obj 

contains keys AB, BC, CD therefore obj.keySet() returns [AB, BC, CD].
Output:
$ javac properties.java
$ java properties
[AB, BC, CD]


Question 24.


What is the output of this program?


import java.util.*;
class stack {
public static void main(String args[]) {
Stack obj = new Stack();
obj.push(new Integer(3));
obj.push(new Integer(2));
obj.pop();
obj.push(new Integer(5));
System.out.println(obj);
}
}
  1.    [3, 5]
  2.    [3, 2]
  3.    [3, 2, 5]
  4.    [3, 5, 2]
 Discuss Question
Answer: Option A. -> [3, 5]

push() and pop() are standard functions of the class stack, push() inserts in the stack 

and pop removes from the  stack.  3 & 2  are inserted using  push()  the pop() is used 

which removes 2 from the stack  then again  push  is used  to  insert  5 hence  stack 

contains elements 3 & 5.

Output:
$ javac stack.java
$ java stack
[3, 5]


Question 25.


What is the output of this program?


class static_out {
static int x;
static int y;
void add(int a , int b){
x = a + b;
y = x + b;
}
}
class static_use {
public static void main(String args[])
{
static_out obj1 = new static_out();
static_out obj2 = new static_out();
int a = 2;
obj1.add(a, a + 1);
obj2.add(5, a);
System.out.println(obj1.x + " " + obj2.y);
}
}
  1.    7 7
  2.    6 6
  3.    7 9
  4.    9 7
 Discuss Question
Answer: Option C. -> 7 9

None.
output:
$ javac static_use.java
$ java static_use
7 9


Question 26.


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.ensureCapacity(3);
System.out.println(obj.size());
}
}
  1.    1
  2.    2
  3.    3
  4.    4
 Discuss Question
Answer: Option A. -> 1

Although obj.ensureCapacity(3); has manually increased the capacity of obj to 3 but the 

value is stored only at index 0, therefore obj.size() returns the total number of elements 

stored in the obj i:e 1, it has nothing to do with ensureCapacity().
Output:
$ javac Output.java
$ java Output
1


Question 27.


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.get("B"));
}
}
  1.    1
  2.    2
  3.    3
  4.    null
 Discuss Question
Answer: Option B. -> 2

obj.get("B") method is used to obtain the value associated with key "B", which is 2.
Output:
$ javac Maps.java
$ java Maps
2



Question 28.


What is the output of this program?


class Output {
public static void main(String args[]) {
double x = 3.1;
double y = 4.5;
double z = Math.max( x, y );
System.out.print(z);
}
}
  1.    true
  2.    false
  3.    3.1
  4.    4.5
 Discuss Question
Answer: Option D. -> 4.5

None.
Output:
$ javac Output.java
$ java Output
4.5


Question 29.


What is the output of this program?


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

HashSet obj creates an hash object which implements Set interface, obj.size() gives the 

number of elements stored in the object obj which in this case is 3.
Output:
$ javac Output.java
$ java Output
[A, B, C] 3


Question 30.


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