Top 15 Core Java pre-interview technical written test questions and answers -- part 4
Q10: When is an object needs to implement the Comparable interface?
a) When adding it to a HashSet.
b) When adding it to a TreeSet.
c) When adding it to a LinkedHashSet.
d) When adding it to any Java Collection class
A10: The answer is b.
1. HashSet is implemented using a HashMap, hence the elements are not ordered.
2. TreeSet is implemented using a tree structure(red-black tree algorithm). The elements in a set are sorted. It offers several methods to deal with the ordered set like first( ), last( ), headSet( ), tailSet( ), etc. Any object you add to it must implement the Comparable interface, otherwise it throws a runtime exception
Exception in thread "main" java.lang.ClassCastException: com.writtentest6.Employee cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at java.util.TreeSet.add(TreeSet.java:255)
TreeSet maintains the elements in the order in which the public int compareTo(String o) method is implemented. Any object that implements the Comparable interface must provide the public int compareTo(String o) method implementation.
3. LinkedHashSet is between a HashSet and TreeSet. It is implemented as a HashMap with a linked list running through it. So, it maintains the elements in the order in which they are added.
Q11: Given the following code
List plainList = new ArrayList(); List<String> specList = new ArrayList<String> ();
a) adding a non String object to specList will throw a compile-time error
b) adding a non String object to specList will throw a runtime error
c) adding a non String object to specList will be slower than adding it to a plainList
d) adding a non String object to specList will throw a checked exception
e) you can add any objects to a plainList
A11: Answer is a & e.
Java generics check happens at compile-time. Learn more at Java Interview Questions & Answers: Compile-time versus runtime. Generics is a bit tricky subject to master, and a must know topic to do well in coding and written tests, hence I have dedicated a whole chapter in my book "Core Java Career Essentials"
Q12: Which of the following are right way to run a thread in the JVM?
a) A class MyThreadClass that extends the Thread class, and
MyThreadClass myT = new MyThreadClass(); myT.start();
b) A class MyThreadClass that extends the Thread class, and
MyThreadClass myT = new MyThreadClass(); myT.run();
c) A class MyThreadClass that implements the Runnable interface, and
MyThreadClass myT = new MyThreadClass(); Thread t = new Thread(myT); myT.start();
d) A class MyThreadClass that implements the Runnable interface, and
MyThreadClass myT = new MyThreadClass(); Thread t = new Thread(myT); myT.run();
A12: Answer is a and c.
You need to invoke the start( ) method and not the run( ) method. Invoking the start( ) method will invoke the run( ) method indirectly.
Q. Why can't you invoke the run( ) method directly?
A. The start method makes sure the code runs in a new thread context. If you called run directly, then it would be like an ordinary method call and it would run in the context of the current thread instead of the new one. Invoking the start( ) method will invoke the run( ) method indirectly with its special code.
Q13: What value will the following method return?
public static int getSomeNumber( ){ try{ return 2; } finally { return 1; } }
a) 1 is returned
b) 2 is returned
c) Compile error "unreachable code is returned"
d) Runtime error "ambiguous return statement"
A13: The answer is a. 1 is returned because 'finally' has the right to override any exception/returned value by the try..catch block. It is a bad practice to return from a finally block as it can suppress any exceptions thrown from a try..catch block.
You may also like
- Top 15 Core Java pre-interview technical written test questions and answers 1 -3
- Top 15 Core Java pre-interview technical written test questions and answers 4 -7
- Top 15 Core Java pre-interview technical written test questions and answers 8 - 9
- Top 15 Core Java pre-interview technical written test questions and answers 10 - 13
- Top 15 Core Java pre-interview technical written test questions and answers 14 - 15
Labels: Core Java, Written tests
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home