What will be the output of the following Java program?

class selection_statements 
   {
       public static void main(String args[])
       {
           int var1 = 5; 
           int var2 = 6;
           if ((var2 = 1) == var1)
               System.out.print(var2);
           else 
               System.out.print(++var2);
       } 
   }

a) 1
b) 2
c) 3
d) 4

1 thought on “What will be the output of the following Java program?”

  1. b
    Explanation: var2 is initialised to 1. The conditional statement returns false and the else part gets executed.
    output:
    $ javac selection_statements.java
    $ java selection_statements
    2

Leave a Comment