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 PassA 
    {
        public static void main(String [] args) 
        {
            PassA p = new PassA();
            p.start();
        }
    
        void start() 
        {
            long [] a1 = {3,4,5};
            long [] a2 = fix(a1);
            System.out.print(a1[0] + a1[1] + a1[2] + " ");
            System.out.println(a2[0] + a2[1] + a2[2]);
        }
    
        long [] fix(long [] a3) 
        {
            a3[1] = 7;
            return a3;
        }
    }
    


  • Options
  • A. 12 15
  • B. 15 15
  • C. 3 4 5 3 7 5
  • D. 3 7 5 3 7 5

  • Correct Answer
  • 15 15 

    Explanation
    Output: 15 15

    The reference variables a1 and a3 refer to the same long array object. When the [1] element is updated in the fix() method, it is updating the array referred to by a1. The reference variable a2 refers to the same array object.

    So Output: 3+7+5+" "3+7+5

    Output: 15 15 Because Numeric values will be added


    More questions

    • 1. What will be the output of the program?
      for(int i = 0; i < 3; i++) 
      { 
          switch(i) 
          { 
              case 0: break; 
              case 1: System.out.print("one "); 
              case 2: System.out.print("two "); 
              case 3: System.out.print("three "); 
          } 
      } 
      System.out.println("done");
      

    • Options
    • A. done
    • B. one two done
    • C. one two three done
    • D. one two three two three done
    • Discuss
    • 2. Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?

    • Options
    • A. java.lang.String
    • B. java.lang.Double
    • C. java.lang.StringBuffer
    • D. java.lang.Character
    • Discuss
    • 3. What will be the output of the program?
      public class Delta 
      { 
          static boolean foo(char c) 
          {
              System.out.print(c); 
              return true; 
          } 
          public static void main( String[] argv ) 
          {
              int i = 0; 
              for (foo('A'); foo('B') && (i < 2); foo('C')) 
              {
                  i++; 
                  foo('D'); 
              } 
          } 
      }
      

    • Options
    • A. ABDCBDCB
    • B. ABCDABCD
    • C. Compilation fails.
    • D. An exception is thrown at runtime.
    • Discuss
    • 4. What will be the output of the program?
      public class Test 
      {
          public static void main (String [] args) 
          {
              final Foo f = new Foo();
              Thread t = new Thread(new Runnable() 
              {
                  public void run() 
                  {
                      f.doStuff();
                  }
              });
              Thread g = new Thread() 
              {
                  public void run() 
                  {
                      f.doStuff();
                  }
              };
              t.start();
              g.start();
          }
      }
      class Foo 
      {
          int x = 5;
          public void doStuff() 
          {
              if (x < 10) 
              {
                  // nothing to do
                  try 
                  {
                      wait();
                      } catch(InterruptedException ex) { }
              } 
              else 
              {
                  System.out.println("x is " + x++);
                  if (x >= 10) 
                  {
                      notify();
                  }
              }
          }
      }
      

    • Options
    • A. The code will not compile because of an error on notify(); of class Foo.
    • B. The code will not compile because of some other error in class Test.
    • C. An exception occurs at runtime.
    • D. It prints "x is 5 x is 6".
    • Discuss
    • 5. What will be the output of the program?
      public class NFE 
      {
          public static void main(String [] args) 
          {
          String s = "42";
              try 
              {
                  s = s.concat(".5");  /* Line 8 */
                  double d = Double.parseDouble(s);
                  s = Double.toString(d);
                  int x = (int) Math.ceil(Double.valueOf(s).doubleValue());
                  System.out.println(x);
              }
              catch (NumberFormatException e) 
              {
                  System.out.println("bad number");
              }
          }
      }
      

    • Options
    • A. 42
    • B. 42.5
    • C. 43
    • D. bad number
    • Discuss
    • 6. Which constructs an anonymous inner class instance?

    • Options
    • A. Runnable r = new Runnable() { };
    • B. Runnable r = new Runnable(public void run() { });
    • C. Runnable r = new Runnable { public void run(){}};
    • D. System.out.println(new Runnable() {public void run() { }});
    • Discuss
    • 7. What is the name of the method used to start a thread execution?

    • Options
    • A. init();
    • B. start();
    • C. run();
    • D. resume();
    • Discuss
    • 8. What will be the output of the program?
      public class CommandArgsTwo 
      {
          public static void main(String [] argh) 
          {
              int x;
              x = argh.length;
              for (int y = 1; y <= x; y++) 
              {
                  System.out.print(" " + argh[y]);
              }
          }
      }
      
      and the command-line invocation is

      > java CommandArgsTwo 1 2 3


    • Options
    • A. 0 1 2
    • B. 1 2 3
    • C. 0 0 0
    • D. An exception is thrown at runtime
    • Discuss
    • 9. What will be the output of the program?
      int i = l, j = -1; 
      switch (i) 
      {
          case 0, 1: j = 1; /* Line 4 */
          case 2: j = 2; 
          default: j = 0; 
      } 
      System.out.println("j = " + j); 
      

    • Options
    • A. j = -1
    • B. j = 0
    • C. j = 1
    • D. Compilation fails.
    • Discuss
    • 10. What will be the output of the program?
      public class BoolTest 
      {
          public static void main(String [] args) 
          {
              int result = 0;
      
              Boolean b1 = new Boolean("TRUE");
              Boolean b2 = new Boolean("true");
              Boolean b3 = new Boolean("tRuE");
              Boolean b4 = new Boolean("false");
      
              if (b1 == b2)  /* Line 10 */
                  result = 1;
              if (b1.equals(b2) ) /* Line 12 */
                  result = result + 10;
              if (b2 == b4)  /* Line 14 */
                  result = result + 100;
              if (b2.equals(b4) ) /* Line 16 */
                  result = result + 1000;
              if (b2.equals(b3) ) /* Line 18 */
                  result = result + 10000;
      
              System.out.println("result = " + result);
          }
      }
      

    • Options
    • A. 0
    • B. 1
    • C. 10
    • D. 10010
    • Discuss


    Comments

    There are no comments.

Enter a new Comment