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 related(mock) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assertion related(mock)" Watch "assertion related(mock)" New topic
Author

assertion related(mock)

Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432


assume assertion is on
output: assert is on

if at line 3(the expression on assert statement) i is returned as false,then why line4 get printed?
why line 2 matters here(which makes i true) regardless of line3(which returns false)?

SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Arjun please tell the source of the question.

I don't really understand the question. Line 2 makes the value of i as true, so there will be no assertion error and line 3 will not be executed. This is why line 4 is executed...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

Ankit Garg wrote:Arjun please tell the source of the question
.

i got this question from one of the page on facebook(whiz-lab).
I don't really understand the question. Line 2 makes the value of i as true, so there will be no assertion error and line 3 will not be executed. This is why line 4 is executed..

that means if line 2 is true then ,line 3 will not be exceuted
and if line 2 is false offcourse assertionerror will be thrown.
then what is the use of using line 3 for returning any value in assertion(assumption)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

hen what is the use of using line 3 for returning any value in assertion

If you know about assertions, you should know the use of line 3. Look at any assertion tutorial like this...
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
The assertion statement has two forms.

The simple form is:

which is equivalent to:

The augmented form is:

which is equivalent to:

So, you can see that in augmented form if Expression1 is true, Expression2 is never called.
That is why, statement on Line 3 (which is part of Line 2 actually) is not called - if assertions are enabled in the code given in the question.


OCPJP6
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

thanks ankit and trivikram for reply,i got your concept
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
Anytime
Good Luck for your SCJP Exam...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: assertion related(mock)
 
Similar Threads
Overloading doubt
overiding method(own code)
Can u explain this assert question
Doubt in Assert statement
mock: assertion