Originally posted by jayu jadhav:
I HAVE DOUBTS FOR FOLLOWING Q.S FROM JAVACAPS EXAM 1. WILL ANYONE PLEASE CLEAR THEM FOR ME?
THANKS.
10). What is the result when you compile the and run the following code?
public class ThrowsDemo {
static void throwMethod() {
System.out.println("Inside throwMethod.");
throw new IllegalAccessException("demo");
}
public static void main(String args[]) {
try {
throwMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught " + e);
}
}
}
A) Compilation error
B) Runtime error
C) Compile successfully, nothing is printed.
D) Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo
ANSWER IS A. WHY?
11) Which statements about garbage collection are true?
A) The garbage collector runs in low memory situations
B) You can run the garbage collector when ever you want.
C) When it runs, it releases the memory allocated by an object, which is no more in use.
D) Garbage collector immediately runs when you set the references to null.
ANSWERS ARE A,C. WHY NOT B TOO?
26) Which of the following are correct, if you compile the following code?
public class CloseWindow extends Frame implements WindowListener {
public CloseWindow() {
addWindowListener(this); // This is listener registration
setSize(300, 300);
setVisible(true);
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public static void main(String args[]) {
CloseWindow CW = new CloseWindow();
}
}
A) Compile time error
B) Run time error
C) Code compiles but Frame does not listen to WindowEvents
D) Compile and runs successfully.
ANSWER IS A. WHY?
39) Which of the following statements are true?
A) An inner class cannot be defined as private.
B) Static methods can be overridden by static methods only.
C) Static variables can be called using class name.
D) Non static variables can be called using class name.
ANSWERS ARE B,C. WHY B? STATIC METHODS CANNOT BE OVERRIDDEN BUT HIDDEN BY STATIC METHODS.
For each checked exception which is a possible result, the throws clause for the method (�8.4.4) or constructor (�8.8.4) must mention the class of that exception or one of the superclasses of the class of that exception.
Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Originally posted by Jane Griscti:
Hi Jayhu, Lam ...
Lam, the compile error in Q.10 is not due to a lack of an instantiation. The <code>throwsMethod()</code> is static and therefore accessible to <code>main()</code>.
A compile error occurs in Q.10 because <code>throwMethod()</code> does not declare that it throws an exception; the signature should be <code>static void throwMethod() throws IllegalAccessException { // method code }</code>
See JLS�11.2
Hope that helps.
We don't have time to be charming! Quick, read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
|