Site hosted by Angelfire.com: Build your free website today!

Java Quiz


    The following code is for Questions 81 and 82

    //Contents of File AnInterface.java

                public interface AnInterface
                {
                    public void methodOne() throws Exception;
                }
 

    //Contents of File AnInterfaceImpl.java

                public class AnInterfaceImpl implements AnInterface
                {
                   public void methodOne()
                    {
                        System.out.println("I will never throw an exception");
                    }
                }

Question 81.

    Read the code below carefully.

            public class ATest
            {
                public static void main(String args[])
                {
                    AnInterface ai = new AnInterfaceImpl();
                    ai.methodOne();
                }
            }

    Attempting to compile and run the above code

  1.     Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
  2.     Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
  3.     Will cause no compile time error and print "I will never throw and Exception the screen".
  4.     Will Cause a run time error .

Question 82.

    Read the code below carefully.

              public class ATest
             {
                 public static void main(String args[])
                 {
                    AnInterfaceImpl ai = new AnInterfaceImpl();
                    ai.methodOne();
                 }
             }

    Attempting to compile and run the above code

  1.     Will cause a compile time error. (Line 5 : Exception must be caught or thrown by main(String))
  2.     Will cause a compile time error for Class AnInterfaceImpl. The method methodOne() be declared with "throws Exception".
  3.     Will cause no compile time error and print "I will never throw and Exception the screen".
  4.     Will Cause a run time error .

Question 83

Read the code below carefully
 

    //Contents of Constants.java
    public class Constants
    {
        public static final String greetingString="Hello";
    }
    //Contents of SomeClass.java
    public class SomeClass
    {
        public static void main(String[] args)
        {
            System.out.println("Greeting String is "+Constants.greetingString);
        }
    }


    The Constants.java file is edited and the greetingString is changed to "Hello World!". The file after editing is shown below.

    public class Constants
    {
        public static final String greetingString="Hello World!";
    }


    On Compilation of Constants.java and running of the application gives. (Note : The client class SomeClass is not compiled)

  1.     A RuntimeException due to incompatable change in class Constants
  2.     An Error due to incompatable change in class Constants
  3.     The program terminates normally and "Hello" is printed on the system console.
  4.     The program terminates normally and "Hello World!" is printed on the system console.



Question 84.

Read the code below carefully
 

import java.util.*;

public class AllFinals
    {
        final Vector v;
        public AllFinals()
        {
        }
   }

The above code will
 

  1. Not compile. Vector v is not initialized.
  2. Will compile and throw a RuntimeException
  3. Will compile and not throw any Exception during runtime. V is initalized to null.



Question 85.

import java.util.*;

    public class AllFinals
    {
        {
                final Vector v;
                v=new Vector();
        }
        public AllFinals()
        {
        }

        public void someMethod()
        {
                System.out.println(v.isEmpty());
        }

   }

An attempt to compile and call the someMethod of the above class at runtime will cause
 

  1. A compilation error : v is not initialized in all constructors
  2. A compilation error : v is not an instance variable
  3. "true" is printed on the console
  4. "false" is printed on the console





Question 86.

//Contents of File AllFinals.java
import java.util.*;

    public class AllFinals
    {
        final Vector v;
        public AllFinals()
        {
                v=new Vector();
        }
        public AllFinals(int i)
        {

        }

        public void someMethod()
        {
                System.out.println(v.isEmpty());
        }

   }
 

An attempt to compile the above code will cause

  1. A compilation error : The final variable is not initialized in all the constructors.
  2. A compilation error : The final instance variable is reassigned after initialization in the constructor
  3. No compilation error : But will cause a run time exception (NullPointerException) if someMethod is called on an instance that was created through the constructor with the integer argument.
  4. A compilation error : The final instance variable is not initialized in the declaration





Question 87.

public class A
{
     final StringBuffer sb = new StringBuffer("I am final");
     public A()
     {
     }
 
     public StringBuffer getSb()
     {
        return this.sb;
     }

    public static void main(String[] args)
    {
        A a = new A();
        StringBuffer localSB = a.getSb();
        localSB.append("....");
        localSB = new StringBuffer();
        System.out.println(localSB.toString());
    }
}

Attempting to compile and run the above application will yield

  1. A compilation error : Final variable being assigned to a non-final handle.
  2. A compilation error : implicitly final localSB being reassigned
  3. A Runtime Error : Attempted reassignment to a final handle (localSB)
  4. No Errors during compilation and execution. An Empty line is printed on the console.





Question 88.

public class A
{
     StringBuffer sb = new StringBuffer("I am final");
     public A()
    {
    }
    public final StringBuffer getSb()
    {
        return this.sb;
    }

    public static void main(String[] args)
   {
        A a = new A();
        StringBuffer localSB = a.getSb();
        localSB.append("....");
        localSB = new StringBuffer();
        System.out.println(localSB.toString());
    }
}

Attempting to compile and run the above application will yeild

  1. A compilation error : Final variable being assigned to a non-final handle.
  2. A compilation error : implicitly final localSB being reassigned
  3. A Runtime Error : Attempted reassignment to a final handle (localSB)
  4. No Errors during compilation and execution. An empty line is printed on the console.






 

Question 89.

public class A
{
     StringBuffer sb = new StringBuffer("I am final");
     public A()
    {
    }
    public  StringBuffer getSb()
    {
        return this.sb;
    }

    public static void main(String[] args)
    {
        A a = new A();
        final StringBuffer localSB = a.getSb();
        localSB.append("....");
        localSB = new StringBuffer();
        System.out.println(localSB.toString());
    }
}

Attempting to compile and run the above application will yeild

  1. A compilation error : non final variable being assigned to a final handle.
  2. No Errors during compilation and execution. "I am final...." is printed on the console.
  3. A compilation error : final localSB being reassigned
  4. A Runtime Error : Attempted reassignment to a final handle (localSB)





Question 90.

//contents of file A.java
public class A
{
         A()
         {
                class B
                {
                        static
                        {
                                 System.out.println("I am getting loaded");
                         }
                }
        }

        A(int i)
        {
                System.out.println("No B this time");
        }
}

class C
{
        public static void main(String[] args)
        {
                A a = new A(1);
        }
}

Attemping to compile and execute the above Code

  1. Compilation error : Inner class inside method cannot have static members or blocks
  2. Compilation error : Inner classes cannot be delared inside constructors
  3. No compilation error : At runtime Class B is not loaded
  4. No compilation error : At runtime class B is loaded and the message "I am getting loaded" is printed on the console.





Question 91.
Read the following code excerpt carefully. //Contents of file A.java public class A
{
        A()
        {
                class B
                {
                        {
                                System.out.println("I am in no-arg constructor");
                        }
                }
        }

        A(int i)
        {
                class B
                {
                        {
                                System.out.println("I am in the arg constructor");
                        }
                }
                new B();
        }
}

class C
{
        public static void main(String[] args)
        {
                A a = new A(1);
        }
}
Which of the following are true

  1. A.java cannot be compiled. Duplicate defination of inner class B.
  2. A.java compiles without any error. An attempt to run C as an application will cause runtime linkage error. Duplicate definations of inner class B are found
  3. Only one class file corresponding to the inner class B is created in the file system.
  4. Two class files corresponding to both inner classes (B) is created in the file system.
  5. The classes compile cleanly and on running C as an application causes "I am in the arg constructor" to be printed on the console

 
 


If you think this quiz is work publishing let me know, If you thik this one sucks please let me know how I can improve it. If you think there is something wrong with any of the questions, you will be doing me a BIG favour letting me know. NOW!!!