📘 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.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
The answer and explanation of each question have been given at end of this post.
YouTube Video
Q1 - Consider the following program and predict the output:
public class Test {
public void print(Integer i) {
System.out.println("Integer");
}
public void print(int i) {
System.out.println("int");
}
public void print(long i) {
System.out.println("long");
}
public static void main(String args[]) {
Test test = new Test();
test.print(10);
}
}
Q2 - What is the output of the following program?
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
s2 = s2.intern();
System.out.println(s1 == s2);
}
}
Q3 - 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
Q4 - 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);
}
}
Q5 - 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");
}
}
Q6 - 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()
Q7 - Consider the following program and predict the output:
class MyThread extends Thread {
@Override
public void run() {
System.out.println("In run method; thread name is: " + Thread.currentThread().getName());
}
}
public class ThreadTest {
public static void main(String args[]) {
Thread myThread = new MyThread();
myThread.run(); // #1
System.out.println("In main method; thread name is: " + Thread.currentThread().getName());
}
}
Q8 - 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
}
}
Q9 - Consider the following program:
public class StrEqual {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("hello");
String s3 = "hello";
if (s1 == s2) {
System.out.println("s1 and s2 equal");
} else {
System.out.println("s1 and s2 not equal");
}
if (s1 == s3) {
System.out.println("s1 and s3 equal");
} else {
System.out.println("s1 and s3 not equal");
}
}
}
s1 and s2 equal
s1 and s3 equal
s1 and s2 equal
s1 and s3 not equal
s1 and s2 not equal
s1 and s3 equal
s1 and s2 not equal
s1 and s3 not equal
Q10 - Consider the following program and predict the output:
public class Test {
public static void main(String[] args) {
String s = new String("5");
System.out.println(1 + 10 + s + 1 + 10);
}
}
Q1
d) int
Q2
b) true
Q3
Base
Derived
DeriDerived
Q4
c) foo(long)
Q5
B. public and protected both can be used.
Q6
In BaseClass.foo()
In Derived.bar()
Q7
c) The program prints the following:
In run method; thread name is: main
In main method; thread name is: main
Q8
d) The program will result in an exception (ClassCastException).
Q9
s1 and s2 not equal
s1 and s3 equal
Q10
c) 115110
Related Posts
Java Multithreading Quiz - Multiple Choice Questions
Java OOPS Quiz - Java Interview OOPS Programs - Part 1.
Java OOPS Quiz - Java Interview OOPS Programs - Part 2.
Comments
Post a Comment
Leave Comment