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 Test 
    {  
        public static void main(String args[]) 
        { 
            int i = 1, j = 0; 
            switch(i) 
            { 
                case 2: j += 6; 
                case 4: j += 1; 
                default: j += 2; 
                case 0: j += 4; 
            } 
            System.out.println("j = " + j); 
        } 
    }
    


  • Options
  • A. j = 0
  • B. j = 2
  • C. j = 4
  • D. j = 6

  • Correct Answer
  • j = 6 

    Explanation
    Because there are no break statements, the program gets to the default case and adds 2 to j, then goes to case 0 and adds 4 to the new j. The result is j = 6.

    More questions

    • 1. Which statement is true?
      class Test1 
      {
          public int value;
          public int hashCode() { return 42; }
      }
      class Test2 
      {
          public int value;
          public int hashcode() { return (int)(value^5); }
      }
      

    • Options
    • A. class Test1 will not compile.
    • B. The Test1 hashCode() method is more efficient than the Test2 hashCode() method.
    • C. The Test1 hashCode() method is less efficient than the Test2 hashCode() method.
    • D. class Test2 will not compile.
    • Discuss
    • 2. Assuming that the equals() and hashCode() methods are properly implemented, if the output is "x = 1111", which of the following statements will always be true?
      x = 0;
      if (x1.hashCode() != x2.hashCode() )  x = x + 1;
      if (x3.equals(x4) )  x = x + 10;
      if (!x5.equals(x6) ) x = x + 100;
      if (x7.hashCode() == x8.hashCode() )  x = x + 1000;
      System.out.println("x = " + x);
      

    • Options
    • A. x2.equals(x1)
    • B. x3.hashCode() == x4.hashCode()
    • C. x5.hashCode() != x6.hashCode()
    • D. x8.equals(x7)
    • Discuss
    • 3. The static method Thread.currentThread() returns a reference to the currently executing Thread object. What is the result of this code?
      class Test 
      {
          public static void main(String [] args) 
          {
              printAll(args);
          }
      
          public static void printAll(String[] lines) 
          {
              for(int i = 0; i < lines.length; i++)
              {
                  System.out.println(lines[i]);
                  Thread.currentThread().sleep(1000);
              }
          }
      }
      

    • Options
    • A. Each String in the array lines will output, with a 1-second pause.
    • B. Each String in the array lines will output, with no pause in between because this method is not executed in a Thread.
    • C. Each String in the array lines will output, and there is no guarantee there will be a pause because currentThread() may not retrieve this thread.
    • D. This code will not compile.
    • Discuss
    • 4. Which answer most closely indicates the behavior of the program?
      public class MyProgram 
      {
          public static void throwit() 
          {
              throw new RuntimeException();
          }
          public static void main(String args[])
          {
              try 
              {
                  System.out.println("Hello world ");
                  throwit();
                  System.out.println("Done with try block ");
              }
              finally 
              {
                  System.out.println("Finally executing ");
              }
          }
      }
      

    • Options
    • A. The program will not compile.
    • B. The program will print Hello world, then will print that a RuntimeException has occurred, then will print Done with try block, and then will print Finally executing.
    • C. The program will print Hello world, then will print that a RuntimeException has occurred, and then will print Finally executing.
    • D. The program will print Hello world, then will print Finally executing, then will print that a RuntimeException has occurred.
    • Discuss
    • 5. Which two code fragments will compile?
      1. interface Base2 implements Base {}
      2. abstract class Class2 extends Base
        { public boolean m1(){ return true; }}
      3. abstract class Class2 implements Base {}
      4. abstract class Class2 implements Base
        { public boolean m1(){ return (7 > 4); }}
      5. abstract class Class2 implements Base
        { protected boolean m1(){ return (5 > 7) }}
      interface Base 
      {
          boolean m1 ();
          byte m2(short s);
      }
      

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 3 and 4
    • D. 1 and 5
    • Discuss
    • 6. Which statement, if placed in a class other than MyOuter or MyInner, instantiates an instance of the nested class?
      public class MyOuter 
      {
          public static class MyInner 
          {
              public static void foo() { }
          }
      }
      

    • Options
    • A. MyOuter.MyInner m = new MyOuter.MyInner();
    • B. MyOuter.MyInner mi = new MyInner();
    • C. MyOuter m = new MyOuter();

      MyOuter.MyInner mi = m.new MyOuter.MyInner();

    • D. MyInner mi = new MyOuter.MyInner();
    • Discuss
    • 7. What will be the output of the program, if this code is executed with the command line:

      > java F0091 world

      public class F0091 
      {    
          public void main( String[] args ) 
          {  
              System.out.println( "Hello" + args[0] ); 
          } 
      }
      

    • Options
    • A. Hello
    • B. Hello Foo91
    • C. Hello world
    • D. The code does not run.
    • Discuss
    • 8. At what point is the Bar object, created on line 6, eligible for garbage collection?
      class Bar { } 
      class Test 
      {  
          Bar doBar() 
          {
              Bar b = new Bar(); /* Line 6 */
              return b; /* Line 7 */
          } 
          public static void main (String args[]) 
          { 
              Test t = new Test();  /* Line 11 */
              Bar newBar = t.doBar();  /* Line 12 */
              System.out.println("newBar"); 
              newBar = new Bar(); /* Line 14 */
              System.out.println("finishing"); /* Line 15 */
          } 
      }
      

    • Options
    • A. after line 12
    • B. after line 14
    • C. after line 7, when doBar() completes
    • D. after line 15, when main() completes
    • Discuss
    • 9. At Point X on line 5, which code is necessary to make the code compile?
      public class ExceptionTest 
      { 
          class TestException extends Exception {} 
          public void runTest() throws TestException {} 
          public void test() /* Point X */ 
          { 
              runTest(); 
          } 
      }
      

    • Options
    • A. No code is necessary.
    • B. throws Exception
    • C. catch ( Exception e )
    • D. throws RuntimeException
    • Discuss
    • 10. Where will be the most chance of the garbage collector being invoked?
      class HappyGarbage01 
      { 
          public static void main(String args[]) 
          {
              HappyGarbage01 h = new HappyGarbage01(); 
              h.methodA(); /* Line 6 */
          } 
          Object methodA() 
          {
              Object obj1 = new Object(); 
              Object [] obj2 = new Object[1]; 
              obj2[0] = obj1; 
              obj1 = null; 
              return obj2[0]; 
          } 
      }
      

    • Options
    • A. After line 9
    • B. After line 10
    • C. After line 11
    • D. Garbage collector never invoked in methodA()
    • Discuss


    Comments

    There are no comments.

Enter a new Comment