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

לפניכם הכותרות של מחלקות Two, One והתכונות שלהן והמחלקה Test הכוללת פעולה ראשית.

language_variants: codeBlock:

public class One
{
 protected int val;
 ....
}
public class Two extends One
{
  private String s;
  private One p;
  ...
}
public class Test
{
  public static void main(String[] args)
  {
     One [] arr = new One [5];
     arr[0] = new One (25);
     arr[1] = new One ();
     arr[2] = new Two ();
     arr[3] = new Two (15, arr[0]);
     arr[4] = new Two (2, "CCC", arr[1]);
  }
}
סעיף א

הרצת הפעולה יוצרת את העצמים הבאים (memory diagram): idx0=One{val=25}, idx1=One{val=10}, idx2=Two{val=10,s="AAA",p=null}, idx3=Two{val=15,s="CCC",p=•}, idx4=Two{val=2,s="CCC",p=•}. כתבו במחלקות One ו-Two את הפעולות הבונות (constructors) הנדרשות לשם הרצת הפעולה הראשית כך שיתקבלו העצמים המתוארים. אין להניח שקיימות פעולות בונות אחרות פעולות או להוסיף פעולות נוספות!

memoryDiagram: topRow: idx0: class: One

val: 25

idx1: class: One

val: 10

idx2: class: Two

s: AAA

idx3: class: Two

val: 15

s: CCC

p: -> idx0 object (One{val=25}), per constructor call new Two(15, arr[0])

idx4: class: Two

val: 2

p: -> arrow target ambiguous, see note

floatingHelperBox_LANGUAGE_DIVERGENCE: javaPageValue: One{val=25}

csharpPageValue: One{val=13}

note: CRITICAL FLAG — the two language printings of this SAME question's memory diagram show a floating helper object box (below the idx3/idx4 columns, apparently a p-pointer target) with DIFFERENT values: 25 on the Java page (p.7), 13 on the C# page (p.15). Everything else in the diagram (idx0-idx4 top-row values, the code, the constructor-call arguments) is byte-identical between languages. This is very likely a print/transcription DEFECT in the source exam booklet (not an intentional per-language variant) since neither value cleanly resolves the aliasing implied by the constructor calls: arr[3]=new Two(15,arr[0]) implies p should alias arr[0] (val=25, matches the Java page only), and arr[4]=new Two(2,"CCC",arr[1]) implies p should alias arr[1] (val=10, matching NEITHER printed floating-box value). Flagged as HIGH-PRIORITY for subject-manager/expert review before this question is used for solving or grading — do NOT silently pick one value as canonical, and do NOT assume the arrow-tracing above is correct without visual re-verification.

confidence: LOW on the floating-box value and its arrow target; MEDIUM-HIGH on the unambiguous top-row values.

סעיף ב

כתבו פעולה המקבלת מערך arr של עצמים מסוג One ומחזירה מערך חדש הכולל עצמים מסוג Two אשר נמצאים ב-arr. אם במערך arr אין עצמים מסוג Two, הפעולה תחזיר null.

שאלות ותגובות על השאלה

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

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

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