Welcome to the Java OOPs Coding Quiz. In this quiz, we present 20 coding MCQ questions to test your coding knowledge on the Java OOPs topic. Each question has a correct and brief explanation.
1. What is the output of the following Java code snippet?
class Animal {
String sound() {
return "Some sound";
}
}
class Dog extends Animal {
String sound() {
return "Bark";
}
}
public class Test {
public static void main(String args[]) {
Animal myDog = new Dog();
System.out.println(myDog.sound());
}
}
Answer:
Explanation:
This is an example of method overriding. The sound() method in Dog overrides the method in the Animal class, so it prints "Bark".
2. What does this Java code snippet output?
class MyClass {
private int x;
public MyClass(int x) {
this.x = x;
}
public int getX() {
return x;
}
}
public class Test {
public static void main(String args[]) {
MyClass obj = new MyClass(5);
System.out.println(obj.getX());
}
}
Answer:
Explanation:
The constructor of MyClass sets the value of x to 5, and getX() returns this value.
3. Identify the output of the following code:
abstract class Shape {
abstract void draw();
}
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();
}
}
Answer:
Explanation:
The abstract method draw() in Shape is implemented in Circle. When called, it prints "Drawing Circle".
4. What will be printed by this Java code?
interface A {
int VAL = 5;
void foo();
}
class B implements A {
public void foo() {
System.out.println("VAL in B: " + VAL);
}
}
public class Test {
public static void main(String args[]) {
B obj = new B();
obj.foo();
}
}
Answer:
Explanation:
VAL is a constant in the interface A. When B implements A, it inherits this constant. Thus, it prints "VAL in B: 5".
5. What does this code snippet output?
class Base {
public void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
}
public class Main {
public static void main(String args[]) {
Base b = new Derived();
b.show();
}
}
Answer:
Explanation:
Although b is a reference of type Base, it refers to an object of type Derived. Hence, Derived's show() is called.
6. What is the result of executing this code?
class Base {
Base() {
System.out.println("Base Constructor Called");
}
}
class Derived extends Base {
Derived() {
System.out.println("Derived Constructor Called");
}
}
public class Test {
public static void main(String args[]) {
Derived d = new Derived();
}
}
c) Base Constructor Called
Derived Constructor Called
Answer:
c) Base Constructor Called
Derived Constructor Called
Explanation:
When an instance of Derived is created, the constructor of Base is called first, followed by the constructor of Derived.
7. What will the following Java code snippet output?
class A {
void methodA() {
System.out.println("method of Class A");
}
}
class B extends A {
void methodB() {
System.out.println("method of Class B");
}
}
public class C extends B {
public static void main(String args[]) {
A a = new C();
a.methodA();
}
}
Answer:
Explanation:
The reference type A can only call methods defined in A or overridden methods in C.
8. What does the following code snippet print?
class Example {
static {
System.out.println("static block");
}
Example() {
System.out.println("constructor");
}
}
public class Test {
public static void main(String[] args) {
Example obj = new Example();
}
}
constructor
static block
Answer:
constructor
Explanation:
Static blocks are executed when the class is loaded, which is before any object creation. Therefore, it prints "static block" followed by "constructor".
9. Determine the output of this Java code:
class A {
int i = 10;
}
class B extends A {
int i = 20;
}
public class Main {
public static void main(String args[]) {
A a = new B();
System.out.println(a.i);
}
}
Answer:
Explanation:
Since the type of reference a is A, a.i refers to the i in class A, not B.
10. What is the result of the following code snippet?
class Outer {
private static String msg = "Hello World";
static class Nested {
void hello() {
System.out.println(msg);
}
}
}
public class Test {
public static void main(String args[]) {
Outer.Nested nested = new Outer.Nested();
nested.hello();
}
}
Answer:
Explanation:
Nested static classes can access private static members of the outer class.
11. What will this Java code snippet output?
interface Printable {
void print();
}
class A implements Printable {
public void print() {
System.out.println("A");
}
}
class B implements Printable {
public void print() {
System.out.println("B");
}
}
public class Test {
public static void main(String args[]) {
Printable p = new B();
p.print();
}
}
Answer:
Explanation:
The reference p of type Printable points to an object of class B, so B's print() method is called.
12. Identify the output of this code:
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
System.out.println("B");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
}
}
B
Answer:
B
Explanation:
When a B object is created, the constructor of A is called first, then the constructor of B.
13. What does this Java code snippet output?
class A {
void foo() {
System.out.println("A::foo");
}
}
class B extends A {
void foo() {
super.foo();
System.out.println("B::foo");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
b.foo();
}
}
B::foo
A::foo
Answer:
B::foo
Explanation:
The foo() method in B calls super.foo(), which invokes A's foo(), followed by its own print statement.
14. What is the output of the following Java code?
final class A {
final void show() {
System.out.println("A::show");
}
}
class B extends A {
void show() {
System.out.println("B::show");
}
}
public class Test {
public static void main(String[] args) {
B b = new B();
b.show();
}
}
Answer:
Explanation:
Class A is declared final, so it cannot be extended. Also, a final method cannot be overridden.
15. What will the following code snippet print?
class A {
void method() {
System.out.println("Method of Class A");
}
}
class B extends A {
@Override
void method() {
System.out.println("Method of Class B");
}
}
public class Test {
public static void main(String args[]) {
A obj = new B();
obj.method();
}
}
Answer:
Explanation:
The method() in class B overrides the method in class A. Since obj is an instance of B, the overridden method in B is called.
16. What is the result of executing this code?
class Person {
String name;
Person(String name) {
this.name = name;
}
}
class Employee extends Person {
Employee() {
super("Unknown");
System.out.println("Employee's name: " + name);
}
}
public class Test {
public static void main(String args[]) {
Employee e = new Employee();
}
}
Answer:
Explanation:
The Employee constructor calls the Person constructor with "Unknown" and then prints the name.
17. What will the following Java code snippet output?
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
private double radius;
Circle(double radius) {
this.radius = radius;
}
double area() {
return Math.PI * radius * radius;
}
}
public class Test {
public static void main(String args[]) {
Shape shape = new Circle(2.0);
System.out.println("Area: " + shape.area());
}
}
Answer:
Explanation:
The area() method in Circle correctly calculates the area of the circle with a radius of 2.0.
18. Identify the output of this code:
interface MyInterface {
default void show() {
System.out.println("Default Method");
}
}
class MyClass implements MyInterface {
public void show() {
System.out.println("Overridden Method");
}
}
public class Test {
public static void main(String args[]) {
MyInterface obj = new MyClass();
obj.show();
}
}
Answer:
Explanation:
MyClass overrides the default show() method of MyInterface, so the overridden version is called.
19. What does this Java code snippet output?
class A {
void display() {
System.out.println("Class A");
}
}
class B extends A {
void display() {
System.out.println("Class B");
}
}
public class Test {
public static void main(String[] args) {
A obj1 = new A();
A obj2 = new B();
obj1.display();
obj2.display();
}
}
Class A
Class B
Class B
Answer:
Class B
Explanation:
obj1 calls display() of A, while obj2 calls the overridden display() in B.
20. What is the result of the following code snippet?
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
class Inner {
void display() {
System.out.println("outer_x = " + outer_x);
}
}
}
public class Main {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Answer:
Explanation:
The Inner class has access to the members of the Outer class. When display() is called, it prints the value of outer_x from Outer.
Comments
Post a Comment
Leave Comment