Menu
Question Index
...

What will be the output of the following program?

class Happy extends Thread {
    final StringBuffer sb1 = new StringBuffer();
    final StringBuffer sb2 = new StringBuffer();
    public static void main(String args[]) {
        final Happy h = new Happy();
        new Thread() {
            public void run() {
                synchronized (this) {
                    h.sb1.append("A");    h.sb2.append("B");
                    System.out.print(h.sb1);
                    System.out.print(h.sb2);
                }
            }
        }.run();
        new Thread() {
            public void run() {
                synchronized (this) {
                    h.sb1.append("D");    h.sb2.append("C");
                    System.out.print(h.sb2);
                    System.out.print(h.sb1);
                }
            }
        }.start();
    }
}


ABDCAD
ABCBCAD
CDADACB
ABBCAD
Output can not be determined
Compilation Error or Runtime Error

Doubts

Problems

Topic: What is Multitasking and Multithreading?

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App