Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Switch and Assertion

 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

when i compile this i get the below compile error

d:\javac TestAssert.java
TestAssert.java:10: missing return statement
}
^
1 error

Please expalin the compilation error.
 
Prabhat Gupta
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As i guess your method String f(int i) return String Object. and that return
statement is missing in your method body .
-------------------------
String f(int i){
switch (i){
case 0: return "A";
case 1: return "B";
case 2: return "C";
default:
assert false;
}
return "String" ;
}

just check above code..it should compile.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi this is the actual qustion.Please give your inputs
-------------------

what will be output after compiling and running with assertion enabled.

Select any 1 option

a)prints 'ABC' and throws AssertionError
b)prints 'ABC' and throws AssertionException
c)prints 'ABC' and exits without any error
d)compilation error
e)none of the above

note: when i inserted a return statement in default block like return "false",then code gives the output 'C'.but as it is it gives the compilation error
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You will get compilation fail error because you are not returning anything from default block...

If you return any kinda string from default then, output will be
A
B
C
[And whatever string you are returning from default block...]
 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any Method which does not have return type "void" should have return statement of the valid type.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15359
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m prabhu, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
m prabhu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
m prabhu, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.

jesper,its from whizlab mock exam
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic