logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Declarations and Access Control Comments

  • Question
  • What will be the output of the program?
    public class ArrayTest 
    { 
        public static void main(String[ ] args)
        { 
            float f1[ ], f2[ ]; 
            f1 = new float[10]; 
            f2 = f1; 
            System.out.println("f2[0] = " + f2[0]); 
        } 
    }
    


  • Options
  • A. It prints f2[0] = 0.0
  • B. It prints f2[0] = NaN
  • C. An error at f2 = f1; causes compile to fail.
  • D. It prints the garbage value.

  • Correct Answer
  • It prints f2[0] = 0.0 

    Explanation
    Option A is correct. When you create an array (f1 = new float[10];) the elements are initialises to the default values for the primitive data type (float in this case - 0.0), so f1 will contain 10 elements each with a value of 0.0. f2 has been declared but has not been initialised, it has the ability to reference or point to an array but as yet does not point to any array. f2 = f1; copies the reference (pointer/memory address) of f1 into f2 so now f2 points at the array pointed to by f1.

    This means that the values returned by f2 are the values returned by f1. Changes to f1 are also changes to f2 because both f1 and f2 point to the same array.


    Declarations and Access Control problems


    Search Results


    • 1. What will be the output of the program?
      public class Test 
      {  
          public static void main(String args[])
          { 
              class Foo 
              {
                  public int i = 3;
              } 
              Object o = (Object)new Foo();
              Foo foo = (Foo)o;
              System.out.println("i = " + foo.i);
          }
      }
      

    • Options
    • A. i = 3
    • B. Compilation fails.
    • C. i = 5
    • D. A ClassCastException will occur.
    • Discuss
    • 2. What will be the output of the program?
      import java.util.*;
      public class NewTreeSet2 extends NewTreeSet 
      {
          public static void main(String [] args) 
          {
              NewTreeSet2 t = new NewTreeSet2();
              t.count();
          }
      }
      protected class NewTreeSet
      {
          void count() 
          {
              for (int x = 0; x < 7; x++,x++ ) 
              {
                  System.out.print(" " + x);
              }
          }
      }
      

    • Options
    • A. 0 2 4
    • B. 0 2 4 6
    • C. Compilation fails at line 2
    • D. Compilation fails at line 10
    • Discuss
    • 3. 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.
    • Discuss
    • 4. 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
    • 5. 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