OCPJP 6 Mock Exam -12

SHARE & COMMENT :

1 . What will be the output?

if("String".toString() == "String")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error.

2 . What will be the output?

if(" String ".trim() == "String")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

3 . What will be the output?


public class A
{
     public void method(Object o)
     {
         System.out.println("Object Verion");
     }

 public void method(String s)
 {
    System.out.println("String Version");
 }

 public static void main(String args[])
 {
     A  = new A();
     .method(null);
 }

}

Choose the one below:

  1. The code does not compile.
  2. The code compiles cleanly and shows “Object Version”.
  3. The code compiles cleanly and shows “String Version”
  4. The code throws an Exception at Runtime.

4 . What will be the output?

public class A
{
     public void method(StringBuffer sb)
     {
         System.out.println("StringBuffer Verion");
     }

 public void method(String s)
 {
    System.out.println("String Version");
 }

 public static void main(String args[])
 {
     A  = new A();
     .method(null);
 }

}

Choose the one below:

  1. The code does not compile.
  2. The code compiles cleanly and shows “StringBuffer Version”.
  3. The code compiles cleanly and shows “String Version”
  4. The code throws an Exception at Runtime.

5 . What will be the output?

public interface A
{
     public abstract void someMethod() throws Exception;
}

A Class implementing this interface should

Choose the one below:

  1. Necessarily be an abstract class.
  2. Should have the method public abstract void someMethod();
  3. Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
  4. Should have the method public void someMethod() which need not throw an Exception.

6 . An Interface can never be private or protected.?

  1. True
  2. False

7 . A Vector class in jdk 1.2?

  1. is public
  2. is final
  3. implements java.util.List
  4. is serializable
  5. has only One constructor

8 . A String Class?

  1. is final
  2. is public
  3. is serializable
  4. has a constructor which takes a StingBuffer Object as an Argument

9 . What will be the output?

public interface A
{
     void someMethod();
}

Choose the one below:

  1. The class which implements A
  2. Should have someMethod which must necessarily be public.
  3. Should have someMethod which could be “friendly” or public
  4. Should have someMethod which should not throw any checked exceptions.
  5. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface defination

10 . What will be the output?

public class A
{

 private int i = j;
 private int j = 10;
 public static void main(String args[])
 {
      System.out.println((new A()).i);
 }

}

Choose the one below:

  1. Compiler error complaining about access restriction of private variables of A.
  2. Compiler error complaining about forward referencing.
  3. No error – The output is 0;
  4. No error – The output is 10;

11 . What will be the output?

    public class A
    {
         private int i = giveMeJ();
         private int j = 10;

         private int giveMeJ()
         {
              return j;
         }

         public static void main(String args[])
         {
              System.out.println((new A()).i);
         }
    }

Choose the one below:

  1. Compiler error complaining about access restriction of private variables of A.
  2. Compiler error complaining about forward referencing.
  3. No Compilation error – The output is 0;
  4. No Compilation error – The output is 10;

12 . What will be the output?

    public class A
    {
         public static void main(String args[])
         {
              System.out.println("Before Try");
              try
              {
              }
              catch(Throwable t)
              {
                   System.out.println("Inside Catch");
              }
              System.out.println("At the End");
         }
    }
 

Choose the one below:

  1. Compiler error complaining about the catch block, where no Throwable object can ever be thrown
  2. Compiler error – Throwable Object can not be caught, only Exceptions must be caught.
  3. No compiler error. The lines “Before Try” and “At the end” are printed on the screen.

13 . What will be the output?

public class A
{
     public static void main(String args[])
     {
          System.out.println("Before Try");
          try
          {
          }
          catch(java.io.IOException t)
          {
               System.out.println("Inside Catch");
          }
          System.out.println("At the End");
      }
}

Choose the one below:

  1. Compiler error complaining about the catch block where no IOException object can ever be thrown.
  2. Compiler error – IOException not found. It must be imported in the first line of the code.
  3. No compiler error. The lines “Before Try” and “At the end” are printed on the screen.

14 . The class java.lang.Exception?

  1. Is public
  2. Extends Throwable
  3. Implements Throwable
  4. Is serializable

