What will be the output of the following code:

public class Main extends Thread {
	 private int a;

	 public Main() {
		 a = 2;
		 start();
		 System.out.println(a);
	 }
	 
	 public void run() {
		 a++;
	 }
	 public static void main( String[] args ) {
		 new Main();
	 }
 }
Explanation
The start() method creates new thread and calls the run() method.
Actually, start() method tells to scheduler to create and run a new thread.
But it's unknown when the execution will start.
So the answer is 'the result is undetermined as it may vary from one execution to another'.

Следи за CodeGalaxy

Мобильное приложение Beta

Get it on Google Play
Обратная Связь
Продолжайте изучать
тесты по Java
Cosmo
Зарегистрируйся сейчас
или Подпишись на будущие тесты