Home / Java Programming / Flow Control :: Finding the output

Java Programming :: Flow Control

  1. What will be the output of the program?

     int x = 3; 
     int y = 1;
     if (x = y) /* Line 3 */
     {     
        System.out.println("x =" + x);
     } 

     

  2. A.

    x = 1

    B.

    x = 3

    C.

    Compilation fails.

    D.

    The code runs with no output.


  3. What will be the output of the program?

     Float f = new Float("12"); 
     switch (f) 
     {    
         case 12: System.out.println("Twelve");     
         case 0: System.out.println("Zero");     
         default: System.out.println("Default");  
    } 
    

  4. A.

    Zero

    B.

    Twelve

    C.

    Default

    D.

    Compilation fails


  5. What will be the output of the program?

      int i = 0;
      while(1

      {
      if(i == 4)
      {
            break;
      }
      ++i;

    }

    System.out.println("i = " + i);

  6. A.

    i = 0

    B.

    i = 3

    C.

    i = 4

    D.

    Compilation fails.


  7. What will be the output of the program?

     public class Delta 
     {     
         static boolean foo(char c)     
         {         
            System.out.print(c);      
            return true;     
        }      
        public static void main( String[] argv )      
        {         
           int i = 0;    
           for (foo('A'); foo('B') && (i 2); foo('C'))    
           {          
              i++;         
              foo('D');     
          }     
       }  
    } 
    

  8. A.

    ABDCBDCB

    B.

    ABCDABCD

    C.

    Compilation fails.

    D.

    An exception is thrown at runtime.


  9. What will be the output of the program?

      for(int i = 0; i 3; i++)
      {     
         switch(i)    
        {          
            case 0: break;        
            case 1: System.out.print("one ");     
            case 2: System.out.print("two ");         
            case 3: System.out.print("three ");      
       }
        
    } 
     System.out.println("done"); 
    

  10. A.

    done

    B.

    one two done

    C.

    one two three done

    D.

    one two three two three done


  11. What will be the output of the program? public class Test
    {

    public static void main(String args[])
    {
        int i = 1, j = 0;

        switch(i)
       {
                     case 2: j += 6;
                     case 4: j += 1;
                     
    default: j += 2;
                     case 0: j += 4;
      }
      System.out.println("j = " + j);

      }

    }

  12. A.

    j = 0

    B.

    j = 2

    C.

    j = 4

    D.

    j = 6


  13. What will be the output of the program?

     boolean bool = true; 
     if(bool = false) /* Line 2 */
     {    
         System.out.println("a");  
     } 
     else if(bool) /* Line 6 */ 
    {
         System.out.println("b");  
    }  
    else if(!bool) /* Line 10 */ 
    {
       €‹System.out.println("c"); /* Line 12 */ 
    }  
    else  
    {    
       System.out.println("d"); 
    } 
    

  14. A.

    a

    B.

    b

    C.

    c

    D.

    d


  15. What will be the output of the program?

     public class Switch2 
     {   
         final static short x = 2;  
         public static int y = 0;   
         public static void main(String [] args)     
         {       
            for (int z=0; z 4; z++)       
            {          
                switch (z)        
                {              
                      case x: System.out.print("0 ");   
                      default: System.out.print("def ");      
                      case x-1: System.out.print("1 ");                               
                              break;        
                      case x-2: System.out.print("2 ");    
                 }     
             }    
         }
     } 
    

     

  16. A.

    0 def 1

    B.

    2 1 0 def 1

    C.

    2 1 0 def def

    D.

    2 1 0 def 1 def 1


  17. What will be the output of the program?

     int i = 0, j = 5; 
     tp: for (;;)     
        {        
              i++;         
              for (;;)       
              {           
                   if(i > --j)        
                  {               
                     break tp;            
                  }       
              }
              System.out.println("i =" + i + ", j = " + j); 
    

  18. A.

    i = 1, j = 0

    B.

    i = 1, j = 4

    C.

    i = 3, j = 4

    D.

    Compilation fails.


  19. What will be the output of the program?

    int I = 0; 
    label:  
            if (I 2) {   
            System.out.print("I is " + I);   
            I++;  
            continue label; 
    } 
    

  20. A.

    I is 0

    B.

    I is 0 I is 1

    C.

    Compilation fails.

    D.

    None of the above