15 . What will be the output?

if("String".trim() == "String".trim())
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

16 . What will be the output?

if( "STRING".toUpperCase() == "STRING")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

17 . What will be the output?

             byte b = 0;
             b += 1;

Choose the one below:

  1. results in b having the value 1.
  2. causes a compiler error.
  3. will require a cast (byte) before 1.

18 . The following express?

            char c = -1;

Choose the one below:

  1. will cause a compiler error as the range of character is between 0 and 2^16 – 1. Will request for an explicit cast.
  2. will not cause a compiler error and c will have the value -1;
  3. c will not represent any ascii character.
  4. c will still be a unicode character.

19 . Which of the following statements are true?

  1. A method can throw an Exception
  2. A method can return an Exception

20 . All the wrapper classes (Integer, Boolean, Float, Short, Long, Double and Character)?

  1. are public
  2. are serializable
  3. are immutatable
  4. extend java.lang.Number
  5. are final

21 . What will be the output?

if("String".substring(0) == "String")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

22 . What will be the output?

if("String".substring(0,6) == "String")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

23 . What will be the output?

if("String".replace('t','t') == "String")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

24 . An Anonymous Inner class?

  1. Does not have a constructor
  2. Can implement an interface
  3. Can extend a non-final Class
  4. Can implement an interface and extend a non-final class (at the same time).

25 . What will be the output?

public class A
{

 private void method1() throws Exception
 {
      throw new RuntimeException();
 }

 public void method2()
 {
     try
     {
       method1();
     }
     catch(RuntimeException e)
     {
          System.out.println("Caught Runtime Exception");
     }
     catch(Exception e)
     {
          System.out.println("Caught Exception");
     }
 }

 public static void main(String args[])
 {
      A a = new A();
      a.method2();
 }

}

Choose the one below:

  1. will not compile.
  2. will compile and show – “Caught Runtime Exception”.
  3. will compile and show – “Caught Exception”.
  4. will compile and show both the messages one after another in the order they appear.

26 . What will be the output?

public XXXX extends something1, something2

Choose the one below:

  1. XXX should be an interface,something1 and something2 need not, for the expression to be legal
  2. XXX should be a class, something1 and something2 must be interfaces for the expression to be legal.
  3. XXX, something1 and something2 must be interfaces for the expression to be legal.
  4. The above statement is alway illegal in Java as multiple inheritance is not supported.

27 . What will be the output?

 public class ADirtyOne
 {
      char a = '\u000A';
 }

Choose the one below:

  1. will complete successfully.
  2. will not compile as 0x000A is out of range for unicode charaters.
  3. will complain about illegal character assignment
  4. will compile but will cause a runtime error in accessing the variable.

28 . What will be the output?

 public class ADirtyOne
 {
      //char a = '\u000A';
 }

Choose the one below:

  1. will complete successfully.
  2. will compile sucessfully but with a warning message.
  3. will not compile – complains on an invalid expression.

29 . What will be the output?

public class AnotherDirtyOne
 {
      private final int i =10;
      private byte k = i;
 }

Choose the one below:

  1. Cause a compilation error due to invalid assignment ( int to byte) and will request for an explicit cast to be done on i [ k=(byte) i ].
  2. Compilation occurs with a warning message – suggesting that the accuracy of k is able
  3. Compilation will occur cleanly without any warning message.
  4. Runtime error occurs when accessing k.

30 . What will be the output?

 interface One
 {
      public void someMethod();
 }

 public class One_impl implements One
 {
      public native void someMethod();
 }

Assuming that the native method is not provided in any local library.

Choose the one below:

  1. Compilation error – implimentations can never be native.
  2. Compilation error – method not found in local libraries.
  3. Runtime Exception – method not found in local libraries.
  4. Compilation successfull but runtime error is thrown if and only if the method someMethod of class One_impl is called.

31 . What will be the output?

if("String".replace('T','t') == "String")
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

32 . What will be the output?

System.out.println("String".substring(0,4));

Choose the one below:

  1. the code will print “Strin” on the screen.
  2. the code will print “Stri” on the screen.
  3. the code will cause a compiler error.

