File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Exam - Exception handling  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Exam - Exception handling  " Watch "Mock Exam - Exception handling  " New topic
Author

Mock Exam - Exception handling

Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Hi All,

I am not able to understand what will be the output of following program
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);}
}}

Answer :
b. With assertions enabled it prints 210210 followed by an AssertionError message.

where as i think answer should be a. With assertions enabled it prints 210210-1 followed by an AssertionError message.

Please make me clear why -1 would not be there in output.
Regards
Patricia
Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
Wenever you perform "%" operator with the negative number the output is always negative.
In above case when you perform -1%3, it evaluates to -1 and as there is no case matching, it goes to default and assert j==2 throws an Assertion error as j is equal to -1.
so the output 210210 followed by an Assertion error is correct.
Hope this helps.
Anil Pradhan
Greenhorn

Joined: Jan 03, 2008
Posts: 15
Hi Samuel,

Whenever an AssertionError is thrown, JVM will shut down by dumping the stack trace to the console. When the j value is reached to -1, the condition in the assert statement will be false since -1!=3. So the print statement System.out.print(j); will never be executed to print "-1" as the AssertionError was thrown.

Hope this helps.
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
Anil,

if it was the case -1!=3 , assertion error thrown, but this was the case with 1!=3 and 0!=3 and we get 210.... in the output. why so???

it might be i am stucking on the little thing, but this is not enough clear to me.

Thanks
Patricia Samuel
Ranch Hand

Joined: Sep 12, 2007
Posts: 300
oops!!!

I am clear with this.

Thanks for all the help.

It is clear to me now.

Thanks
MuraliH Krishna
Greenhorn

Joined: Jan 19, 2008
Posts: 9
hi patricia,
can you please explain what you have understood. after running the program the output i am getting is 210210-1. the code i compiled is

public class AssertTest2 {
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) {
AssertTest2 a = new AssertTest2();
for (int i=5; i >= -1; i--) {a.m1(i);}
}}

Thanks.
Murali
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi Murali,

Guess, you din't enable assertions while running

Regards,
Vishwa


Blog
Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 62
Hi,

With reference to the above snippet of code.

with the assertions enabled at runtime using the command"java -ea A", the Assertion error is thrown.

And the if the program is run using the command "java A", then the o/p is 210210-1

Note: here single expression assertion is used.


Thanks,
Deepak
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Anil Pradhan:
..When the j value is reached to -1, the condition in the assert statement will be false since -1!=3.


I think the lines in bold caused the confusion. It should have been -1!=2 as that is what it is defined to assert!

since the value -1 is not matching with 2, an assertion error is thrown and thereby the next statement (SOP) is not executed.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock Exam - Exception handling
 
Similar Threads
assertions
Assertion
assertions doubt
Assertion
Assertion querry