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");
A -
Finished
B -
Exception
C -
Compilation fails.
D -
Arithmetic Exception
2-
Which statement is true?
A -
A try statement must have at least one corresponding catch block.
B -
Multiple catch statements can catch the same class of exception more than once.
C -
An Error that might be thrown in a method must be declared as thrown by that method, or be handled within that method.
D -
Except in case of VM shutdown, if a try block starts to execute, a corresponding finally block will always start to execute.
3-
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?
A -
class Test1 will not compile.
B -
The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
C -
The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
D -
class Test2 will not compile.
4-
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 */
A -
Compilation fails.
B -
It prints "barhi"
C -
An error occurs at runtime.
D -
It prints "foobarhi"
5-
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?
A -
after line 12
B -
after line 14
C -
after line 7, when doBar() completes
D -
after line 15, when main() completes
6-
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?
A -
After line 7
B -
After line 8
C -
After the start() method completes
D -
When the instance running this code is made eligible for garbage collection.
7-
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?
A -
0
B -
1
C -
2
D -
3
8-
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?
A -
Line 5
B -
Line 6
C -
Line 12
D -
Line 14
9-
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));
}
}
A -
3.0
B -
-3.0
C -
NaN
D -
Compilation fails.
10-
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);