***** ============================
3.7 What is displayed when the following piece of code is compiled and executed?
class Test {
public static void main(String [] args) {
Base b = new Subclass();
System.out.println(b.x);
System.out.println(b.method());
}
}
class Base {
int x = 2;
int method(){
return x;
}
}
class Subclass extends Base {
int x = 3;
int method() {
return x;
}
}
A. Nothing. The code fails to compile because the object b is not created in a valid way.
B. 2 3
C. 2 2
D. 3 3
E. 3 2子类引用赋给父类,运行时按父类变量赋值,而方法则用子类的
Answer: B
** =======================
3.8 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled.)
1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. for(int i=0;i <10;i++)
6. {
7. try
8. {
9. assert i%2==0 : i--;
10. System.out.println("Even number : " + i);
11. }
12. catch(AssertionError ae)
13. {
14. System.out.println("Odd number : " + ++i);
15. }
16. }
17. }
18. }
A. It will print odd and even numbers from 0 to 9 correctly (0 even and 1 odd)
B. It will print odd and even numbers from 0 to 9 incorrectly (0 odd and 1 even)
C. Compilation error at line 9
D. Compilation error at line 10
E. It will result in an infinite loop
Answer: A
** ====================
3.9 What will happen when you attempt to compile and run the following code?
(Assume that the code is compiled and run with assertions enabled)
1. public class AssertTest
2. {
3. public static void main(String args[])
4. {
5. float f1 = Float.NaN;
6. float f2 = f1;
7. float f3 = 1.2f;
8.
9. try
10. {
11. assert (f2 == f1) : f2 = 2;
12. f3 = 1.5f;
13. }catch(AssertionError ae)
14. {
15. f3++;
16. }
17.
18. f3 += f2;
19. System.out.println("f3 = " + f3);
20. }
21. }
A. Compilation error at line 5
B. Compilation error at line 7
C. It will print - f3 = 3.5
D. It will print - f3 = 4.2
E. It will print - f3 = NaN
Answer: D
**** =================
3.10 Which of the following will compile without errors?
A. Byte b = new Byte(123);
B. Byte b = new Byte("123");
C. Byte b = new Byte();//Constructor Summary
Byte(byte value)
Constructs a newly allocated Byte object that represents the specified byte value.
Byte(String s)
Constructs a newly allocated Byte object that represents the byte value indicated by the String parameter.
b = 123;
D. Byte b = new Byte((int)123.4);
Answer: B
**** =====================
3.11 What will happen when we try to compile the following code?
1. public void WhichArray( Object x )
2. {
3. if( x instanceof int[] )
4. {
5. int[] n = (int[]) x ;
6. for( int i = 0 ; i < n.length ; i++ )
7. {
8. System.out.println("integers = " + n[i] );
9. }
10. }
11. if( x instanceof String[] )
12. {
13. System.out.println("Array of Strings");
14. }
15. }
A. The compiler objects to line 3 comparing an Object with an array.
B. The compiler objects to line 5 casting an Object to an array of int primitives.
C. The compiler objects to line 11 comparing an Object to an array of Objects.
D. It compiles without error.
Answer: D
**** ==================
3.12 Which of the following statements about threading are true?
A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
B. You can obtain a mutually exclusive lock on any object.
C. You can't obtain a mutually exclusive lock on any object.
D. Thread scheduling algorithms are platform dependent.
Answer: BD