import java.awt.*; class Ticker extends Component { public static void main (String [] args) { Ticker t = new Ticker(); /* Missing Statements? */ } }
which two of the following statements, inserted independently, could legally be inserted into missing section of this code? package testpkg.p1; public class ParentUtil { public int x = 420; protected int doStuff() { return x; } } package testpkg.p2; import testpkg.p1.ParentUtil; public class ChildUtil extends ParentUtil { public static void main(String [] args) { new ChildUtil().callStuff(); } void callStuff() { System.out.print("this " + this.doStuff() ); /* Line 18 */ ParentUtil p = new ParentUtil(); System.out.print(" parent " + p.doStuff() ); /* Line 20 */ } }
which statement is true?
/* Missing Statement? */ public class foo { public static void main(String[]args)throws Exception { java.io.PrintWriter out = new java.io.PrintWriter(); new java.io.OutputStreamWriter(System.out,true); out.println("Hello"); } }
What line of code should replace the missing statement to make this program compile?
/* Missing statements? */ public class NewTreeSet extends java.util.TreeSet { public static void main(String [] args) { java.util.TreeSet t = new java.util.TreeSet(); t.clear(); } public void clear() { TreeMap m = new TreeMap(); m.clear(); } }
which two statements, added independently at beginning of the program, allow the code to compile? class A { protected int method1(int a, int b) { return 0; } }
Which is valid in a class that extends class A?
class Bar { } class Test { Bar doBar() { Bar b = new Bar(); /* Line 6 */ return b; /* Line 7 */ } public static void main (String args[]) { Test t = new Test(); /* Line 11 */ Bar newBar = t.doBar(); /* Line 12 */ System.out.println("newBar"); newBar = new Bar(); /* Line 14 */ System.out.println("finishing"); /* Line 15 */ } }
At what point is the Bar object, created on line 6, eligible for garbage collection?
class Boo { Boo(String s) { } Boo() { } } class Bar extends Boo { Bar() { } Bar(String s) {super(s);} void zoo() { // insert code here } }
which one create an anonymous inner class from within class Bar?
class Foo { class Bar{ } } class Test { public static void main (String [] args) { Foo f = new Foo(); /* Line 10: Missing statement? */ } }
which statement, inserted at line 10, creates an instance of Bar?
class HappyGarbage01 { public static void main(String args[]) { HappyGarbage01 h = new HappyGarbage01(); h.methodA(); /* Line 6 */ } Object methodA() { Object obj1 = new Object(); Object [] obj2 = new Object[1]; obj2[0] = obj1; obj1 = null; return obj2[0]; } }
Where will be the most chance of the garbage collector being invoked?
class Test { private Demo d; void start() { d = new Demo(); this.takeDemo(d); /* Line 7 */ } /* Line 8 */ void takeDemo(Demo demo) { demo = null; demo = new Demo(); } }
When is the Demo object eligible for garbage collection?
class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for(int i = 0; i < lines.length; i++) { System.out.println(lines[i]); Thread.currentThread().sleep(1000); } } }
the static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
class Test1 { public int value; public int hashCode() { return 42; } } class Test2 { public int value; public int hashcode() { return (int)(value^5); } }
which statement is true?
class X implements Runnable { public static void main(String args[]) { /* Missing code? */ } public void run() {} }
Which of the following line of code is suitable to start a thread?
class X2 { public X2 x; public static void main(String [] args) { X2 x2 = new X2(); /* Line 6 */ X2 x3 = new X2(); /* Line 7 */ x2.x = x3; x3.x = x2; x2 = new X2(); x3 = x2; /* Line 11 */ doComplexStuff(); } }
after line 11 runs, how many objects are eligible for garbage collection?
import java.awt.Button; class CompareReference { public static void main(String [] args) { float f = 42.0f; float [] f1 = new float[2]; float [] f2 = new float[2]; float [] f3 = f1; long x = 42; f1[0] = 42.0f; } }
which three statements are true? import java.io.*; public class MyProgram { public static void main(String args[]) { FileOutputStream out = null; try { out = new FileOutputStream("test.txt"); out.write(122); } catch(IOException io) { System.out.println("IO Error."); } finally { out.close(); } } }
and given that all methods of class FileOutputStream, including close(), throw an IOException, which of these is true?
public class Test { public static void main(String [] args) { int [] [] [] x = new int [3] [] []; int i, j; x[0] = new int[4][]; x[1] = new int[2][]; x[2] = new int[5][]; for (i = 0; i < x.length; i++) { for (j = 0; j < x[i].length; j++) { x[i][j] = new int [i + j + 1]; System.out.println("size = " + x[i][j].length); } } } }
interface Base { boolean m1 (); byte m2(short s); }
which two code fragments will compile?