נבנה על ידיBuilt by Ariel Yohimovich

JavaJava

מבחן 2: מתודות ומערכיםExam 2: Methods and arrays

מתודות, pass-by-value, מערכים, ArrayList, static וחריגות

Methods, pass-by-value, arrays, ArrayList, static and exceptions

60 דקותminutes4 שאלות בחירהmultiple choice3 שאלות קודcoding questions100 נקודותpoints
60: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.

1. מה יודפס?What is printed? 5
java
static void inc(int x) {
    x++;
}

int a = 5;
inc(a);
System.out.println(a);
2. מה יודפס?What is printed? 5
java
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 9;
System.out.println(a[0]);
3. מה קורה כשמריצים?What happens when it runs? 5
java
int[] a = new int[3];
System.out.println(a[3]);
4. מה יודפס?What is printed? 5
java
int count = 0;
for (int i = 2; i < 10; i += 2) {
    count++;
}
System.out.println(count);

חלק ב: שאלות קודPart B: coding questions

שאלהQuestion 5

סכום ספרותA digit sum

כתבו static int digitSum(int n) שמחזירה את סכום הספרות של מספר אי-שלילי, בלולאת while עם % ו-/ (בלי להמיר למחרוזת).

Write static int digitSum(int n) returning the sum of a non-negative number's digits, with a while loop using % and / (without converting to a string).

איך הפלט אמור להיראות?What should the output look like?
10 0
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: n % 10 הספרה האחרונה, n / 10 מוריד אותה.n % 10 is the last digit, n / 10 removes it.
הפתרון שלנוOur solution
Main.java
public class Main {
    static int digitSum(int n) {
        int total = 0;
        while (n > 0) {
            total += n % 10;
            n /= 10;
        }
        return total;
    }

    public static void main(String[] args) {
        System.out.println(digitSum(2026) + " " + digitSum(0));
    }
}
פלטOutput
10 0

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.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

היפוך במקוםReversing in place

כתבו static void reverse(int[] a) שהופכת את המערך במקום (בלי מערך חדש), עם שני אינדקסים שנפגשים באמצע.

Write static void reverse(int[] a) that reverses the array in place (no new array), with two indexes that meet in the middle.

איך הפלט אמור להיראות?What should the output look like?
[5, 4, 3, 2, 1]
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: for (int i = 0, j = a.length - 1; i < j; i++, j--).for (int i = 0, j = a.length - 1; i < j; i++, j--).
רמזHint 2: משתנה זמני להחלפה.A temporary variable for the swap.
הפתרון שלנוOur solution
Main.java
import java.util.Arrays;

public class Main {
    static void reverse(int[] a) {
        for (int i = 0, j = a.length - 1; i < j; i++, j--) {
            int tmp = a[i];
            a[i] = a[j];
            a[j] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        reverse(a);
        System.out.println(Arrays.toString(a));
    }
}
פלטOutput
[5, 4, 3, 2, 1]

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.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

סכום שמתעלם מזבלA sum that ignores junk

כתבו static String sumValid(String[] items) שמחברת את הפריטים שהם מספרים שלמים ומדלגת על השאר, ומחזירה sum 12, skipped 2. NumberFormatException נתפסת לכל פריט בנפרד.

Write static String sumValid(String[] items) that adds the items that are whole numbers and skips the rest, returning sum 12, skipped 2. NumberFormatException is caught for each item separately.

איך הפלט אמור להיראות?What should the output look like?
sum 12, skipped 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: try/catch בתוך הלולאה, לא סביבה.try/catch inside the loop, not around it.
הפתרון שלנוOur solution
Main.java
public class Main {
    static String sumValid(String[] items) {
        int sum = 0;
        int skipped = 0;
        for (String item : items) {
            try {
                sum += Integer.parseInt(item.trim());
            } catch (NumberFormatException e) {
                skipped++;
            }
        }
        return "sum " + sum + ", skipped " + skipped;
    }

    public static void main(String[] args) {
        System.out.println(sumValid(new String[]{"4", "x", " 3", "5", "2.5"}));
    }
}
פלטOutput
sum 12, skipped 2

יש יותר מדרך נכונה אחת. אם הבדיקות עוברות, גם הפתרון שלכם נכון.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.

נבנה על ידיBuilt by Ariel Yohimovich