Chapter: missed questions Flashcards
True or False:
If a Thread’s priority is not specified explicitly then it gets a priority of Thread.NORM_PRIORITY
In such a case, the Thread gets the same priority as the thread that has created it.
So if a thread t1 having a priority Thread.MAX_PRIORITY creates a thread t2, t2 will also get a priority of Thread.MAX_PRIORITY.
Considering the following code what is the result:
class Person {
String name;
public Person(String name) { this.name = name; }
}
class MyClass { TreeSet set = new TreeSet(); public static void main(String... args) { set.add(new Person("Math")); System.out.println("added Math"); set.add(new Person("Peter")); System.out.println(set); } }
It will first print added math.
Then it will throw a ClassCastException as Person does not implement the Comparable interface. The TreeSet is a sortedset so it needs to use the compareTo method.
Which of these group of statements are valid?
- { { } }
- { continue ; }
- block : { break block ; }
- block : { continue block ; }
- The break keyword can only be used if there exists an enclosing loop construct ( i.e. while, do-while or for ).
1 and 3 are valid
Continue can only be used in loops.
What is the result?
class Game { public void play() throws Exception { System.out.println("Playing..."); } }
public class Soccer extends Game { public void play() { System.out.println("Playing Soccer..."); } public static void main(String[] args) { Game g = new Soccer(); g.play(); } }
It will not compile.
It does not override play() properly, missing throws clause.
What is the result? class Test { public static void main(String[] args) { int i = 4; int ia[][][] = new int[i][i = 3][i]; System.out.println( ia.length + ", " + ia[0].length+", "+ ia[0][0].length); } }
4,3,3
In an array creation expression, there may be one or more dimension expressions, each within brackets. Each dimension expression is fully evaluated before any part of any dimension expression to its right. The first dimension is calculated as 4 before the second dimension expression sets ‘i’ to 3.
Note that if evaluation of a dimension expression completes abruptly, no part of any dimension expression to its right will appear to have been evaluated.
Which of these are not part of the StringBuffer class?
trim() ensureCapacity(int ) append(boolean ) reverse( ) setLength(int)
trim();
It’s part of the String class
Consider the following code:
public class FinalizeTest { private String value; public FinalizeTest(String text) { this.value = text; } public static void main(String[] args) { FinalizeTest f1 = new FinalizeTest("Hello"); FinalizeTest f2 = new FinalizeTest("World"); f1 = null; f2 = null; } public void finalize(){ System.out.println(this.value); super.finalize(); } }
Which of the following statements correctly describe the behavior this class when it is run?
1. hello world is a possible output. 2. It will never produce any output. 3. It will not compile. 4. It will throw an exception at run time.
super.finalize() throws a Throwable, so should either be try-catched or added to the throws clause.
What is the result?
public class TestClass { public static int getSwitch(String str) { return (int) Math.round( Double.parseDouble(str.substring(1, str.length()-1)) ); } public static void main(String args []) { switch(getSwitch(args[0])) { case 0 : System.out.print("Hello "); case 1 : System.out.print("World"); break; default : System.out.print(" Good Bye"); } } }
Hello World
str.substring(1, str.length()-1) => “–0.50”.substring(1, (6-1) ) => -0.5
Math.round(-0.5) = 0.0
so getSwitch(…) returns 0 if passed an argument of “–0.50”.
Now, there is no “break” in case 0 of switch. so the control falls through to the next case ( ie. case 1) after printing Hello. At case 1, it prints World. And since there is a break. default is not executed.
What should be the return type of the following method?
public RETURNTYPE methodX( byte by) { double d = 10.0; return (long) by/d*3; }
double
Note that the cast (long) applies to ‘by’ not to the whole expression.
( (long) by ) / d * 3;
Now, division operation on long gives you a double. So the return type should be double.
Consider the following class…
class TestClass { static int x; public static void main(String[] args) { System.out.println(this.x); } }
main method is a static method, so it has no reference to this.
Consider the following code:
class Super { static String ID = "QBANK"; } class Sub extends Super { static { System.out.print("In Sub"); } } class Test { public static void main(String[] args) { System.out.println(Sub.ID); } }
What will be the output when class Test is run ?
It will print ‘In Sub’ and ‘QBANK’.
What will be the result of attempting to compile and run the following program?
public class TestClass { public static void main(String args[ ] ) { StringBuffer sb = new StringBuffer("12345678"); sb.setLength(5); sb.setLength(10); System.out.println(sb.length()); } }
It will print 10.
Although it truncates the string to length 5 but setLength(10) will append 5 spaces (actually null chars ie. \u0000).
What can be returned by the return statement of methodX()?
publicbyte methodX() {
return INSERT_CODE_HERE; }
- 1000
- 10
- new Integer(10);
- new Byte(10);
- 10
Since the return type is byte, the return value must fit into a byte (A byte has a range of -128 to 127). Therefore 1000 cannot be a valid value. Further, an Integer object cannot be unboxed to a byte. But new Byte((byte)10);, would be valid. Had the return type been int, return new Integer(0); would have been valid.
What happens when a thread executes wait() method on an object without owning the object’s lock?
If the thread does not own the lock, it throws an IllegalMonitorStateException upon calling wait().
Given:
enum Season { SUMMER, WINTER, SPRING, FALL }
What will the following code print?
Season s = Season.SPRING; switch(s) { case SUMMER : System.out.println("SUMMER"); case default : System.out.println("SEASON"); case WINTER : System.out.println("WINTER"); }
case default : System.out.println(“SEASON”); is syntactically wrong. It should just be:
default : System.out.println(“SEASON”);