Interview Questions

Output question 9.

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

69. Output question 9.

class MyThread extends Thread {
MyThread() {
System.out.print("1 ");
}
public void run() {
System.out.print("2 ");
}
}
public class MyClass {
public static void main(String[] args) {
Thread thread1 = new MyThread() {
public void run() {
System.out.print("3 ");
}
};
thread1.start();
}
}

Answer.

new MyThread() > created instance of an anonymous inner class.
constructor was called which printed 1
than overridden run() method of anonymous inner class was invoked, which printed 3.
/*OUTPUT
1 3
*/

(Continued on next question...)

Other Interview Questions