Win a copy of Spring Boot in Practice this week in the Spring 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Switch

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code fragment
switch( x ) {
case 100 : System.out.println( "One hundred" );break;
case 200 : System.out.println( "Two hundred" );break;
case 300 : System.out.println( "Three hundred" );break;
}
choose all of the declarations of x that will not cause a compiler error. [Check all correct answers] (one or more may be correct)
A) byte x = 100 ;
B) short x = 200 ;
C) int x = 300 ;
D) long x = 400 ;
what will be the output
according to me it should be A B C
but it's B C
will anybody explain me y ?
thanks
rahul
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rahul
i tested the code, and byte x = 100; worked.
so it should be A,B,C.
 
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason this doesn't work is because all the case constants should be able to fit in the range of the variable type. a bytes variable range is from -128(2^7) - 127(2^7-1). Therefore 300 cannot fit into a byte so "byte x" cannot be used.
 
Rahul Pawar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks lee ,but still on exam cram site it is mentioned that ans is B C
i am posting u all the link of the site.
please go thru it
http://www.examcram.com/studyresource/qod/QODQuestion.asp?

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rahul!
Answers b and c are correct. The type used in the switch statement must accommodate all of the values in the case statements. For this reason, both b and c are acceptable. There are two considerations that the compiler checks; the variable in the switch statement must be capable of assuming all of the values in the case statements, and it must be convertible to an int. Answer a is incorrect; x cannot be a byte type because the case constants 200 and 300 are not compatible. Answer d is incorrect because switch statements cannot use long values. You would have to have a specific cast: switch((int)x) before the compiler would accept it.
 
Ronnie Phelps
Ranch Hand
Posts: 329
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
200 is too large also.
 
lee dalais
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Rahul
i originally tried the whole code sample, but now i did, and
Ronnie has the answer.
 
lee dalais
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, i meant i originally did not try the whole code but then i did and ronnie has the answer.
sorry again
 
Rahul Pawar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ronnie
u r correct
thanks
 
Rahul Pawar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u all for ur immediate reply
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic