CS2030S – MIDTERM ASSESSMENT FOR Solved

$ 20.99
Category:

Description

CS2030 Programming Methodology II

INSTRUCTIONS TO CANDIDATES
1. This assessment paper contains 30 questions and comprises 11 printed pages, including this page.
2. Write all your answers in the answersheet provided.
3. The total marks for this assessment is 40. Answer ALL questions.
4. This is a OPEN BOOK assessment. You are only allowed to refer to hardcopies materials.
5. All questions in this assessment paper use Java 11.
Section I: Multiple Choice Questions (5 points)
• For each of the questions below, write your answer in the corresponding answer box on the answer sheet. Each question is worth 1 marks.
• If multiple answers are equally appropriate, pick one and write the chosen answer in the answer box. Do NOT write more than one answer in the answer box.
• If none of the answers is appropriate, write X in the answer box.
1. (1 point) Java is described as a statically typed language. What does this mean?
A. A class can have only one superclass.
B. Some fields and methods to be declared as static .
C. Once the compile-time type of a variable is decided, it cannot be changed.
D. A variable of type T cannot be typecasted to another type S , unless S and T are related.
2. (1 point) The code
Array<Number> a = new Array<Integer>(3); does not compile because generic types are:
A. contravariant
B. invariant C. covariant
D. supervariant
3. (1 point) Consider the following generic class:
class Wrapper<U extends Comparable<U>> { U value;
}
After type erasure, what will the type of value be?
A. Object
B. Comparable<U> C. Comparable
D. Wrapper
4. (1 point) A method signature consists of:
A. The method name, the type of arguments, the number of arguments, and the order of the arguments
B. The method name, the type of arguments, the number of arguments, the return type, and the order of the arguments
C. The method name, the type of arguments, the return type, and the number of arguments. D. The method name, the number of arguments, and the order of the arguments
5. (1 point) What best describes what impact the final keyword has in this class?
final class Record {
private int id; private String value;
public int getID() {
return this.id;
}
public String getValue() {
return this.value;
}
}
A. All fields cannot be changed in any child class of Record .
B. All methods cannot be overridden in any child class of Record .
C. Record cannot be inherited.
D. No interfaces can be implemented by Record .
Section II: Interface and Abstract Class (3 points)
The description below applies to Questions 6 – 8. Consider the following:
interface I {
}
abstract class A<T> {
}
class C extends A<Integer> implements I { }
I i = new A<Integer>();
I i = new C();
A<String> a = new C();
Section III: Exceptions (3 points)
The following applies to Questions 9 – 11. This question is graded by a bot. Please make sure you write your answer exactly as how Java would print its output to avoid unnecessary penalty.
Consider the following classes Main and SSHClient , where:
PasswordIncorrectException <: AuthenticationException <: Exception
class Main {
void start() {
try {
SSHClient client = new SSHClient(); client.connectPENode(); } catch (Exception e) {
System.out.println(“Main”);
}
} }
class SSHClient {
void connectPENode() throws Exception {
try {
// Line A (Code that could throw an exception)
} catch (AuthenticationException e) {
System.out.println(“SSHClient”);
}
}
}
After calling:
new Main().start()
9. (1 point) What would be printed if an Exception is thrown from Line A of connectPENode ?
10. (1 point) What would be printed if an AuthenticationException is thrown from Line A of connectPENode ?
11. (1 point) What would be printed if a PasswordIncorrectException is thrown from Line A connectPENode ?
Section IV: Dynamic Binding (8 points)
The following description applies to Question 12 – 15.
This question is graded by the bot. Please make sure you write your answer exactly as how Java would print its output to avoid unnecessary penalty. Consider the following four classes:
class A { void foo(A a) {
System.out.println(“class: A, parameter: A”); }
}
class B extends A {
@Override void foo(A a) {
System.out.println(“class: B, parameter: A”);
}
void foo(B a) {
System.out.println(“class: B, parameter: B”); }
}
class C extends B { void foo(C a) {
System.out.println(“class: C, parameter: C”); }
}
class D extends C {
@Override void foo(B a) {
System.out.println(“class: D, parameter: B”); }
}
We initialize four variables as follows:
A a = new D();
B b = new D();
C c = new D();
D d = new D();
12. (1 point) What will be printed if we call:
a.foo(d);
13. (1 point) What will be printed if we call:
b.foo(d);
14. (1 point) What will be printed if we call:
c.foo(d);
15. (1 point) What will be printed if we call:
d.foo(d);
Section V: Generic Typing (12 points)
The description below applies to Question 16 – 27. Suppose we have the following classes:
import java.util.List;
interface Trainable {
}
class Animal {
}
class Mammal extends Animal {
}
class Dog extends Mammal implements Trainable, Comparable<Mammal> {
@Override
public int compareTo(Mammal m) { return 1;
}
}
class ShihTzu extends Dog {
}
class A {
static <U, T extends U> U foo(List<? super T> list) { U u = null; return u;
} } and the following variables:
Dog dog = new Dog();
List<Dog> dogList = List.of(dog);
Indiciate if the following statement is true or false:
16. (1 point) Animal <: Trainable
17. (1 point) Dog <: Comparable<Animal>
18. (1 point) ShihTzu <: Comparable<Mammal>
19. (1 point) ShihTzu <: Comparable<? extends Animal>
20. (1 point) ShihTzu <: Comparable<? super ShihTzu>
21. (1 point) Comparable<ShihTzu> <: Comparable<? extends Trainable>
dog = A.<Mammal,Dog>foo(dogList);
dog = A.<Dog,ShihTzu>foo(dogList);
dog = A.<ShihTzu,ShihTzu>foo(dogList);
dog = A.<ShihTzu,Dog>foo(dogList);
dog = A.<Dog,Dog>foo(dogList);
27. (1 point) If we call
dog = A.foo(dogList);
What will U and T be inferred as?
Section VI: OO Principles (9 points)
The following applies to Questions 28 – 30.
Consider the following classes.
The class Time encapsulates a time measurement in units of seconds and milliseconds.
class Time { public int second; public int millisecond;
public Time(int second, int millisecond) { this.second = second; this.millisecond = millisecond;
}
}
The class Interval encapsulates a time interval and consists of a starting time (begin) and an ending time (end).
class Interval { private Time begin; private Time end;
public Interval(Time begin, Time end) { this.begin = begin; this.end = end;
}
public int durationInMs() { return (end.second * 1000 + end.millisecond)
– (begin.second * 1000 + begin.millisecond); }
}
A Jiffy is an interval with fixed duration of 10ms.
class Jiffy extends Interval { public Jiffy() { super(new Time(0, 0), new Time(0, 10));
}
@Override
public int durationInMs() { return 10;
}
}
28. (3 points) The code above violates information hiding. True or false? Please provide a rationale for your answer.
29. (3 points) The code above violates the ”tell, don’t ask” principle. True or false? Please provide a rationale for your answer.
30. (3 points) The code above violates the Liskov substitution principle. True or false? Please provide a rationale for your answer.
END OF PAPER

Reviews

There are no reviews yet.

Be the first to review “CS2030S – MIDTERM ASSESSMENT FOR Solved”

Your email address will not be published. Required fields are marked *