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

נתונות המחלקות A, B, C הבאות:

language_variants: codeBlock:

public class A
{
    private static int countA = 0;
    protected int x;
    public A()
      {
            countA++;
            this.x = 0;
    }
    public A(int x)
      {
            countA++;
            this.x = x;
    }
    public String toString()
      {
            return "A:"+this.x;
    }
    public int doIt(int z)
      {
            return this.x * z;
    }
    public static int getCount(){ return countA;  }
}// end of class A

public class B extends A
{
    protected int y;
    private static int countB = 0;
    public B(int y)
      {
            this.y = y;
            countB++;
    }
    public B(int x, int y)
      {
            super(x);
            countB++;
            this.y = y;
    }
    public String toString()
      {
        return "B:"+super.toString()+":"+this.y;
    }
    public static int getCount(){return countB;}
    public int doIt(int z)
      {
            return super.doIt(z) + this.y;
     }
} //end of class B
public class C extends B
{
    private A myA;
    private static int countC = 0;
    public C(int x, int y)
      {
        super(x, y);
        this.myA = new A(x+y);
        countC++;
    }
    public static int getCount(){return countC;}
    public String toString()
      {
        return "C:"+super.toString()+" ["+this.myA+"]";
    }
}// end of class C

semanticDeltaNote: TWO real (not cosmetic) C#-only language mechanics on display here, both directly relevant to this exam corpus's known trap class: (1) ToString() needs override starting IN CLASS A ITSELF (not just in B/C) because it overrides System.Object.ToString() which is already virtual on every C# class by inheritance — verified present and correctly placed (public override string ToString() in A, B, and C). DoIt needs the more familiar pattern: virtual declared in A, override in B (C inherits B's override without redeclaring) — also verified present and correctly placed, so polymorphic arr[k].DoIt(k+1) dispatch (part ה) will resolve to the runtime type's override in C#, matching Java's implicit-override behavior for doIt. (2) GetCount() is STATIC, so it cannot be virtual/overridden at all — C# instead requires the new keyword in B and C to explicitly hide the base class's same-named static member (public static new int GetCount()), verified present in both B and C; Java requires no equivalent keyword for static-method hiding. This is a DISTINCT trap from virtual/override (hiding vs. overriding) and matters for part ג (A.GetCount()/B.GetCount()/C.GetCount() are static calls through the class name, unaffected by hiding-vs-overriding) vs part ה (arr[k].DoIt(k+1) is an instance call through a runtime-polymorphic reference, where virtual/override IS the operative mechanism). Flag for Stage-4 solve: run the code-oracle independently per language, do not assume trace-transfer, and do not conflate the hiding trap (GetCount) with the overriding trap (DoIt/ToString) when writing evaluation guidelines.

סעיף א

ציירו תרשים UML שמייצג את הקשרים בין המחלקות A, B, C. יש לסמן ירושה באמצעות חץ והכלה באמצעות סימן.

סעיף ב

נתון קטע הקוד הבא, בפעולה הראשית במחלקה אחרת. עקבו אחרי ביצוע קטע הקוד. יש לצייר את כל העצמים שנוצרו ולציין את התכונות שלהם.

setupCode:

A[] arr = new A[5];
arr[0] = new A(13);
arr[1] = new B(5, 10);
arr[2] = new C(2, 7);
arr[3] = new C(3, 3);
arr[4] = new B(5);
סעיף ג

לקוד של סעיף ב' התוסף הבא, רשמו מה יהיה הפלט בהתאם:

codeBlock:

System.out.println("A objects: " + A.getCount());
System.out.println("B objects: " + B.getCount());
System.out.println("C objects: " + C.getCount());
סעיף ד

לקוד של סעיף ב' התוסף הבא, רשמו מה יהיה הפלט:

codeBlock:

for (int k=0; k < arr.length; k++)
 System.out.println(arr[k]);
סעיף ה

codeBlock:

for (int k=0; k <arr.length; k++)
 if(arr[k] instanceof B)
     System.out.println(arr[k].doIt(k+1));
שאלות ותגובות על השאלה

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

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

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