In part 1, I have posted a few useful Java OOPS programming questions and answers (code snippets with output). This post is a continuation of Part 1, In this post, I included a few more 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 each question). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.
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()");
}
}
Which one of the following options correctly describes the behavior of this program?
a)
This program results in a compiler error in the line marked with the comment BASE_FOO_CALL.
b) This program prints the following:
In BaseClass.foo()
In BaseClass.bar()
c) This program prints the following:
In BaseClass.foo()
In Derived.bar()
d) This program prints the following:
In Derived.foo()
In Derived.bar()
Answer
a)
Derived
Derived
Explanation: The dynamic type of the instance variable obj2 remains the same (i.e., Derived). Thus, when print() is called on obj2, it calls the derived class version of the method.
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);
}
}
Which one of the following options correctly describes the output of this program? a) foo(Integer)
b) foo(short)
c) foo(long)
d) foo(int ...)
Answer
c) foo(long)
Explanation: For an integer literal, the JVM matches in the following order: int, long, Integer, int.... In other words, it first looks for an int type parameter; if it is not provided, then it looks for long type; and so on. Here, since the int type parameter is not specified with an overloaded method, it matches with 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();
}
}
A. Compiler error: no constructor provided for the class.
B. Compiles without errors, and when run, it prints the following: red: 0 green: 0 blue: 0.
C. Compiles without errors, and when run, it prints the following: red: 10 green: 10 blue: 10.
D. Compiles without errors, and when run, crashes by throwing NullPointerException.
Answer
B. Compiles without errors, and when run, it prints the following: red: 0 green: 0 blue: 0.
Explanation - Note that a constructor does not have a return type; if a return type is provided, it is treated as a method in that class. In this case, since Color had void return type, it became a method named Color() in the Color class, with the default Color constructor provided by the compiler. By default, data values are initialized to zero, hence the output.)
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");
}
}
a. Only protected can be used.
B. public and protected both can be used.
C. public, protected, and private can be used.
d. Only public can be used.
Answer
B. public and protected both can be used.
(You can provide only a less restrictive or same-access modifier when overriding a method.)
Let me know if this post is helpful so that I will prepare such articles/posts for you.
Comments
Post a Comment
Leave Comment