logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Flow Control See What Others Are Saying!
  • Question
  • What will be the output of the program?
    public class Switch2 
    {
        final static short x = 2;
        public static int y = 0;
        public static void main(String [] args) 
        {
            for (int z=0; z < 4; z++) 
            {
                switch (z) 
                {
                    case x: System.out.print("0 ");
                    default: System.out.print("def ");
                    case x-1: System.out.print("1 ");  
                                break;
                    case x-2: System.out.print("2 ");
                }
            }
        }
    }
    


  • Options
  • A. 0 def 1
  • B. 2 1 0 def 1
  • C. 2 1 0 def def
  • D. 2 1 0 def 1 def 1

  • Correct Answer
  • 2 1 0 def 1 def 1 

    Explanation
    When z == 0 , case x-2 is matched. When z == 1, case x-1 is matched and then the break occurs. When z == 2, case x, then default, then x-1 are all matched. When z == 3, default, then x-1 are matched. The rules for default are that it will fall through from above like any other case (for instance when z == 2), and that it will match when no other cases match (for instance when z==3).

    More questions

    • 1. Which one of the following will declare an array and initialize it with five numbers?

    • Options
    • A. Array a = new Array(5);
    • B. int [] a = {23,22,21,20,19};
    • C. int a [] = new int[5];
    • D. int [5] array;
    • Discuss
    • 2. What allows the programmer to destroy an object x?

    • Options
    • A. x.delete()
    • B. x.finalize()
    • C. Runtime.getRuntime().gc()
    • D. Only the garbage collection system can destroy an object.
    • Discuss
    • 3. What will be the output of the program?
      class A 
      { 
          public A(int x){} 
      } 
      class B extends A { } 
      public class test 
      { 
          public static void main (String args []) 
          {
              A a = new B(); 
              System.out.println("complete"); 
          } 
      }
      

    • Options
    • A. It compiles and runs printing nothing
    • B. Compiles but fails at runtime
    • C. Compile Error
    • D. Prints "complete"
    • Discuss
    • 4. What will be the output of the program?
      boolean bool = true; 
      if(bool = false) /* Line 2 */
      {
          System.out.println("a"); 
      } 
      else if(bool) /* Line 6 */
      {
          System.out.println("b"); 
      } 
      else if(!bool) /* Line 10 */
      {
          System.out.println("c"); /* Line 12 */
      } 
      else 
      {
          System.out.println("d"); 
      }
      

    • Options
    • A. a
    • B. b
    • C. c
    • D. d
    • Discuss
    • 5. What will be the output of the program?
      class s1 extends Thread
      { 
          public void run() 
          { 
              for(int i = 0; i < 3; i++) 
              { 
                  System.out.println("A"); 
                  System.out.println("B"); 
              } 
          } 
      } 
      class Test120 extends Thread 
      { 
          public void run() 
          { 
              for(int i = 0; i < 3; i++) 
              { 
                  System.out.println("C"); 
                  System.out.println("D"); 
              } 
          } 
          public static void main(String args[]) 
              { 
              s1 t1 = new s1(); 
              Test120 t2 = new Test120(); 
              t1.start(); 
              t2.start(); 
          } 
      }
      

    • Options
    • A. Compile time Error There is no start() method
    • B. Will print in this order AB CD AB...
    • C. Will print but not be able to predict the Order
    • D. Will print in this order ABCD...ABCD...
    • Discuss
    • 6. What will be the output of the program?
      class Exc0 extends Exception { } 
      class Exc1 extends Exc0 { } /* Line 2 */
      public class Test 
      {  
          public static void main(String args[]) 
          { 
              try 
              {  
                  throw new Exc1(); /* Line 9 */
              } 
              catch (Exc0 e0) /* Line 11 */
              {
                  System.out.println("Ex0 caught"); 
              } 
              catch (Exception e) 
              {
                  System.out.println("exception caught");  
              } 
          } 
      }
      

    • Options
    • A. Ex0 caught
    • B. exception caught
    • C. Compilation fails because of an error at line 2.
    • D. Compilation fails because of an error at line 9.
    • Discuss
    • 7. What will be the output of the program?
      class Test 
      {
          public static void main(String [] args) 
          {
              int x= 0;
              int y= 0;
              for (int z = 0; z < 5; z++) 
              {
                  if (( ++x > 2 ) && (++y > 2)) 
                  {
                      x++;
                  }
              }
              System.out.println(x + " " + y);
          }
      }
      

    • Options
    • A. 5 2
    • B. 5 3
    • C. 6 3
    • D. 6 4
    • Discuss
    • 8. Which of the following are true statements?

      1. The Iterator interface declares only three methods: hasNext, next and remove.
      2. The ListIterator interface extends both the List and Iterator interfaces.
      3. The ListIterator interface provides forward and backward iteration capabilities.
      4. The ListIterator interface provides the ability to modify the List during iteration.
      5. The ListIterator interface provides the ability to determine its position in the List.

    • Options
    • A. 2, 3, 4 and 5
    • B. 1, 3, 4 and 5
    • C. 3, 4 and 5
    • D. 1, 2 and 3
    • Discuss
    • 9. What will be the output of the program?
      public class Test 
      { 
          public static void main (String[] args) 
          {
              String foo = args[1]; 
              String bar = args[2]; 
              String baz = args[3]; 
              System.out.println("baz = " + baz); /* Line 8 */
          } 
      }
      
      And the command line invocation:

      > java Test red green blue


    • Options
    • A. baz =
    • B. baz = null
    • C. baz = blue
    • D. Runtime Exception
    • Discuss
    • 10. Which cannot directly cause a thread to stop executing?

    • Options
    • A. Calling the SetPriority() method on a Thread object.
    • B. Calling the wait() method on an object.
    • C. Calling notify() method on an object.
    • D. Calling read() method on an InputStream object.
    • Discuss


    Comments

    There are no comments.

Enter a new Comment