What will be the result of a following code execution?
 
public static void Main(string[] args) 
{ 
      int k = 1; 
      Console.WriteLine(k++ + ++k);
}
 
Explanation
(k++ + ++k):
1) k++ is equal to 1. But this expression increments value of k, so k is already equal to 2. Thus we have (1 + ++k), where k = 2.
2) ++k returns value of k (which is 2) incremented by 1, so it returns 3 3) Final result is (1 + (2 + 1)) = 4

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes