SCJP Module-2 Question-19


 

SCJP Module-2 Question-19

The program given below check your knowledge of core flow control of core Java and also very useful in preparation of SCJP examination.

The program given below check your knowledge of core flow control of core Java and also very useful in preparation of SCJP examination.

What will be the output of the following block of code: 

public class Example19{
static int a = 7;

public static void main(String[] args) {
String str = "";
for (int b = 0; b < 3; b++) {
a++;
switch (a) {
case 8:
str += "8 ";
case 9:
str += "9 ";
case 10: {
str += "10 ";
break;
}
default:
str += "d ";
case 13:
str += "13 ";
}
}
System.out.println(s);
}

static {
a++;
}
}

What will be the output of the following block of code: 

1.   9 10 10 d 13

2.   d 13 10  9 10

3.   10 10 9 d 13

4.   9 10 10 13 d

Answer :

(1)

Explanation :

Firstly, the static block will execute & increment the value of  After it, At first iteration of the loop ,the  output will be 9 10. And at second iteration ,the output will be 10 & At last iteration ,the output will be d,13.

Ads