logo

CuriousTab

CuriousTab

Discussion


Home Java Programming Threads Comments

  • Question
  • What will be the output of the program?
    public class SyncTest 
    {
        public static void main (String [] args) 
        {
            Thread t = new Thread() 
            {
                Foo f = new Foo();
                public void run() 
                {
                    f.increase(20);
                }
            };
        t.start();
        }
    }
    class Foo 
    {
        private int data = 23;
        public void increase(int amt) 
        {
            int x = data;
            data = x + amt;
        }
    }
    
    and assuming that data must be protected from corruption, what?if anything?can you add to the preceding code to ensure the integrity of data?


  • Options
  • A. Synchronize the run method.
  • B. Wrap a synchronize(this) around the call to f.increase().
  • C. The existing code will cause a runtime exception.
  • D. Synchronize the increase() method

  • Correct Answer
  • Synchronize the increase() method 

    Explanation
    Option D is correct because synchronizing the code that actually does the increase will protect the code from being accessed by more than one thread at a time.

    Option A is incorrect because synchronizing the run() method would stop other threads from running the run() method (a bad idea) but still would not prevent other threads with other runnables from accessing the increase() method.

    Option B is incorrect for virtually the same reason as A?synchronizing the code that calls the increase() method does not prevent other code from calling the increase() method.


    Threads problems


    Search Results


    • 1. What will be the output of the program?
      public class WaitTest 
      {
          public static void main(String [] args) 
          {
              System.out.print("1 ");
              synchronized(args)
              {
                  System.out.print("2 ");
                  try 
                  {
                          args.wait(); /* Line 11 */
                  }
                  catch(InterruptedException e){ }
              }
              System.out.print("3 ");
          }
      }
      

    • Options
    • A. It fails to compile because the IllegalMonitorStateException of wait() is not dealt with in line 11.
    • B. 1 2 3
    • C. 1 3
    • D. 1 2
    • Discuss
    • 2. What will be the output of the program?
      public class Q126 implements Runnable 
      { 
          private int x; 
          private int y; 
      
          public static void main(String [] args) 
          { 
              Q126 that = new Q126(); 
              (new Thread(that)).start( ); /* Line 8 */
              (new Thread(that)).start( ); /* Line 9 */
          } 
          public synchronized void run( ) /* Line 11 */
          { 
              for (;;) /* Line 13 */
              { 
                  x++; 
                  y++; 
                  System.out.println("x = " + x + "y = " + y); 
              } 
          } 
      }
      

    • Options
    • A. An error at line 11 causes compilation to fail
    • B. Errors at lines 8 and 9 cause compilation to fail.
    • C. The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
    • D. The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
    • Discuss
    • 3. What will be the output of the program?
      class MyThread extends Thread 
      {
          public static void main(String [] args) 
          {
              MyThread t = new MyThread();
              Thread x = new Thread(t);
              x.start(); /* Line 7 */
          }
          public void run() 
          {
              for(int i = 0; i < 3; ++i) 
              {
                  System.out.print(i + "..");
              }
          }
      }
      

    • Options
    • A. Compilation fails.
    • B. 1..2..3..
    • C. 0..1..2..3..
    • D. 0..1..2..
    • Discuss
    • 4. What will be the output of the program?
      class MyThread extends Thread 
      { 
          MyThread() {} 
          MyThread(Runnable r) {super(r); } 
          public void run() 
          { 
              System.out.print("Inside Thread ");
          } 
      } 
      class MyRunnable implements Runnable 
      { 
          public void run() 
          { 
              System.out.print(" Inside Runnable"); 
          } 
      } 
      class Test 
      {  
          public static void main(String[] args) 
          { 
              new MyThread().start(); 
              new MyThread(new MyRunnable()).start(); 
          } 
      }
      

    • Options
    • A. Prints "Inside Thread Inside Thread"
    • B. Prints "Inside Thread Inside Runnable"
    • C. Does not compile
    • D. Throws exception at runtime
    • Discuss
    • 5. What will be the output of the program?
      public class Test107 implements Runnable 
      { 
          private int x; 
          private int y; 
      
          public static void main(String args[]) 
          {
              Test107 that = new Test107(); 
              (new Thread(that)).start(); 
              (new Thread(that)).start(); 
          } 
          public synchronized void run() 
          {
              for(int i = 0; i < 10; i++) 
              { 
                  x++; 
                  y++; 
                  System.out.println("x = " + x + ", y = " + y); /* Line 17 */
              } 
          } 
      } 
      

    • Options
    • A. Compilation error.
    • B. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
    • C. Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
    • D. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
    • Discuss
    • 6. What will be the output of the program?
      public class ThreadDemo 
      { 
          private int count = 1; 
          public synchronized void doSomething() 
          { 
              for (int i = 0; i < 10; i++) 
                  System.out.println(count++); 
          } 
          public static void main(String[] args) 
          { 
              ThreadDemo demo = new ThreadDemo(); 
              Thread a1 = new A(demo); 
              Thread a2 = new A(demo); 
              a1.start(); 
              a2.start(); 
          } 
      } 
      class A extends Thread 
      { 
          ThreadDemo demo; 
          public A(ThreadDemo td) 
          { 
              demo = td; 
          } 
          public void run() 
          { 
              demo.doSomething(); 
          } 
      }
      

    • Options
    • A. It will print the numbers 0 to 19 sequentially
    • B. It will print the numbers 1 to 20 sequentially
    • C. It will print the numbers 1 to 20, but the order cannot be determined
    • D. The code will not compile.
    • Discuss
    • 7. 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
    • 8. What will be the output of the program?
      class Test116 
      { 
      static final StringBuffer sb1 = new StringBuffer(); 
      static final StringBuffer sb2 = new StringBuffer(); 
      public static void main(String args[]) 
      { 
          new Thread() 
          { 
              public void run() 
              { 
                  synchronized(sb1) 
                  { 
                      sb1.append("A"); 
                      sb2.append("B"); 
                  } 
              } 
          }.start(); 
      
          new Thread() 
          { 
              public void run() 
              { 
                  synchronized(sb1) 
                  { 
                      sb1.append("C"); 
                      sb2.append("D"); 
                  } 
              } 
          }.start(); /* Line 28 */
      
          System.out.println (sb1 + " " + sb2); 
          } 
      }
      

    • Options
    • A. main() will finish before starting threads.
    • B. main() will finish in the middle of one thread.
    • C. main() will finish after one thread.
    • D. Cannot be determined.
    • Discuss
    • 9. What will be the output of the program?
      class MyThread extends Thread 
      {
          public static void main(String [] args) 
          {
              MyThread t = new MyThread();
              t.start();
              System.out.print("one. ");
              t.start();
              System.out.print("two. ");
          }
          public void run() 
          {
              System.out.print("Thread ");
          }
      }
      

    • Options
    • A. Compilation fails
    • B. An exception occurs at runtime.
    • C. It prints "Thread one. Thread two."
    • D. The output cannot be determined.
    • Discuss
    • 10. What will be the output of the program?
      class s1 implements Runnable 
      { 
          int x = 0, y = 0; 
          int addX() {x++; return x;} 
          int addY() {y++; return y;} 
          public void run() { 
          for(int i = 0; i < 10; i++) 
              System.out.println(addX() + " " + addY()); 
      } 
          public static void main(String args[]) 
          { 
              s1 run1 = new s1(); 
              s1 run2 = new s1(); 
              Thread t1 = new Thread(run1); 
              Thread t2 = new Thread(run2); 
              t1.start(); 
              t2.start(); 
          } 
      }

    • Options
    • A. Compile time Error: There is no start() method
    • B. Will print in this order: 1 1 2 2 3 3 4 4 5 5...
    • C. Will print but not exactly in an order (e.g: 1 1 2 2 1 1 3 3...)
    • D. Will print in this order: 1 2 3 4 5 6... 1 2 3 4 5 6...
    • Discuss


    Comments

    There are no comments.

Enter a new Comment