Sail E0 Webinar

MCQs

Total Questions : 97 | Page 6 of 10 pages
Question 51.
public Object m()
{
Object o = new Float(3.14F);
Object [] oa = new Object[l];
oa[0] = o; /* Line 5 */
o = null; /* Line 6 */
oa[0] = null; /* Line 7 */
return o; /* Line 8 */
}


When is the Float object, created in line 3, eligible for garbage collection?


  1.    just after line 5
  2.    just after line 6
  3.    just after line 7
  4.    just after line 8
 Discuss Question
Answer: Option C. -> just after line 7

Option A is wrong. This simply copies the object reference into the array.

Option B is wrong. The reference o is set to null, but, oa[0] still maintains the reference to

the Float object.

Option C is correct. The thread of execution will then not have access to the object.


Question 52.


What is the output of this program?


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

obj.entrySet() method is used to obtain a set that contains the entries in the map. 

This method provides set view of the invoking map.
Output:
$ javac Maps.java
$ java Maps
[A=1, B=2, C=3]


Question 53.


What is the output of this program?


class box {
int width;
int height;
int length;
int volume;
box() {
width = 5;
height = 5;
length = 6;
}
void volume() {
volume = width*height*length;
}
}
class constructor_output {
public static void main(String args[])
{
box obj = new box();
obj.volume();
System.out.println(obj.volume);
}
}
  1.    100
  2.    150
  3.    200
  4.    250
 Discuss Question
Answer: Option B. -> 150

None.
output:
$ constructor_output.java
$ constructor_output
150


Question 54.


What is the output of this program?


class main_class {
public static void main(String args[])
{
int x = 9;
if (x == 9) {
int x = 8;
System.out.println(x);
}
}
}
  1.    9
  2.    8
  3.    Compilation error
  4.    Runtime error
 Discuss Question
Answer: Option C. -> Compilation error

Two variables with the same name can't be created in a class.
output:
$ javac main_class.java
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Duplicate local variable x


Question 55.

Which of these class relies upon its subclasses for complete implementation of its methods?


  1.    Object class
  2.    abstract class
  3.    ArrayList class
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> abstract class

None.


Question 56.

Which interface provides the capability to store objects using a key-value pair?


  1.    Java.util.Map
  2.    Java.util.Set
  3.    Java.util.List
  4.    Java.util.Collection
 Discuss Question
Answer: Option A. -> Java.util.Map

An object that maps keys to values. A map cannot contain duplicate keys; each key can 

map to at most one value.

Question 57.

Which of these statement is incorrect?


  1.    Every class must contain a main() method.
  2.    Applets do not require a main() method at all.
  3.    There can be only one main() method in a program.
  4.    main() method must be made public.
 Discuss Question
Answer: Option A. -> Every class must contain a main() method.

Every class does not need to have a main() method, there can be only one main() method 

which is made public.


Question 58.
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 */
X x4 = new X();
x2 = x4; /* Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}


After line 8 runs. how many objects are eligible for garbage collection?


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

By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that "Java is pass by value," so the reference variable x is not affected by the m1() method.

Ref: http://www.javaworld.com/javaworld/javaqa/2000-05/03-qa-0526-pass.html


Question 59.

What is Collection in Java?


  1.    A group of objects
  2.    A group of classes
  3.    A group of interfaces
  4.    None of the mentioned
 Discuss Question
Answer: Option A. -> A group of objects

A collection is a group of objects, it is similar to String Template Library (STL) 

of C++ programming language.


Question 60.

Which of these is static variable defined in Collections?


  1.    EMPTY_SET
  2.    EMPTY_LIST
  3.    EMPTY_MAP
  4.    All of the mentioned
 Discuss Question
Answer: Option D. -> All of the mentioned

None.


Latest Videos

Latest Test Papers