The type of the constructor call when an object is created.
What is method overriding?
Redefining a method in a subclass that is already defined in its superclass.
What is a superclass?
A class from which other classes inherit.
What is a subclass?
A class that inherits from another class (superclass).
What is static method?
A method that belongs to the class itself rather than to any specific instance.
What is non-static method?
A method that requires an object of its class to be called.
What is inheritance?
A mechanism where a new class inherits properties and behaviors from an existing class.
What is polymorphism?
The ability of an object to take on many forms.
What is the difference between static and dynamic binding?
Static binding is resolved at compile time, dynamic at runtime.
What are the differences between static and dynamic method dispatch?
Static dispatch: Determined at compile time, faster. Dynamic dispatch: Determined at runtime, more flexible.
What are the differences between method overloading and method overriding?
Overloading: Multiple methods with the same name but different parameters in the same class. Overriding: A method in a subclass with the same name and parameters as a method in its superclass.
What is the difference between inheritance and polymorphism?
Inheritance: Mechanism for code reuse and creating 'is-a' relationships. Polymorphism: Ability of an object to take on many forms, often achieved through inheritance.
What is the difference between an abstract class and an interface?
Abstract Class: Can have concrete methods and instance variables. Interface: Can only have abstract methods and constants (until Java 8).
What are the differences between early and late binding?
Early Binding: Method call is resolved at compile time. Late Binding: Method call is resolved at runtime.
What are the differences between compile-time and runtime errors?
Compile-time errors: Detected during compilation. Runtime errors: Occur during program execution.
What is the difference between a class and an object?
Class: Blueprint for creating objects. Object: Instance of a class.
What is the difference between a local variable and an instance variable?
Local variable: Declared inside a method. Instance variable: Declared inside a class but outside any method.
What are the differences between procedural and object-oriented programming?
Procedural: Focuses on procedures/functions. Object-oriented: Focuses on objects and their interactions.
What is the difference between composition and inheritance?
Composition: 'Has-a' relationship, objects contain other objects. Inheritance: 'Is-a' relationship, classes inherit from other classes.
What does the following code output?
java
public class A {
public static void callOne() {
System.out.println("1");
}
}
public class B extends A {
public static void callOne() {
System.out.println("3");
}
}
public class Main {
public static void main(String[] args) {
A a = new B();
a.callOne();
}
}
1
What does the following code output?
java
public class A {
public void callTwo() {
System.out.println("2");
}
}
public class B extends A {
@Override
public void callTwo() {
System.out.println("4");
}
}
public class Main {
public static void main(String[] args) {
A a = new B();
a.callTwo();
}
}
4
What does the following code output?
java
class Animal {
public void makeSound() {
System.out.println("Generic animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("Woof!");
}
}
public class Main {
public static void main(String[] args) {
Animal myAnimal = new Dog();
myAnimal.makeSound();
}
}
Woof!
What does the following code output?
java
class Vehicle {
public void start() {
System.out.println("Vehicle starting");
}
}
class Car extends Vehicle {
@Override
public void start() {
System.out.println("Car starting");
}
}
public class Main {
public static void main(String[] args) {
Vehicle myVehicle = new Vehicle();
Vehicle myCar = new Car();
myVehicle.start();
myCar.start();
}
}
Vehicle starting
Car starting
What does the following code output?
java
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape1 = new Shape();
Shape shape2 = new Circle();
shape1.draw();
shape2.draw();
}
}
Drawing a shape
Drawing a circle
What does the following code output?
java
class Employee {
public void displaySalary() {
System.out.println("Employee salary");
}
}
class Manager extends Employee {
@Override
public void displaySalary() {
System.out.println("Manager salary");
}
}
public class Main {
public static void main(String[] args) {
Employee emp = new Manager();
emp.displaySalary();
}
}
Manager salary
Identify the error in the following code:
java
class Parent {
public void show() {
System.out.println("Parent's show()");
}
}
class Child extends Parent {
public void display() {
System.out.println("Child's display()");
}
}
public class Main {
public static void main(String[] args) {
Parent obj = new Parent();
obj.display();
}
}
The Parent class does not have a display() method. It is defined only in the Child class.
What does the following code output?
java
class Base {
public void printMessage() {
System.out.println("Base class message");
}
}
class Derived extends Base {
@Override
public void printMessage() {
System.out.println("Derived class message");
}
}
public class Main {
public static void main(String[] args) {
Base obj = new Derived();
obj.printMessage();
}
}
Derived class message
What does the following code output?
java
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
class Herbivore extends Animal {
@Override
public void eat() {
System.out.println("Herbivore is eating plants");
}
}
public class Main {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Herbivore();
animal1.eat();
animal2.eat();
}
}
Animal is eating
Herbivore is eating plants
What does the following code output?
java
class Device {
public void powerOn() {
System.out.println("Device is powering on");
}
}
class Phone extends Device {
@Override
public void powerOn() {
System.out.println("Phone is powering on");
}
}
public class Main {
public static void main(String[] args) {
Device myDevice = new Phone();
myDevice.powerOn();
}
}