Wednesday, 27 February 2013

30. What will be output if you try to compile and run the following code, but there

30. What will be output if you try to compile and run the following code, but there
is no file called Hello.txt in the current directory?



Choices:
a. No such file found
b. No such file found , -1
c. No such file found, Doing finally, -1
d. 0


Answer:-
C is correct. Firstly after construction of Test class, System.out.println(myMethod()) will be invoked. This will invoke myMe thod(). Since Hello.txt is not present in the current directory, FileNotFoundException will be thrown in the try block. It will be caught in the catch block, where first "No such file found" will be printed. Then return -1 will be executed. But please note that before control passes back to main method, finally block will be executed and thus "Doing finally" will be printed. Finally "-1" will be printed.

==

import java.io.*;

public class Test
{
        public static void main(String argv[])
  {
        Test t = new Test();
        System.out.println(t.myMethod());
  }

                public int myMethod()
  {
        try
  {
                FileInputStream dis = new FileInputStream("Hello.txt");
                }
  catch (FileNotFoundException fne)
  {
                  System.out.println("No such file found");
                        return -1;
                }
                               catch(IOException ioe)
  {
                }
  finally
  {
        System.out.println("Doing finally");
        }
        return 0;
        }
}

 

29. What will be printed to the standard output when the following program

29. What will be printed to the standard output when the following program
is run and the Test button is pressed 3 times.



Choices:
a. Clicked
Clicked again
Clicked
Clicked again
Clicked
Clicked again
b. Clicked
Clicked
Clicked
c. Clicked
Clicked
Clicked again
Clicked
Clicked again
Clicked again
d. Clicked
Clicked again


Answer:-

C is the correct choice. When the button is clicked for the first time, the actionPerformed method is invoked and it prints "Clicked". In actionPerformed, one more listener is registered with the button. So when the button is clicked for the second time, the actionPerformed of both the listeners is invoked. So it prints "Clicked" and then "Clicked again". Now one more listener is registered with the button. So for the third time the button is clicked, 3 actionPerformed methods are invoked - result is the printing of "Clicked" by first listener,"Clicked again" by the second and "Clicked again" by the third one. Thus C is the correct choice.
==
import java.awt.*;
import java.awt.event.*;

public class ActionTest extends Frame
{
                 public ActionTest()
  {
  Button test=new Button("Test");
  add(test);
  test.addActionListener(new ActionListener()
  {
  public void actionPerformed(ActionEvent e)
  {
  System.out.println("Clicked");
  Button b=(Button)e.getSource();
  b.addActionListener(new ActionListener()
  {
  public void actionPerformed(ActionEvent e)
  {
  System.out.println("Clicked again");
  }
  });
  }
  });

                                setSize(100,100);
    setVisible(true);
                                setTitle("My Frame");
      }

  public static void main(String rgs[])
  {
                              new ActionTest();
  }
}

28. What will happen when you attempt to compile and run the following code?

28.  What will happen when you attempt to compile and run the following code?



Choices:
a.The above code in its current condition will not compile.
b. In order to make the MyThread class prints "mt1" (100 times) followed by
"mt2" (100 times), mt1.join(); can be placed at //XXX position.
c. In order to make the MyThread class prints "mt1" (100 times) followed by
"mt2" (100 times), mt1.sleep(100); can be placed at //XXX position.
d. In order to make the MyThread class prints "mt1" (100 times) followed by
"mt2" (100 times), mt1.run(); can be placed at //XXX position.
e. In order to make the MyThread class prints "mt1" (100 times) followed by
"mt2" (100 times), there is no need to write any code.

Answer:-
A and B are correct. In its current condition, the above code will not compile as "InterruptedException" is never thrown in the try block. The compiler will give following exception: "Exception java.lang.InterruptedException is never thrown in the body of the corresponding try statement." Note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operating system and other running threads, the thread on which start is called will get executed. After making the above code to compile (by changing the InterruptedException to some other type like Exception), the output can't be predicted (the order in which mt1 and mt2 will be printed can't be guaranteed). In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join() can be placed at //XXX position. The join() method waits for the Thread on which it is called to die. Thus on calling join() on mt1, it is assured that mt2 will not be executed before mt1 is completed. Also note that the join() method throws InterruptedException, which will cause the above program to compile successfully. Thus choice A and B are correct. 
==

public class MyThread extends Thread
{
  String myName;

                MyThread(String name)
  {
                  myName = name;
                }

  public void run()
  {
  for(int i=0; i<100;i++)
  {
  System.out.println(myName);
  }
  }

  public static void main(String args[])
  {
  try
  {
                                         MyThread mt1 = new MyThread("mt1");
                                         MyThread mt2 = new MyThread("mt2");
  mt1.start();
                                                // XXX
  mt2.start();
  }
                                 catch(InterruptedException ex)
  {
  }
  }
}

 

27. What results from trying to compile and run the following code?

27. What results from trying to compile and run the following code?


Choices:
a. The output is 12 100
b. Compilation error at line 12 because once you chain a DataOutputStream onto the
FileOutputStream, you can't write directly to the FileOutputStream.
c. An exception occurs at Run time at line 20 because there are only two bytes written
in the file "abc" and the code tries to read a byte and then an integer.
d. Compilation error occurs at line 20 because there are only two bytes written in the file "abc"
and the code tries to read a byte and then an integer.

Answer:-
C is correct.
There is nothing wrong in writing to a FileOutputStream even after chaining a DataOutputStream onto it. C is correct as FileOutputStream writes 100 as a byte whereas the code tries to read it as integer. Of course A and D are incorrect (For the reasons just explained).

26. How can you replace the comment at the end of main() with code that will write the

26. How can you replace the comment at the end of main() with code that will write the
integers 0 through 9?


Answer:-
A is correct.
In order to write a primitive data type such as an int, you need to use a FilterInputStream subclass such as DataInputStream.
This class defines writeInt(), which is perfect for our needs.



25. What is displayed when the following is executed?

25. What is displayed when the following is executed?



Choices:
a. Compile time error
b. Run time error
c. prints : Parent's method2()
           Parent's method1()
d. prints : Parent's method2()
           Child's method1()
Answer:-
C is correct.
The code will compile without any error and also will not give any run time error.
The variable p refers to the Child class object.
The statement p.method2() on execution will first look for method2() in Child class.
Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed.
Now from the method2() , there is a call to method1().
Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked.
Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called.
Thus C is correct answer.

24. What is displayed when the following code is compiled and executed?

24. What is displayed when the following code is compiled and executed?


Choices:
a. Same
Equals
b. Equals
c. Same
d. The code compiles, but nothing is displayed upon execution.
e. The code fails to compile.


Answer:-
B is correct.
Here s1 and s2 are two different object references, referring to different objects in memory.
Please note that operator == checks for the memory address of two object references being compared and not their value.
The "equals()" method of String class compares the values of two Strings.
Thus s1==s2 will return "false" while s1.equals(s2) will return "true".
Thus only "Equals" will be printed.