Exams questions examples 1
שיטת הפתקים רק שאלות רלוונטיות מקבלות מענה. שאלות מיותרות שתשובותיהן נמצאות על גבי מסמך הבחינה לא מקבלות מענה. שאלות שמראות חוסר הבנה של החומר לא מקבלות מענה. במידה וצוות הקורס סבור כי השאלה דורשת התייחסות, הדבר יירשם על הלוח בכל הכיתות. 2
Exam example 1. y - x what נק' ) לפניך הפעולה הרקורסיבית מקבלת כפרמטרים שני מספרים שלמים ו 10 (? מה יהיה הפלט כתוצאה מזימון הפעולה what public static int what(int x, int y) if(x < 0) return what(-x, y); if(y < 0) return what(x, -y); if(x == 0 && y == 0) return 0; return 100 * what(x/10,y/10) + 10*(x%10) + y%10; } // what System.out.println(what(7,9)); System.out.println(what(-7,19)); System.out.println(what(17,-9)); System.out.println(what(-7,-19)); 3
Exam example 1 - solution what(-7,19) if(x<0) T return 1* what(7,19); if(y<0) return what(x, -y); if(x==0 && y== 0) return 0; return 100*what(x/10,y/10) + 10*(x%10) + y%10; } // what 179 what(7,19) if(x<0) F return what(-x,y); if(y<0) F return what(x, -y); if(x==0 && y== 0) F return 0; return 100*what(0,1) + 10*7+ 9; } // what -179 System.out.println(what(7,9)); 79 System.out.println(what(-7,19)); -179 System.out.println(what(17,-9)); -1079 System.out.println(what(-7,-19)); 179 1 what(0,1) if(x<0) F return what(-x,y); if(y<0) F return what(x, -y); if(x==0 && y== 0) F return 0; return 100*what(0,0) + 10*0+ 1; } // what 0 what(0,0) if(x<0) F return what(-x,y); if(y<0) F return what(x, -y); if(x==0 && y== 0) T return 0; return 100*what(x/10,y/10) + 10*(x%10)+y%10; } // what 4
Exam example 2 לפניך הפעולה הרקורסיבית kar מקבלת מספר שלם, k מערך arr שאיבריו מטיפוס שלם, ושני מספרים שלמים נוספים,e s,כאשר.s<=e ( 25 נק' ) public static int kar(int k, int[ ] arr, int s, int e) if(s == e) if(arr[s] > k) return a[s]; else return 0; else int p1 = kar(k, a, s, (s+e)/2); int p2 = kar(k, a, ((s+e)/2) + 1, e); return p1+p2; } // outer if } // kar 5
Exam example 2, cont. 2 8 4 14 5 18 : נתון מערך a System.out.println(kar( 6, a, 1, 5 )( ; : kar נק' ) א. ( 10 לפניך זימון הפעולה עקוב אחר ביצוע הפעולה בעבור המערך a מזימון הפעולה. והזימון הנתון, ורשום מה יהיה הפלט כתוצאה? ב. ( 15 נק' ) מה מבצעת הפעולה kar הסבר את תשובתך. 6
Exam example 2 - solution 2 8 4 14 5 18 public static int kar(int k, int[ ] arr, int s, int e) if(s == e) if(arr[s] > k) return a[s]; else return 0; else int p1 = kar(k, a, s, (s+e)/2); int p2 = kar(k, a, ((s+e)/2) + 1), e); return p1+p2; } // outer if } //kar סעיף ב. ( 15 נק' ) k - e הפעולה מחזירה את סכום כל האיברי המערך בין המקומות s ו )כולל(, הגדולים מ
Exam example 3 כתוב פעולה ) נק' ( 25 public static char[ ] remstars(string sentence) שמקבלת מחרוזת sentence שהיא משפט ומצמצמת אין אין להשאיר להשאיר כוכביות כוכביות אסור שיהיו שתי כוכביות הפעולה מחזירה מערך תווים המצומצמת. דוגמה: הפעולה תהפוך את המחרוזת לפני המילה הראשונה בסוף המשפט ברצף במשפט כוכביות במשפט.,כאשר כל איבר מהווה תו המתאים במחרוזת ***this**is***an***example**of***sentence**** this*is*an*example*of*sentence" ל- 8
Exam example 3,cont. ***this**is***an***example**of***sentence**** index value 0 t 1 h 2 i 3 s 4 * 5 i 6 s... Formal parameter : String sentence Returned value : reference to array of char 27 n 28 c 29 e 9
Exam example 3 - solution public static char[ ] remstars(string str) sentar = new char[str.length()]; // help array for(int k = 0; k < str.length(); k++) sentarr[k] = str.charat(k); int i = 0, j = 0, first = 0; // loop counters and help variable while( i < str.length() ) if( sentarr[i]!= * ) first = 1; sentarr[j++] = sentarr[i++]; } // if else if(first!= 0) sentarr[j++] = sentarr[i++]; first = 0; } // if else i++; } // while char[ ] finarr = new char[j-1]; // returned array for(int k = 0; k < j -1; k++) finarr[k] = sentarr[k]; return finarr; } // remstars 10
Exam example 4 11
Exam example 4 - solution public static int equalsum(int arr[ ]) int suml = 0, sumr = 0; for(int i = 0; i < arr.length; i++) sumr += arr[i]; for(int j = 0; j < arr.length; j++) sumr -= arr[j]; suml += arr[j]; if(sumr == suml) return j; } //for return -1; } // equalsum 12
Exam example 5 ( 25 נק' ) פעולה string) public static String find(string מקבלת כפרמטר מחרוזת string ומחפשת בה את תת-המחרוזת הארוכה ביותר המורכבת מאותיות בסדר אלפבית. הפונקציה מחזירה העתק של תת-המחרוזת הזאת. בתכנית הבאה הפלט הוא: ghijkl public static void main(string[] args) String str = "cabcefghabcdeghijklabc"; String longstr = find(str); System.out.println(longstr); } // main השלם את הקטעים החסרים בפעולה 13
public static String find(string string) char start, end; int s1= 0,s2 = 0,i=0, max = 0, ji = 0,jo = 0; boolean flag=false; String outstring; while (??? 1???) s1= i; start = string.charat(i); end = (??? 2???) while (??? 3???) flag=true; start = end; if(??? 4???) (??? 5???) end = string.charat(i+1); } // inner while if(flag) (??? 6???) if(s2-s1 > max) max = (??? 7???) ji = s1; jo = s2; (??? 8???) } // inner if } // outer if (??? 9???) } // outer while if(jo==string.length() - 1) outstring=string.substring(ji,(jo+1)); else (??? 10???) return outstring; } // find public static String find(string string) char start, end; int s1=0,s2=0,i=0, max=0, ji = 0,jo = 0; boolean flag=false; String outstring; while(i<string.length() - 1) s1= i; start = string.charat(i); end = string.charat(i+1); while( start + 1 == end ) flag=true; start = end; if( i < string.length() - 2 ) i++; end = string.charat(i+1); } // inner while if(flag) s2 = i+1; if(s2 - s1 > max) max = s2 - s1; ji = s1; jo = s2; flag = false; } // inner if } // outer if i++; } // outer while if(jo == string.length() - 1) outstring = string.substring(ji,(jo+1)); else outstring = string.substring(ji,jo); return outstring; } // find
Exam example 6 רשימה L תיקרא הרשימה אינה ריקה. מספר האיברים בה מתחלק ב- 3 רשימה משולשת אם היא מקיימת את התנאים האלה: בלי שארית. האיברים בשליש הראשון של הרשימה מכילים את אותם ערכים שמכילים האיברים בשליש השני של הרשימה ואותם ערכים שמכילים האיברים בשליש השלישי של הרשימה. הערכים מסודרים באותו סדר בכל אחד מהשלישים. לדוגמה: הרשימה L5 שלפניך היא רשימה משולשת באורך 9. כתוב פעולה list) public static boolean istriplelist(list <Integer> שתקבל רשימה list של מספרים שלמים כפרמטר בבודקת אם רשימה list היא רשימה משולשת. אפשר להשתמש בפעולות של המחלקות,List<T> Node<T> בלי לממש אותן. 15
Exam example 6 help methods // פעולה המחזירה את המספר האיברים ברשימה --- public static int listsize(list <Integer> lst) int count = 0; Node<Integer> pos = lst.getfirst(); while(pos!= null) count++; pos = pos.getnext(); } // while return count; } // listsize --- אם אתה משתמש בפעולות נוספות חובה עליך לממש ולתעד אותן. --- פעולה המקבלת רשימה lst ומספר שלם n--- // --- ומחזירה הפנייה לאיבר שנמצא במקום n ברשימה --- // public static Node<Integer> nplace(list <Integer> lst, int n) Node<Integer> pos = lst.getfirst(); while(pos!= null && n > 0) n--; pos = pos.getnext(); } // while return pos; } // nplace 16
Example 6 istriplelist method public static boolean istriplelist(list<integer> list) int num = listsize(list) / 3; if( num == 0 && num %3!= 0) return false; Node<Integer> pos1 = lst.getfirst(); Node<Integer> pos2 = nplace(list, num); Node<Integer> pos3 = nplace(list, 2*num); while (pos3!= null) if(pos1.getdata()!= pos2.gedata() pos1.getdata()!= pos3.getdata()) return false; pos1 = pos1.getnext(); pos2 = pos2.getnext(); pos3 = pos3.getnext(); } // while return true; } // istriplelist 17
Exam example 7 קבוצה ממשית ( RealSet ) היא אוסף של מספרים ממשים בלי חזרות ובלי סדר מחייב של הערכים. לפניך עוסף חלקי של הפעלות במחלקה : RealSet תיאור שם הפעולה הפעולה בונה קבוצה ממשית ריקה. RealSet() int size() הפעולה מחזירה את מסםר האיברים בקבוצה. הפעולה מוסיפה לקבוצה את המספר num אם num) void insert(double הוא לא נמצא בקבוצה. הפעולה מוחקת את המספר num אם הוא נמצא num) void remove(double בקבוצה. double findbiggest() הפעלה מחזירה את המספר הגדול בקבוצה. הנח שהקבוצה אינה ריקה. 18
Exam example 7,cont.. public static RealSet copyset(realset rs) א. ( 10 נק' ) כתוב פעולה שתקבל קבוצה ממשית rs לא ריקה, ותחזיר קבוצה ממשית חדשה שתכיל את כל האיברים שיש ב- rs לאחר סיום ביצוע הפעולה, על rs להכיל את כל המספרים שהיא הכילה לפני ביצוע הפעולה, ורק אותם. ב. ( 15 נק' ) כתוב פעולה rs) public static RealSet buidnegnum(realset שתקבל קבוצה ממשית rs לא ריקה, ותחזיר קבוצה ממשית שתכיל רק את המספרים השליליים שנמצאים ב-.rs לאחר סיום ביצוע הפעולה, על rs להכיל את כל המספרים שהיא הכילה לפני ביצוע הפעולה, ורק אותם. עליך להשתמש בפעולה rs) copyset(realset שכתבת בסעיף א. הערה: אתה יכול להשתמש בפעולות המחלקה RealSet בלי לממש אותן. אם אתה משתמש בפעולות נוספות, עליך לממש ולתעד אותן. 19
Exam example 7 - solution public static RealSet copyset(realset rs) RealSet rs1 = new RealSet(); RealSet rs2 = new RealSet(); סעיף א' int n = rs.size(); for( int i = 0; i < n; i++ ) double x = rs.findbiggest(); rs1.insert(x); rs2.insert(x); rs.remove(x); } // for for( int i = 0; i < n; i++ ) double x = rs1.findbiggest(); rs.insert(x); rs1.remove(x); } // for return rs2; } // copyset 20
Exam example 7 - solution public static RealSet buidnegnum(realset rs) RealSet rs1 = copyset(rs); double x = rs1.findbiggest(); while ( rs1.size() > 0 && x > 0 ) rs1.remove(x); x = rs.findbiggest(); } // while return rs1; } // buidnegnum סעיף ב' 21
Exam example 8 Point private double x private double y Point(double x, double y) double getx( ) double gety( ) void setx(double x) void sety(double y) String tostring( ) : Point לפניך UML של המחלקה ) כתבו פעולה המקבלת כפרמטרים 3 חתימת הפעולה צריכה להיות: נק' נקודות ובודקת אם הן על אותו הישר. א. ( 10 public static boolean inline(point p1, Point p2, Point p3) תזכורת: הנקודות (x3,y3) (x1,y1), (x2,y2), הן על אותו ישר אם ורק אם: x y 1 1 x y 2 2 = x y 1 1 x y 3 3 22
Exam example 8, cont. true ) נק' ב. )15 כתבו פעולה המקבלת כפרמטר מערך של נקודות ומחזירה אם במערך קיימות 3 נקודות ששייכות לאותו הישר, אחרת הפונקציה תחזיר.false חתימת הפעולה צריכה להיות: public static boolean testline(point[ ] points) הערות: אפשר להשתמש בפעולות של המחלקה Point בלי לממש אותן. חובה להשתמש בפעולה inline של סעיף א' )אפילו אם לא כתבתם(. 23
Exam example 8, cont. סעיף א' public static boolean inline(point p1, Point p2, Point p3) double w1,w2; w1 = (p1.getx() p2.getx()) * (p1.gety() p3.gety()); w2 = (p1.getx() p3.getx()) * (p1.gety() p2.gety()); return w1 == w2; } // inline 24
Exam example 8 - solution סעיף ב' public static boolean testline(point[ ] points) for (int i = 0; i < points.length - 2; i++) for(int j = i+1;j < points.length - 1; j++) for(int k = j + 1;k < points.length; k++) if( inline(points[i], points[j], points[k] ) return true; return false; } // testline 25
Exam example 9 כתובו את הפעולה הבאה: public static boolean islistcont( List<Integer> lst1, List<Integer> lst2) הפעולה מקבלת כפרמטרים 2 הפניות לרשימות של מספרים שלמים lst1 ו.lst2 הפעולה מחזירה true אם כל האיברים ברשימה lst1 נמצאים ברשימה lst2 ו- false אחרת..true מחזירה islistcont לדוגמה: עבור הרשימות הבאות הפעולה lst1: 5 -> 4 -> 1 -> 8 -> null lst2: 19 -> 5 -> 6 -> 8 -> 8 -> 4 -> 15 -> 1 -> null.lst2 עבור הרשימות הבאות הפעולה islistcont מחזירה false כי הערך 1 לא מופיע ברשימה lst1: 5 -> 4 -> 1 -> 8 -> null lst2: 19 -> 5 -> 6 -> 8 -> 8 -> 4 -> 15 -> 33 -> null Node שנלמדו. - List - - הערה: מותר להשתמש בלי לממש אותן בפעולות של המחלקות ו 26
Exam example 9 - solution public static boolean islistcont(list<integer> lst1, List<Integer> lst2) Node<Integer> node1 = lst1.getfirst(); while(node1!= null) boolean found = false; Node<Integer> node2 = lst2.getfirst(); while((node2!= null) && (! found)) if(node1.getdata() == node2.getdata()) found = true; node2 = node2.getnext(); } // inner while if(! found) return false; node1 = node1.getnext(); } // outer while return true; } // islistcont 27
Exam example 10.)price( )pages( - לפניך מחלקה : Book כל ספר במחלקה מוגדר ע"י התכונות הבאות שם הסופר,)writer( מספר העמודים ומחיר public class Book private double price; private int pages; private String writer; } // Book כתוב פעולה books) public static Book[ ] namewritersort(list<book> המקבלת רשימת ספרים של המחברים Kipling ו Swift ומחזירה מערך בו ספריו של Kipling יופיעו לפני ספריו של.Swift כלומר בתחילת המערך יופיעו כל ספריו של Kipling בסדר בו הם הופיעו ברשימה המקורית ולאחר מכן יופיעו כל ספריו של Swift בסדר בו הם הופיעו ברשימה המקורית. הערה: אפשר להשתמש בכול הפעולות של מחלקות Node, String, List וגם בפעולות של מחלקה Book )פעולה בונה, פעולות SET ו - GET ) בלי לממש אותן. 28
Exam example 10 - solution public static Book[ ] namewritersort(list<book> books) int numk = 0; // number of Kipling's books int nums = 0; // number of Swift's books for( Node<Book> curr = books.getfirst(); curr!= null; curr = curr.getnext() ) if(curr.getdata().getwriter().compareto("kipling") = = 0) numk++; if(curr.getdata().getwriter().compareto("swift") = = 0) nums++; } // for Book [ ] arr = new Book [numk + nums]; // output array int indk = 0; // Kipling's books array index int inds = numk; // Swift's books array index Node<Book> temp = books.getfirst(); while(temp!= null) if(temp.getdata().getwriter().compareto("kipling") = = 0) arr[indk++] = temp.getdata(); else arr[inds++] = temp.getdata(); temp = temp.getnext(); } // while return arr; } // namewritersort 29
Exam example 11 א. נתונה הפעולה הרקורסיבית הבאה : Node<Integer> public static revlist1)node<integer> lst, Node<Integer> node) הפעולה הופכת את סדר האיברים ברשימה list )האיבר הראשון ברשימה אחרי שינוי הוא האיבר האחרון ברשימה list המקורית, האיבר השני ברשימה אחרי שינוי הוא האיבר אחד לפני האחרון ברשימה )...list. הפעולה מחזירה את ההפניה לאיבר הראשון ברשימה מהופכת. זימון לפעולה revlist1 מהפעולה הראשית ( main ) מתבצע באופן הבא : Node<Integer> st = revlist1( list.getfirst( ), null ); Node<Integer> revlist1(node<integer> lst, Node<Integer> node) Node<Integer> temp; if (?? 1?? ) return node; temp =?? 2??;?? 3??; return revlist1(?? 4??); } // revlist1 2 נק ' null... 1. lst == 3 נק'.. lst.getnext();. 2. 3 נק' lst.setnext(node);.. 3. 3 נק' temp,lst;.. 4. השלם את הקטעים החסרים : 30
Exam example 11 public static Node<Integer> revlist2(node<integer> lst) ב. הפעולה אטרטיבית ( לא רקורסיבית ) הבאה:.?? n מבצעת בדיוק את אותה המשימה המתוארת בסעיף א'. השלימו בדפי התשובות את הקטעים החסרים המסומנים ב-?? public static Node<Integer> revlist2(node<integer> lst) Node<Integer> temp1, temp2 =?? 5??; while (?? 6?? ) temp1 =?? 7??;?? 8?? temp2 =?? 9??; lst = temp1; } // while return?? 10??; } // revlist2 נק' temp2=null;. 5. 3 נק'.... 6. lst!= null 2 נק' lst.getnext();... 7. 2 ק' lst.setnext(temp2);.... 8. 3 נק' lst;.. 9. 2 נק' temp2;... 10. 2.Node ו List הערה: בשני הסעיפים מותר להשתמש - בלי לממש אותן - בפעולות של המחלקות 31
Goodbye and Good luck! I wish you all the very best for your exams ( not only Java ), my students! IPC202 32