33 . What will be the output?

if("String".replace('g','G') == "String".replace('g','G'))
    System.out.println("Equal");
else
    System.out.println("Not Equal");

Choose the one below:

  1. the code will compile an print “Equal”.
  2. the code will compile an print “Not Equal”.
  3. the code will cause a compiler error

34 . What will be the output?

 public class ADirtyOne
 {
      public static void main(String args[])
      {
           System.out.println(Math.abs(Integer.MIN_VALUE));
      }
 }

Choose the one below:

  1. Cause a compiler error.
  2. Cause no error and the value printed on the screen is less than zero.
  3. Cause no error and the value printed on the screen is one more than Integer.MAX_VALUE
  4. Will throw a runtime exception due to overflow – Integer.MAX_VALUE is less in magnitue than Integer.MIN_VALUE.

35 . What will be the output?

 public class ADirtyOne
 {
      public static void main(String args[])
      {
           System.out.println(Math.min(0.0,-0.0));
      }
 }

Choose the one below:

  1. Cause a compiler Error.
  2. Cause no error and print the value 0.0 on the screen.
  3. Cause no error and prints the value -0.0 on the screen.

36 . What will be the output?

 public class Base
 {
      public void aMethod() throws ClassNotFoundException
      {
      }
 }

 public class Derived extends Base
 {
      public void aMethod() throws RuntimeException
      {
      }
 }

Assuming that the classes are in two seperate files, compilation of the Dervied.java causes

Choose the one below:

  1. A compiler error because RuntimeException is not a subclass if ClassNotFoundException.
  2. No compiler error.

37 . What will be the output?

        Math.round(Float.MAX_VALUE);

Choose the one below:

  1. Returns Integer.MAX_VALUE.
  2. Returns a closest integer to Float.MAX_VALUE;
  3. Causes a compilation error.
  4. Causes a runtime Exception

38 . What will be the output?

import java.awt.*;
 public class TestFrame extends Frame
 {

  Button bNorth = new Button("North");
  Button bSouth = new Button("South");
  Button bEast = new Button("East");
  Button bWest = new Button("West");
  Button bCenter = new Button("Center");

  public TestFrame()
  {
       setLayout(new BorderLayout());
       add(bSouth,BorderLayout.SOUTH);
       add(bWest,BorderLayout.WEST);
       add(bEast,BorderLayout.EAST);
       add(bNorth,BorderLayout.NORTH);
       add(bCenter);

       setLayout(new FlowLayout());

       validate();
       pack();
       setVisible(true);
  }

  public static void main(String args[])
  {
       TestFrame tf = new TestFrame();
  }

 }

Choose the one below:

  1. Compilation error – a Layout cannot be set twice for a component.
  2. Compilation error – One button is added without specifing the position in the borderLayout
  3. No Compilation Error. The Buttons are arranged in a line in the order (From left to right) “North”,”South”,”West”,”East” and “Center”.
  4. No Compilation Error. The Buttons are arranged in a line in the order (From left to right) “South”,”West”,”East”,”North” and “Center”.
  5. No Compilation Error. The Buttons are arranged in the north , south, west, east and center regions, as in a borderlayout. Any further additions will follow the rules of FlowLayout manager.

39 . What will be the output?

 import java.awt.*;
 public class TestFrame extends Frame
 {

      Button bNorth = new Button("North");
      Button bSouth = new Button("South");
      Button bEast = new Button("East");
      Button bWest = new Button("West");
      Button bCenter = new Button("Center");

      public TestFrame()
      {
           setLayout(new FlowLayout());
           add(bNorth);
           add(bSouth);
           add(bWest);
           add(bEast);
           add(bCenter);

           setLayout(new BorderLayout());
           validate();
           setSize(300,300);
           setVisible(true);
      }

      public static void main(String args[])
      {
           TestFrame tf = new TestFrame();
      }
 }

