Questions# 181-200




Question 181)

    what does the following expression return?

         (0.0 == -0.0)
 

  1.  true
  2.  false

Question 182)

     What does the following expression print on the screen?

            System.out.println(Math.min(Float.NaN, Float.POSITIVE_INFINITY));
 

  1.  NaN
  2.  Infinity
  3.  The expression throws a runtime exception - NaN is an illegal argument.

Question 183)

        What does the following expression return

            Math.max(Float.POSITIVE_INFINITY,Double.POSITIVE_INFINITY);
 

  1.  Float.POSITIVE_INFINITY
  2.  Double.POSITIVE_INFINITY
  3.  runtime Exception

Question 184)

    Read the following code carefully.

public class AStringQuestion
{
    static String s1;
    static String s2;

    public static void main(String args[])
    {
         s2 = s1+s2;

        System.out.println(s2);
    }
}


    Attempting to compile and run the code
 

  1. Will cause a compilation error.
  2. Runtime Execption - NullPointerException in the 2nd line of the main method.
  3. Will compile successfully and print nullnull on the screen.
  4. Will compile successfully and print an empty line on the screen.
  5. Will compile successfully and print nothing on the screen.



Question 185)

    Read the code below carefully.

public class AQueryClass
{
    public static synchronized method1()
    {
        //lots and lots of code here.
     }

    public sychronized void method2()
    {
        //lots of code here too.
    }
}

Assuming that all the code inside the method is legal and cannot be a cause for compilation error -
 
  1. An attempt to compile will cause a compilation error. Static methods cannot be synchronized.
  2. Compilation will be successfull. The Object instantiated will have a lock which has to be gained by Threads calling any of the two methods.
  3. Compilation will be successfull. There will exist a Class wide lock which will have to be gained by the Thread calling method1 and an instance lock for each instance which will have to be gained by the Thread making a call to method2 on the Object instance. The class wide lock and the instance lock being independent of each other.
  4. Compilation will be successfull. There will exist a Class wide lock which will have to be gained by the Thread calling method1 and an instance lock for each instance which will have to be gained by the Thread making a call to method2 on the Object instance. The class wide lock and the instance lock being mutually exclusive.

Question 186)

    Class fields with the following modifiers will not be serialized
 

  1. private
  2. static
  3. transient
  4. protected

Question 187)

    Assume that Cat is a class and String[] args is the argument passed to the public static void main(String args[]) method of the class. The class is executed with the following command line string

c:\somedirectory> java Cat

An expression in the main method is as follows

System.out.println(args.length);
 
 

  1. The above expression will cause a '0' appear on the command line.
  2. Will throw a NullPointerException.
  3. Will a blank line to appear.

Question 188)

    A demon Thread group

  1. has only demon threads.
  2. can have non demon threads.
  3. does not exist after all non demon threads in the group have finished executing.
  4. does not exist after all the threads in the group have finished executing.

Question 189)

Assume that th is an instance holding a thread object. th.start() causes the thread to start running and eventually complete its execution. The object reference by th is not accessable any more and is garbage collected when the garbage collecter runs.
 

  1. True
  2. False

Question 190)

    Read the code below carefully.

import java.io.*;

public class OutOut
{
 public static void main(String args[]) throws IOException
 {
  PrintStream pr = new PrintStream(new FileOutputStream("outfile"));

  System.out = pr;

  System.out.println("Lets see what I see now??");
 }
}

  1. The code causes a compiler error. out is a declared final in System and cannot be assigned to pr.
  2. The code causes a runtime Exception due the assignment to a final variable.
  3. The code compiles and runs success fully.  A file called "outfile" is created and "Lets see what I see now??" is printed in the same.

Question 191)

Read the following piece of code carefully

public abstract class AbstractClass
{
     public AbstractClass()
     {
          System.out.println("this is an abstract class constructor!");
     }

     public void aMethod()
     {
          System.out.println("This is in the method in the abstract class");
     }

}

Attempting to compile and run the code will cause
  1. Compiler error - abstract classes cannot have constructors.
  2. Compiler error - the method AbstractClass does not have a valid return type.
  3. Compiler error - the class cannot be declared as abstract as it does not have any unimplemented methods.
  4. No compiler error - the class is practically not an abstract class and can be instantiated.
  5. No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the extended class will call the constructor of the abstract class (implicitly or explicitly).
  6. No compiler error - the class cannot be instantiated directly. It has to be extended to an non-abstract class. The constructors of the abstract class will never be called.

Question 192)

Read the following piece of code carefully.

import java.io.IOException;
 

public class Question72
{
    public Question72() throws IOException
    {
        throw new IOException();
    }
}
Assume that the defination of Question72E begins with the line

    public class Question72E extends Question72

