• 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

Assertions

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,I got it from http://www.danchisholm.net/july21/topic/section2/assertions1.html

class D
{
private boolean b1, b2;
public void setB1(boolean b)
{
b1 = b;
}
public void setB2(boolean b)
{
b2 = b;
}
public void m1 ()
{
if (!b2 & !b1)
{
System.out.print("A");
}
else if (!b2 & b1)
{
System.out.print("B");
}

else if (b2 & !b1)
{
System.out.print("C");
}
else
{
assert false;
}
}
public static void main (String[] args) {
D d = new D();
d.setB1(true); d.setB2(true);
d.m1();
}}

Which statements are true?

a. With assertions enabled it prints an AssertionError message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an AssertionError message.
d. With assertions disabled it prints nothing.
e. An assertion should not be placed at any location that the programmer believes will never be reached under normal operating conditions.
f. The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program

Answers: a,d,f.
I couldn't understand this ,can anyone explain the logic and how did assert reach here?

Thanks in advance

Preparing Scjp 1.5
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its simple Preetha...

You have set b1 and b2 to true by calls to setB1 and setB2.

And in your if else conditions, you finally hit the final else part (because for & to pass, both the operands should evaluate to true).

In the final else part, you say "assert false", which means, "throw assertion error here if the condition is false". Since, you have explicitly mentioned false, it WILL throw an assertion error, but only if assertions are enabled by -ea or -enableassertions while running java command...

If assertions are not enabled, your "assert false" statement does not mean anything..ie. it does not come into picture during runtime.

So, as per the above explanation, options a and d are correct.

Assertions are placed in such control blocks like if-else to ensure that the program flow does not reach some unintended code...in the eg you have given, we are ensuring that both b1 and b2 variables are true. If anyone is false, assertion error is thrown. As per this, option f is correct.

Hope I am clear.

A small suggestion: Please use the "CODE" marker to properly indent your code...You know, its difficult to read the code if it is not indented, that too, in such code where there are many if-else's
[ November 17, 2008: Message edited by: Rekha Srinath ]
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic