logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Declarations and Access Control Comments

  • Question
  • What will be the output of the program?
    public class A
    { 
        void A() /* Line 3 */
        {
            System.out.println("Class A"); 
        } 
        public static void main(String[] args) 
        { 
            new A(); 
        } 
    }
    


  • Options
  • A. Class A
  • B. Compilation fails.
  • C. An exception is thrown at line 3.
  • D. The code executes with no output.

  • Correct Answer
  • The code executes with no output. 

    Explanation
    Option D is correct. The specification at line 3 is for a method and not a constructor and this method is never called therefore there is no output. The constructor that is called is the default constructor.

    Declarations and Access Control problems


    Search Results


    • 1. What will be the output of the program?
      interface Count 
      {
          short counter = 0;
          void countUp();
      }
      public class TestCount implements Count 
      {
          public static void main(String [] args) 
          {
              TestCount t = new TestCount();
              t.countUp();
          }
          public void countUp() 
          {
              for (int x = 6; x>counter; x--, ++counter) /* Line 14 */
              {
                  System.out.print(" " + counter);
              }
          }
      }
      

    • Options
    • A. 0 1 2
    • B. 1 2 3
    • C. 0 1 2 3
    • D. 1 2 3 4
    • E. Compilation fails
    • Discuss
    • 2. 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