Choose the one below:

  1. Will cause a compilation error – a Layout cannot be set after a component has been added with a preset Layout Manager.
  2. Will cause a Runtime Exception – a Layout cannot be set after a component has been added with a preset Layout Manager.
  3. Will compile cleanly and throw no runtime Exception. Only the button with label “Center” is visible and occupies the whole screen.
  4. Will compile cleanly an throw no runtime Exception. All the buttons are arranged in a single line. Any other component added in future will follow the rules of the BorderLayout Manager.
  5. Will compile and run cleanly, but no component is visible.
  6. Will compile cleanly and throw no runtime Exception. The buttons are arranged as listed below.
    Button LabelPosition
    CenterCenter
    NorthNorth
    SouthSouth
    EastEast
    WestWest

40 . A frame uses BorderLayout Management and has components added to all the regions. One resizing the Frame Some space becomes available. The space is alloted to the regions, in which Order of preference?

Choose the one below:

  1. North , South, West, East and then Center.
  2. North , West, South, Center and then Center.
  3. Center, East, West, South and then North.
  4. West, Center, South, North and then East.

41 . What will be the output?

import java.awt.*;

 public class TestFrame extends Frame
 {
      Button firstOne = new Button("One");
      Button secondOne = new Button("Two");

      public TestFrame()
      {
           add(firstOne,BorderLayout.NORTH);
           add(secondOne,BorderLayout.NORTH);

           setSize(400,400);
           setVisible(true);
      }
      public static void main(String args[])
      {
           TestFrame tf = new TestFrame();
      }
 }

Choose the one below:

  1. Causes compilation error – a component cannot be added to region which is already occupied by another component.
  2. Causes Runtime Exception – a component cannot be added to region which is already occupied by another component.
  3. Neither 1 or 2. The Frame comes up and only the button with label “Two” occupies the entire North region of the Frame.
  4. Addition of secondOne causes firstOne to be removed from the container.
  5. Addition of the secondOne causes the firstOne to be hidden in the container.

42 . What will be the output?

 import java.awt.*;

 public class TestFrame extends Frame
 {
      public TestFrame()
      {
           Button one = new Button("One");
           Button two = new Button("Two");
           Button three = new Button("Three");
           setLayout(new FlowLayout());

           add(one);
           add(two);
           add(three);

           two.setVisible(false);

           setSize(1000,1000);
           setVisible(true);
           validate();
      }
     public static void main(String args[])
     {
           TestFrame tf = new TestFrame();
     }
 }

Choose the one below:

  1. If the above code runs, the buttons – one and three are laid out in a single row from left to right with a gap in between.
  2. If the above code runs, the buttons – one and three are laid out in a single row from left to right with no gap in between.
  3. Code does not compile – a component can not be hidden after being added to a container.
  4. Code gets compiled successfully but throws runtime Exception – a component can not be hidden after being added to a container.

43 . What will be the output?


import java.awt.*;

 public class TestFrame extends Frame
 {
      public TestFrame()
      {
           setLayout(new GridLayout(2,1));
           for(int i = 1 ; i <= 4 ;++i)
           {
               add(new Button(Integer.toString(i)));
           }

           pack();
           setVisible(true);
      }

      public static void main(String args[])
      {
           TestFrame tf = new TestFrame();
      }
 }

Choose the one below:

  1. The code above will not compile – The number of components added is more than the magnitude of row * columns of the Grid Layout Manager.
  2. The code will throw a runtime Exception – The number of components added is more than the magnitude of row * columns of the Grid Layout Manager.
  3. The code will compile cleanly and when run Four buttons are visible in 2 rows and 2 columns.
  4. The code will compile and when run, Four buttons are seen in a single Column.

44 . What will be the output?


 import java.awt.*;

 public class TestFrame extends Frame
 {
      public TestFrame()
      {
           setLayout(new GridLayout());
           for(int i = 1 ; i <= 4 ;++i)
           {
                add(new Button(Integer.toString(i)));
           }

           pack();
           setVisible(true);
       }

      public static void main(String args[])
      {
           TestFrame tf = new TestFrame();
      }
 }

Choose the one below:

  1. The code will not compile – the grid layout does not have a no-argument constructor..
  2. The code compiles and when run all the buttons are seen in a single column.
  3. The code compiles and when run all the buttons are seen in a singe row.
  4. The code compiles and when run all button are added one on top or another and only the last one added is visible.
  5. The code compiles , but throws a runtime Exception when components are added.

