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 Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion" Watch "Assertion" New topic
Author

Assertion

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
class C {
int a, b, c;
public void setA(int i) {a = i; assert validateC() : c;}
public void setB(int i) {b = i; assert validateC() : c;}
private boolean validateC() {
return c > a + 2 * b;
}
public int m1(int i) {
c = a + b + i;
assert validateC() : c;
return c;
}
public C(int i) {
c = i; assert validateC() : c;
}
public static void main(String[] args) {
C c = new C(251); c.setA(50); c.setB(100);
}}

Which statements are true?
a. If assertions are not enabled at run time it prints an error message.
b. If assertions are not enabled at run time it prints nothing.
c. With assertions enabled it prints an error message.
d. With assertions enabled it prints nothing.
e. The assert statement is being used to check a class invariant--something that must be true about each instance of the class.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.

Here the answer is E.

I am not understanding what does the statement at E mean ?

Also I want an example for the statement of F
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Please Use Code tags, that would be helpful for others as well as you also.
Requesting to modify the above post.

Thanks


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Preconditions are the checks which are made before the method executes further.
For eg:


Here in the above example at 1 we are confirming that the value of i should always be greater than 0, otherwise the method should not proceed.
so the statement f) The assert statements are being used to check a precondition--something that must be true when the method is invoked.
says that whenever the method is invoked the arguments would be checked for correctness first.

Hope this helps.
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579


when an instance of class C is created ,What happens?--a,b,c are initialised .Right?They make the state of that particular instance.Now in above validate methd ,a,b,c are being checked to ensure that they follow an specific condition i.e. instance follows some constraint via assert statement that is something that must be true about each instance of the class.
********************************
2nd problem answer:
public void asd(int speed)
{
assert speed>0;
}
In above ,The assert statements are being used to check a precondition--something that must be true when the method is invoked.


<i>--Agrah Upadhyay--</i><br />Final Year B.Tech SCJP,SCWCD,SCBCD <br /> <br /><b>Now since the real test for any choice is having to make the same choice again,knowing full well what it might cost.</b>-Oracle
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340

A class invariant is a constraint on a class�s state that must be met before and after execution of any non-private method of a class. (Private methods might be used to restore the required state after execution of a non-private method.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion
 
Similar Threads
Contradiction between two Qs - Help!!!
dan exam doubt 13
Reg Assertions
questions about assert
Dan's questions on assertions: inconsistent?