Control Flow  «Prev  Next»


try catch statement Java - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 

1. Which lines of output are displayed by the following program?
class Question1 {
 public static void main(String[] args) {
  for(int n=0; n<5; ++n) {
   switch(n) {
   case 0:
    System.out.print(n);
   case 1:
    System.out.print(n+1);
    break;
   case 2:
    System.out.print(n+2);
    break;
   case 3:
    System.out.print(n+3);
   default:
    System.out.println(n-1);
    break;
   }
  }
 }
}


Please select all the correct answers.
  A. 01
  B. 3
  C. 4
  D. 012462


2. Which lines of output are displayed by the following program?
class Question2 {
 public static void main(String[] args) {
  int i,j;
  outer: for(i=0, j=0; i+j < 20; ++i, j += i) {
   inner: while(i < j) {
    if(j == i + 1) continue outer;
    else if(j == i + 2) break inner;
    else ++i;
   }
   System.out.println(i*j);
  }
 }
}

Please select all the correct answers.
  A. 0
  B. 18
  C. 24
  D. 99

3. Which lines of output are displayed by the following program?
class Question3 {
 public static void main(String[] args) {
  for(int i=0;i<100;++i) {
   try {
    if(i % 3 == 0) throw new Exception();
    try {
     if(i % 2 == 0) throw new Exception();
     System.out.println(i);
    }catch (Exception inner) {
     i *= 2;
    }finally{
     i = i + 2;     
    }
   }catch (Exception outer) {
    i += 3;
   }finally{
    i *= 3;
   }
   System.out.println(i);
  }
 }
}


Please select all the correct answers.
  A. 0
  B. 66
  C. 67
  D. 207