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.
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment