logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Operators and Assignments See What Others Are Saying!
  • Question
  • What will be the output of the program?
    class Two 
    {
        byte x;
    }
    
    class PassO 
    {
        public static void main(String [] args) 
        {
            PassO p = new PassO();
            p.start();
        }
    
        void start() 
        {
            Two t = new Two();
            System.out.print(t.x + " ");
            Two t2 = fix(t);
            System.out.println(t.x + " " + t2.x);
        }
    
        Two fix(Two tt) 
        {
            tt.x = 42;
            return tt;
        }
    }
    


  • Options
  • A. null null 42
  • B. 0 0 42
  • C. 0 42 42
  • D. 0 0 0

  • Correct Answer
  • 0 42 42 

    Explanation
    In the fix() method, the reference variable tt refers to the same object (class Two) as the t reference variable. Updating tt.x in the fix() method updates t.x (they are one in the same object). Remember also that the instance variable x in the Two class is initialized to 0.

    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