45 . Which of the following statements are true about setLayout() method in java.awt.ScrollPane?

  1. Does nothing.
  2. Throws UnsupportedMethodException when called.
  3. It is not overriden in java.awt.ScrollPane.
  4. Sets the layout to the specified Layout Manager.

46 . A class which has all its constructors declared as private?

  1. Cannot be instantiated by any other class.
  2. Cannot be extended.
  3. Both i and ii.
  4. has to be declared final.

47 . The GridBagConstraints Class?

  1. Is serializable.
  2. Is cloneable.
  3. belongs to the java.awt package.
  4. extends Object.

48 . What will be the output?

import java.awt.*;
public class TestFrame extends Frame
{
         public TestFrame()
        {
                CheckboxGroup chg = null;
                Checkbox ch = new Checkbox("Test",true,chg);

                setLayout(new FlowLayout());
                add(ch);
                pack();
                setVisible(true);
          }
        public static void main(String args[])
        {
            TestFrame tf = new TestFrame();
        }
}

Choose the one below:

  1. will cause a compilation error as the checkbox group is null in the constructor.
  2. will compile successfully but throws a runtime exception because the checkbox group is null in the constructor of the check box
  3. will compile and run successfully. The checkbox appears as a single radio button which is always true.
  4. will compile and run successfully. The checkbox bears its original appearence and does not appear as a radio button.

49 . What will be the output?

import java.awt.*;
public class TestFrame extends Frame
{
         public TestFrame()
        {
                CheckboxGroup chg = new CheckboxGroup();
                Checkbox ch = new Checkbox("Test",true,chg);

                setLayout(new FlowLayout());
                add(ch);
                pack();
                setVisible(true);
          }
        public static void main(String args[])
        {
            TestFrame tf = new TestFrame();
        }
}

Choose the one below:

  1. will cause a compilation error as the checkbox group contains only one checkbox.
  2. will compile successfully but throws a runtime exception because the checkbox group contains only one checkbox.
  3. will compile and run successfully. The checkbox appears as a single radio button which is always true.
  4. will compile and run successfull. The checkbox bears its original appearence and does not appear as a radio button.

50 . Which of the following methods of the java.io.File class throws a checked Exceptions?

  1. getCanonicalPath()
  2. getCanonicalFile()
  3. getAbsolutePath()
  4. getAbsoluteFile()
  5. createTempFile()
  6. createNewFile()
  7. mkdir()
  8. mkdirs()
  9. toURL()

Answers

1 : 1 is correct.

2 : 2 is correct.

3 : 3 is correct.

4 : 1 is correct.

5 : 4 is correct.

6 : 2 is correct.

7 : 1,3 & 4 is correct.

8 : 1,2,3 & 4 is correct.

9 : 1 & 3 is correct.

10 : 2 is correct.

11 : 3 is correct.

12 : 3 is correct.

13 : 1 is correct.

14 : 1,2 & 4 is correct.

15 : 1 is correct.

16 : 1 is correct.

17 : 1 is correct.

18 : 1 is correct.

19 : 1 & 2 is correct.

20 : 1,2,3 & 5 is correct.

21 : 1 is correct.

22 : 1 is correct.

23 : 1 is correct.

24 : 1,2 & 3 is correct.

25 : 2 is correct.

26 : 3 is correct.

27 : 3 is correct.

28 : 3 is correct.

29 : 3 is correct.

30 : 4 is correct.

31 : 1 is correct.

32 : 2 is correct.

33 : 2 is correct.

34 : 2 is correct.

35 : 3 is correct.

36 : 2 is correct.

37 : 1 is correct.

38 : 4 is correct.

39 : 5 is correct.

40 : 1 is correct.

41 : 3 & 4 is correct.

42 : 2 is correct.

43 : 3 is correct.

44 : 3 is correct.

45 : 1 is correct.

46 : 3 is correct.

47 : 1,2,3 & 4 is correct.

48 : 4 is correct.

49 : 3 is correct.

50 : 1,2,5,6 & 9 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

*

Close
Please support the site
By clicking any of these buttons you help our site to get better