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

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

language_variants: codeBlock:

public abstract class A {
  public int x;

  public A() {
   this.x = 1;
  }
  public abstract int F(int v);

  public virtual void F(A a) {
    x = a.x;
  }

  public override
        string ToString(){
    return "x =" + this.x;
  }
} //end of class A

public class B : A {
 public B():base() {
 }
 public B(int val):base(){
    this.x = F(val);
 }
 public override int F(int val){
    return this.x + val;
 }
 public virtual void F(B b){
    this.x = this.x * b.x;
 }
 public override string ToString(){
   return "B: " + base.ToString();
 }
} //end of class B

public class D : B {
  public D(int val):base(){
        this.x = val - this.x;
  }
  public override void F(A a){
    this.x = this.x + a.x + 1;
  }
  public override void F(B b){
    this.x = this.x * b.x;
  }
  public void F(D d)  {
    this.x = d.x - 1;
  }
 public override string ToString(){return "D: " + base.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);
Console.WriteLine(a);
a = new D(m);
Console.WriteLine(a);
סעיף ב

codeSnippet:

b = new B(3);
m = b.F(4);
Console.WriteLine(m);
m = b.F(m);
Console.WriteLine(m);
סעיף ג

codeSnippet:

a = new B(5);
m = a.F(a.F(2));
Console.WriteLine(m);
סעיף ד

codeSnippet:

a = new D(10);
b = new B(20);
a.F(b);
Console.WriteLine(a);
סעיף ה

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

codeSnippet:

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

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

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

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