Sail E0 Webinar

MCQs

Total Questions : 97 | Page 3 of 10 pages
Question 21.


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);
obj.clear(2);
System.out.print(obj);
}
}
  1.    {0, 1, 3, 4}
  2.    {0, 1, 2, 4}
  3.    {0, 1, 2, 3, 4}
  4.    {0, 0, 0, 3, 4}
 Discuss Question
Answer: Option A. -> {0, 1, 3, 4}

None.
Output:
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}


Question 22.

Which statement is true?


  1.    All objects that are eligible for garbage collection will be garbage collected by the garbage collector.
  2.    Objects with at least one reference will never be garbage collected.
  3.    Objects from a class with the finalize() method overridden will never be garbage collected.
  4.    Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.
 Discuss Question
Answer: Option D. -> Objects instantiated within anonymous inner classes are placed in the garbage collectible heap.

All objects are placed in the garbage collectible heap.

Option A is incorrect because the garbage collector makes no guarantees.

Option B is incorrect because islands of isolated objects can exist.

Option C is incorrect because finalize() has no such mystical powers.


Question 23.


What is the output of this program?


class area {
int width;
int length;
int area;
void area(int width, int length) {
this.width = width;
this.length = length;
}
}
class Output {
public static void main(String args[])
{
area obj = new area();
obj.area(5 , 6);
System.out.println(obj.length + " " + obj.width);
}
}
  1.    0 0
  2.    5 6
  3.    6 5
  4.    5 5
 Discuss Question
Answer: Option C. -> 6 5

this keyword can be used inside any method to refer to the current object. this is always a 

reference to the object on which the method was invoked.
output:
$ javac Output.java
$ java Output
6 5


Question 24.


What is the output of this program?


class box {
int width;
int height;
int length;
}
class mainclass {
public static void main(String args[])
{
box obj = new box();
System.out.println(obj);
}
}
  1.    0
  2.    1
  3.    Runtime error
  4.    Garbage value
 Discuss Question
Answer: Option D. -> Garbage value

Object obj of box class contains reference to the memory which was given to its class 

instances. Printing obj will print the address of the memory.
output:
$ javac mainclass.java
$ java mainclass
box@130671e


Question 25.


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);
obj.clear(2);
System.out.print(obj);
}
}
  1.    {0, 1, 3, 4}
  2.    {0, 1, 2, 4}
  3.    {0, 1, 2, 3, 4}
  4.    {0, 0, 0, 3, 4}
 Discuss Question
Answer: Option A. -> {0, 1, 3, 4}

None.
Output:
$ javac Bitset.java
$ java Bitset
{0, 1, 3, 4}


Question 26.

What is the numerical range of char?


  1.    0 to 32767
  2.    0 to 65535
  3.    -256 to 255
  4.    -32768 to 32767
 Discuss Question
Answer: Option B. -> 0 to 65535

The char type is integral but unsigned. The range of a variable of type char is from 0 to 

216-1 or 0 to 65535. Java characters are Unicode, which is a 16-bit encoding capable of

 representing a wide range of international characters. If the most significant nine bits of 

a char are 0, then the encoding is the same as seven-bit ASCII.


Question 27.


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 28.


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);
Collections.shuffle(list);
while(i.hasNext())
System.out.print(i.next() + " ");
}
}
  1.    2 8 5 1
  2.    1 5 8 2
  3.    1 2 5 8
  4.    Any random order
 Discuss Question
Answer: Option D. -> Any random order

shuffle - randomizes all the elements in a list.
Output:
$ javac Collection_Algos.java
$ java Collection_Algos
1 5 2 8
(output will be different on your system)



Question 29.


What is the output of this program?


class box {
int width;
int height;
int length;
}
class mainclass {
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}
  1.    1
  2.    2
  3.    Runtime error
  4.    Garbage value
 Discuss Question
Answer: Option A. -> 1

When we assign an object to another object of same type, all the elements of right side 

object gets copied to object on left side of equal to, =, operator.
output:
$ javac mainclass.java
$ java mainclass
1


Question 30.

Which statement is true?


  1.    Programs will not run out of memory.
  2.    Objects that will never again be used are eligible for garbage collection.
  3.    Objects that are referred to by other objects will never be garbage collected.
  4.    Objects that can be reached from a live thread will never be garbage collected.
 Discuss Question
Answer: Option D. -> Objects that can be reached from a live thread will never be garbage collected.

Option D is correct.

Option C is wrong. See the note above on Islands of Isolation (An object is eligible for garbage 

collection when no live thread can access it - even though there might be references to it).

Option B is wrong. "Never again be used" does not mean that there are no more references to 

the object.

Option A is wrong. Even though Java applications can run out of memory there another answer 

supplied that is more right.


Latest Videos

Latest Test Papers