Home / Java Programming / Operators and Assignments :: Discussion

Discussion :: Operators and Assignments

  1. What will be the output of the program?

    class Equals 
    {    
       public static void main(String [] args)     
       {
           int x = 100;      
           double y = 100.1;       
           boolean b = (x = y); /* Line 7 */                
           System.out.println(b);   
       } 
    }  
    

  2. A.

    true

    B.

    false

    C.

    Compilation fails

    D.

    An exception is thrown at runtime

    View Answer

    Workspace

    Answer : Option C

    Explanation :

    The code will not compile because in line 7, the line will work only if we use (x==y) in the line. The == operator compares values to produce a boolean, whereas the = operator assigns a value to variables.

    Option A, B, and D are incorrect because the code does not get as far as compiling. If we corrected this code, the output would be false.

     


Be The First To Comment