• 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

dan exam doubt 13

 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic