Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pl explain

 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Q29 {
public static void main(String[] args) {
int j = 0;
Q29 test = new Q29();
try {
for (j=1 ; j < 5 ; j++)
{
j+= j;
}
int i = test.process() / (j = 5);
}
catch (Exception e) {
System.out.println(e);
System.out.println("Value of j = " + j);
}
}

int process() throws Exception {
throw new Exception("Exception Encountered");
}
}
What is the Output?
1.Prints Value of j = 2
2.Throws Arithmetic Exception.
3.Prints java.lang.Exception : Exception Encountered and Value of j = 7.
4.Prints Value of j = 10
5. Prints java.lang.Exception : Exception Encountered and Value of j = 2.
answer is 2:
can anyone explain.Thanks
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu,
The answer should be 3.
For an explanation, please see example in JLS 15.7.1.
 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry the answer is 3.thanks nain
 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i still have a doubt of how value of j=7...when the loop completes the value of j is 11.Pl correct me is iam wrong
 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i still have a doubt of how value of j=7...when the loop completes the value of j is 11.Pl correct me is iam wrong
 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu,
Will you tell me why you think j should be 11?
Then, we can discuss it from there. Ok?
 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the value of j at the end of the for loop is 11...i thought so
 
Anu Kris
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam sorry.....ignore the previous msg.....value of i=7,i got it.Thanks for the help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic