The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Following is a question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Following is a question" Watch "Following is a question" New topic
Author

Following is a question

Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Following is a question:-


_______________________Begin snip_____________________________________
If the following code is successfully compiled and run without explicitly enabling assertions, what will happen?
class Language{
public static final int java=1;
public static final int pascal=2;
public static final int csharp=3;

}
public class Mgos {
static int lang=0;
public static void main(String argv[]){
switch(lang){
case Language.java:
System.out.println("java");
break;
case Language.pascal:
System.out.println("pascal");
break;
case Language.csharp:
System.out.println("csharp");
break;
default:
assert false : lang;
}

}

}

1) An unmatched parameter exception will be thrown
2) An assert exception will be thrown
3) The program will run with no output
4) Output of "csharp"
___________end snip_________________________________________

what should be the answer and why ?

Thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This might be a good exercise in preparing for the exam: Have you compiled and run this program -- both with and without assertions enabled? What was your result? Is this different than what you expected? If so, how?

Code with UBB tags...

[ September 26, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Andy Chang
Greenhorn

Joined: Mar 24, 2004
Posts: 5
If assertions are disabled, the program will run with no output.

However, if assertions are enabled, the assert will fail and an AssertionError will be thrown.
An acceptable alternative is:
default:
throw new AssertionError(lang);


Netty poestel
Ranch Hand

Joined: Sep 20, 2004
Posts: 131
Thanks all.
I'm fine about the assert statement.
If I take it out, there is compilation but no output.
This no output was my bother.
Do I understand that static int lang=0;
thus it's value will remain 0 throughout this code piece.

so the 'switch(lang)' is evaluating to switch(0) and since 0 is not the constants '1','2' and '3' thus none of the case gets printed out...right ?

however if I compile with -source 1.4 and the run with -ea, then the assertion error gets thrown...
well it got clear as I wrote this mail....so thanks for those feedbacks..
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Netty,

That's exactly right. (And of course, the default is for assertions to be disabled without -ea.)

It's important to compile and run a couple of these before the exam. Reading all about assertions is one thing, but there's nothing like practice to verify and reinforce your understanding.

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Following is a question