SCJP 6 Mock exam for Threads


Threads is a valuable knowledge for every developer. This free mock exam presents important Thread skills required by the SCJP exam.

Make sure to study the API of Runnable, Thread and Object before moving on.

  1. Runnable is an interface. (1 correct answer)
    1. true
    2. false
  2. Runnable belongs to the java.lang package. (1 correct answer)
    1. true
    2. false
  3. How many methods does the Runnable interface define? (1 correct answer)
    1. One: run()
    2. Two: run() and start()
    3. Three: run(), start() and join()
  4. Runnable defines a method run. What’s the signature? (1 correct answer)
    1. public void run()
    2. public void run() throws InterruptedException
    3. public void run(Runnable r)
    4. public void run(Runnable r) throws InterruptedException
  5. Class Thread implements the Runnable interface. (1 correct answer)
    1. true
    2. false
  6. Consider these methods.
          ⇒ run()
          ⇒ start()
          ⇒ join()
          ⇒ sleep()
          ⇒ yield()
          ⇒ currentThread()
    How many of them are defined in the Thread class? (1 correct answer)

    1. One.
    2. Two.
    3. Three.
    4. All.
  7. Consider these methods.
          ⇒ run()
          ⇒ start()
          ⇒ join()
          ⇒ sleep()
          ⇒ yield()
          ⇒ currentThread()
    How many of them are static? (1 correct answer)

    1. One.
    2. Two.
    3. Three.
    4. All.
  8. Consider these methods.
          ⇒ run()
          ⇒ start()
          ⇒ join()
          ⇒ sleep()
          ⇒ yield()
          ⇒ currentThread()
    How many of them declare a checked exception? (1 correct answer)

    1. One.
    2. Two.
    3. Three.
    4. All.
  9. What’s the signature of the method start()? (1 correct answer)
    1. public void start()
    2. public void start() throws InterruptedException
    3. public void start() throws IllegalThreadStateException
  10. What’s the signature of the method yield()? (1 correct answer)
    1. public void yield()
    2. public void yield() throws InterruptedException
    3. public static void yield()
    4. public static void yield() throws InterruptedException
  11. There are 2 overloaded methods of Thread with the name sleep. (1 correct answer)
    1. true
    2. false
  12. There are 3 overloaded methods of Thread with the name join. (1 correct answer)
    1. true
    2. false
  13. The Thread class has three public static and final integer fields: MIN_PRIORITY, NORM_PRIORITY, MAX_PRIORITY. (1 correct answer)
    1. true
    2. false
  14. Consider these methods.
          ⇒ wait()
          ⇒ notify()
          ⇒ notifyAll()
    How many of them are defined in the Object class? (1 correct answer)

    1. One.
    2. All.
    3. None.
  15. Consider these methods.
          ⇒ wait()
          ⇒ notify()
          ⇒ notifyAll()
    How many of them are static? (1 correct answer)

    1. One.
    2. All.
    3. None.
  16. Consider these methods.
          ⇒ wait()
          ⇒ notify()
          ⇒ notifyAll()
    How many of them declare a checked exception? (1 correct answer)

    1. One.
    2. All.
    3. None.
  17. There are 3 overloaded methods of Object with the name wait. (1 correct answer)
    1. true
    2. false
  18. Will this code compile successfully? (1 correct answer)
    class Test extends Runnable {
    
    }
    
    1. Yes.
    2. No.
  19. Will this code compile successfully? (1 correct answer)
    class Test implements Runnable {
    
    }
    
    1. Yes.
    2. No.
  20. Will this code compile successfully? (1 correct answer)
    class Test implements Runnable {
        void run() {
        }
    }
    
    1. Yes.
    2. No.
  21. Will this code compile successfully? (1 correct answer)
    class Test implements Runnable {
        public void run() {
        }
        void run(Runnable r) {
        }
        void run(String... s) {
        }
    }
    
    1. Yes.
    2. No.
  22. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
    
    }
    
    1. Yes.
    2. No.
  23. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test() {
            super();
        }
    }
    
    1. Yes.
    2. No.
  24. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test() {
            super("Nikos");
        }
    }
    
    1. Yes.
    2. No.
  25. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test(String name) {
            super(name);
        }
    }
    
    1. Yes.
    2. No.
  26. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test(Runnable job, String name) {
            super(job);
        }
    }
    
    1. Yes.
    2. No.
  27. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public Test(Runnable job, String name) {
            super(job, name);
        }
    
    }
    
    1. Yes.
    2. No.
  28. Will this code compile successfully? (1 correct answer)
    import java.util.NavigableSet;
    class Test extends Thread {
        public void run(NavigableSet<Thread> set) {
        }
    }
    
    1. Yes.
    2. No.
  29. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public void run() throws InterruptedException {
        }
    }
    
    1. Yes.
    2. No.
  30. Will this code compile successfully? (1 correct answer)
    class Test extends Thread {
        public void join() throws InterruptedException {
        }
    }
    
    1. Yes.
    2. No.
  31. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println("run 1");
        }
        public void run(String s) {
            System.out.println("run 2");
        }
        public static void main(String[] args) {
            new Test().start();
        }
    }
    
    1. It prints “run 1″.
    2. It prints “run 2″.
    3. Compilation fails.
  32. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println("run 1");
        }
        public void run(String s) {
            System.out.println("run 2");
        }
        public static void main(String[] args) {
            new Test().start("hi");
        }
    }
    
    1. It prints “run 1″.
    2. It prints “run 2″.
    3. Compilation fails.
  33. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test {
        public static void main(String[] args) {
            String name = Thread.currentThread().getName();
            System.out.println(name);
        }
    }
    
    1. It prints nothing.
    2. It prints “main”.
    3. It prints “Thread-0″.
    4. Compilation fails because an InterruptedException is not handled.
  34. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test extends Thread {
        public static void main(String[] args) {
            String name = Thread.currentThread().getName();
            System.out.println(name);
        }
    }
    
    1. It prints nothing.
    2. It prints “main”.
    3. It prints “Thread-0″.
    4. Compilation fails because an InterruptedException is not handled.
    5. Compilation fails because Test does not implement the run() method.
  35. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            new Thread().run();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  36. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.run();
            thread.run();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  37. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            new Thread().start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  38. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.start();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  39. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.start();
            thread = new Thread();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  40. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.start();
            Thread.yield();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  41. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            Thread thread = new Thread();
            thread.run();
            thread.start();
        }
    }
    
    1. Compilation fails.
    2. It compiles and runs fine.
    3. An IllegalThreadStateException is thrown at runtime.
  42. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            new Thread(new Thread(new Thread())).start();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  43. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            new Thread(new Thread(new Thread())).run();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  44. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        Thread thread = new Thread() {
            public void run() {
                System.out.println("Hello!");
            }
        };
        thread.start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  45. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        new Thread() {
            public void run() {
                System.out.println("Hello!");
            }
        }.start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  46. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        new Runnable() {
            public void run() {
                System.out.println("Hello!");
            }
        }.start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  47. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
    public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                System.out.println("Hello!");
            }
        }).start();
    }
    }
    
    1. It prints “Hello!”.
    2. Compilation fails.
  48. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job implements Runnable {
        public void run() {
            System.out.println("Working...");
        }
    }
    public class My {
        public static void main(String[] args) {
            Thread thread1 = new Thread(new Job());
            Thread thread2 = new Thread(new Job());
            thread1.start();
            thread2.start();
        }
    }
    
    1. Compilation fails.
    2. It prints “Working…” three times.
    3. It prints “Working…” one time and then an exception is thrown.
  49. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job implements Runnable {
        public void run() {
            System.out.println("Working...");
        }
    }
    public class My {
        public static void main(String[] args) {
            Job job = new Job();
            Thread thread1 = new Thread(job);
            Thread thread2 = new Thread(job);
            thread1.start();
            thread2.start();
        }
    }
    
    1. Compilation fails.
    2. It prints “Working…” three times.
    3. It prints “Working…” one time and then an exception is thrown.
  50. What is the output? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            Test test = new Test();
            System.out.println(test.isAlive());
            test.start();
        }	
    }
    
    1. false false
    2. false true
    3. true false
    4. true true
  51. What is the output? (1 correct answer)
    public class Test extends Thread {
        public void run() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            Test test = new Test();
            System.out.println(test.isAlive());
            test.run();
        }	
    }
    
    1. false false
    2. false true
    3. true false
    4. true true
  52. What is the output? (1 correct answer)
    public class Test extends Thread {
        public Test() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            new Test();
        }	
    }
    
    1. false
    2. true
  53. What is the output? (1 correct answer)
    public class Test extends Thread {
        public Test() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            new Test().run();
        }	
    }
    
    1. false
    2. true
  54. What is the output? (1 correct answer)
    public class Test extends Thread {
        public Test() {
            System.out.println(isAlive());
        }
        public static void main(String[] args) {
            new Test().start();
        }	
    }
    
    1. false
    2. true
  55. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
    public class Account {
        private Integer number = 0;
        synchronized public void setNumber(Integer number) {
            this.number = number;
        }
        synchronized public Integer getNumber() {
            return number;
        }
    }
    
    1. Yes.
    2. No.
  56. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
    public class Account {
        private Integer number = 0;
        public void setNumber(Integer number) {
            synchronized (this) {
                this.number = number;
            }
        }
        synchronized public Integer getNumber() {
            return number;
        }
    }
    
    1. Yes.
    2. No.
  57. Is this class thread-safe? In other words, is it carefully designed to protect its state from concurrent access? (1 correct answer)
    public class Account {
        private Integer number = 0;
        public void setNumber(Integer number) {
            synchronized (this) {
                this.number = number;
            }
        }
        public Integer getNumber() {
            synchronized (this) {
                return number;
            }
        }
    }
    
    1. Yes.
    2. No.
  58. These classes are defined in a single file. There’s a value object Account that carefully protects it’s state from concurrent access, a Client class of type Thread that puts some money in an account, and a main method that simply starts two clients.
    class Account {
        private Integer number = 0;
        public synchronized void setNumber(Integer number) {
            this.number = number;
        }
        public synchronized Integer getNumber() {
            return number;
        }
    }
    
    class Client extends Thread {
        Account account;
        public Client(Account account) {
            this.account = account;
        }
        public void run() {
            for (int i = 1; i <= 1000; i++) {
                account.setNumber(account.getNumber() + 1);
    
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) throws Exception {
            Account account = new Account();
            Client one = new Client(account);
            Client two = new Client(account);
            one.start();
            two.start();
            one.join();
            two.join();
            // here
        }
    }
    

    Just before the main method exits, the account’s number field is guaranteed to have value 2000.

    1. true
    2. false
  59. These classes are defined in a single file.
    class Account {
        private Integer number = 0;
        public synchronized void increase() {
            number++;
        }
        public synchronized Integer getNumber() {
            return number;
        }
    }
    
    class Client extends Thread {
        Account account;
        public Client(Account account) {
            this.account = account;
        }
        public void run() {
            for (int i = 1; i <= 1000; i++) {
                account.increase();
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) throws Exception {
            Account account = new Account();
            Client one = new Client(account);
            Client two = new Client(account);
            one.start();
            two.start();
            one.join();
            two.join();
            // here
        }
    }
    

    Just before the main method exits, the account’s number field is guaranteed to have value 2000.

    1. true
    2. false
  60. These classes are defined in a single file.
    class Account {
        private Integer number = 0;
        public void increase() {
            number++;
        }
        public synchronized Integer getNumber() {
            return number;
        }
    }
    
    class Client extends Thread {
        Account account;
        public Client(Account account) {
            this.account = account;
        }
        public void run() {
            for (int i = 1; i <= 1000; i++) {
                account.increase();
            }
        }
    }
    
    public class Run {
        public static void main(String[] args) throws Exception {
            Account account = new Account();
            Client one = new Client(account);
            Client two = new Client(account);
            one.start();
            two.start();
            one.join();
            two.join();
            // here
        }
    }
    

    Just before the main method exits, the account’s number field is guaranteed to have value 2000.

    1. true
    2. false
  61. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
        public static void main(String[] args) {
            String message = "Hello!";
            message.wait(); // 1
            System.out.println(message);
        }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  62. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
        public static void main(String[] args) {
            String message = "Hello!";
            try {
                message.wait();
            } catch (InterruptedException e) {
            }
            System.out.println(message);
        }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  63. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
    public static void main(String[] args) {
        String message = "Hello!";
        synchronized (args) {
            try {
                message.wait();
            } catch (InterruptedException e) {
            }
        }
        System.out.println(message);
    }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  64. What happens when this code gets compiled and executed? (1 correct answer)
    public class Test  {
    public static void main(String[] args) {
        String message = "Hello!";
        synchronized (message) {
            try {
                message.wait();
            } catch (InterruptedException e) {
            }
        }
        System.out.println(message);
    }
    }
    
    1. Compilation fails.
    2. It prints “Hello!”.
    3. An IllegalMonitorStateException is thrown at runtime.
    4. Nothing gets printed, as the main thread just waits at line 1.
  65. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
        private Integer number = 0;
        public void run() {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
        }
        public Integer getNumber() {
            return number;
        }
    }
    public class Test {
        public static void main(String[] args) {
            Job thread = new Job();
            thread.start();
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  66. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
        private Integer number = 0;
        public void run() {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
        }
        public Integer getNumber() {
            return number;
        }
    }
    public class Test {
        public static void main(String[] args) 
        throws InterruptedException {
            Job thread = new Job();
            thread.start();
            synchronized (thread) {
                thread.wait();
            }
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  67. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
    private Integer number = 0;
        public void run() {
        synchronized (this) {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
            notify();
        }
        }
        public Integer getNumber() {
            return number;
        }
    }
    public class Test {
        public static void main(String[] args) throws Exception {
            Job thread = new Job();
            thread.start();
            synchronized (thread) {
    
                thread.wait();
            }
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  68. These classes are defined in the same file. What is the output? (1 correct answer)
    class Job extends Thread {
        private Integer number = 0;
        public void run() {
            for (int i = 1; i < 1000000; i++) {
                number++;
            }
        }
        public Integer getNumber() {
            return number;
        }
    }
    
    public class Test {
        public static void main(String[] args) throws Exception {
            Job thread = new Job();
            thread.start();
            thread.join();
            System.out.println(thread.getNumber());
        }
    }
    
    1. It prints 0.
    2. It prints 999999.
    3. The output is not guaranteed to be any of the above.
  69. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            notifyAll();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  70. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            new Object().notifyAll();
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  71. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            synchronized (new Object()) {
                new Object().notifyAll();
            }
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.
  72. What happens when this code gets compiled and executed? (1 correct answer)
    public class Run {
        public static void main(String[] args) {
            System.out.print("A ");
            final Object test = new Object();
            synchronized (test) {
                test.notifyAll();
            }
            System.out.println("B");
        }
    }
    
    1. It prints “A B”.
    2. Compilation fails.
    3. It prints “A ” and an exception is thrown.

© 2008 Nikos Pougounias. This is a free contribution to the Java community. Please distribute it for free. http://nikojava.wordpress.com

Answers

  1. a
  2. a
  3. a
  4. a
  5. a
  6. d
  7. c
  8. b
  9. a
  10. c
  11. a
  12. a
  13. a
  14. b
  15. c
  16. a
  17. a
  18. b
  19. b
  20. b
  21. a
  22. a
  23. a
  24. a
  25. a
  26. a
  27. a
  28. a
  29. b
  30. b
  31. a
  32. c
  33. b
  34. b
  35. b
  36. b
  37. b
  38. c
  39. b
  40. c
  41. b
  42. a
  43. a
  44. a
  45. a
  46. b
  47. a
  48. b
  49. b
  50. b
  51. a
  52. a
  53. a
  54. a
  55. a
  56. a
  57. a
  58. b
  59. a
  60. b
  61. a
  62. c
  63. c
  64. d
  65. c
  66. b
  67. b
  68. b
  69. b
  70. c
  71. c
  72. a

References

A friendly community of people preparing for SCJP can be found at the JavaRanch SCJP forum.

Please contact me for any suggestion.

Thanks.

About these ads

25 Responses to SCJP 6 Mock exam for Threads

  1. Nikos says:

    Thanks to Maggie Zhou for her corrections on questions 42 and 43!

  2. Garima says:

    For the question no 59, answer should be b i.e. false. I ran and checked.

  3. Nikos says:

    Hi Garima, now questions 58, 59 and 60 are cut and clear! Just provide the account a print method and check it again! Thanks.

  4. Sexualxe says:

    Hello you forum best. And Bye. just test. . :) :) :) :) :) :):) :) :) :) :) :)

  5. Orsi says:

    Dear Nikos,

    I do not understand why example 66 works the way it does. I have tried and it really prints what you say it does, but why, when nobody calls notify()? Also, if I add a Thread.sleep(1000) before entering the synchronized block for waiting, it hangs. What’s the _important_ difference between the two cases? I kind of think that if something does not work if I sleep in it a little, then it is not certain to work at all.

    Similar question for example 67: it hangs if I add a Thread.sleep() before entering the synch block containing the wait call. Which is logical, because the notify() happens before the wait is entered. Then why can we say that without the sleep it certainly works? Does it not depend on the scheduler?

    Thanks in advance!

  6. mashimaro says:

    hi, I ran question 48.
    “Working…” is only printed twice? not three times?

    btw, thanks for your mock exams!

  7. [...] Free SCJP Mock exam for Threads [...]

  8. Aruna says:

    Very Good Practice Material.Thanks A lot.

  9. kalyan says:

    Hi can you please explain questions 58,60,66,67 ?
    You are right..but why..i couldn’t infer….

    The remaining questions are great for self test in Threads.
    Thanks

  10. Michał B says:

    Hi all.
    I would like to explain in short some questions:
    q58 – you have 2 independent methods “setNumber” and “getNumber”, two threads cannot get into one of them in the same time becouse of synchronization, this is clear, but line “account.setNumber(account.getNumber() + 1);” is unsafe becouse two threads may read the same number (getNumber()) (one thread get into method, read value and release lock, then second thread get into the same method and read the same value) then both tread wants to set their values (setNumber), so You dont have quarantee that the sequence will be: getNumber, setNumber, getNumber, setNumber… it can be: getNumber, getNumber, setNumber,setNumber

    q60: – in this example You have quarantee sequence, read and set value (increment)

    q66: – when the thread is dead (ended run method) it release lock (if thread end his work before main thread run wait method program will be wait for all)

    g67: – similar to above example, when thread run notify method and go out from synchronized block main thread wake up, but You have to know that You imply that the thread will finish his work after main method run wait method on thread object.

    I hope this help somone. Best regards

  11. Cris says:

    Hi

    First of all – good job.

    Look again at question 8 – I believe 3 methods (not 2) are static there.

  12. lavanya says:

    one of the excellent site for scjp aspirants….!!!!!!!!!!!!!!!!!!!!!!!!!!!1

  13. ankur garg says:

    Hello Niko
    Can you please explain output of Q 66?

  14. Laura says:

    Hey! Thanks for these questions, they are really useful!
    But I have a doubt regarding to questions number 24 and 66, though I checked them and the answer for both are correct, I can’t get how they work the way they do ..
    If someone is willing to give a hand here please, I’d really appreciate it :)
    Thanks again!

  15. coder89 says:

    Michał B: q60 is correct because increment/decrement operation is NOT an atomic operation and it takes more than 1 processor cycles.

    However questions 66 & 67 ale incorrect because if you have any processes running on your machine which slow down your machine it may cause that scheduler switch threads before main thread lock thread object and than main thread will wait until run method will end. (the effect will be the same if you call Thread.sleep() method between Thread.start() and synchronized block) In that case the program may hang on in Thread.wait() method.

  16. Jurica says:

    Shouldn’t the answer number 2 in question 48 be “It prints “Working…” two times.” instead of “It prints “Working…” three times.” ?

  17. Anup Sharma says:

    I hope it will help …

  18. Anup Sharma says:

    jst wait and watch

  19. Question 48, the correct answer is:
    It prints “Working…” TWO times.

  20. Oleg Ignatov says:

    52 correct answers

  21. Muhammad says:

    I think Q9 answer is C not A

    public void start() IllegalThreadStateException

  22. krobin93 says:

    In ques 48 and 49 … it prints twice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 79 other followers

%d bloggers like this: