1. For the following:
public class Test {
public static void main(String args[]) {
new Test().m1();
}
private void m1() {
new TestInner().m2();
}
private class TestInner {
private void m2() {
System.out.print("m2()");
}
}
}
a) Gives output: m2() b) Compile error. m2() has private access c) Compile error. TestInner is private d) Compile error. Cannot access private m1() from main()
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is a) Gives output: m2() Outer class can access inner class members.
Correct! Gives output: m2() Outer class can access inner class members.
2.
public class Test {
public static void main(String args[]) {
try {
new Test().m1();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void m1() throws Exception {
try {
m2();
}
catch(Exception e) {
e.setMessage("new error msg");
throw e;
}
}
public void m2() throws Exception {
throw new Exception("m2 error msg");
}
}
a) Gives outpout: new error msg b) Gives outpout: m2 error msg c) Compile error. d) None of above
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Compile error No setMessage() in Exception. Exception object cannot be modified.
Correct! c) Compile error. No setMessage() in Exception. Exception object cannot be modified.
3. An Object can be created by :
A. With keyword 'new'
B. Using Factory method
C. Calling clone() method of an object
D. Passing object to copy constructor
E. Using java reflection
F. Using Object de-serialization
a) A, B, C b) A, C, D c) A, B, C, F d) All of them
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) All of them
Correct! All of them can be used
4. What is the output?
import java.sql.SQLException;
public class Test {
public static void main(String args[]) {
try {
new Test().save();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void save() throws Exception, SQLException {
throw new SQLException("db error");
}
}
a) Compile error: declaration should be throws SQLException, Exception b) Compile error. Cannot throw a checked exception c) Runtime error d) Gives output : db error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is d) Gives output : db error
Correct! Gives output : db error
5. Which is true about an anonymous inner class?
a) It can extend exactly one class and implement exactly one interface b) It can extend exactly one class and can implement multiple interfaces c) It can implement multiple interfaces regardless of whether it also extends a class d) It can extend exactly one class or implement exactly one interface
Wrong! Try again..
Wrong Again! Try one more chance.
Wrong! The correct answer is d) It can extend exactly one class or implement exactly one interface
Example : Runnable r = new Runnable() {
public void run() {
// logic here
}
};
Correct! It can extend exactly one class or implement exactly one interface
Example : Runnable r = new Runnable() {
public void run() {
// logic here
}
};
6. java.io.Externalizable interface is implemented by a class in order to decide what fields are to be serialized.
Which are the methods to be overridden?
A. public void writeExternal(ObjectOutput out) throws IOException
B. public void readExternal(ObjectInput in) throws IOException
C. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
D. public void writeExternal(DataOutput out) throws IOException
E. public void readExternal(DataInput in) throws IOException, ClassNotFoundException
F. public void readExternal(DataInput in) throws IOException
a) A, B b) D, E c) A, C d) D, F
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) A, C These methods are called when the Object is to be serialized/de-serialzied. We can use the write methods for primitive values or the writeObject method for objects, strings and arrays of the ObjectOutput interface. Similarly, we can use the read methods for primitive values or the readObject method for objects, strings and arrays of the ObjectInput interface. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.
Correct! A, C These methods are called when the Object is to be serialized/de-serialzied. We can use the write methods for primitive values or the writeObject method for objects, strings and arrays of the ObjectOutput interface. Similarly, we can use the read methods for primitive values or the readObject method for objects, strings and arrays of the ObjectInput interface. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.
7. For the following :
public class Test {
public static void main(String... args) {
System.out.print("Test");
Test.InnerTest it = new Test.InnerTest();
it.main("hi");
}
private static class InnerTest {
public static void main(String... args) {
System.out.println("InnerTest");
}
}
}
a) Gives output: Test b) Compile Error c) Gives output: Test InnerTest d) Runtime error
Wrong! Try again.
Wrong Again! Try one more chance.
Wrong! The correct answer is c) Gives output: Test InnerTest