Questions
What will be the output of the program?
int I = 0;
label:
if (I < 2) {
System.out.print("I is " + I);
I++;
continue label;
}
- I is 0
- I is 0 I is 1
- Compilation error
- None of the above
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 Test1 will not compile.
- The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
- The Test1 hashCode() method is less efficient than the Test2 hashCode()method.
- class Test2 will not compile.
What will be the output of the program?
int i = 0, j = 5;
tp: for (;;)
{
i++;
for (;;)
{
if(i > --j)
{
break tp;
}
}
System.out.println("i =" + i + ", j = " + j);
- i = 1, j = 0
- i = 1, j = 4
- i = 3, j = 4
- Compilation fails.
Which statement is true?
- A try statement must have at least one corresponding catch block.
- Multiple catch statements can catch the same class of exception more than once.
- An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
- Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
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?
- after line 12
- after line 14
- after line 7, when doBar() completes
- after line 15, when main() completes
What will be the output of the program?
try
{
int x = 0;
int y = 5 / x;
}
catch (Exception e)
{
System.out.println("Exception");
}
catch (ArithmeticException ae)
{
System.out.println(" Arithmetic Exception");
}
System.out.println("finished");
- finished
- Exception
- Compilation fails
- Arithmetic Exception
What will be the output of the program?
public class Foo
{
Foo()
{
System.out.print("foo");
}
class Bar
{
Bar()
{
System.out.print("bar");
}
public void go()
{
System.out.print("hi");
}
} /* class Bar ends */
public static void main (String [] args)
{
Foo f = new Foo();
f.makeBar();
}
void makeBar()
{
(new Bar() {}).go();
}
}/* class Foo ends */
- Compilation fails.
- An error occurs at runtime.
- It prints "foobarhi"
- It prints "barhi"
What will be the output of the program?
String a = "newspaper";
a = a.substring(5,7);
char b = a.charAt(1);
a = a + b;
System.out.println(a);
- apa
- app
- apea
- apep
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?
- After line 7
- After line 8
- After the start() method completes
- When the instance running this code is made eligible for garbage collection.
public class Test { }
What is the prototype of the default constructor?
- Test( )
- Test(void)
- public Test( )
- public Test(void)
public class X
{
public static void main(String [] args)
{
X x = new X();
X x2 = m1(x); /* Line 6 /
X x4 = new X();
x2 = x4; / Line 8 */
doComplexStuff();
}
static X m1(X mx)
{
mx = new X();
return mx;
}
}
After line 8 runs. how many objects are eligible for garbage collection?
- 0
- 1
- 2
- 3
What will be the output of the program?
public class SqrtExample
{
public static void main(String [] args)
{
double value = -9.0;
System.out.println( Math.sqrt(value));
}
}
- 3.0
- -3.0
- NaN
- Compilation fails.
What will be the output of the program?
public class Test
{
public static void main(String args[])
{
class Foo
{
public int i = 3;
}
Object o = (Object)new Foo();
Foo foo = (Foo)o;
System.out.println("i = " + foo.i);
}
}
- i = 3
- Compilation fails.
- i = 5
- A ClassCastException will occur.
What is the narrowest valid returnType for methodA in line 3?
public class ReturnIt
{
returnType methodA(byte x, double y) /* Line 3 */
{
return (long)x / y * 2;
}
}
- int
- byte
- long
- double
Which two cause a compiler error?
float[ ] f = new float(3);
float f2[ ] = new float[ ];
float[ ]f1 = new float[3];
float f3[ ] = new float[3];
float f5[ ] = {1.0f, 2.0f, 2.0f};
- 2, 4
- 3, 5
- 4, 5
- 1, 2
What will be the output of the program?
public class A
{
void A() /* Line 3 */
{
System.out.println("Class A");
}
public static void main(String[] args)
{
new A();
}
}
- Class A
- Compilation fails.
- An exception is thrown at line 3.
- The code executes with no output.
public class Test
{
public void foo()
{
assert false; /* Line 5 /
assert false; / Line 6 /
}
public void bar()
{
while(true)
{
assert false; / Line 12 /
}
assert false; / Line 14 */
}
}
What causes compilation to fail?
- Line 5
- Line 6
- Line 12
- Line 14
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
int x= 0;
int y= 0;
for (int z = 0; z < 5; z++)
{
if (( ++x > 2 ) || (++y > 2))
{
x++;
}
}
System.out.println(x + " " + y);
}
}
- 5 3
- 8 2
- 8 3
- 8 5
What will be the output of the program?
public class MyProgram
{
public static void main(String args[])
{
try
{
System.out.print("Hello world ");
}
finally
{
System.out.println("Finally executing ");
}
}
}
- Nothing. The program will not compile because no exceptions are specified.
- Nothing. The program will not compile because no catch clauses are specified.
- Hello world.
- Hello world Finally executing
You want subclasses in any package to have access to members of a superclass. Which is the most restrictive access that accomplishes this objective?
- public
- private
- protected
- transient
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Which is valid in a class that extends class A?
- public int method1(int a, int b) {return 0; }
- private int method1(int a, int b) { return 0; }
- public short method1(int a, int b) { return 0; }
- static protected int method1(int a, int b) { return 0; }
interface Base
{
boolean m1 ();
byte m2(short s);
}
which two code fragments will compile?
interface Base2 implements Base {}
abstract class Class2 extends Base
{ public boolean m1(){ return true; }}
abstract class Class2 implements Base {}
abstract class Class2 implements Base
{ public boolean m1(){ return (7 > 4); }}
abstract class Class2 implements Base
{ protected boolean m1(){ return (5 > 7) }}
- 1 and 2
- 2 and 3
- 3 and 4
- 1 and 5
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?
f1 == f2
f1 == f3
f2 == f1[1]
x == f1[0]
f == f1[0
- 1, 2 and 3
- 2, 4 and 5
- 3, 4 and 5
- 1, 4 and 5
What will be the output of the program?
class Test
{
public static void main(String [] args)
{
Test p = new Test();
p.start();
}
void start()
{
boolean b1 = false;
boolean b2 = fix(b1);
System.out.println(b1 + " " + b2);
}
boolean fix(boolean b1)
{
b1 = true;
return b1;
}
}
- true true
- false true
- true false
- false false
What will be the output of the program?
class PassS
{
public static void main(String [] args)
{
PassS p = new PassS();
p.start();
}
void start()
{
String s1 = "slip";
String s2 = fix(s1);
System.out.println(s1 + " " + s2);
}
String fix(String s1)
{
s1 = s1 + "stream";
System.out.print(s1 + " ");
return "stream";
}
}
- slip stream
- slipstream stream
- stream slip stream
- slipstream slip stream