***** =================
9.4 Which of the following are correct and proper usages of assertions?
A:private void doSomething(int value) {
assert (value > 0 && value < 1000) : "The value must be in the range [1 -999]";
// more code
}
B:public void doSomething(int value) {
assert (value > 0 && value < 1000) : "The value must be in the range [1 -999]";
// more code
}
C:private int doSomething() {
// compute result
assert result > 0 : "Incorrect result";
return result;
}
D:public int doSomething() {
// compute result
assert result > 0 : "Incorrect result";
return result;
}
A. A is a proper usage
B. B is a proper usage
C. C is a proper usage
D. D is a proper usage
Answer: B??????????
Note: This is a very important concept in assertions. Assertions can be used to test preconditions, postconditions and invariants. Java insists that assertions should not be used for testing the preconditions of the public methods. Instead, preconditions on public methods are enforced by explicit checks inside methods resulting in particular, specified exceptions. This is essential for two reasons-
(1) Exceptions can be explicitly mentioned in the throws cause of the method, and checked exceptions must be caught while invoking the method.
(2) Assertions can be disabled while running the code.
However, postcondition checks are best implemented via assertions, whether or not they are specified in public methods.
**** ==================
9.5 Which of the following can be inserted at line 12 so that the code compiles and prints the odd numbers in the given range?
1. import java.util.ArrayList;
2.
3. public class OddNumbers
4. {
5. public static void main(String args[])
6. {
7. ArrayList oddNumbers = new ArrayList();
8. for(int i=0;i < 10;i++)
9. {
10. if(i%2 == 0)
11. continue;
12. // what should be inserted here?
13. oddNumbers.add(number);
14. System.out.println(number);
15. }
16. }
17. }
A. int number = i;
B. Integer number = new Integer(i);
C. Byte number = new Byte(i);
D. Integer number = Integer.parseInt("" + i);
E. Object number = new Integer(i);
Answer: BE
** =====================
9.6 What will happen when you attempt to compile and run the following code?
interface MyInterface{
int x = 0;
int myMethod(int x);
}
class MyImplementation implements MyInterface{
public int myMethod(int x){
return super.x;
}
}
public class MyTest{
public static void main(String args[]) {
MyInterface mi = new MyImplementation();
System.out.println(mi.myMethod(10));
}
}
A. 0
B. 10
C. Compilation error
D. None of the above
Answer: The statement "super.x;" inside myMethod() will cause compilation error as the super class of MyImplementation class (Object class) doesn't define variable x. Please note that MyInterface is not the super class of MyImplementation class as referred by super.x.
**** =========================
9.7 What is displayed when the following piece of code is executed?
class Test extends Thread{
public void run(){
System.out.println("1");
yield();
System.out.println("2");
suspend();
System.out.println("3");
resume();
System.out.println("4");
}
public static void main(String []args) {
Test t = new Test();
t.start();
}
}
A. 1 2 3 4
B. 1 2 3
C. 1 2
D. Nothing. This is not a valid way to create and start a thread.
E. 1
Answer: C
Note: The code will run, but the thread suspends itself after displaying "2". Threads cannot un-suspend themselves (since they ARE suspended and therefore not running!).
** =======================
9.8 Consider the following code fragment, and select the correct statements(s).
1. public class Test extends MyBase implements MyInterface
2. {
3. int x = 0;
4.
5. public Test(int inVal) throws Exception
6. {
7. if (inVal != this.x)
8. {
9. throw new Exception("Invalid input");
10. }
11. }
12.
13. public static void main(String args[])
14. {
15. Test t = new Test(4);
16. }
17. }
A. The code fails to compile at line 1. It is not valid to both implement an interface and extend from a parent class simultaneously.
B. The code fails to compile at line 5. It is not valid for constructors to throw exceptions.
C. The code fails to compile at line 9, because this is not valid way to throw an exception.
D. The code fails to compile at line 15. The compiler complains that there is an uncaught exception.
E. The code fails to compile at line 7, because this is not a valid way to reference variable x.
Answer: D
**** ===================
9.9 The default implementation of equals() method in Object class does not perform "deep" comparison. State true or false.
A. True
B. False
Answer: A