professor-curious-logo

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

Flip to see [answer/question]
Flip to see [answer/question]

All Flashcards

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();
 }
}

Phone is powering on

What is the significance of static type in method calling?

Determines which method is called for static methods.

What is the significance of dynamic type in method calling?

Determines which method is called for non-static methods.

How does inheritance relate to polymorphism?

Inheritance allows subclasses to be treated as instances of their superclass, enabling polymorphism.

Explain the concept of method overriding in polymorphism.

Subclasses provide specific implementations of methods already defined in their superclass, allowing different behavior for the same method call.

Why is understanding static and dynamic types important for polymorphism?

It clarifies which version of a method will be executed at runtime, especially in inheritance hierarchies.

Explain the difference between compile-time and runtime polymorphism.

Compile-time polymorphism (static binding) is resolved during compilation, while runtime polymorphism (dynamic binding) is resolved during execution.

What is the role of the @Override annotation?

It indicates that a method is overriding a method in the superclass and helps prevent errors.

What is the Liskov Substitution Principle?

Subtypes must be substitutable for their base types without altering the correctness of the program.

How does polymorphism increase code flexibility?

Allows writing code that can work with objects of different classes in a uniform way.

Explain the concept of 'is-a' relationship in inheritance.

A subclass 'is-a' type of its superclass, meaning it inherits its properties and behaviors.

What is the definition of static type?

The type a variable is declared as.

What is the definition of dynamic type?

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.