File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Switch and Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Switch and Assertion" Watch "Switch and Assertion" New topic
Author

Switch and Assertion

m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79

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

Joined: Jan 22, 2008
Posts: 135
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

Joined: Jan 29, 2008
Posts: 79
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

Joined: Mar 07, 2008
Posts: 70


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...]


Manoj Kumar
sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Any Method which does not have return type "void" should have return statement of the valid type.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
m prabhu
Ranch Hand

Joined: Jan 29, 2008
Posts: 79
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
 
Don't get me started about those stupid light bulbs.
 
subject: Switch and Assertion