1. For the following:
public class Test {
public static void main(String args[]) {
Scanner scan = new Scanner("1234");
System.out.println(scan.nextInt());
}
}
a) Gives output: 1234 b) Compile error c) Exception at runtime d) None of the above
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Gives output: 1234
Correct! Gives output: 1234
2. What is the output?
public class Test {
public static void main(String args[]) {
System.out.println(A.B.C.i);
}
public static class A {
public static class B {
public static class C {
public static int i=8;
static {
i =8;
}
}
}
}
}
a) Compile error : illegal static declaration in inner class b) Compile error : Cannot have nested inner classes c) Gives output: 8 d) Compile error : Cannot access i from main
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Gives output: 8 All are static and so can be accessed without instance being created
Correct! c) Gives output: 8 All are static and so can be accessed without instance being created
3. What is output?
import java.util.Date;
public class Test {
public static void main(String args[]) {
Date d1 = new Date();
Date d2 = (Date) d1.clone();
System.out.println(d1=d2);
}
}
a) Compile error: Cannot clone Date b) Gives output: false c) Gives output: true d) Prints current date-time
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Prints current date-time d1=d2 is assignment
Correct! Prints current date-time d1=d2 is assignment
4. public class Test {
public static void main(String args[]) {
String a = "abc";
System.out.println(a.hashCode());
String b = "def";
System.out.println(b.hashCode());
a = a+b;
System.out.println(a.hashCode());
}
}
a) Prints three numbers all of which are same b) First and third numbers are same c) Compile error d) Prints three different numbers
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Prints three different numbers For String object always a new instance is created
Correct! Prints three different numbers For String object always a new instance is created
5. To prevent a field from being saved when an object is serialized,
The field should be marked as :
a) strictfp b) deserialize c) volatile d) transient
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) transient
Correct! transient
6. For the following :
public class Test {
public static void main(String args[]) {
String [][] names = {
{"Mr.", "Mrs.", "Ms."},
{"John", "Gupta", "Hegde", "Khan"},
{" : M", " : F"}
};
String[][] arrNew = new String[2][2];
System.arraycopy(names, 1, arrNew, 0, 2);
System.out.println(arrNew[0][1] + arrNew[1][0]);
}
}
a) Gives output : Gupta : M b) Gives output : Mrs.John c) Compile Error d) Throws ArrayIndexOutOfBoundsException
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Gives output : Gupta : M
Correct! Gives output : Gupta : M
7. public class Test extends Parent{
public void calculate() {
System.out.println("in Test");
}
public static void main(String[] args) {
Parent p = new Test();
p.calculate();
}
}
public class Parent {
private void calculate() {
System.out.println("in Parent");
}
}
a) class Test will not compile b) Gives Output: in Test c) Gives Output: in Parent d) class Parent will not compile
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong!The correct answer is a) class Test will not compile calculate() is not visible in Parent
Correct! class Test will not compile calculate() is not visible in Parent