• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dan exam doubt 12

 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

in one question


class A {
private void m1 (int i) {
assert i < 10 : i; System.out.print(i);
}
public void m2 (int i) {
assert i < 10 : i; System.out.print(i);
}
public static void main (String[] args) {
A a = new A(); a.m1(11); a.m2(12);
}}

Which statements are true?

a. If assertions are enabled at run time it prints an error message.
b. With assertions enabled it prints nothing.
c. With assertions disabled it prints an error message.
d. With assertions disabled it prints 1112.
e. With assertions disabled it prints nothing.
f. The assert statements are being used to check a precondition--something that must be true when the method is invoked.
g. Method m1 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a non-public method.
h. Method m2 is an example of an improper use of an assert statement: an assert statement should not be used for argument checking in a public method.



---------
the answer were a,d,f,h

in this...y With assertions disabled it prints 1112.
and y not With assertions disabled it prints an error message i.e "c" option ?
but when if it is dissable...how complier treat the word assert in code ?
if it is not declared as identifier.. in above too..its not identifier....w.r.t. diablity of assertion

pls explain

thanx
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a. If assertions are enabled at run time it prints an error message.

I think the key here is that the question is asking about assertions being enabled / disabled at runtime. Perhaps your are thinking of compile-time enabling without the -source 1.4.

If assertions aren't enable at compile time then you'd get a boatload of errors.
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good point carol

but are u sure that they are asking about runtime enable/disable of assertion ??


anyway,, Thanx a ton


Amit
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am, but obviously you are not (sure that they are asking about runtime enable/disable of assertion).

1. See Compiling with assertions vs. Enabling and Disabling Assertions.

2. The answers given in your original post match the behavior for runtime enabling/disabling (did you try to compile and run these?) .

3. Try a google search and see what you get when you search for enabling assertions. e.g.

From Enabling Assertions from the command-line:

e104. Enabling Assertions from the Command Line
The command line options -ea and -da allow you to enable and disable assertion in a package subtree or in a class. Here are several examples of the switches. Enable assertions in all non-system classes:
java -ea MyApp


Enable assertions in all classes in the unnamed package:
java -ea:... MyApp


Enable assertions in all classes in a package subtree:
java -ea:com.mycompany... MyApp


Enable assertions in a particular class:
java -ea:com.mycompany.MyCompany MyApp


Disable assertions in all classes in a package subtree:
java -da:com.mycompany... MyApp


Disable assertions in a particular class:
java -da:com.mycompany.MyClass MyApp


Multiple assertion switches on the command line are processed from left to right. Enable assertions in all classes in a package subtree except for one class:
java -ea:com.mycompany... -da:com.mycompany.MyClass MyApp


[ February 24, 2005: Message edited by: Carol Enderlin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic