In this post, I have included a few useful Java OOPS programming questions and answers (code snippets with output). I suggest you guys try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.
The answer and explanation of each question have given at end of this post.
The answer and explanation of each question have given at end of this post.
Check out Part 2 - Java OOPS Quiz - Java Interview OOPS Programs - Part 2.
Q1 - What will be the output of the following program?
class Base {
public Base() {
System.out.println("Base");
}
}
class Derived extends Base {
public Derived() {
System.out.println("Derived");
}
}
class DeriDerived extends Derived {
public DeriDerived() {
System.out.println("DeriDerived");
}
}
public class Test {
public static void main(String[] args) {
Derived b = new DeriDerived();
}
}
a)
Base
Derived
DeriDerived
b)
Derived
DeriDerived
c)
DeriDerived
Derived
Base
d)
DeriDerived
Derived
Q2 - Consider the following program:
class Base {
public Base() {
System.out.print("Base ");
}
public Base(String s) {
System.out.print("Base: " + s);
}
}
class Derived extends Base {
public Derived(String s) {
super(); // Stmt-1
super(s); // Stmt-2
System.out.print("Derived ");
}
}
class Test {
public static void main(String[] args) {
Base base = new Derived("Hello ");
}
}
Select three correct options from the following list:
a) Removing Stmt-1 will make the program compilable and it will print the following: Base Derived.
b) Removing Stmt-1 will make the program compilable and it will print the following: Base: Hello Derived.
c) Removing Stmt-2 will make the program compilable and it will print the following: Base Derived.
d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base Derived.
e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base: Hello Derived.
Q3 - Consider the following program and choose the correct option from the list of options:
class Base {
public void test() {
}
}
class Base1 extends Base {
public void test() {
System.out.println("Base1");
}
}
class Base2 extends Base {
public void test() {
System.out.println("Base2");
}
}
class Test {
public static void main(String[] args) {
Base obj = new Base1();
((Base2) obj).test(); // CAST
}
}
a) The program will print the following: Base1.
b) The program will print the following: Base2.
c) The compiler will report an error in the line marked with comment CAST.
d) The program will result in an exception (ClassCastException).
Q4 - Consider the following program and predict the behavior of this program:
class Base {
public void print() {
System.out.println("Base:print");
}
}
abstract class Test extends Base { // #1
public static void main(String[] args) {
Base obj = new Base();
obj.print(); // #2
}
}
a) Compiler error “an abstract class cannot extend from a concrete class” at statement #1.
b) Compiler error “cannot resolve call to print method” at statement #2.
c) The program prints the following: Base:print.
d) The program will throw a runtime exception of AbstractClassInstantiationException.
Q5 - Consider the following program:
class SuperClass {
SuperClass() {
foo();
}
public void foo() {
System.out.println("In SuperClass.foo()");
}
}
class SubClass extends SuperClass {
private String member;
public SubClass() {
member = "HI";
}
public void foo() {
System.out.println("In SubClass.foo(): " + member.toLowerCase());
}
}
public class Test {
public static void main(String[] args) {
SuperClass reference = new SubClass();
reference.foo();
}
}
This program prints the following:
a) In SuperClass.foo()
b) In Derived.foo(): hi
c) In SuperClass.foo() In Derived.foo(): hi
d) This program throws a NullPointerException.
Q6 - Which one of the following relationships describes the OO design concept of “composition”?
a) is-a
b) is-a-kind-of
c) has-a
d) is-implemented-in-terms-of
e) composed-as
Answers
Q1
Answer :
a) Base
Derived
DeriDerived
Explanation: Whenever a class gets instantiated, the constructor of its base classes (the constructor of the root of the hierarchy gets executed first) gets invoked before the constructor of the instantiated class.
Q2
Answer:
b) Removing Stmt-1 will make the program compilable and it will print the following:
Base: Hello Derived.
c) Removing Stmt-2 will make the program compilable and it will print the following:
Base Derived.
d) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print
the following: Base Derived.
Explanation: If you remove Stmt-1, a call to super(s) will result in printing Base: Hello, and then the constructor of the Derived class invocation will print Derived. Similarly, the removal of Stmt-2 will also produce the correct program. In fact, if you remove both these statements, you will also get a compilable program.
Q3
Answer:
d) The program will result in an exception (ClassCastException).
Explanation: The dynamic type of variable obj is Base1 that you were trying to cast into Base2. This is not supported and so results in an exception.
Q4
Answer:
c) The program prints the following: Base:print.
Explanation: It is possible for an abstract class to extend a concrete class (though such inheritance often doesn’t make much sense). Also, an abstract class can have static methods. Since you don’t need to create an object of a class to invoke a static method in that class, you can invoke the main() method defined in an abstract class.
Q5
Answer:
d) This program throws a NullPointerException.
Explanation: In this program, the SuperClass constructor calls the method foo() that is overridden in the derived class. Thus, in this program, since the SubClass object is created, the call to the SuperClass constructor will result in calling the method SubClass.foo().
When the derived class object is created, first the base class constructor is called, followed by the call to the derived class constructor. Note that the member variable is initialized only in the derived class constructor. Thus, when the base class constructor executes, the derived class constructor has not initialized the member variable to “HI” yet. So this program results in a NullReferenceException.
Q6
Answer:
c) has-a
Composition is a design concept that refers to the has-a relationship.
Check out Part 2 - Java OOPS Quiz - Java Interview OOPS Programs - Part 2.
Related Posts
Java Multithreading Quiz - Multiple Choice Questions
Java OOPS Quiz - Java Interview OOPS Programs - Part 2.
Comments
Post a Comment
Leave Comment