Question
Option 1 : 10 25 / 10 25
Detailed Solution
सही उत्तर 10 25 / 10 25 है।
Question:
What is the output of the following Java program involving field hiding and dynamic method dispatch? / फ़ील्ड हाइडिंग और गतिशील मेथड डिस्पैच वाले निम्नलिखित Java प्रोग्राम का आउटपुट क्या होगा? class Base { int x = 10; int get() { return x; } } class Derived extends Base { int x = 20; int get() { return x + 5; } } public class Test { public static void main(String[] args) { Base obj = new Derived(); System.out.println(obj.x + " " + obj.get()); } }
Correct Answer: 10 25 / 10 25
Fields are resolved by the reference type, so obj.x is Base's 10; methods are dynamically dispatched, so obj.get() calls Derived's version, returning 20 + 5 = 25. / फ़ील्ड संदर्भ प्रकार से तय होते हैं, अतः obj.x बेस का 10 है; मेथड गतिशील रूप से बँधते हैं, अतः obj.get() डेराइव्ड संस्करण चलाता है, जो 20 + 5 = 25 देता है।