Sail E0 Webinar

MCQs

Total Questions : 97 | Page 4 of 10 pages
Question 31.

Which of the folowing stements are incorrect?


  1.    Default constructor is called at the time of declaration of the object if a constructor has not been defined.
  2.    Constructor can be parameterized.
  3.    finalize() method is called when a object goes out of scope and is no longer needed.
  4.    finalize() method must be declared protected.
 Discuss Question
Answer: Option C. -> finalize() method is called when a object goes out of scope and is no longer needed.

finalize() method is called just prior to garbage collection. it is not called when object goes 

out of scope.


Question 32.


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.sort(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.    2 1 8 5
 Discuss Question
Answer: Option C. -> 1 2 5 8

Collections.sort(list) sorts the given list, the list was 2->8->5->1 after sorting it 

Question 33.


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 34.
/* Missing Statement ? */
public class foo
{
public static void main(String[]args)throws Exception
{
java.io.PrintWriter out = new java.io.PrintWriter();
new java.io.OutputStreamWriter(System.out,true);
out.println("Hello");
}
}


What line of code should replace the missing statement to make this program compile?


  1.    No statement required.
  2.    import java.io.*;
  3.    include java.io.*;
  4.    import java.io.PrintWriter;
 Discuss Question
Answer: Option A. -> No statement required.

The usual method for usingimporting  the java packages/classes is by using an import

statement at the top of your code. However it is possible to explicitly import the specific 

class that you want to use as you use it which is shown in the code above. The disadv-

-antage of this however is that every time you create a new object you will have to use 

the class path in the case "java.io" then the class name in the long run leading to a lot 

more typing.

Question 35.


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

What allows the programmer to destroy an object x?


  1.    x.delete()
  2.    x.finalize()
  3.    Runtime.getRuntime().gc()
  4.    Only the garbage collection system can destroy an object.
 Discuss Question
Answer: Option D. -> Only the garbage collection system can destroy an object.

Option D is correct. When an object is no longer referenced, it may be reclaimed by the garbage 

collector. If an object declares a finalizer, the finalizer is executed before the object is reclaimed to

 give the object a last chance to clean up resources that would not otherwise be released. When

 a class is no longer needed, it may be unloaded.

Option A is wrong. I found 4 delete() methods in all of the Java class structure. They are:

      1. delete() - Method in class java.io.File : Deletes the file or directory denoted by this abstract 

          pathname.

     2. delete(int, int) - Method in class java.lang.StringBuffer : Removes the characters in a substring 

         of this StringBuffer.          

     3. delete(int, int) - Method in interface javax.accessibility.AccessibleEditableText : Deletes the 

        text between two indices 

     4. delete(int, int) - Method in class : javax.swing.text.JTextComponent.AccessibleJTextComponent; 

        Deletes the text between two indices


None of these destroy the object to which they belong.

Option B is wrong. I found 19 finalize() methods. The most interesting, from this questions point of view, was the finalize() method in class java.lang.Object which is called by the garbage collector on an object when garbage collection determines that there are no more references to the object. This method does not destroy the object to which it belongs.

Option C is wrong. But it is interesting. The Runtime class has many methods, two of which are:

     1. getRuntime() - Returns the runtime object associated with the current Java application.

     2. gc() - Runs the garbage collector. Calling this method suggests  that the Java virtual 

        machine expend effort toward recycling unused objects in order to make the memory

        they currently occupy available for quick reuse. When control returns from the method 

       call, the virtual machine has made its best effort to recycle all discarded objects. Interesting 

      as this is, it doesn't destroy the object.


Question 37.


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();
obj.width = 10;
obj.height = 2;
obj.length = 10;
int y = obj.width * obj.height * obj.length;
System.out.print(y);
}
}
  1.    12
  2.    200
  3.    400
  4.    100
 Discuss Question
Answer: Option B. -> 200

 None.
output:
$ javac mainclass.java
$ java mainclass
200


Question 38.


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

Which collection class allows you to access its elements by associating a key with an 

element's value, and provides synchronization?


  1.    java.util.SortedMap
  2.    java.util.TreeMap
  3.    java.util.TreeSet
  4.    java.util.Hashtable
 Discuss Question
Answer: Option D. -> java.util.Hashtable

Hashtable is the only class listed that provides synchronized methods. If you need synchronization 

great; otherwise, use HashMap, it's faster.


Question 40.


What is the output of this program?


class box {
int width;
int height;
int length;
int volume;
void finalize() {
volume = width*height*length;
System.out.println(volume);
}
protected void volume() {
volume = width*height*length;
System.out.println(volume);
}
}
class Output {
public static void main(String args[])
{
box obj = new box();
obj.volume();
}
}
  1.    150
  2.    200
  3.    Runtime error
  4.    Compilation error
 Discuss Question
Answer: Option A. -> 150

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


Latest Videos

Latest Test Papers