logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Java.lang Class See What Others Are Saying!
  • Question
  • What will be the output of the program?
    public class ExamQuestion6 
    {
        static int x; 
        boolean catch()
        {
            x++; 
            return true; 
        } 
        public static void main(String[] args)
        {
            x=0; 
            if ((catch() | catch()) || catch()) 
                x++; 
            System.out.println(x); 
        } 
    }
    


  • Options
  • A. 1
  • B. 2
  • C. 3
  • D. Compilation Fails

  • Correct Answer
  • Compilation Fails 

    Explanation
    Initially this looks like a question about the logical and logical shortcut operators "|" and "||" but on closer inspection it should be noticed that the name of the boolean method in this code is "catch". "catch" is a reserved keyword in the Java language and cannot be used as a method name. Hence Compilation will fail.

    More questions

    • 1. What will be the output of the program (in jdk1.6 or above)?
      public class BoolTest 
      {
          public static void main(String [] args) 
          {
              Boolean b1 = new Boolean("false");
              boolean b2;
              b2 = b1.booleanValue();
              if (!b2) 
              {
                  b2 = true;
                  System.out.print("x ");
              }
              if (b1 & b2) /* Line 13 */
              {
                  System.out.print("y ");
              }
              System.out.println("z");
          }
      }
      

    • Options
    • A. z
    • B. x z