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
Зареєструйся Зараз
або Підпишись на майбутні тести