Home / Java Programming / Assertions :: Finding the output

Java Programming :: Assertions

  1. What will be the output of the program?

       public class Test
       {     
          public static void main(String[] args)    
         {       
          
           int x = 0;    
           assert (x > 0) ? "assertion failed" : "assertion passed" ;             
           System.out.println("finished");   
         }
      } 
    
    

  2. A.

    finished

    B.

    Compiliation fails.

    C.

    An AssertionError is thrown and finished is output.

    D.

    An AssertionError is thrown with the message "assertion failed."

    View Answer

    Workspace

    Discuss Discuss in Forum


  3. What causes compilation to fail?

    public class Test 

    {

      public void foo()

      {  

         assert false; /* Line 5 */             assert false; /* Line 6 */

      }

      public void bar()

      {  

          while(true)  

          {  

                assert false; /* Line 12 */          }

          assert false; /* Line 14 */

         

       }     

  4. A.

    Line 5

    B.

    Line 6

    C.

    Line 12

    D.

    Line 14

    View Answer

    Workspace

    Discuss Discuss in Forum


  5.  

      public class Test
      {    
         public void foo()    
         {        
              assert false; /* Line 5 */       
              assert false; /* Line 6 */  
         }
          public void bar()    
         {
              while(true)      
              {           
                assert false; /* Line 12 */    
              }         
                assert false;  /* Line 14 */ 
        }  
    } 

    What causes compilation to fail?

     

  6. A.

    Line 5

    B.

    Line 6

    C.

    Line 12

    D.

    Line 14

    View Answer

    Workspace

    Discuss Discuss in Forum


  7. What will be the output of the program?

     public class Test 
     {    
        public static int y;   
        public static void foo(int x)    
        {     
            System.out.print("foo ");     
            y = x;    
        }    
        public static int bar(int z)   
        {        
           System.out.print("bar ");        
           return y = z;     
        }
        public static void main(String [] args )    
        {   
           int t = 0;   
           assert t > 0 : bar(7);     
           assert t > 1 : foo(8); /* Line 18 */   
           System.out.println("done ");     
       } 
    } 
    

  8. A.

    bar

    B.

    bar done

    C.

    foo done

    D.

    Compilation fails

    View Answer

    Workspace

    Discuss Discuss in Forum


  9.  public class Test2

     {

       public static int x;   

       public static int foo(int y)

       {

        return y * 2;

       }

       public static void main(String [] args)

       {

        assert z > 0; /* Line 11 */

        assert z > 2: foo(z); /* Line 12 */

        if ( z 7 )

            assert z > 4; /* Line 14 */

          switch (z)

          {

           case 4: System.out.println("4 ");     

           case 5: System.out.println("5 ");   

           default: assert z 10;

       }

       if ( z 10 )

            assert z > 4: z++; /* Line 22 */   

            System.out.println(z);

       }

    }

    which line is an example of an inappropriate use of assertions?

  10. A.

    Line 11

    B.

    Line 12

    C.

    Line 14

    D.

    Line 22

    View Answer

    Workspace

    Discuss Discuss in Forum