Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock Exam - Exception handling

 
Patricia Samuel
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 300
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops!!!

I am clear with this.

Thanks for all the help.

It is clear to me now.

Thanks
 
MuraliH Krishna
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Murali,

Guess, you din't enable assertions while running

Regards,
Vishwa
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic