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

JavaJava

מבחן 3: OOP, היסודותExam 3: OOP basics

מחלקות ועצמים, בנאים, encapsulation, static ו-final, equals ו-hashCode

Classes and objects, constructors, encapsulation, static and final, equals and hashCode

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.

1. מה יודפס?What is printed? 5
java
String a = new String("hi");
String b = new String("hi");
System.out.println((a == b) + " " + a.equals(b));
2. מה יודפס?What is printed? 5
java
static class Counter {
    static int n = 0;

    Counter() {
        n++;
    }
}

new Counter();
new Counter();
System.out.println(Counter.n);
3. מה יודפס?What is printed? 5
java
final int[] a = {1};
a[0] = 5;
System.out.println(a[0]);
4. למה שדות של מחלקה הם בדרך כלל private?Why are a class's fields usually private? 5

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

שאלהQuestion 5

חשבון בנקA bank account

מחלקה Account עם יתרה private. deposit(double) זורקת IllegalArgumentException על סכום 0 או שלילי. withdraw(double) זורקת IllegalStateException כשאין מספיק כסף. getBalance(). main מדפיס 70.0.

An Account class with a private balance. deposit(double) throws IllegalArgumentException on 0 or a negative amount. withdraw(double) throws IllegalStateException when there isn't enough money. getBalance(). main prints 70.0.

איך הפלט אמור להיראות?What should the output look like?
70.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: בודקים לפני שמשנים.Check before you change.
הפתרון שלנוOur solution
Main.java
public class Main {
    public static void main(String[] args) {
        Account acc = new Account();
        acc.deposit(100);
        acc.withdraw(30);
        System.out.println(acc.getBalance());
    }
}

class Account {
    private double balance;

    void deposit(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("amount must be positive");
        }
        balance += amount;
    }

    void withdraw(double amount) {
        if (amount > balance) {
            throw new IllegalStateException("not enough money");
        }
        balance -= amount;
    }

    double getBalance() {
        return balance;
    }
}
פלטOutput
70.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

נקודות בלי כפילויותPoints without duplicates

מחלקה Point עם x ו-y (final int). כתבו equals ו-hashCode כך שנקודות עם אותן קואורדינטות שוות, ו-toString שמחזיר (1, 2). main מכניס ל-HashSet את (1,2), (3,4), (1,2) ומדפיס 2.

A Point class with x and y (final int). Write equals and hashCode so points with the same coordinates are equal, and toString returning (1, 2). main puts (1,2), (3,4), (1,2) into a HashSet and prints 2.

איך הפלט אמור להיראות?What should the output look like?
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: Objects.hash(x, y).Objects.hash(x, y).
הפתרון שלנוOur solution
Main.java
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        Set<Point> points = new HashSet<>();
        points.add(new Point(1, 2));
        points.add(new Point(3, 4));
        points.add(new Point(1, 2));
        System.out.println(points.size());
    }
}

class Point {
    private final int x;
    private final int y;

    Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object o) {
        return o instanceof Point p && x == p.x && y == p.y;
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y);
    }

    @Override
    public String toString() {
        return "(" + x + ", " + y + ")";
    }
}
פלטOutput
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 7

ספרייה קטנהA small library

מחלקה Library שמחזיקה private List<String> של ספרים זמינים ו-private List<String> של מושאלים. add(title), lend(title) מחזירה true אם הספר זמין (ומעבירה אותו), אחרת false. giveBack(title) מחזירה true אם הוא היה מושאל. available() מחזירה כמה זמינים. main מדפיס true false 1 true 2.

A Library class holding a private List<String> of available books and a private List<String> of lent ones. add(title), lend(title) returns true if the book is available (and moves it), otherwise false. giveBack(title) returns true if it was lent. available() returns how many are available. main prints true false 1 true 2.

איך הפלט אמור להיראות?What should the output look like?
true false 1 true 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: list.remove(title) מחזיר true אם הוא הסיר משהו.list.remove(title) returns true if it removed something.
הפתרון שלנוOur solution
Main.java
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Library lib = new Library();
        lib.add("Dune");
        lib.add("Emma");
        boolean a = lib.lend("Dune");
        boolean b = lib.lend("Dune");
        int n = lib.available();
        boolean c = lib.giveBack("Dune");
        System.out.println(a + " " + b + " " + n + " " + c + " " + lib.available());
    }
}

class Library {
    private final List<String> shelf = new ArrayList<>();
    private final List<String> lent = new ArrayList<>();

    void add(String title) {
        shelf.add(title);
    }

    boolean lend(String title) {
        if (!shelf.remove(title)) {
            return false;
        }
        lent.add(title);
        return true;
    }

    boolean giveBack(String title) {
        if (!lent.remove(title)) {
            return false;
        }
        shelf.add(title);
        return true;
    }

    int available() {
        return shelf.size();
    }
}
פלטOutput
true false 1 true 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