• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam 1 switch question (Java OCA 8 Programmer I Study Guide, Sybex)

 
Aleksandra Pestova
Ranch Hand
Posts: 36
2
Firefox Browser Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 5 and 6 in the attached image seem weird to me.
I thought it would give a compiler error, but the correct answer is B: Congratulations.

I was thinking about a nested switch as well, but then default comes in a weird position.

Could someone please give me a hint what's going on here?
switch_question.JPG
[Thumbnail for switch_question.JPG]
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how It would look.

  • I hope you know since Java 7 we can use String object in the expression of a switch statement.
  • String year has value "Senior" so It matches to case "Senior": on line no 9 then executes It's statement i.e. System.out.print("Congratulations");
  • You can have default label anywhere in switch block but at most one.
  • You can have case labels without break which causes fall through.

  • Edited:
    What is fall through?
  • When a case label is matched and that case label doesn't have break statement then after executing statements of matched case label control will go to next case label and execute It's statement called fall through statements. It will continue until either It encounter break Or closing curly brace of Switch block.
  • Each break statement terminates the enclosing switch statement.
  • Example of fall through:
  • Output:
    I'm Freshman
    I'm default
    I'm Sophomore
    I'm Junior
  • In above example year = "Freshman" which matches case label at line no 3 and executes It's statement System.out.println("I'm Freshman"); but case "Freshman": doesn't have break which is valid but makes control go to next further case labels i.e. default:, case "Sophomore": , case "Junior": and executes their statements but after case "Junior": control doesn't flow to next case "Senior": because case "Junior": has break which terminates enclosing switch statement means control comes out of switch(year).
  •  
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • One correct answer, get two likes Can you guess the output of following code?
  •  
    Aleksandra Pestova
    Ranch Hand
    Posts: 36
    2
    Firefox Browser Java Notepad
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot Ganesh! I didn't expect that you could just leave empty case statements.

    Answer to your quiz questions: 11.
    The first matching case is 5, then x becomes x=7. Since neither this case, nor following cases have a break statement, then default and case 2 are executed and x becomes 11. 
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Correct , yes you're welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic