• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

assertions

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
I have two questions on assertions. These are in Khalid & Rolf Book.

Q1: Assuming assertions are enabled, which of these assertion statements will throw an error?

a. assert true:true;
b. assert true:false;
c. assert false:true;
d. assert false:false;

Can you explain this?

Q2: Given the following command, which classes would have assertions enabled?
java -ea -da:com... net.example.LaunchTranslator

a: com.example.Translator
b: java.lang.String
c: dot.com.Boom
d: net.example.LaunchTranslator
e: java.lang.AssertionError

I know that SystemClasses are disabled. so options 'b' and 'e' are disabled. option 'd' is exactly what is given in the statement so it is enabled.
Given that classes in the com package and its subpackages are disabled so options 'a' and 'c' are also disabled. Is it correct? But given option 'c' is enabled. How come option 'c' is enabled?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the first Q:


Q1: Assuming assertions are enabled, which of these assertion statements will throw an error?

a. assert true:true;
b. assert true:false;
c. assert false:true;
d. assert false:false;



I think it is (c) and/or (d) (is it select one or two answers?) because assert false will cause the assertion error.


Q2: Given the following command, which classes would have assertions enabled?
java -ea -da:com... net.example.LaunchTranslator

a: com.example.Translator
b: java.lang.String
c: dot.com.Boom
d: net.example.LaunchTranslator
e: java.lang.AssertionError



As per my understanding: a,c,b,e: disabled, d: enabled

However, i wonder if b and e are also enabled, since it looks as if d is too obvious an answer!
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi shah,
In Q1: asked to select two. But read that "assert:false" is legal.
So options 'c' and 'd' are legal but not correct?

in Q2: answers are 'c' and 'd'

Thanks
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gayatri,
i dont know about assert. can you please explain me.
sri.
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sri,
I will explain about the assertions briefly here. If you need more about this you can refer to any java books or websites.

An assertion is a statement in java containing a boolean expression that is assumed to be true when the statement is executed. The system reports an "AssertionError" if the expression evaluates to false. It is used for debugging purposes:

assert(a > 0); // throws an AssertionError if a <= 0

Assertions can be in two forms:


assert Expression1 ;
assert Expression1 : Expression2 ;

Expression1 should always result in a boolean value.

Expression2 can be anything that results in a value. The value is used to generate a String message that displays more debugging information.
For instance:

class AssertionTest
{
int a ;
public void func()
{
assert (a > 0): "a is negative" ;
// Remaining code
}
}

In the above code, the assertion expression is true if a is positive. If a is negative or zero, an AssertionError is thrown with the message "a is negative."

Assertions are disabled by default. To compile with assertions enabled, you need to use the source 1.4 flag:


javac -source 1.4 AssertionTest.java

To enable assertions at runtime, use the -enableassertions or -ea flags.

For selective disabling of assertions at runtime, use the -da or -disableassertions flags.

For enabling assertions in the system classes, use the -esa or -dsa flags. Assertions can also be enabled or disabled on a package basis.

java -ea AssertionTest

Appropriate and inappropriate use of assertions:
You can place an assertion at any location that you don't expect to be reached normally. Assertions can be used to validate the parameters passed to a private method. However, assertions should not be used to validate parameters passed to public methods because a public method must check its arguments regardless of whether assertions are enabled or not. However, you can test postconditions with assertions in both public and non-public methods. Also, assertions should not change the state of a program in any manner.

Hope you understand and let me know if you have any questions.
Good luck
 
kiennjal shah
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not understand as to how dot.com.boom has assertions enabled since com package has assertions disabled. so com and all subpackages would not recognize assert as a keyword.

then, howcome (c) is one of the correct answers for Q2?

thanks

-Kiennjal
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
That was my doubt too? How come option c is correct. According to my knowledge only 'd' is enabled so it is the only one answer I guess but given 'c' and 'd' are correct.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q2: Given the following command, which classes would have assertions enabled?
java -ea -da:com... net.example.LaunchTranslator

a: com.example.Translator
b: java.lang.String
c: dot.com.Boom
d: net.example.LaunchTranslator
e: java.lang.AssertionError

Here the com and its subpackages are disabled.. so the option c is enabled since "dot" is not a subpackage of com.. bcoz we are invoking like dot.com and not com.dot..
Also the option d is disabled as it is given in the disable assertion part..how come the option d is enabled???
 
gayathri mukkavilli
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The statement

java -ea -da:com... net.example.LaunchTranslator

means, run net.example.LaunchTranslator with enable assertions for all classes and packages except com and its subpackages. so option 'd' is enabled since it is not come under com and its subpackages.
-da belongs to only com... not belongs to net.example.LaunchTranslator
 
Evildoers! Eat my justice! And this tiny ad's justice too!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic