What will happen when you attempt to compile and run the following code?
Options :public class TestThread extends Thread{
public static void main(String[] args) {
new TestThread().start();
new TestThread().start();
}
public void run(){
SafeClass st = new SafeClass("abc");
SafeClass st1 = new SafeClass("xyz");
}
}
class SafeClass {
private String str;
public SafeClass(String s){
str = s;
str.toUpperCase();
System.out.println(str+" ");
}
}
a. It will print - ABC XYZ ABC XYZ in that sequence.
b. It will print - ABC ABC XYZ XYZ in that sequence
c. It will print - ABC and XYZ twice, but the sequence cannot be determined.
d. It will print - abc and xyz twice, but the sequence cannot be determined.
e. The code will not compile.
Answer: D
First of all the system.out.println method prints only lower case abc and xyz because the output of str.toUpperCase() is not assigned to str. So options A, B, C are incorrect. Option E is incorrect because the code compiles properly. So the remaining is option D which is correct because for sure the threads are going to print abc and xyz twice but the order of their execution cannot be predicted.
No comments:
Post a Comment