📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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();
}
}
Base
Derived
DeriDerived
Derived
DeriDerived
DeriDerived
Derived
Base
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 ");
}
}
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
}
}
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
}
}
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();
}
}
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
a) Base
Derived
DeriDerived
Q2
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.
Q3
d) The program will result in an exception (ClassCastException).
Q4
c) The program prints the following: Base:print.
Q5
d) This program throws a NullPointerException.
Q6
c) has-a
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