Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Whan happens when you compile and run the following code?

  1. public class Q5 {
  2.  public static void main(String args[]){
  3.   int i = 10;
  4.   int j = 12;
  5.   i+=++j;
  6.   System.out.println(i);
  7.  }
  8. }
a. Program will print 13
b. Program will print 23
c. Program will print 22
d. Program will print 12

  Answer:

b is the correct choice. j is incremented before the assignment. Remember that ++j is pre-increment. So, value of j is incremented first and then assigned. j++ would have been assigned first and then incremented. Don't forget that it's i+= so 13 would be added to 10 (value of i) to make 23.
Your rating: None Average: 5 (2 votes)
Share this
Anonymous's picture

asdasd

asdasd

Post new comment

5 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.