0% found this document useful (0 votes)
8 views

SYSC2004_Midterm_C_Solns

The document provides sample solutions for the SYSC2004 Midterm Winter 2023, including multiple-choice questions on Java programming concepts, UML notation, and class design. It covers topics such as run-time errors, object definitions, method documentation, and class relationships. Additionally, it includes code snippets and grading criteria for various sections of the exam.

Uploaded by

os10ronnie1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

SYSC2004_Midterm_C_Solns

The document provides sample solutions for the SYSC2004 Midterm Winter 2023, including multiple-choice questions on Java programming concepts, UML notation, and class design. It covers topics such as run-time errors, object definitions, method documentation, and class relationships. Additionally, it includes code snippets and grading criteria for various sections of the exam.

Uploaded by

os10ronnie1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

SYSC2004 Midterm Winter 2023

Section C: Sample Solutions

SECTION 1 (…/10)
Q1 –Which of the following errors is a run-time error in Java?
a) The program has an expression with a division by zero
b) The program violates the rules of Java
c) The program does not produce the expected result
d) All of the above are run-time errors.

Q2 – In Java, an object is:


a) A variable declaration whose type is a class
b) An instance of a class
c) An entity with identity, state, and behavior
d) All of the above
e) None of the above

Q3 – In UML notation, underline means:


a) Abstract
b) Protected
c) Static
d) Private
e) Public

Q4 – When we declare a class as abstract:


a) It cannot contain any fields.
b) It cannot contain any methods.
c) It cannot contain any concrete (i.e. non-abstract) methods.
d) All of the above.
e) None of the above.

Q5 – Javadoc comments end with

a) **/
b) //
c) /
d) */

Page 1 of 6
Q6 – Assume all the required classes are implemented, and the default constructor is available on all classes. How
many objects are in the following program after #line A is executed?
public class Client{
public static void main (String[] args ){
Die d1 = new Die();
Die d2 = new Die();
Die d3 = d2;
d1 = d2;
Player p1 = new Player();
Player p2 = new Player();
Player p3 = p2;
Game g = new Game(); #Line A
}
}

a) 3
b) 4
c) 5
d) 6
e) 7

Q7 – The Java programming language directly supports Multiple inheritance.


a) True
b) False

Q8 – An attribute declared as protected can be accessed from within the class where it is declared and also from:
a) Only classes in the same package
b) Only classes in the same package, and the superclasses of the class where it is declared
c) Only classes in the same package, and the subclasses of the class where it is declared
d) Only the superclasses of the class where it is declared
e) Only the subclasses of the class where it is declared

Q9 – Which convention does Java follow when passing primitive data types as input parameters to methods?
a) Pass-by-value
b) Pass-by-reference

Q10 – Which of the following declarations for creating a LinkedList is correct?


a) LinkedList <String> my_book = new LinkedList <String>();
b) LinkedList <int> my_numbers = new LinkedList <int>();
c) LinkedList <String> my_book = {“This”, “is”, “my”, “book”};
d) Both a and b are correct
e) Both a and c are correct

Page 2 of 6
SECTION 2: getCost (/4)
// Javadoc 1.5 broken down as per below
/** // 0.2
* getCost returns the cost of the customer's order, or 0 if the
* customer hasn't started an order. // 0.3: must include method name
* (We assume the parameters are not null.) // not required in the Javadoc
*
* @param customer The customer adding to an order
// 0.5 (tag (0.2), name of the parameter (0.15), explanation (0.15))
* @return The cost of the customer's order
// 0.5 (tag (0.2), data type not included (0.15), explanation (0.15))
*/
public double getCost(Customer customer) { // 0.5
ArrayList<Product> products = orders.get(customer); // get products // 0.5
if (products==null) { // 0.25
return 0; // customer doesn't have an order
}
double cost = 0;
for (Product product: products) { // walk through all products // 0.5: for each required, max of 2 otherwise
cost += product.getCost(); // 0.5
}
return cost; // initializing and returning cost 0.25
}

SECTION 3: Retailer (/10)


public class Retailer extends Business // 0.5
{
private HashMap<Customer,ArrayList<Product>> orders; // 0.5 (deduct 0.25 if include superclass attributes)

public Retailer(String name) // out of 2; 0.5 for signature


{
super(name); // 1 (must use super)
this.orders = new HashMap<Customer,ArrayList<Product>>(); // 0.5
// student may use this.orders in another method but it is required
// somewhere for full marks. Deduct 0.5 if “this” is not used correctly somewhere
}

public boolean startOrder(Customer customer) { // out of 1.5; 0.5 for signature


if (orders.get(customer)!=null) { // 0.5 for if and returning false
return false;
}
orders.put(customer,new ArrayList<Product>()); // 0.5 for put and returning true
return true;
}

Page 3 of 6
public boolean addProduct(Customer customer, Product product) { // out of 1.5; 0.5 for signature
ArrayList<Product> products = orders.get(customer); // 0.25
if (products==null) { // 0.25
return false;
}
products.add(product); // 0.5 for the add and returning true
orders.put(customer,products); // not required but no deduction if put or replace used here
return true;
}

public boolean removeProduct(Customer customer, Product product) { // out of 4; 0.5 for signature
ArrayList<Product> products = orders.get(customer); // 0.25
if (products==null) { // 0.25
return false;
}
Iterator<Product> iter = products.iterator(); // 0.5: if they do not use iterator, the max grade is 2
while (iter.hasNext()) { // 0.5
if (iter.next().equals(product)) { // 1: must use equals
iter.remove(); // 0.5
return true; // 0.25
}
}
return false; // 0.25
}
}

(Section 4 on next page)

Page 4 of 6
SECTION 4: UML (/6)

6 marks.
(.../0.5) - Four classes with correct names

Relationships (…/1.5):
(.../0.5) - Inheritance relationship between Business and Retailer (0.25 correct arrow, 0.25 correct direction)
(.../0.5) - Aggregation relationship between Product and Retailer (0.25 correct arrow, 0.25 correct direction)
(.../0.5) - Aggregation relationship between Customer and Retailer (0.25 correct arrow, 0.25 correct direction)

Note: No deduction for using composition instead of aggregation for both of the above, i.e. solid diamonds, or
for using association instead of aggregation. For association the arrow will be a regular arrow with a “v”
arrowhead, but in the opposite direction (i.e. the “v”s next to Product and Customer).

-0.25 marks for each extra arrow. 0/1.5 if there are arrows between all classes.

(continued on next page)


Page 5 of 6
(.../4): Class Retailer (.../2) and Class Customer (.../2)
• attributes (..../0.5) - 0.1 correct names, 0.2 correct visibility, 0.2 correct data types; -0.25 for each
missing attribute
• constructor (..../0.5) - 0.1 correct name, 0.1 correct visibility, 0.2 correct parameters and types, 0.1 no
return type; there should only be one constructor.
• methods (..../1) - 0.1 correct names, 0.2 correct visibility, 0.4 correct parameters and types, 0.3 correct
return type; -0.35 for each missing method

Page 6 of 6

You might also like