MOCK PAPER 4
Question no -1
What is the output for the below code ?
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public class Test {
public static void main(String... args) {
A a = new A();
Thread t = new Thread(a);
Thread t1 = new Thread(a);
t.setName("t");
t1.setName("t1");
t.setPriority(10);
t1.setPriority(-3);
t.start();
t1.start();
}
}
Options are
A.t t1
B.t1 t
C.t t
D.Compilation succeed but Runtime Exception
Answer :
D is the correct answer.
Thread priorities are set using a positive integer, usually between 1 and 10. t1.setPriority(-3); throws java.lang.IllegalArgumentException.
What is the output for the below code ?
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
public class Test {
public static void main(String... args) {
A a = new A();
Thread t = new Thread(a);
Thread t1 = new Thread(a);
t.setName("t");
t1.setName("t1");
t.setPriority(10);
t1.setPriority(-3);
t.start();
t1.start();
}
}
Options are
A.t t1
B.t1 t
C.t t
D.Compilation succeed but Runtime Exception
Answer :
D is the correct answer.
Thread priorities are set using a positive integer, usually between 1 and 10. t1.setPriority(-3); throws java.lang.IllegalArgumentException.
Question no -2
What is the output for the below code ?
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
1. public class Test {
2. public static void main(String... args) {
3. A a = new A();
4. Thread t = new Thread(a);
5. t.setName("good");
6. t.start();
7. }
8. }
Options are
A.good
B.null
C.Compilation fails with an error at line 5
D.Compilation succeed but Runtime Exception
Answer :
A is the correct answer. Thread.currentThread().getName() return name of the current thread.
What is the output for the below code ?
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
1. public class Test {
2. public static void main(String... args) {
3. A a = new A();
4. Thread t = new Thread(a);
5. t.setName("good");
6. t.start();
7. }
8. }
Options are
A.good
B.null
C.Compilation fails with an error at line 5
D.Compilation succeed but Runtime Exception
Answer :
A is the correct answer. Thread.currentThread().getName() return name of the current thread.
Question no -3
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
Boolean expr = true;
if (expr) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
options
A)true
B)Compile Error - can't use Boolean object in if().
C)false
D)Compile Properly but Runtime Exception.
Correct answer is : A
Explanations : In the if statement, condition can be Boolean object in jdk1.5 and jdk1.6.
In the previous version only boolean is allowed.
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
Boolean expr = true;
if (expr) {
System.out.println("true");
} else {
System.out.println("false");
}
}
}
options
A)true
B)Compile Error - can't use Boolean object in if().
C)false
D)Compile Properly but Runtime Exception.
Correct answer is : A
Explanations : In the if statement, condition can be Boolean object in jdk1.5 and jdk1.6.
In the previous version only boolean is allowed.
Question no -4
What is the output for the below code ?
public class Test {
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(0, 59);
int total = list.get(0);
System.out.println(total);
}
}
options
A)59
B)Compile time error, because you have to do int total = ((Integer)(list.get(0))).intValue();
C)Compile time error, because can't add primitive type in List.
D)Compile Properly but Runtime Exception.
Correct answer is : A
Explanations :Manual conversion between primitive types (such as an int) and wrapper classes
(such as Integer) is necessary when adding a primitive data type to a collection in jdk1.4 but
The new autoboxing/unboxing feature eliminates this manual conversion in jdk 1.5 and jdk 1.6.
Question no -5
What is the output for the below code ?
public class B {
public String getCountryName(){
return "USA";
}
public StringBuffer getCountryName(){
StringBuffer sb = new StringBuffer();
sb.append("UK");
return sb;
}
public static void main(String[] args){
B b = new B();
System.out.println(b.getCountryName().toString());
}
}
options
A)Compile with error
B)USA
C)UK
D) Runtime Exception
Correct answer is : A
Explanations : You cannot have two methods in the same class with signatures that only differ by return type.