OCPJP 6 Mock Exam -13

1.What will be the output?

if( "String".endsWith(""))
    Sytem.out.println("True");
else
    System.out.println("False");

Choose the one below:

  1. True
  2. False

2 . What will be the output?

if( "String".startsWith(""))
    Sytem.out.println("True");
else
    System.out.println("False");

Choose the one below:

  1. True
  2. False

3 . What will be the output?

    public class TestClass
    {
        public static void main(String Args[])
        {
            StringBuffer sb1 = new StringBuffer("String");
            StringBuffer sb2 = new StringBuffer("String");
            if(sb1.equals(sb2))
            {
                //lots of code
            }
         }
    }

Is the line marked “lots of code” ever reached?

  1. Yes
  2. No

4 . What will be the output?

public class NiceThreads implements Runnable
{
     public void run()
     {
      while(true)
      {
      }
     }

     public static void main(String args[])
     {
          NiceThreads nt1 = new NiceThreads();
          NiceThreads nt2 = new NiceThreads();
          NiceThreads nt3 = new NiceThreads();

          nt1.run();
          nt2.run();
          nt3.run();
     }
}

Choose the one below:

  1. The code does not compile – “nt2.run() is never reached”
  2. The code compiles and runs 3 non ending non demon threads
  3. The code compiles but runs only 1 non ending, non demon thread

5 . When does the JVM exit?

  1. After the main method returns
  2. After all the non demons threads created by the application complete
  3. After all the demon threads created by the application complete
  4. When a thread executes System.exit();
  5. When an uncaught exception is thrown in a non demon thread
  6. When an uncaught exception is thrown in a demon thread

6 . Read the following code, which is a part of a synchronized method of a monitor?

public synchronized void someMethod()
{
    //lots of code

    try
    {
        Thread.sleep(500);
    }
    catch(InterruptedException e)
    {
        //do some crap here.
    }
    //more and more code here
}

Choose the one below:

  1. The code causes compilation error – sleep cannot be called inside synchronized methods
  2. The code causes compilation error – sleep is not a static method of java.lang.Thread
  3. The Thread sleeps for at least 500 milliseconds in this method if not interrupted
  4. When the thread “goes to sleep” it releases the lock on the object
  5. The “sleeping” Threads always have the lock on the Object

7 . The no-argument constructor provided by the compiler when no constructor is explicitly provided in the code?

  1. is always public
  2. is always “friendly”
  3. always defaults to the access modifier provided for the class
  4. depends on the compilation options of javac

8 . Which of the following is the direct base class of java.awt.AWTEvent.?

  1. java.lang.Object.
  2. java.util.EventObect.

9 . Interface methods can be declared with the following modifiers?

  1. public
  2. none (i.e., no access modifier)
  3. private
  4. static
  5. native
  6. synchronized

10 . Which of the following are true about the class defined inside an interface?

  1. it is not possible in the java Laungage
  2. The class is always public
  3. The class is always static
  4. the class methods cannot call the methods declared in the interface
  5. the class methods can call only the static methods declared in the interface

11 . what does the following expression return?

         (0.0 == -0.0)

Choose the one below:

  1. true
  2. false

12 . What does the following expression print on the screen?

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

Choose the one below:

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

13 . What does the following expression return?

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

Choose the one below:

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

14 . What will be the output?

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

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

        System.out.println(s2);
    }
}

Choose the one below:

  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

15 . What will be the output?

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

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

Choose the one below:

  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

16 . Class fields with the following modifiers will not be serialized?

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

17 . 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);

Choose the one below:

  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

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

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

20 . What will be the output?

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??");
 }
}
<strong>Choose the one below:</strong>
<ol>
<li>The code causes a compiler error. out is a declared final in System and cannot be assigned to pr</li>
<li>The code causes a runtime Exception due the assignment to a final variable</li>
<li>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</li>
</ol>

<strong>21 . What will be the output?</strong>
1
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");
     }

}

Choose the one below:

  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

22 . What will be the output?

import java.io.IOException;

public class 72
{
    public 72() throws IOException
    {
        throw new IOException();
    }
}

Assume that the defination of 72E begins with the line.

    public class 72E extends 72

It is required that none of the constructors of 72E should throw any checked exception.
Choose the one below:

  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

23 . What will be the output? (This is an extention to 72.)

public abstact class 73 extends 72
{
    public abstract void method();
}

Choose the one below:

  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

24.What will be the output?


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();
    }
}

Choose the one below:

  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

25 . What will be the output?


public class A
{
    A()
    {
    }
}

Choose the one below:

  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

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

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

28 . The factory class java.util.Collections?

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

29 . What will be the output?

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);
  }
 }

Choose the one below:

  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"
  5. The program prints a blank line on the screen

30 . What will be the output?

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

Choose the one below:

  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

31 . What will be the output?

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

Choose the one below:

  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

32 . What will be the output?

    //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)

Choose the one below:

  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

33 . What will be the output?

import java.util.*;

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

Choose the one below:

  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

34 . What will be the output?

import java.util.*;

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

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

   }

Choose the one below:

  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

35 . What will be the output?

//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());
        }

   }

Choose the one below:

  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

36 . What will be the output?

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());
    }
}

Choose the one below:

  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

37 . What will be the output?

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());
    }
}

Choose the one below:

  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

38 . What will be the output?

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());
    }
}

Choose the one below:

  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)

39 . What will be the output?

//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);
        }
}

Choose the one below:

  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

40 . What will be the output?

//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);
        }
}

Choose the one below:

  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

Answers

1 : 1 is correct.

2 : 1 is correct.

3 : 2 is correct.

4 : 3 is correct.

5 : 2 & 4 is correct.

6 : 3 & 5 is correct.

7 : 3 is correct.

8 : 2 is correct.

9 : 1 & 2 is correct.

10 : 2,3 & 4 is correct.

11 : 1 is correct.

12 : 1 is correct.

13 : 2 is correct.

14 : 3 is correct.

15 : 3 is correct.

16 : 2 & 3 is correct.

17 : 1 is correct.

18 : 2 & 4 is correct.

19 : 2 is correct.

20 : 1 is correct.

21 : 5 is correct.

22 : 3 is correct.

23 : 3 is correct.

24 : 4 is correct.

25 : 1,2 & 3 is correct.

26 : 1 is correct.

27 : 3 is correct.

28 : 2 is correct.

29 : 1 is correct.

30 : 3 is correct.

31 : 1 is correct.

32 : 3 is correct.

33 : 3 is correct.

34 : 1 is correct.

35 : 2 is correct.

36 : 1 is correct.

37 : 4 is correct.

38 : 4 is correct.

39 : 3 is correct.

40 : 1 is correct.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*