אתם מתרגלים שאלה מתוך מה"ט מבני נתונים ותכנות מונחה עצמים — הנדסאי תוכנהמבחן 2025 · קיץ מועד א · שאלה 8כל שאלות המבחן ←
תכנות מונחה עצמיםפולימורפיזם ומחלקות מופשטות

נתונות המחלקות האלה: [A, B, D, Driver]. בכל אחד מהסעיפים הבאים (*****) תוחלף לקטע קוד רלוונטי.

language_variants: codeBlock:

public abstract class A
{
  protected int x;

  public A() {
   this.x = 1;
  }
  public abstract int f(int v);
  public void f(A a) {
    x = a.x;
  }
  public String toString(){
    return "x =" + this.x;
  }
} //end of class A

public class B extends A {
 public B() {
    super();
 }
 public B(int val) {
    this.x = f(val);
 }
 public int f(int val) {
    return this.x + val;
 }
 public void f(B b) {
    this.x = this.x * b.x;
 }
 public String toString(){
   return "B: " +
             super.toString();
 }
} //end of class B

public class D extends B {
  public D(int val)  {
        this.x = val - this.x;
  }
  public void f(A a)  {
    this.x = this.x + a.x + 1;
  }
  public void f(B b)  {
    this.x = this.x * b.x;
  }
  public void f(D d)  {
    this.x = d.x - 1;
  }
  public String toString(){
    return "D: " +
          super.toString();
 }

} //end of class D

public class Driver
{
  public static void main
            (String [] args){
    A a;
    B b;
    int m;
//     ************
  }
}

fieldAccessDelta: class A's field x is public in C# but protected in Java — cross-language authoring inconsistency, does not change traced output for the shown code, but is a genuine delta beyond the pilot's known taxonomy. See dualVariantFinding.newObservationsThisPaper.

semanticDeltaNote: SEMANTIC TRAP CHECKED, CONFIRMED CORRECT: every C# override in B/D is properly paired with a virtual ancestor declaration — A.F(A a) virtual → D.F(A a) override (dispatches correctly through an Employee/A-typed reference); B.F(B b) virtual (new virtual, not overriding anything in A) → D.F(B b) override; A.F(int v) abstract → B.F(int val) override (implements abstract); D.F(D d) is a plain new overload (no ancestor virtual F(D), correctly has no override keyword). Java's implicit overriding on matching signatures reproduces the identical dispatch outcome — no virtual/override mismatch found in this question.

סעיף א

מהו הפלט של קטע הקוד שלפניכם:

codeSnippet:

m = 11;
a = new B(m);
System.out.println(a);
a = new D(m);
System.out.println(a);
סעיף ב

codeSnippet:

b = new B(3);
m = b.f(4);
System.out.println(m);
m = b.f(m);
System.out.println(m);
סעיף ג

codeSnippet:

a = new B(5);
int m = a.f(a.f(2));
System.out.println(m);

note: Java re-declares int m here even though Driver.main's shared template already declares int m; at the top — a literal duplicate local-variable declaration (Java compile error as printed). C# reuses the existing m without re-declaring. See dualVariantFinding.newObservationsThisPaper.

סעיף ד

codeSnippet:

a = new D(10);
b = new B(20);
a.f(b);
System.out.println(a);
סעיף ה

אחרי ביצוע קטע הקוד הבא, הערך של התכונה x של העצם d יהיה 5. מהו הערך המשתנה של m?

codeSnippet:

D d = new D(m);
b = new B();
((A)d).f(b);
שאלות ותגובות על השאלה

🎓 לא הבנתם משהו? קבלו הסבר נוסף ממרצה לתכנות

שאלו כאן — ותקבלו מענה מוסמך.

🎓 מרצה לתכנות עונה כאן — תקבלו מענה מקצועי