A. Question:
Assume that 'a' is substituted with the following code:
p.printClassName1();
What will happen when we try to execute the program?

Answer: The String "Person" will be printed.
Explanation: The method printClassName1() is in class Person, therefore it refers to the field className in this class, which value is “Person” (there is no overloading for fields).

B. Question:
Assume that 'a' is substituted with the following code:
p.printClassName2();
What will happen when we try to execute the program?

Answer: The String "Student" will be printed.
Explanation: The method printClassName2() in class Student overloads the method in class Person (since both are public methods), therefore it refers to the field className in class Student, which value is “Student”.

C. Question:
Assume that 'a' is substituted with the following code:
p.printName();
What will happen when we try to execute the program?

Answer: The code will not compile.
Explanation: The reference to the field “name” in method getName() in class Student should be changed with a call to the method super.getName(). The String that will be printed after this correction is “Name: Moti (student)”.
Remark: Since many students answered this question wrong, the answer “Name: Moti (student)” was also accepted.

D. Question:
Assume that 'a' is substituted with the following code:
p.printId1();
What will happen when we try to execute the program?

Answer: The String "Id: 1234" will be printed.
Explanation: The method getId() in class Person is private, therefore the method that will be called from printed() is the method getId() in class Person (and not in class Student). Since, the value of the field “id” is “1234” this is the String that will be printed.

E. Statements:

  • A class can implement only one interface. FALSE
  • A class can inherit (extend) from only one class. TRUE
  • An abstract class does not have to include abstract methods. TRUE
  • The next two methods, that reside in the same class, are a correct example for overloading:
    int add(int x, int y) { … }
    double add(int x, int y) { … }
    FALSE