• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

on assertions

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai
here bydefaultly the assertions are disabled
so it should print finished
but why this code is giveing compile time error

Given:
1. public class Test {
2. public static void main(String[] args) {
3. int x = 0;
4. assert (x > 0) ? "assertion failed" : "assertion passed";
5. System.out.println("Finished");
6. }
7. }
What is the result?
A. finished
B. Compilation fails.
C. An AssertionError is thrown and finished is output.
D. An AssertionError is thrown with the message "assertion failed".
E. An AssertionError is thrown with the message "assertion passed".
Answer: B

by
velan vel
 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
assert (x > 0) ? "assertion failed" : "assertion passed";

the two syantaxes for assert statments are

1. assert expression1;
2. assert expression1:expression2;

u r using something like (but not exactly) case 2. the expression is incorrect...and does not exqactly return boolean value

in ur case < (x>0) ? "assertion failed" ) turns out to be expression 1
and it doesnt return a boolean

hence compiler error
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic