Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 21

Given the code fragments:

class MyThread implements Runnable {

private static AtomicInteger count = new AtomicInteger (0);

public void run () {

int x = count.incrementAndGet();

System.out.print (x+" ");

}

}

and

Thread thread1 = new Thread(new MyThread());

Thread thread2 = new Thread(new MyThread());

Thread thread3 = new Thread(new MyThread());

Thread [] ta = {thread1, thread2, thread3};

for (int x= 0; x < 3; x++) {

ta[x].start();

}

Which statement is true?

    Correct Answer: A

    The code uses the AtomicInteger class to ensure atomicity when incrementing the count. When the threads are started, they will each increment the shared AtomicInteger variable, resulting in the numbers 1, 2, and 3 being printed. However, the threading context means the order in which these numbers appear is unpredictable due to the concurrent execution of the threads.

Discussion
thetechOption: A

A is correct answer. Tested.

InnovationOption: A

A is correct, tested

sansay61Option: A

a is correct. there's no sync block or method in runnable.

asdfjhfgjuaDCVOption: A

A is the correct answer: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter GDP: "); int GDP = Integer.parseInt(br.readLine()); System.out.println("GDP entered: " + GDP); } }

asdfjhfgjuaDCV

Its for other question

asdfjhfgjuaDCVOption: A

A is the correct answer. import java.util.concurrent.atomic.AtomicInteger; class MyThread implements Runnable { private static AtomicInteger count = new AtomicInteger(0); public void run() { int x = count.incrementAndGet(); System.out.print(x + " "); } } public class Main { public static void main(String[] args) { Thread thread1 = new Thread(new MyThread()); Thread thread2 = new Thread(new MyThread()); Thread thread3 = new Thread(new MyThread()); Thread[] ta = { thread1, thread2, thread3 }; for (int x = 0; x < 3; x++) { ta[x].start(); } } }

steefaandOption: A

A is correct.

r1muka5Option: A

Correct answer is A.

WilsonKKerllOption: A

Answer is A

Ritesh_Option: B

B is correct answer

Samps

try running same code several times