• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Following is a question

 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic