###############
test 5
###############
***** ========================
5.1 What will happen when you attempt to run the following code from command line using command – java AssertTest?
(Assume that the code is compiled with assertions enabled)
1. public class AssertTest
2. {
3. static int i = 10;
4. public static void main(String args[])
5. {
6. i = i*2;
7. try
8. {
9. assert isValid() : i = i/4;
10. }
catch(AssertionError ignore){}
11.
12. System.out.println("i = " +i);
13. }
14.
15. public static boolean isValid()
16. {
17. i = i * 2;
18. return false;
19. }
20. }
A. It will print - i = 20
B. It will print - i = 10
C. It will print - i = 40
D. It will print - i = 5
E. None of these
Answer: A
Note: It is not runtime Assertion enabled! And assert Expression1 cannot have side effect.
*** ===============
5.2 What will happen when you attempt to compile and run the following code?
1. public class WrapperDemo
2. {
3. public static void main(String args[])
4. {
5. Character c = new Character('A');
6. Integer i = new Integer('A');
7.
8. int a = c.intValue();
9. int b = i.intValue();
10.
11. if(a == b)
12. System.out.println("Both are equal");
13. else
14. System.out.println("Both are not equal");
15. }
16. }
A. It will print - Both are equal
B. It will print - Both are not equal
C. It will throw NumberFormatException at runtime
D. Compilation error at line 8
E. Compilation error at line 6
Answer: D
Note: The Wrapper class Boolean and Character do not extend java.lang.Number,and do not implement its intValue() method.
** =====================
5.3 Given a byte with a value of 01110111, which of the following statements will produce 00111011?
Note : 01110111 = 0x77
A. 0x77 << 1;
B. 0x77 >>> 1;
C. 0x77 >> 1;
D. B and C.
E. None of the above
Answer: D
Note: Since the leading bit(first bit from left) is a 0, both the >> and >>> operators work identically.
** ==================
5.4 A class with all its constructors as private can't be extended and instantiated by any other class. True/False?
A. True
B. False
Answer: A
Note: From the child class default constructor, there is a default call to the parent class default constructor.
*** =======================
5.5 Which of the following statements are correct?
A. Arithmetic promotion of object references requires explicit casting.
B. Both primitives and object references can be both converted and cast.// primitives is the base data types
C. Only primitives are converted automatically; to change the type of an object reference, you have to do a cast.
D. Only object references are converted automatically; to change the type of a primitive, you have to do a cast.
E. Casting of numeric types may require a runtime check.
Answer: B
Note: only the casting of object references may potentially require a runtime check.
***** ========================
5.6 Comment if the following code implements hashCode() method of Object class correctly? Select the most appropriate answer.
1. public class CorrectHashCode
2. {
3. private int number;
4.
5. public CorrectHashCode(int i)
6. {
7. number = i;
8. }
9.
10. public int hashCode()
11. {
12. int i = (int)(Math.random() * 100);
13. return i * number;
14. }
15.
16. // other methods
17. }
A. The code implements hashCode() method correctly
B. The code does not implement hashCode() method correctly
C. This implementation of hashCode() method is correct if only if the equals() method is not overridden
D. The given code does not contain enough information to comment on the correctness of the hashCode() method implementation
E. None of these
Answer: B
***** ====================
5.7 What will happen if you compile/run this code? Assume the code to be written inside main().
int i = 012;
int j = 034;
int k = 056;
int l = 078;//error, 八进制越过范围
System.out.println(i);
System.out.println(j);
System.out.println(k);
A. Prints 12, 34 and 56.
B. Prints 24, 68 and 112.
C. Prints 10, 28 and 46.
D. Compilation error.
Answer: D
**** =====================
5.8 Which of the following is a valid way to declare an abstract method which is intended to be public?
A. public abstract method();
B. public abstract void method();
C. public abstract void method(){}
D. public virtual void method();
E. public void method() implements abstract;
Answer: B
Note: the {} braces indicate that the method is actually implemented, and is therefore not abstract. In other words, even empty braces are a valid way to define a method.
** ====================
5.9 What is the effect of adding the sixth element to a Vector created using the following code?
new Vector(5, 10);
A. An IndexOutOfBounds exception is thrown.
B. The vector grows in size to a capacity of 10 elements.
C. The vector grows in size to a capacity of 15 elements.
D. Nothing. Size is the number of the elements in vector, while capacity will increase. The Vector will have grown when the fifth element was added, because Vector elements are zero-based.
*** ================
5.10 You have created a TimeOut class as an extension of Thread, the purpose being to print a"Time's Up" message if the Thread is not interrupted within 10 seconds of being started. Here is the run method which you have coded.
1. public void run()
2. {
3. System.out.println("Start!");
4. try
5. {
6. Thread.sleep(10000 );
7. System.out.println("Time's Up!");
8. }
9. catch(InterruptedException e)
10. {
11. System.out.println("Interrupted!");
12. }
13. }
Given that a program creates and starts a TimeOut object, which of the following statements is true?
A. Exactly 10 seconds after the start method is called, "Time's Up!" will be printed.
B. Exactly 10 seconds after "Start!" is printed, "Time's Up!" will be printed.
C. The delay between "Start!" being printed and "Time's Up!" will be 10 seconds plus or minus one tick of the system clock.
D. If "Time's Up!" is printed you can be sure at least 10 seconds have elapsed since "Start!" was printed.
Answer: D