📘 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
Check out Part 1 - Java OOPS Quiz - Java Interview OOPS Programs - Part 1The answer and explanation of each question have given at the end of each question.
Q1 - Consider the following program:
public class BaseClass {
private void foo() {
System.out.println("In BaseClass.foo()");
}
void bar() {
System.out.println("In BaseClass.bar()");
}
public static void main(String[] args) {
BaseClass po = new DerivedClass();
po.foo(); // BASE_FOO_CALL
po.bar();
}
}
class DerivedClass extends BaseClass {
void foo() {
System.out.println("In Derived.foo()");
}
void bar() {
System.out.println("In Derived.bar()");
}
}
This program results in a compiler error in the line marked with the comment BASE_FOO_CALL.
In BaseClass.foo()
In BaseClass.bar()
In BaseClass.foo()
In Derived.bar()
In Derived.foo()
In Derived.bar()
Answer
Derived
Derived
Q2 - Consider the following program:
public class Overloaded {
public static void foo(Integer i) {
System.out.println("foo(Integer)");
}
public static void foo(short i) {
System.out.println("foo(short)");
}
public static void foo(long i) {
System.out.println("foo(long)");
}
public static void foo(int... i) {
System.out.println("foo(int ...)");
}
public static void main(String[] args) {
foo(10);
}
}
Answer
c) foo(long)
Q3 - What will be the output of this program?
class Color {
int red, green, blue;
void Color() {
red = 10;
green = 10;
blue = 10;
}
void printColor() {
System.out.println("red: " + red + " green: " + green + " blue: " + blue);
}
}
public class Test {
public static void main(String[] args) {
Color color = new Color();
color.printColor();
}
}
Answer
B. Compiles without errors, and when run, it prints the following: red: 0 green: 0 blue: 0.
Q4 - Look at the following code and choose the right option for the word :
// Shape.java
public class Shape {
protected void display() {
System.out.println("Display-base");
}
}
// Circle.java
public class Circle extends Shape { <
< access - modifier > void display() {
System.out.println("Display-derived");
}
}
Answer
B. public and protected both can be used.
Comments
Post a Comment
Leave Comment