This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes dan exam doubt 13 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 "dan exam doubt 13" Watch "dan exam doubt 13" New topic
Author

dan exam doubt 13

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hiii
in one question

Question 14
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.



its answer are option ...b,d,e
with explanation...
This question is an example of using assertions to check a class invariant--something that must be true about each instance of the class. Although a class invariant must be true before and after the execution of each public method, the invariant is typically only checked at the end of each method and constructor.


but i m not able to clear with the explanation at all.........

kindly explain in details


Thanks and Regards, Amit Taneja
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364

its answer are option ...b,d,e
with explanation...
This question is an example of using assertions to check a class invariant--something that must be true about each instance of the class. Although a class invariant must be true before and after the execution of each public method, the invariant is typically only checked at the end of each method and constructor.


It is checking "something that must be true about each instance of the class" -- that c > a + 2 * b

It is checking it at the end of each method and constructor.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
not cleared ??

seriously can anybody please explain me what the quetion and its explantion about in details...

or give good link to assertion which cover these topics... ?

Thanks and Regards,
Amit
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Sun's JavaTM 2 SDK, Standard Edition, version 1.4.2 Summary of New Features and Enhancements

Link from new features to Programming with Assertions

From the section on Class invariants:

Class Invariants
A class invariants is a type of internal invariant that applies to every instance of a class at all times, except when an instance is in transition from one consistent state to another. A class invariant can specify the relationships among multiple attributes, and should be true before and after any method completes. For example, suppose you implement a balanced tree data structure of some sort. A class invariant might be that the tree is balanced and properly ordered.
The assertion mechanism does not enforce any particular style for checking invariants. It is sometimes convenient, though, to combine the expressions that check required constraints into a single internal method that can be called by assertions. Continuing the balanced tree example, it might be appropriate to implement a private method that checked that the tree was indeed balanced as per the dictates of the data structure:

// Returns true if this tree is properly balanced
private boolean balanced() {
...
}

Because this method checks a constraint that should be true before and after any method completes, each public method and constructor should contain the following line immediately prior to its return:

assert balanced();
It is generally unnecessary to place similar checks at the head of each public method unless the data structure is implemented by native methods. In this case, it is possible that a memory corruption bug could corrupt a "native peer" data structure in between method invocations. A failure of the assertion at the head of such a method would indicate that such memory corruption had occurred. Similarly, it may be advisable to include class invariant checks at the heads of methods in classes whose state is modifiable by other classes. (Better yet, design classes so that their state is not directly visible to other classes!)


If that still doesn't do it for you, please be a little more explicit about what isn't clear.
[ February 24, 2005: Message edited by: Carol Enderlin ]
 
 
subject: dan exam doubt 13
 
Similar Threads
Dan's questions on assertions: inconsistent?
Reg Assertions
Contradiction between two Qs - Help!!!
questions about assert
Assertion