aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assertions doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assertions doubt" Watch "assertions doubt" New topic
Author

assertions doubt

veda vyas sista
Greenhorn

Joined: Sep 21, 2007
Posts: 10
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.

could anyone please tell me how answer is A & E.
ilkin esrefli
Greenhorn

Joined: Nov 01, 2007
Posts: 25

Hi
It is not right, answer is not A&E. Correct answer is B&E.
Because when i=-1, j=-1%3=-1 hten default case run and assert j==2 throws AssertionError and program couldn't print j as -1.
So answer:
b. With assertions enabled it prints 210210 followed by an AssertionError message.
e. With assertions disabled it prints 210210-1
thanks..


SCJP 5
 
 
subject: assertions doubt
 
Similar Threads
Assertion
Assertion question
Assertion
Assertion querry
assertions