Some traps try to point out obscure features of Java or the machines they run on but miss the obvious which is far more important IMHO.
A good example is MDC Java Traps
1. The code in finally block will never get executed in the following program? True/False
try {
if (choice) {
while (true)
} else {
system .exit(1):
}
}finally {
codetocleanup();
}
The answer provided: True
A better answer: It doesn't compile, There is a ';' missing from while(true)
2. Write an nearest equivalent of size operator in C. Hint: Use Runtime class.
The answer provided: Use of Runtime.freememory() (see the page for the full answer) It notes this is unreliable, and it is.
A better answer: Use Instrumentation.getObjectSize()
3. Which one is faster in java ?
a. for(int i = 100000; i > 0; i--) {}
b. for(int i = 1; i < 100001; i++) {}
The answer provided: a.
A better answer: The performance is notional and clarify/easy of maintenance is far more important, use 2.
4. Which one is faster in java ?
a. Math.max(a,b);
b. (a>b)?a:b
The answer provided: 2.
A better answer: neither, Method 1 is inlined by the JVM so you should use the one which is clearer, I suggest 1.
5. Is Array operations are faster or of Vector.
The answer provided: Array.
A better answer: It is important not to confuse array e.g. a[] with the Array class which is a helper class for arrays. Using the Array class is often slower than using a Vector or an array. In comparing array and Vector, array will often be faster, but some operations such as adding to/removing from the end of a Vector will be typically faster than doing so from an array (as you have to copy the whole array to do this)
6. What will be the value of Point p after methods in a and b if the value before method call is (700,800).
a.
static void changePoint ( Point p) {
p.x = 100; p.y=200;
}
b.
static void changePoint(Point p) {
p=new Point(100,200);
}
Actually p doesn't exist after methods are called. The object which was p might not exist either. You cannot tell from this code.
7. MyClass.java and empty file is valid source file. True/False?
The question doesn't make sense.
13. A class without a method can be run by JVM if its ancestor class has main. True/False?
A class without a method can be run by the JVM regardless of its ancestors.
14. GC is a high priority thread. True/False?
The answer provided: False, GC is a low priority thread.
A better answer: The question is misleading and the answer doesn't give you useful information. The priority of threads is often not honoured depending on the platform and if you are low on memory the GC can take "priority" over all other threads.
21. Which one is not correct
a. x = = Float.NaN
b. Float.isNan(x);
c. Myobject .equals(float.NaN);
The answer provided: a.
A better answer: none of them. b) The method should be "isNaN", c) The class is "Float" not "float"
25. If you have reference variable of parent class type and you assign a child class object to that variable and invoke static method. Which method will be invoked? Parent/Child.
It is assumed that the static method is called on the reference variable. However, if you invoke a static method any class can be called.
26. Local variables can not be declared static or final or transient .True/False?
Local variables can be final!
28. What is the use of transient variable? Can a transiant variable be static?
All local variables and static fields are effectively transient.
31. Map implements collection. True/False?
"Map" implements a collection, but "Map" doesn't extend "Collection". note the capital!.
40. What is the rule regarding overriding methods throwing exceptions?
An overriding method cannot declare a more generic checked exception that its parent. This does not hold for unchecked exceptions.
41. Member variables are resolved compiletime or runtime?
Member fields and local variables are resolved at compile time. However fields accessed via reflections are resolved at run time.
44. Can we have static method in interface?
No, but you can have static methods in a nested class of an interface and thus defined "inside" the interface.
45. Can an interface have variables? Can these variables be transient?
An interface can have static final fields. Static fields are implicitly transient.
Add Comment