Questions# 161-180




Question 161)

    Read the following piece of code carefully.
 

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


 An Attempt  to compile and  run the above piece of code
 

  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 i or ii. 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.

Question 162)

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

  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.

Question 163)

     Read the code below carefully.
 

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

  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.

Question 164)

    Read the code below carefully.
 

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

  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.

Question 165)

     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.

Question 166)

    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.

Question 167)

     The GridBagConstraints Class

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

Question 168)

    Read the following code carefully
 

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


    An Attempt to compile and run the above code

  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.

Question 169)

    Read the following code carefully
 

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


    An Attempt to compile and run the above code

  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.

Question 170)

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

Question 171)

    Read the code below carefully.
 

if( "String".endsWith(""))
    Sytem.out.println("True");
else
    System.out.println("False");
   The code produces
  1. True
  2. False

Question 172)

    Read the code below carefully.
 

if( "String".startsWith(""))
    Sytem.out.println("True");
else
    System.out.println("False");
   The code produces
  1. True
  2. False


 

Question 173)

    Read the code below carefully.
 

    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?


Question 174)

    Read the code below carefully.
 

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

  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.

Question 175)

    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.

Question 176)

    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
}

  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.

Question 177)

     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

Question 178)

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

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



Question 179)

     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.



Question 180)

    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.

Back   Answers   Next      Index   Home