ModelA CS CS141 2 18
ModelA CS CS141 2 18
Student ID:
Section No.:
Instructions:
1 10
2 10
3 10
4 10
Total 40
Page 1 of 8
There is only one error in each of the following codes. Identify and underline the error, and
explain the cause of each error.
a) abstract class A {
//Class A Members
}
class B {
//Class B Members
}
class C extends A, B {
//Class C Members
}
Answer: a class cannot extend more than one class. //2 marks
b) public class A {
public A(){
System.out.println(1);
super();
System.out.println(2);
}
}
Answer: super() must be the first statement in the constructor. //2 marks
c) interface X
{
void methodX();
}
class Y implements X
{
private void methodX()
{
System.out.println("Method X");
}
}
Answer: The visibility of methodX() has been reduced to private while implementing it in
the class //2 marks
Page 2 of 8
Answer: This block is unreachable (compile time error) because the exception is already
caught by above catch block //2 marks
// Constructor
public GenericBox(E content) {
this.content = content;
}
public E getContent() {
return content;
}
Answer: The type parameter <E> is not declared after the class name //2 marks
Page 3 of 8
a) Create a checked exception class called DeniedStudentException with two constructors: the first
constructor has no parameters and the second takes a message string.
Page 4 of 8
Page 5 of 8
Write a generic method computeCost that takes an array of generic type which must implements the
following Item interface.
computeCost computes and returns the sum of all items price after multiplying each item’s price by
1.05 except if:
• the item is a reference to a Food class, or
• the item’s price is less than 100.0
public static <T extends Item> double computeCost( T[] items ) //2 mark
{
double sum = 0 ; //1 mark
Page 6 of 8
• Set the width and height of a window to 300 and 70, respectively.
setSize(300,70);
• Create a text field with default text “0” and number of columns is 5
setLayout(new FlowLayout());
Complete the following code skeleton to reproduce the same interface using Java Swing library
Page 7 of 8
import javax.swing.*;
import java.awt.*;
// Declare constructor
public CounterUI() {
// set layout
setLayout(new FlowLayout());
Page 8 of 8