SCJP Flow Control and exception handling
By: admin - Tuesday, September 9th, 2008 at 10:53 am
QUESTION1
What is the result of attempting to compile and run this code ?
public class Test {
public static void main(String[] args){
int j = 0;
for(; j < 3; j++){
if (j==1) break out;
System.out.print(j + "\n");
}
out:{System.out.println("bye");}
}
}
1. The code will fail to compile.
2. The code will run with no out put
3. This will run and print 0, 1 , 2 and "bye"
4. This will run and print 1 and "bye"
ANS : 1
This will fail to compile because the labelled
block does not enclose the break statement.
QUESTION2
What is the result of attempting to compile and run this code ?
class A extends Exception{}
class B extends A{}
class C extends B{}
public class Test {
static void aMethod() throws C{ throw new C(); }
public static void main(String[] args){
int x = 10;
try { aMethod(); }
catch(A e) { System.out.println("Error A");}
catch(B e) { System.out.println("Error B");}
}
}
1. Compiler error
2. It will print "Error A"
3. It will print "Error B"
4. The exception will go uncaught by both catch blocks
ANS : 1
This will not compile because B is a subclass of A,
so it must come before A. For multiple catch statements
the rule is to place the the subclass exceptions before
the superclass exceptions.
QUESTION3
Will the print line statement execute here?
while(true?true:false)
{
System.out.println("hello");
break;
}
1. Yes
2. No
ANS : 1
The boolean condition here evaluates to true
so the print line will execute once.
QUESTION4
What is the result of trying to compile and run this?
public class Test {
public static void main(String[] args){
for(int i=0; i < 2; i++)
{
continue;
System.out.println("Hello world");
}
}
}
1. Prints "Hello world" once
2. Prints "Hello world" twice
3. Compiler error
4. Runs without any output
ANS : 3
This will not compile because the print line statement
is unreachable.
QUESTION5
Consider this code.
public class Test {
public static void main(String[] args){
for(int j = 0; j < 1 ; j++)
{
if (j < 1) continue innerLoop;
innerLoop: for(int i = 0; i < 2; i++)
{
System.out.println("Hello world");
}
}
}
}
What is the result of attempting to compile and run this.
1. The code will not compile.
2. It will run and print "Hello world" twice.
3. It will run and print "Hello world" once.
4. It will run and print "Hello world" thrice.
5. It will run with no output
ANS : 1
QUESTION6
What will the output be ?
public static void main(String[] args){
char c = '\u0042';
switch(c) {
default:
System.out.println("Default");
case 'A':
System.out.println("A");
case 'B':
System.out.println("B");
case 'C':
System.out.println("C");
}
}
1. Prints - Default , A , B , C
2. Prints - A
3. Prints - B , C
4. Prints - A, B , C , Default
5. Prints - Default
ANS : 3
QUESTION7
What wil be printed out when this method runs ?
void getCount(){
int counter = 0;
for (int i=10; i>0; i--) {
int j = 0;
while (j > 10) {
if (j > i) break;
counter++;
j++;
}
}
System.out.println(counter);
}
1. 64
2. 53
3. 76
4. 0
ANS : 4
Counter never gets incremented because the inner loop
is never entered.
QUESTION8
Is this code legal ?
class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
public class Test{
void thrower() throws ExceptionB{
throw new ExceptionB();
}
public static void main(String[] args){
Test t = new Test();
try{t.thrower();}
catch(ExceptionA e) {}
catch(ExceptionB e) {}
}
}
1. Yes
2. No
ANS : 2
QUESTION9
Is this legal ?
class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
public class Test{
void thrower() throws ExceptionA{
throw new ExceptionA();
}
public static void main(String[] args){
Test t = new Test();
try{t.thrower();}
catch(ExceptionB e) {}
}
}
1. Yes
2. No
ANS : 2
correct answer/s : 2
QUESTION10
Is this legal ?
class ExceptionA extends Exception {}
class ExceptionB extends ExceptionA {}
class A{
void thrower() throws ExceptionA{
throw new ExceptionA();
}
}
public class B extends A{
void thrower() throws ExceptionB{
throw new ExceptionB();
}
}
1. Yes
2. No
ANS : 1