Question 141)

         Read this piece of code carefully

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


Answers

  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

Question 142)

         Read this piece of code carefully

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


Answers

  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

Question 143)

         Read this piece of code carefully

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


Answers

  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

Question 144)

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

Question 145)

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

}
The above lines of code -
 
  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.

Question 146)

     public XXXX extends something1, something2
 

  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.



Question 147)

 public class ADirtyOne
 {
      char a = '\u000A';
 }
 An attempt to compile the above class
  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.

Question 148)

 public class ADirtyOne
 {
      //char a = '\u000A';
 }
 An attempt to compile the above class
  1.  will complete successfully.
  2.  will compile sucessfully  but with a warning message.
  3.  will  not compile - complains on an invalid expression.

Question 149)

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


An attempt to compile and run the above code will

  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 questionable
  3. Compilation will occur cleanly without any warning message.
  4. Runtime error occurs when accessing k.

Question 150)

 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, an attempt to compile and run the above lines of code will cause
 

  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.

Question 151)

         Read this piece of code carefully

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


Answers

  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

Question 152)

         Read this piece of code carefully

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


Answers

  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.

Question 153)

         Read this piece of code carefully

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


Answers

  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

Question 154)

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


 an attempt to compile and run the above class will

  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.

Question 155)

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


 An attempt to compile and run the above class will
 

  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.

Question 156)

 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
 
  1. A compiler error because RuntimeException is not a subclass if ClassNotFoundException.
  2. No compiler error.

Question 157)

        Math.round(Float.MAX_VALUE);
 
 

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

Question 158)

        Read the code below carefully
 

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

 }


 What will be the effect trying compile and run the above class?
 

  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.



Question 159)

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


 Attemping to compile and run the above code
 

  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 Label
Position
Center 
Center
North 
North
South 
South
East
East 
 West
West 


Question 160)

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?
 

  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.

Back   Answers   Next      Index   Home