Java Find Output Program Questions and Answers - OOPs Concepts

Here are 10 MCQ Java code snippets related to object-oriented programming (OOP) principles, including inheritance, polymorphism, and encapsulation, with some tricky programs.

These output programs demonstrate different aspects of Java OOP, including class hierarchy, method overriding, abstract classes, interfaces, and instance variables.

1. What will be the output of the following Java code?

class Animal {
    String name = "Animal";
    void makeSound() {
        System.out.println("Some sound");
    }
}
class Dog extends Animal {
    String name = "Dog";
    void makeSound() {
        System.out.println("Bark");
    }
}
public class Test {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        System.out.println(myDog.name);
        myDog.makeSound();
    }
}
a) Dog Bark
b) Animal Bark
c) Dog Some sound
d) Animal Some sound

2. What will be the output of the following Java code?

class Base {
    Base() {
        System.out.print("Base ");
    }
}
class Derived extends Base {
    Derived() {
        System.out.print("Derived ");
    }
}
public class Main {
    public static void main(String[] args) {
        new Derived();
    }
}
a) Base Derived
b) Derived Base
c) Derived
d) Base

3. What will be the output of the following Java code?

class Shape {
    void draw() {
        System.out.println("Drawing Shape");
    }
}
class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}
public class Test {
    public static void main(String[] args) {
        Shape shape = new Circle();
        shape.draw();
    }
}
a) Drawing Shape
b) Drawing Circle
c) Compilation error
d) Runtime error

4. What will be the output of the following Java code?

class A {
    static void method() {
        System.out.println("Class A");
    }
}
class B extends A {
    static void method() {
        System.out.println("Class B");
    }
}
public class Test {
    public static void main(String[] args) {
        A obj = new B();
        obj.method();
    }
}
a) Class A
b) Class B
c) Compilation error
d) Runtime error

5. What will be the output of the following Java code?

class Parent {
    private void display() {
        System.out.println("Parent display()");
    }
}
class Child extends Parent {
    void display() {
        System.out.println("Child display()");
    }
}
public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        // obj.display();  // This line is commented because it won't compile
    }
}
a) Parent display()
b) Child display()
c) Compilation error
d) No output

6. What will be the output of the following Java code?

abstract class Device {
    abstract void turnOn();
}
class Phone extends Device {
    void turnOn() {
        System.out.println("Phone turned on");
    }
}
public class Test {
    public static void main(String[] args) {
        Device myDevice = new Phone();
        myDevice.turnOn();
    }
}
a) Phone turned on
b) Device turned on
c) Compilation error
d) Runtime error

7. What will be the output of the following Java code?

interface Printable {
    default void print() {
        System.out.println("Printing");
    }
}
class Document implements Printable {}
public class Test {
public static void main(String[] args) {
    Printable doc = new Document();
    doc.print();
}
}
a) Printing
b) Compilation error
c) No output
d) Runtime error

8. What will be the output of the following Java code?

class Counter {
    int count = 0;
    Counter() {
        count++;
    }
}
public class Test {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        System.out.println(c1.count + " " + c2.count);
    }
}
a) 1 1
b) 2 2
c) 1 2
d) 2 1

9. What will be the output of the following Java code?

class Account {
    private String name = "Default Account";
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
public class Test {
    public static void main(String[] args) {
        Account acc = new Account();
        acc.setName("Personal Account");
        System.out.println(acc.getName());
    }
}
a) Default Account
b) Personal Account
c) Null
d) Compilation error

10. What will be the output of the following Java code?

class Person {
    String name;
    int age;
    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    void display() {
        System.out.println(name + " is " + age + " years old.");
    }
}
public class Test {
    public static void main(String[] args) {
        Person p = new Person("John", 25);
        p.display();
    }
}
a) John is 25 years old.
b) John is 0 years old.
c) null is 25 years old.
d) Compilation error

Comments