Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Switch Statement

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have this program from OCA book


What is the output of the following program?
A. 4
B. 5
C. 6
D. 7
E. 13
F. The code will not compile because of line 7.
i execute this program and i have c)6 is the right response but i don't undrestand how to get this result
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please always let the thread title tell people what the question is about. Please don't write line numbers inside code tags; the tags add line numbers automatically.

Go through the code with a pencil and paper and write down the values of x, y, and count as you go through that program. That should explain it to you.
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I merged your stuff with the following thread. I hope that is okay by you.
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have thie progrem of OCA book:

what is the output of the following program?
A.4
B.5
C.6
D.7
E.13
F.the code will not compile because of the line 7
i execute this code and i get the right response is c.6 but i don'y undrestand whay the result is 3
 
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please QuoteYourSources    

If I'm not wrong this is from Assessment Test question 4, OCA 8 Sybex book. Book answer is correct i.e. 6
There was error in explanation in first printing of this book click here --> OCAJP8 Erratas Sybex which seems fixed in 2nd printing.

emma robert wrote: but i don'y undrestand whay the result is 3

where did you get this result 3 from?
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes this is the fourth question of  the oracle Certified Associate Java SE8 Programmer I i ,I found it difficult to understand the switch block!!
 
Ganesh Patekar
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

emma roberts wrote:I found it difficult to understand the switch block!!

What exactly you think you haven't understood about switch block, will you please elaborate more? May be by explaining the flow of this example?
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not understand how to have the result 6 and I get used to the structure  {

that's why I did not understand the

 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual prohibition as on most websites against writing ALL UPPER CASE also applies to thread titles.
Please don't ask the same question in two threads; fortunately I can merge this threa‍d with your previous t‍hread.
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

emma roberts wrote:I did not understand how to have the result 6 and I get used to the structure  {
. . .

You have actually coped it incorrectly; the { should go after switch (var).
Please have a look in the Java™ Tutorials, and note carefully what the statement break; means. Also note what its absence means.
 
Ganesh Patekar
Bartender
Posts: 1248
86
Hibernate jQuery Eclipse IDE Angular Framework Spring MySQL Database AngularJS Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

emma roberts wrote:I get used to the structure  {

that's why I did not understand the


Niether It's mandatory for a case label to have break statement after It nor default to be at specific location. Go through the link CB mentioned.
Since this is your beginning to read the OCA book I guess?  you can note down assessment marks and  go ahead reading book chapter by chapter to discover such mysteries of Java. If you can't resist curiosity then go-to chapter 2, page no 72. Peruse the switch statement part If still confusion persist, no worries you are always welcome ...
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not used to having this switch structure that starts with default with case inside even in the book OCA they talked  about standard format structure

I did not understand how the case in default will be executed
 
emma roberts
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i can not understand when my y is neither 0 nor 1 which value will be returned by switch block
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which of the three options do you think will be used? Have you looked at the tutorial link I gave you yesterday?
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic