Question

1.
2.
3.
4.
Answer (Detailed Solution Below)

Option 1 : 10 25 / 10 25

Advertisement

Detailed Solution

सही उत्तर 10 25 / 10 25 है।

Key Points
Helpful?
PrepSnapprepsnap.in • Part -A

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

1.
10 25 / 10 25
2.
20 25 / 20 25
3.
10 15 / 10 15
4.
20 20 / 20 20

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 देता है।

Downloaded from PrepSnap (prepsnap.in)