Collections, lambdas, streams, Optional, exceptions and files
75 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
75:00השעון רק עוזר להתאמן. אפשר גם בלי.The timer is just for practice. You can skip it.
חלק א: שאלות בחירה (5 נקודות לשאלה)Part A: multiple choice (5 points each)
התשובות מתגלות רק כשמסיימים את המבחן.Answers are revealed only when you finish the exam.
חלק ב: שאלות קודPart B: coding questions
שאלהQuestion 5
שכיחות מיליםWord frequency
כתבו static Map<String, Integer> wordFreq(String text) שסופרת כל מילה (רצף אותיות באנגלית), באותיות קטנות, ב-TreeMap. main מדפיס {a=1, be=2, not=1, or=1, to=2}.
Write static Map<String, Integer> wordFreq(String text) counting each word (a run of English letters), in lowercase, in a TreeMap. main prints {a=1, be=2, not=1, or=1, to=2}.
איך הפלט אמור להיראות?What should the output look like?
{a=1, be=2, not=1, or=1, to=2}
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:text.toLowerCase().split("[^a-z]+"), ודלגו על מחרוזת ריקה.text.toLowerCase().split("[^a-z]+"), and skip an empty string.
הפתרון שלנוOur solution
Main.java
import java.util.Map;
import java.util.TreeMap;
publicclass Main {
staticMap<String, Integer> wordFreq(String text) {
Map<String, Integer> freq =newTreeMap<>();
for (String w : text.toLowerCase().split("[^a-z]+")) {
if (!w.isEmpty()) {
freq.merge(w, 1, Integer::sum);
}
}
return freq;
}
publicstaticvoidmain(String[] args) {
System.out.println(wordFreq("To be, or not to be... a"));
}
}
פלטOutput
{a=1, be=2, not=1, or=1, to=2}
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (25 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 6
הכי טוביםThe best ones
כתבו static List<String> topN(Map<String, Integer> scores, int n) עם stream: n השמות עם הניקוד הגבוה ביותר, ובתיקו לפי סדר האלף-בית. main מדפיס [dana, avi, noa].
Write static List<String> topN(Map<String, Integer> scores, int n) with a stream: the n names with the highest scores, and on a tie in alphabetical order. main prints [dana, avi, noa].
איך הפלט אמור להיראות?What should the output look like?
[dana, avi, noa]
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (25 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.
שאלהQuestion 7
שורות שבורותBroken rows
record Book(String title, int year). כתבו חריגה משלכם class BadRowException extends Exception, ו-static Book parse(String row) throws BadRowException לשורה title;year (שנה בין 1450 ל-2100). ב-main עברו על השורות: ספר תקין מודפס, ושורה שבורה מדפיסה skip: והודעת החריגה.
record Book(String title, int year). Write your own exception class BadRowException extends Exception, and static Book parse(String row) throws BadRowException for a title;year row (a year between 1450 and 2100). In main go through the rows: a valid book is printed, and a broken row prints skip: and the exception's message.
איך הפלט אמור להיראות?What should the output look like?
Book[title=Dune, year=1965]
skip: bad row: Emma
skip: bad year: 19x2
skip: bad year: 3000
Main.java
ההשוואה נעשית בדפדפן מול הפלט האמיתי של הפתרון שלנו. אם הקוד נפל, הדביקו את הודעת השגיאה ותקבלו הסבר.The comparison happens in your browser, against our solution's real output. If the code crashed, paste the error message and get an explanation.
רמזHint 1:throw new BadRowException("bad year: " + p[1]).throw new BadRowException("bad year: " + p[1]).
רמזHint 2:תפסו NumberFormatException בתוך parse והפכו אותה ל-BadRowException.Catch NumberFormatException inside parse and turn it into a BadRowException.
הפתרון שלנוOur solution
Main.java
import java.util.List;
publicclass Main {
static Book parse(String row) throws BadRowException {
String[] p = row.split(";");
if (p.length!=2) {
thrownew BadRowException("bad row: "+ row);
}
int year;
try {
year =Integer.parseInt(p[1].trim());
} catch (NumberFormatException e) {
thrownew BadRowException("bad year: "+ p[1].trim());
}
if (year <1450|| year >2100) {
thrownew BadRowException("bad year: "+ year);
}
returnnew Book(p[0].trim(), year);
}
publicstaticvoidmain(String[] args) {
for (String row :List.of("Dune;1965", "Emma", "Ulysses;19x2", "Kim;3000")) {
try {
System.out.println(parse(row));
} catch (BadRowException e) {
System.out.println("skip: "+ e.getMessage());
}
}
}
}
record Book(String title, int year) {
}
class BadRowException extendsException {
BadRowException(String message) {
super(message);
}
}
פלטOutput
Book[title=Dune, year=1965]
skip: bad row: Emma
skip: bad year: 19x2
skip: bad year: 3000
יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.There is more than one right way. If the checks pass, your solution is right too.
מחווןRubric (30 נקודותpoints)
כשכל הבדיקות עוברות, כל השורות מסומנות. אם לא, סמנו בעצמכם מה כן עשיתם נכון.When every check passes, all rows are ticked. If not, tick what you did get right.