Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 41
What will be the result of compiling and executing Test class?
- package com.udayankhattry.oca;
- public class Test {
- public static void main(String[] args) {
- int val = 25;
- if(val++ < 26) {
- System.out.println(val++);
- }
- }
- }
- A 25
- B 26
- C Program executes successfully but nothing is printed on to the console
- D 27
Reveal correct answer
Correct answer: B
Explanation
Initially val = 25.
'if(val++ < 26)' means 'if(25 < 26)', value of val (25) is used in the boolean expression and then value of val is incremented by 1, so val = 26.
25 < 26 is true, control goes inside if-block and executes System.out.println(val++); This prints current value of val to the console, which is 26 and after that increments the value of val by 1, so val becomes 27.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
