📘 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
These questions may be asked in interviews, or similar questions may appear in interviews, so prepare yourself.
Learn OOPs at OOPs Concepts in Java with Realtime Examples
Tricky Java Coding Programs | Part 1 - OOP Concepts | Tricky Java Programming/Coding Questions
Tricky Java Coding Programs | Part 2 - OOP Concepts | Tricky Java Programming/Coding Questions
Tricky Java Coding Programs | Part 3 - OOP Concepts | Tricky Java Programming/Coding Questions
1. What is the output of the following Java program?
class Automobile {
private String drive() {
return "Driving vehicle";
}
}
class Car extends Automobile {
protected String drive() {
return "Driving car";
}
}
public class ElectricCar extends Car {
@Override
public final String drive() {
return "Driving an electric car";
}
public static void main(String[] wheels) {
final Car car = new ElectricCar();
System.out.print(car.drive());
}
}
B. Driving an electric car
C. Driving car
D. The code does not compile
Answer :
Explanation:
2. 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");
}
}
b) public and protected both can be used.
c) public, protected, and private can be used.
d) Only the public can be used.
Answer :
B. public and protected both can be used.Explanation:
You can provide only a less restrictive or same-access modifier when overriding a method.3. What will be the output of the following Java 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
Answer:
Base
Derived
DeriDerived
Explanation:
4. What is the output of the following Java program?
class One{
public One(){
System.out.print("One,");
}
}
class Two extends One{
public Two(){
System.out.print("Two,");
}
}
class Three extends Two{
public Three(){
System.out.print("Three");
}
}
public class Test{
public static void main(String[] args){
Three three = new Three();
}
}
Answer:
c) One,Two,Three
Explanation:
5. 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 ");
}
}
a) Removing Stmt-1 will make the program compilable and it will print the following: Base Derived.
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.
e) Removing both Stmt-1 and Stmt-2 will make the program compilable and it will print the following: Base: Hello Derived.
Answer:
Explanation:
6. What is the output of the following Java program?
abstract class Car {
static {
System.out.print("1");
}
public Car(String name) {
super();
System.out.print("2");
}
{
System.out.print("3");
}
}
public class BlueCar extends Car {
{
System.out.print("4");
}
public BlueCar() {
super("blue");
System.out.print("5");
}
public static void main(String[] gears) {
new BlueCar();
}
}
b) 12354
c) 13245
d) The code does not compile.
Answer:
Explanation:
7. What is the output of the following Java program?
class Math {
public final double secret = 2;
}
class ComplexMath extends Math {
public final double secret = 4;
}
public class InfiniteMath extends ComplexMath {
public final double secret = 8;
public static void main(String[] numbers) {
Math math = new InfiniteMath();
System.out.print(math.secret);
}
}
B. 4
C. 8
D. The code does not compile.
Answer:
Explanation:
8. What is the output of the following Java program?
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);
}
}
b) long
c) Integer
d) int
Answer:
Explanation:
9. What is the output of the following Java program?
class One{
public static void print(){
System.out.println("1");
}
}
class Two extends One{
public static void print(){
System.out.println("2");
}
}
public class Test{
public static void main(String args[]){
One one = new Two();
one.print();
}
}
Answer:
b) 1
Explanation:
10. What is the output of the following Java program?
class Parent{
public void className(){
System.out.println("Parent");
}
}
class Child extends Parent{
void className(){
System.out.println("Child");
}
}
public class Test{
public static void main(String[] args){
Parent parent = new Child();
parent.className();
}
}
Answer:
c) Compile-time error
Explanation:
11. What is the output of the following Java program?
class Demo{
public Demo(int i){
System.out.println("int");
}
public void Demo(short s){
System.out.println("short");
}
}
public class Test{
public static void main(String[] args){
short s = 10;
Demo demo = new Demo(s);
}
}
Answer:
a) int
Comments
Post a Comment
Leave Comment