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;
            do while ( I < 1 )
            System.out.print("I is " + I);
            while ( I > 1 ) ;
        }
    }
    


  • Options
  • A. I is 1
  • B. I is 1 I is 1
  • C. No output is produced.
  • D. Compilation error

  • Correct Answer
  • No output is produced. 

    Explanation
    There are two different looping constructs in this problem. The first is a do-while loop and the second is a while loop, nested inside the do-while. The body of the do-while is only a single statement-brackets are not needed. You are assured that the while expression will be evaluated at least once, followed by an evaluation of the do-while expression. Both expressions are false and no output is produced.

    More questions

    • 1. What will be the output of the program?

      System.out.println(Math.sqrt(-4D));


    • Options
    • A. -2
    • B. NaN
    • C. Compile Error
    • D. Runtime Exception
    • Discuss
    • 2. What will be the output of the program?
      package foo; 
      import java.util.Vector; /* Line 2 */
      private class MyVector extends Vector 
      {
          int i = 1; /* Line 5 */
          public MyVector() 
          { 
              i = 2; 
          } 
      } 
      public class MyNewVector extends MyVector 
      {
          public MyNewVector () 
          { 
              i = 4; /* Line 15 */
          } 
          public static void main (String args []) 
          { 
              MyVector v = new MyNewVector(); /* Line 19 */
          } 
      }
      

    • Options
    • A. Compilation will succeed.
    • B. Compilation will fail at line 3.
    • C. Compilation will fail at line 5.
    • D. Compilation will fail at line 15.
    • Discuss
    • 3. What will be the output of the program?
      TreeSet map = new TreeSet();
      map.add("one");
      map.add("two");
      map.add("three");
      map.add("four");
      map.add("one");
      Iterator it = map.iterator();
      while (it.hasNext() ) 
      {
          System.out.print( it.next() + " " );
      }
      

    • Options
    • A. one two three four
    • B. four three two one
    • C. four one three two
    • D. one two three four one
    • Discuss
    • 4. Which three statements are true?

      1. Assertion checking is typically enabled when a program is deployed.
      2. It is never appropriate to write code to handle failure of an assert statement.
      3. Assertion checking is typically enabled during program development and testing.
      4. Assertion checking can be selectively enabled or disabled on a per-package basis, but not on a per-class basis.
      5. Assertion checking can be selectively enabled or disabled on both a per-package basis and a per-class basis.

    • Options
    • A. 1, 2 and 4
    • B. 2, 3 and 5
    • C. 3, 4 and 5
    • D. 1, 2 and 5
    • Discuss
    • 5. What will be the output of the program?
      class s implements Runnable 
      { 
          int x, y; 
          public void run() 
          { 
              for(int i = 0; i < 1000; i++) 
                  synchronized(this) 
                  { 
                      x = 12; 
                      y = 12; 
                  } 
              System.out.print(x + " " + y + " "); 
          } 
          public static void main(String args[]) 
          { 
              s run = new s(); 
              Thread t1 = new Thread(run); 
              Thread t2 = new Thread(run); 
              t1.start(); 
              t2.start(); 
          } 
      }
      

    • Options
    • A. DeadLock
    • B. It print 12 12 12 12
    • C. Compilation Error
    • D. Cannot determine output.
    • Discuss
    • 6. What will be the output of the program?
      public class ThreadTest extends Thread 
      { 
          public void run() 
          { 
              System.out.println("In run"); 
              yield(); 
              System.out.println("Leaving run"); 
          } 
          public static void main(String []argv) 
          { 
              (new ThreadTest()).start(); 
          } 
      }
      

    • Options
    • A. The code fails to compile in the main() method
    • B. The code fails to compile in the run() method
    • C. Only the text "In run" will be displayed
    • D. The text "In run" followed by "Leaving run" will be displayed
    • Discuss
    • 7. Which statement is true?

    • Options
    • A. A static method cannot be synchronized.
    • B. If a class has synchronized code, multiple threads can still access the nonsynchronized code.
    • C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.
    • D. When a thread sleeps, it releases its locks.
    • Discuss
    • 8. Which of the following are legal lines of code?

      1. int w = (int)888.8;
      2. byte x = (byte)1000L;
      3. long y = (byte)100;
      4. byte z = (byte)100L;

    • Options
    • A. 1 and 2
    • B. 2 and 3
    • C. 3 and 4
    • D. All statements are correct.
    • Discuss
    • 9. What will be the output of the program?
      int I = 0;
      label:
          if (I < 2) {
          System.out.print("I is " + I);
          I++;
          continue label;
      }
      

    • Options
    • A. I is 0
    • B. I is 0 I is 1
    • C. Compilation fails.
    • D. None of the above
    • Discuss
    • 10. What will be the output of the program?
      interface Foo141 
      { 
          int k = 0; /* Line 3 */
      } 
      public class Test141 implements Foo141 
      {
          public static void main(String args[]) 
          {
              int i; 
              Test141 test141 = new Test141(); 
              i = test141.k; /* Line 11 */
              i = Test141.k; 
              i = Foo141.k; 
          } 
      }
      

    • Options
    • A. Compilation fails.
    • B. Compiles and runs ok.
    • C. Compiles but throws an Exception at runtime.
    • D. Compiles but throws a RuntimeException at runtime.
    • Discuss


    Comments

    There are no comments.

Enter a new Comment