Question
What will be the output of the following Java snippet?
class A { Â Â Â public void display() { Â Â Â Â Â Â Â System.out.println("Class A"); Â Â Â }} class B extends A { Â Â Â public void display() { Â Â Â Â Â Â Â System.out.println("Class B"); Â Â Â }} public class Test { Â Â Â public static void main(String[] args) { Â Â Â Â Â Â Â A obj = new B(); Â Â Â Â Â Â Â obj.display(); Â Â Â }}Solution
This program demonstrates runtime polymorphism in Java through method overriding. When the method display() is called on the object obj, the overridden method in class B is executed. This is because obj is instantiated as new B(), and at runtime, the JVM determines the appropriate method to execute based on the actual type of the object. Thus, even though obj is declared as type A, the overridden display() method in B is executed, producing the output "Class B" . This behavior is a key feature of Java's dynamic method dispatch mechanism. Explanation of Incorrect Options: A) Class A : This would be true if the display() method in class B was not overridden. However, since B overrides A's method, this option is incorrect. C) Compilation Error : The code is syntactically correct, so it compiles without any issues. D) Runtime Error : The program runs successfully, as all method calls are valid and properly resolved at runtime. E) None of the above : This is incorrect, as the correct output is explicitly "Class B" .
Which of the following best differentiates between server-based virtualization and hypervisor-based virtualization?
Which of the following is true about triggers in a relational database?
In the context of asymmetric encryption, which of the following is a key feature of public-private key pairs?
What is the primary purpose of an Entity-Relationship (ER) Diagram in database design?
What is the time complexity of searching an element in a balanced binary search tree (BST) with nnn nodes?
A code flow involves processing a stream of data where elements are added to the front and removed from the front. Which type of linked list would provi...
Which design pattern is most suitable for controlling access to a resource by limiting the number of clients that can use it concurrently?
 What is the maximum number of nodes in a binary tree of height 'h' (where root is at height 0)?
Union-Find with Path Compression achieves nearly:
Which data structure is used for undo operations in text editors?