It is required that none of the constructors of Question72E should throw any checked exception.
 

  1. It can be achived by placing the call to the superclass with  a super keyword , which is placed in a try block with a catch block to handle the IOException thrown by the super class.
  2. It can be achived by avoiding explicit calls to the base class constructor.
  3. It cannot be done in the Java Laungage with the above definition of the base class.

Question 193)

Read the following piece of code carefully. (This is an extention to question 72.)

public abstact class Question73 extends Question72
{
    public abstract void method();
}


An attempt to compile the above class definition

  1. will cause a compiler error - non-abstract classes cannot be extended to abstract classes.
  2. will cause a compiler error - a constructor must be provided which may or may not throw an IOException
  3. will cause a compiler error - a constructor must be provided which must throw an IOException or one of its super types.
  4. will not cause any compiler error. The class definition is perfectly legal.

Question 194)

Read the following piece of code carefully.
 

class Base
{
    Base()
    {
        System.out.println("Message 1 : In the base class constructor");
    }
}

abstract class Derived1 extends Base
{
    Derived1()
    {
        System.out.println("Message 2 : In the abstract class Derived1\'s constructor");
    }
}

public class Derived2 extends Derived1
{
    public Derived2()
    {
        System.out.println("Message 3 : In the derived2 class\'s constructor.");
    }
 
    public static void main(String args[])
    {
        Derived2 d2 = new Derived2();
    }
}

An attempt to compile and run the above code
 
  1. will cause a compiler error. The non-abstract classes cannot be extended to the abstract classes.
  2. will cause a compiler error.  The abstract classes cannot have constructors defined.
  3. will not cause any compiler error. The lines "Message 1..." and "Message 3 ... " are printed on the screen.
  4. will not cause any compiler error. The lines "Message 1..." and "Message 2...." and Message 3...." are printed on the screen.

Question 195)

Read the following piece of code carefully.
 

public class A
{
    A()
    {
    }
}
  1. The class A can be referenced outside the package in which it is defined.
  2. The class A cannot be instantiated outside the package in which it is defined.
  3. The class A cannot be extended outside the package in which it is defined.
  4. The class A can be referenced, instantiated or extended anywhere.
  5. The above code will cause a compiler error. The constructors of public class have to be public.

Question 196)

Read the code below carefully.

import  com.abhilash.abhilash;

public class One
{
    public static void main()
    {
        abhilash a = new abhilash();
    }
}

  1. The code will fail to compile. The class com.abhilash.abhilash can never be imported.
  2. The code will compile. Runtime error occurs if one of the class in the import  does not exist and is referenced.
  3. The code will compile and run with out any problem.

Question 197)

    If the finalize() method of an object is re-references an object so that it becomes in-eligible for garbage collection
 

  1. The compiler will throw an error.
  2. The garbage collector "collects" the object anyway.
  3. The garbage collector does not collect the object in the present sweep. But when the object becomes eligible for garbage collection again, its finalize method will not be called by the garbage collector (i.e., if the garbage collector gets a chance to run.). It will simply be garbage collected.
  4. The object can never be garbage collected and hence leads to memory-leak. Each time the garbage collector calls finalize before "collecting" the object the object "resurrects" itself.

Question 198)

    If a Runtime Exception is thrown in the finalize method -
 

  1. The running application crashes.
  2. The exception is simply ignored and the object is garbage collected.
  3. The exception is simply ignored, but the object is not garbage collected.
  4. The Exception causes the JVM to crash.

Question 199)

The factory class java.util.Collections

  1. is public
  2. implements the java.util.Collection interface.

Question 200)

import java.io.*;

     public class TransientWriter implements Externalizable
     {
 
         private transient String s = "Hope I can ever be persistant!";

         public void writeExternal(ObjectOutput oOut) throws IOException
         {
             oOut.writeObject(s);
         }
 
         public void readExternal(ObjectInput oIn) throws IOException, ClassNotFoundException
         {
             s=(String)oIn.readObject();
         }

   public String toString()
   {
    return s;
   }
     }

  class K
  {
   public static void main(String args[]) throws IOException, ClassNotFoundException
   {
   TransientWriter tw = new TransientWriter();
   ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("tw.out"));
   out.writeObject(tw);

   ObjectInputStream in = new ObjectInputStream(new FileInputStream("tw.out"));

   TransientWriter tw2 = (TransientWriter) in.readObject();
   System.out.println(tw2);
  }
 }

Attempting to compile and run the above code
 

  1. will cause a compiler error due to the attempt to write a transient object.
  2. will cause a runtime exception when an attempt is made to write a transient object.
  3. will not cause any runtime error and the transient object is writen to the file named "tw.out".
  4. will not cause any runtime error and the transient object is not written to the file named "tw.out". The program prints a blank line on the screen.

 


Back   Answers   Next      Index   Home