Question
In Java, what will be the output of the following code
snippet? public class Test { Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â int x = 5; Â Â Â Â Â Â Â x = x++ + ++x; Â Â Â Â Â Â Â System.out.println(x); Â Â Â }}Solution
The output of the program is 13 . This result can be analyzed by understanding how the post-increment (x++) and pre-increment (++x) operators work in Java.
- Initial value of x is 5 .
- In the expression x = x++ + ++x:
- Post-increment (x++) : The value of x (5) is used first, and then it is incremented to 6.
- Pre-increment (++x) : The value of x is incremented first (to 7), and then this incremented value is used in the expression.
- The expression becomes: x = 5 + 7, resulting in x = 13.
A train started from station P and preceded towards station Q at a speed of 90 km/h. 54 minutes later, another train started from station Q and preceded...
Length of a train, whose speed is 60 km/h, is 300 metres. Find the time taken (in seconds) by train to cross a bridge of length 600 metres.
- Train 'X' departed from Chennai to Hyderabad at 7:00 a.m. Train 'Y' left Hyderabad for Chennai at 9:00 a.m., traveling at 65 km/h. They crossed each other ...
A 250-meter-long train crosses a 150-meter-long train coming from the opposite direction in 20 seconds. If the speed of the first train is 45 km/h, then...
Train 'A' crosses a pole in 35 seconds and takes 40 seconds to cross a platform that is 120 meters long. Train 'B' is half the length of Train 'A' and t...
The ratio of the length of two trains P and Q is 5:6 and the ratio of the time taken by both trains to cross a man standing on a platform is 2:3. If the...
Two trains A and B, were proceeding in the same direction on parallel tracks at 23 km/hr and 77 km/hr respectively. A man noticed that it took exactly 1...
Speeds of train 'A' and train 'B' are in the ratio 4:5, respectively. Train 'B' can completely overtake train 'A' in 180 seconds. If lengths of train 'A...
Train A running at a speed of 48 km/hr crosses train B in 20 seconds. Find the speed of train B (in km/hr), if the sum of the length of train A and B is...
A train traveling at 43.2 km/h takes 'm' seconds to pass a pole. What is the length of the train in meters?