MOCK PAPER 3
1)
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}
1)Prints nothing
2)Prints: 1
3)Prints: 01
4)Compile-time error
5)None of the above
Ans: 2
Explanation:
a.run() method was called instead of a.start(); so the full program runs as a single thread so a.run() is guaranteed to complete
2)
class bike
{
}
class arr extends bike{
public static void main(String[] args) {
arr[] a1=new arr[2];
bike[] a2;
a2=a1; //3
arr[] a3;
a3=a1; //5
}}
1)compile time error at line 3
2)compile time error at line 5
3)Runtime exception
4)The code runs fine
5)None of the above
Ans: 4
Explanation:
bike is the superclass of arr.so they are compatible(superobject=subobject)
but subobject=superobject not allowed
3)
class C{
public static void main (String[] args) {
String s1="hjhh"; // 1
String s2="\u0002"; //2
String s3="'\'"; //3
}}
1)compile time error at line 1
2)compile time error at line 2
3)compile time error at line 3
4)Runtime exception
5)the code runs without any error
Ans: 5
Explanation:
A String literal is a sequence of characters enclosed in double quotes
4)Which data type is wider for the purpose of casting: float or long?
1)float
2)long
Ans: 1
Explanation:
float is wider than long, because the entire range of long fits within the range of float.
more
1)
class A extends Thread {
private int i;
public void run() {i = 1;}
public static void main(String[] args) {
A a = new A();
a.run();
System.out.print(a.i);
}}
1)Prints nothing
2)Prints: 1
3)Prints: 01
4)Compile-time error
5)None of the above
Ans: 2
Explanation:
a.run() method was called instead of a.start(); so the full program runs as a single thread so a.run() is guaranteed to complete
2)
class bike
{
}
class arr extends bike{
public static void main(String[] args) {
arr[] a1=new arr[2];
bike[] a2;
a2=a1; //3
arr[] a3;
a3=a1; //5
}}
1)compile time error at line 3
2)compile time error at line 5
3)Runtime exception
4)The code runs fine
5)None of the above
Ans: 4
Explanation:
bike is the superclass of arr.so they are compatible(superobject=subobject)
but subobject=superobject not allowed
3)
class C{
public static void main (String[] args) {
String s1="hjhh"; // 1
String s2="\u0002"; //2
String s3="'\'"; //3
}}
1)compile time error at line 1
2)compile time error at line 2
3)compile time error at line 3
4)Runtime exception
5)the code runs without any error
Ans: 5
Explanation:
A String literal is a sequence of characters enclosed in double quotes
4)Which data type is wider for the purpose of casting: float or long?
1)float
2)long
Ans: 1
Explanation:
float is wider than long, because the entire range of long fits within the range of float.
more