Home / Java Programming / Operators and Assignments :: Discussion

Discussion :: Operators and Assignments

  1. What will be the output of the program?

    class SC2 
    {    
       public static void main(String [] args)     
       {          
           SC2 s = new SC2();   
           s.start();  
       }    
    
       void start()    
       {        
          int a = 3;     
          int b = 4;            
          System.out.print(" " + 7 + 2 + " ");           
          System.out.print(a + b);            
          System.out.print(" " + a + b + " ");         
          System.out.print(foo() + a + b + ");     
          System.out.println(a + b + foo());       
    
       }     
    
        String foo()     
        {       
           return "foo";  
        } 
     } 
    

  2. A.

    9 7 7 foo 7 7foo

    B.

    72 34 34 foo34 34foo

    C.

    9 7 7 foo34 34foo

    D.

    72 7 34 foo34 7foo

    View Answer

    Workspace

    Answer : Option D

    Explanation :

    Because all of these expressions use the + operator, there is no precedence to worry about and all of the expressions will be evaluated from left to right. If either operand being evaluated is a String, the + operator will concatenate the two operands; if both operands are numeric, the + operator will add the two operands.


Be The First To Comment