test 1
##########
*** ================
1.1 Which of the following are valid constructors of the class java.lang.AssertionError?
A. AssertionError()
B. AssertionError(String detailMessage)
C. AssertionError(boolean detailMessage)
D. AssertionError(int detailMessage)
E. AssertionError(StackTraceElement detailMessage)
Answer: ACD
**** ================
1.2 What should be inserted at line 7 so that the code compiles and guarantees to print -
Apple Cricket Banana in the same order.
1. import java.util.*;
2.
3. public class SetTest
4. {
5. public static void main(String args[])
6. {
7. // what should be inserted here?
8.
9. set.add("Apple");
10. set.add("Cricket");
11. set.add("Banana");
12.
13. Iterator iter = set.iterator();
14. while(iter.hasNext())
15. {
16. System.out.print(iter.next() + " ");
17. }
18. }
19. }
A. Set set = new LinkedHashSet();
B. Set set = new HashSet();
C. Set set = new TreeSet();
D. TreeSet maintains the Set elements sorted according to their natural order; however other sets do not guarantee iteration order of the set. In particular, they do not guarantee that the order will remain constant over time.
Answer: A
Note: JDK 1.4 adds a new collection class LinkedHashSet to the collections framework. LinkedHashSet remembers the order in which objects are inserted.
TreeSet maintains its elements in their natural order, hence iterating will produce "Apple Banana Cricket " instead of "Apple Cricket Banana ". Hence option C is incorrect.
HashSet is a basic implementation of Set. It makes no guarantees as to the iteration order of the set;in particular.
**** ==============================
1.3 What will happen when you attempt to compile and run the following code?
public class Test{
int i = 0;
public static void main(String argv[]) {
Test t = new Test();
t.myMethod();
}
public void myMethod(){
while(true) {
try {
wait();
}catch (InterruptedException e) {}
i++;
}
}
}
A. Compile time error, no matching notify within the method.
B. Compile and run but an infinite looping of the while method.
C. Compilation and run without any output.
E. Runtime Exception "IllegalMonitorStatException".
Answer: E
Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.
***** ====================
1.4 What will be the result of executing the following main() method?
1. public static void main(String[] args)
2. {
3. String myString;
4. int x = 100;
5.
6. if (x < 100) myString = "x is less than 100";
7. if (x > 100) myString = "x is greater than 100";
8. System.out.println(myString.length());
9. }
A. The code will compile with a warning that variable myString might not have been initialized
B. The code will compile without any warnings
C. The code will not compile. But it will compile and print the length of myString if the variable myString is initialized in an unconditional statement before line 8
D. None of these
Answer: D??
Note: myString may be initiated to be null and then run time error.
**** ==================
1.5 What results from running the code below?
int a = -5; int b = -2;
System.out.println(a % b);
A. 0
B. 1
C. -1
D. A compiler error saying "%" operator is not valid for negative numbers.
Answer: C
Note: To calculate a % b(a modulus b), the simplest way is, drop any negative signs from both the operands and calculate the result. Now if the left-hand side operand is negative negate the result, the sign of right-hand side operand doesn't matter. In our case (-5 % -2) is equal to -(5 % 2) = -(1) = -1, hence C is correct. If the calculate a%b(a=5, b=-2), the result will be 1.
** =================
1.6 What is the return type of method add(Object o) of Set interface, and what does it indicate?
A. It returns an int representing hash code of the newly added object
B. It returns an Integer object representing hash code of the newly added object
C. It returns a boolean. Returns true if this set already contains the specified element.
D. It returns a boolean. Returns true if this set did not already contain the specified element.
E. Return type is void. Any error in the add operation is reported by throwing an exception.
F. None of these
Answer: D
*** =====================
1.7 Which of the following classes implement java.util.Map interface?
A. Hashtable
B. HashMap
C. Dictionary
D. IdentityHashMap
E. Vector
Answer: ABD
Note: The abstract class Dictionary is a legacy class and as per Java API documentation, it is obsolete. It does not implement Map interface.
** ===================
1.8 The package favorite.fruits contains different classes for different fruits. Orange is one of the classes in this package. Assume that all the classes from this package are compiled with assertions enabled. Which of the following will enable assertions at runtime for this package, but will disable it for the class Orange, using standard JDK 1.4?
A. java -ea:favorite.fruits... -da:favorite.fruits.Orange <Main Class>
B. java -ea:favorite.fruits...,-da:favorite.fruits.Orange <Main Class>
C. java -ea:favorite.fruits... -da:Orange <Main Class>
D. java -ea:favorite.fruits... -da Orange <Main Class>
E. None of these
Answer: A+++++++++++++++++++++++
**** ========================
1.9 What is the valid declaration for the finalize() method?
A. protected void finalize() throws Throwable
B. final finalize()
C. public final finalize()
D. private boolean finalize()
E. private final void finalize() throws Exception
Answer: A
** ====================
1.10 What is the result of compiling and executing the following code?
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. The code fails to compile in the main() method.
B. The code fails to compile in the run() method.
C. Only the text "In run" will be displayed.
D. The text "In run" followed by "Leaving run" will be displayed.
E. The code compiles correctly, but nothing is displayed.
Answer: D
##########
*** ================
1.1 Which of the following are valid constructors of the class java.lang.AssertionError?
A. AssertionError()
B. AssertionError(String detailMessage)
C. AssertionError(boolean detailMessage)
D. AssertionError(int detailMessage)
E. AssertionError(StackTraceElement detailMessage)
Answer: ACD
**** ================
1.2 What should be inserted at line 7 so that the code compiles and guarantees to print -
Apple Cricket Banana in the same order.
1. import java.util.*;
2.
3. public class SetTest
4. {
5. public static void main(String args[])
6. {
7. // what should be inserted here?
8.
9. set.add("Apple");
10. set.add("Cricket");
11. set.add("Banana");
12.
13. Iterator iter = set.iterator();
14. while(iter.hasNext())
15. {
16. System.out.print(iter.next() + " ");
17. }
18. }
19. }
A. Set set = new LinkedHashSet();
B. Set set = new HashSet();
C. Set set = new TreeSet();
D. TreeSet maintains the Set elements sorted according to their natural order; however other sets do not guarantee iteration order of the set. In particular, they do not guarantee that the order will remain constant over time.
Answer: A
Note: JDK 1.4 adds a new collection class LinkedHashSet to the collections framework. LinkedHashSet remembers the order in which objects are inserted.
TreeSet maintains its elements in their natural order, hence iterating will produce "Apple Banana Cricket " instead of "Apple Cricket Banana ". Hence option C is incorrect.
HashSet is a basic implementation of Set. It makes no guarantees as to the iteration order of the set;in particular.
**** ==============================
1.3 What will happen when you attempt to compile and run the following code?
public class Test{
int i = 0;
public static void main(String argv[]) {
Test t = new Test();
t.myMethod();
}
public void myMethod(){
while(true) {
try {
wait();
}catch (InterruptedException e) {}
i++;
}
}
}
A. Compile time error, no matching notify within the method.
B. Compile and run but an infinite looping of the while method.
C. Compilation and run without any output.
E. Runtime Exception "IllegalMonitorStatException".
Answer: E
Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.
***** ====================
1.4 What will be the result of executing the following main() method?
1. public static void main(String[] args)
2. {
3. String myString;
4. int x = 100;
5.
6. if (x < 100) myString = "x is less than 100";
7. if (x > 100) myString = "x is greater than 100";
8. System.out.println(myString.length());
9. }
A. The code will compile with a warning that variable myString might not have been initialized
B. The code will compile without any warnings
C. The code will not compile. But it will compile and print the length of myString if the variable myString is initialized in an unconditional statement before line 8
D. None of these
Answer: D??
Note: myString may be initiated to be null and then run time error.
**** ==================
1.5 What results from running the code below?
int a = -5; int b = -2;
System.out.println(a % b);
A. 0
B. 1
C. -1
D. A compiler error saying "%" operator is not valid for negative numbers.
Answer: C
Note: To calculate a % b(a modulus b), the simplest way is, drop any negative signs from both the operands and calculate the result. Now if the left-hand side operand is negative negate the result, the sign of right-hand side operand doesn't matter. In our case (-5 % -2) is equal to -(5 % 2) = -(1) = -1, hence C is correct. If the calculate a%b(a=5, b=-2), the result will be 1.
** =================
1.6 What is the return type of method add(Object o) of Set interface, and what does it indicate?
A. It returns an int representing hash code of the newly added object
B. It returns an Integer object representing hash code of the newly added object
C. It returns a boolean. Returns true if this set already contains the specified element.
D. It returns a boolean. Returns true if this set did not already contain the specified element.
E. Return type is void. Any error in the add operation is reported by throwing an exception.
F. None of these
Answer: D
*** =====================
1.7 Which of the following classes implement java.util.Map interface?
A. Hashtable
B. HashMap
C. Dictionary
D. IdentityHashMap
E. Vector
Answer: ABD
Note: The abstract class Dictionary is a legacy class and as per Java API documentation, it is obsolete. It does not implement Map interface.
** ===================
1.8 The package favorite.fruits contains different classes for different fruits. Orange is one of the classes in this package. Assume that all the classes from this package are compiled with assertions enabled. Which of the following will enable assertions at runtime for this package, but will disable it for the class Orange, using standard JDK 1.4?
A. java -ea:favorite.fruits... -da:favorite.fruits.Orange <Main Class>
B. java -ea:favorite.fruits...,-da:favorite.fruits.Orange <Main Class>
C. java -ea:favorite.fruits... -da:Orange <Main Class>
D. java -ea:favorite.fruits... -da Orange <Main Class>
E. None of these
Answer: A+++++++++++++++++++++++
**** ========================
1.9 What is the valid declaration for the finalize() method?
A. protected void finalize() throws Throwable
B. final finalize()
C. public final finalize()
D. private boolean finalize()
E. private final void finalize() throws Exception
Answer: A
** ====================
1.10 What is the result of compiling and executing the following code?
public class ThreadTest extends Thread {
public void run() {
System.out.println("In run");
yield();
System.out.println("Leaving run");
}
public static void main(String args []) {
(new ThreadTest()).start();
}
}
A. The code fails to compile in the main() method.
B. The code fails to compile in the run() method.
C. Only the text "In run" will be displayed.
D. The text "In run" followed by "Leaving run" will be displayed.
E. The code compiles correctly, but nothing is displayed.
Answer: D