I am doing mock
test Valiveru's Home.
I have the fol doubts about some anwsers.
1 Given the code below
public class ThreadDemo extends
Thread[
public void run(){
System.out.println("Thread started");
try{
Thread.sleep(20000); //20 seconds sleep
System.out.println("I am Back from my sleep");
}
catch(InterruptedException e){
System.out.println("I got Interrupted");
}
}
}
Which of the following statements are true?
A.Exactly 20 seconds after the start method
is called "I am Back from my sleep" be printed.
B.We can be sure that atleast after 20 seconds elapsed,
"I am Back from sleep" will be printed.
C.The delay between output "Thread started" and "I am Back
from sleep" is less or more than 20 seconds.
D.None of the above.
The answer is B. But I think should be D. because if thread is interrupted,then "I am Back from sleep" will never be printed.
2. Which of the following are true about inner classes
A.Inner classes can only be instantiated in its outer class
B.Inner classes can be protected or private
C.Anonymous inner class can be both an explicit subclass
and implements an interface.
D.Anonymous inner classes declare and instantiated at the
same place.
E.An inner class can be static only when it's outer class
is static.
F.An instance of an Anonymous inner class can only be
created in it's outer class
The answer is B and D.But I think should be B,D,and F.
3. Select all the following statements about 'wait' method that are true
A.wait() is static final method that cannot be overrided.
B.wait() is an instance method of object class.
C.wait() is an instance method of Thread class.
D.Thread must have lock an the object involved to call
wait().
The answer is B and D.
But I think B, C and D. Because Thread is a subclass of Object,so it inherits wait() method from Object.So wait() should be an instance method of Thread class too.
4. Given that method aMethod() throws BaseException, SubException and RuntimeException of the following exception hierarchy
java.lang.Exception
|
+ - - BaseException
|
+ - - SubException
|
+ - - java.lang.RuntimeException
Which of the following are legal
A.public class MyClass {
public void myMethod(){
aMethod();
}
}
B.public class MyClass{
public void myMethod() throws BaseException,RuntimeException{
aMethod();
}
}
C.public class MyClass{
public void myMethod() throws BaseException{
aMethod();
}
}
D.public class MyClass{
public void myMethod() throws Exception{
aMethod();
}
}
E.public class MyClass{
public void myMethod() throws RuntimeException {
aMethod();
}
}
Answer are C and D.Why B is wrong?Since a method can freely throw RuntimeException which is not a checked Exception.
5. Which of the follwing is true about static modifier.
A.static can be applied to : instance variables, methods,
code Segments and classes.
B.a static method cannot be overridden.
C.inner classes can be both static & private.
D.a static reference cannot be made through non static
method or code block.
E.abstract & static both can be applied together to a
method.
The answer are B,C,D.
Obviously,D is wrong. I choose B,C.
Hope anyone can clear these questions
Thx in advance