wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion querry Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion querry" Watch "Assertion querry" New topic
Author

Assertion querry

Yati Tan
Ranch Hand

Joined: May 28, 2005
Posts: 56
Here is a question :

class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0"); break;
case 1: System.out.print("1"); break;
default:
assert j == 2;
System.out.print(j);
}}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}

Which statements are true?

a. With assertions enabled it prints 210210-1 followed by an AssertionError message.
b. With assertions enabled it prints 210210 followed by an AssertionError message.
c. With assertions enabled it prints only 210210.
d. With assertions enabled it prints nothing.
e. With assertions disabled it prints 210210-1
f. With assertions disabled it prints only 210210
g. Assertions should not be used within the default case of a switch statement.

The answer to this is band e.

Iam unable to understand b, can anybody help me !!


Santana Iyer
Ranch Hand

Joined: Jun 13, 2005
Posts: 335
class A {
void m1(int i) {
int j = i % 3;
switch (j) {
case 0: System.out.print("0"); break;
case 1: System.out.print("1"); break;
default:
assert j == 2;
System.out.print(j);
}}
public static void main (String[] args) {
A a = new A();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}

when we pass 5 4 3 2 1 0 we get output 210210 now
When we pass -1 see to m1 at that time
j = -1 % 3 means j = -1
not we reach default where
assert -1 == 2; // assert false;

so assertion error is thrown
and so System.out.println(j) wont be executed.

hope this helps
Yati Tan
Ranch Hand

Joined: May 28, 2005
Posts: 56
Tx alot Santana !!

Yati
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion querry