SCJP Certification

Sun Certified Java Programmer Certification exam essentials

November 23rd, 2009

What will be the output of the following program code?

Java Questions, by Daisy Williams.

class Ques0357{
public static void main(String[] argv){
int s = 2;
switch (s) {
case 1:
System.out.println(“Fred”);
break;
case 2:
System.out.println(“Fast”);
case 3:
System.out.println(“Tech”);
default:
System.out.println(“default”);
}
}
}

The break statement is used inside the code block following the switch statement to terminate the execution of a statement sequence. As soon as a break statement is encountered, the program control is transferred to the first line of code that follows the entire switch statement.

In the given program, the argument to the switch statement is an int value 2. Therefore, the program control will be transferred at the matching case label, i.e., at line number 8. As there is no break statement after line number 7, the execution of the code will continue to fall through and all the statements after line number 8 will be executed. Therefore, the execution of the program will display Fast Tech default as the output.

http://www.scjp-certification.com/what-is-the-for-each-loop.html

Share

Back Top

Responses to “What will be the output of the following program code?”

Comments (0) Trackbacks (0) Leave a comment Trackback url
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*