Java Intermediate

0% completed

Previous
Next
Quiz
Question 1
What is inheritance in Java?
A
A mechanism where a subclass inherits properties and methods from a superclass.
B
A process of encapsulating data within a class.
C
A method to create multiple objects simultaneously.
D
A way to overload methods within a class.
Question 2
What is the purpose of the super keyword in Java?
A
To call a method in the current class.
B
To refer to the superclass and invoke its methods or constructors.
C
To declare a static variable.
D
To create a new object of the same class.
Question 3
Consider the following code snippet:
class Animal {
    String sound = "Some sound";
}
class Dog extends Animal {
    String sound = "Bark";
    void printSound() {
        System.out.println(sound);
    }
}
public class Test {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.printSound();
    }
}
What will be printed?
A
Some Sound
B
BarkSome sound
C
Compilation error due to duplicate variables.
D
Bark
Question 4
Examine the following code snippet:
class Vehicle {
    Vehicle() {
        System.out.println("Vehicle created");
    }
}
class Car extends Vehicle {
    Car() {
        super();
        System.out.println("Car created");
    }
}
public class Test {
    public static void main(String[] args) {
        Car car = new Car();
    }
}
What is the output?
A
Car created
B
Vehicle created Car created
C
Vehicle created
D
Car created Vehicle created
Question 5
How do access modifiers affect inheritance?
A
Private members are inherited but cannot be accessed directly by the subclass.
B
Public members are not inherited.
C
Protected members are only accessible in the superclass.
D
Default (package-private) members are accessible only outside the package.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next