COMPX102-20B Object-Oriented Programming
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
2020 B SEMESTER EXAMINATIONS
Object-Oriented Programming
COMPX102-20B (HAM)
1. (a) What terms describe the most suitable relationship between Staff and School, Staff and Lecturer, respectively?
A. Composition, Aggregation
B. Aggregation, Inheritance
C. Inheritance, Composition
D. Inheritance, Aggregation (2 marks)
(b) Which of the following are stored in the heap memory in C#?
A. Local variables
B. Objects
C. Class definitions
D. Parameters
E. Static variables
F. Constants (2 marks)
(c) Drag and drop an object-oriented programming terminology to match its definition.
A construct that defines objects of the same type
An entity in the real world that can be distinctly identified
A instance variable that holds data associated with a class and its objects. A special kind of function that is defined in a class definition.
Class, Data Field, Method, Object (2 marks)
(d) Drag and drop a modifier to match the description of its accessibility in C#
______ Can be accessed by any other code in the same assembly or another
assembly that references it
______ Can be accessed only by code in the same class.
______ Can be accessed by code in the same class and derived class
______ Can be accessed only by code in the same assembly
Internal, Private, Protected, Public (2 marks)
(e) Drag and drop a testing terminology to match its description
______ Testing Individual units/components of a software are tested
______ Testing Individual units are combined and tested as a group
______ Testing To evaluate the system’s compliance with the business requirements
______ Testing Testing without looking at the code
______ Testing Write test code to ensure that all parts of the code are executed as
expected
Acceptance, Black-Box, Integration, Unit, White-Box (2 marks)
2. (a) Add the following binary numbers: 1110 and 11101. (2 marks)
(b) What is the range of decimal numbers that can be stored in 8-bit 2s complement form . (2 marks)
(c) Write – 105 in 8-bit 2s complement form . (2 marks)
(d) Write binary 01101010 in hexadecimal . (2 marks)
(e) Explain why there is no G in hexadecimal? (2 marks)
3 . (a) Which of the following statements are true?
A. A method can be overloaded in the same class.
B. A method can be overridden in the same class.
C. If a method overloads another method, these two methods must have the same signature.
D. If a method overrides another method, these two methods must have the same signature.
E. A method in a subclass can overload a method in the superclass. (2 marks)
(b) Which of the following statements are true?
A. Use the private modifier to encapsulate data fields.
B. Encapsulating data fields keeps the data safe within the class itself.
C. Encapsulating data fields gives us the freedom to change the internal representation without it affecting the external (public) view.
D. Encapsulating data fields helps prevent human errors.
E. Encapsulating data fields makes code short (2 marks)
(c) Which of the following statements regarding abstract classes are true?
A. An abstract class cannot be instantiated using the new operator.
B. An abstract class can have subclasses.
C. A subclass of a concrete superclass can be abstract.
D. A subclass of an abstract superclass can be abstract.
E. A subclass can override a concrete method in a superclass to declare it abstract.
F. An abstract class can be used as a data type. (2 marks)
(d) Which of the following statements regarding interfaces are true?
A. You can only inherit from one class, but many interfaces.
B. An interface is like a class but all methods are abstract.
C. A class that implements an interface must implement all interface methods.
D. An interface can have a constructor.
E. You need use the override keyword when implementing an interface method. (2 marks)
(e) Which of the following statements regarding constructors are true?
A. An object can define more than one constructor.
B. Each constructor must have a unique signature.
C. A constructor does not have to assign a value to every field in the object.
D. If no constructor is defined, the compiler provides one for you.
E. An abstract class cannot have a constructor. (2 marks)
4. (a) What is the truth table for the following circuit , where the left-hand numbers are the two inputs R and S and the previous output Q0? The right-hand number is the new output Q1 .
(4 marks)
INPUTS |
OUTPUT |
||
R |
S |
Q0 |
Q1 |
0 |
0 |
0 |
|
0 |
0 |
1 |
|
0 |
1 |
0 |
|
0 |
1 |
1 |
|
1 |
0 |
0 |
|
1 |
0 |
1 |
|
1 |
1 |
0 |
|
1 |
1 |
1 |
|
(b) Assume you have a working RS flip-flop subcircuit component . Draw how you would
use your RS flip-flop subcircuit to build a clocked flip-flop . (2 marks)
(c) Suppose that your clocked R-S flip-flop circuit from (b) is not putting out voltage and not receiving voltage at either input . What voltage will the flip-flop put out at the end of each of the following steps?
(i) Voltage is supplied at R .
(ii) Voltage is supplied at S .
(iii) Voltage at R is turned off.
(iv) Voltage is supplied at Clock .
(v) Voltage at Clock is turned off.
(vi) Voltage at S is turned off.
(vii) Voltage is supplied at R . (4 marks)
5. Problem Description:
Staff in the School of Computing and Mathematical Sciences at a small but well-respected university get their computing equipment updated on a three-to-four-year basis . The Technical Support Group requires a program to keep track of what computers the staff have and when they need to be replaced . For each staff member , the program needs to keep track of their name and office number, and the desktop and/or laptop computer that they have . Academic and research staff may have a desktop or a laptop computer, or both . General staff will only have desktop computers .
The school has standardised the computing equipment that staff can buy, so that there are only a few computer setups that are available, and each is assigned a lifespan, in months, for how long it should last until it needs replacing . Laptop setups normally include additional hardware, such as a dock, or cables/adaptors to work with an external monitor . The school is also looking at whether devices, such as smartphones and tablets, could also be kept track of in this way . Smartphones would also require storing what phone number , provider and plan they are on .
When a computer setup is purchased for a staff member , the program needs to keep track of the date it was bought, and what the cost was when it was purchased . The program should then be able to calculate the replacement date by adding the computer setups lifespan to the purchase date .
(a) Identify 5 nouns in the description above that would make suitable candidates for
representation as objects in the design of a program that supports the required functionality . (2 marks)
(b) Identify two nouns in the above description that would benefit from sharing the same
base-class through inheritance . Detail any separation of fields between the base-class and its two subclasses . Note: the nouns chosen need not be restricted to those given in your answer to (a) . (2 marks)
(c) Write a suitable enumerated type for the types of staff members , i .e . academic staff, general staff and research staff. (2 marks)
(d) Identify two classes that are in a ‘has a’ relationship in the above description . Show how these two classes would be related using the UML diagram below .
(2 marks)
(e) Assume class Manager : Person {}
(i) Which of the following will cause a run time error?
1. Manager m = new Manager (); Person p = (Person) m;
2. Person p = new Person (); Manager m = (Manager) p;
(ii) Which of the following will cause a compile time error?
1. Person p = new Person (); Manager m = p;
2. Manager m = new Manager ();
Person p = m; (2 marks)
6. The following is a syntactic description of a Turing Machine:
: S才及才部 t
St 0 0 工 St
St t t 工 St
St _ _ t SZ
: S才及才部 Z
SZ 0 t * 叮及t才
SZ t 0 t SZ
SZ _ t * 叮及t才
Where the start state is S1 and the input tape consists of 0s, 1s and the blank symbol .
The syntax used in the description above is the same as the on-line Turing Machine studied in lectures, where:
• Each line contains one tuple of the form '<current state> <current symbol> <new symbol> <direction> <new state>'.
• You can use any number or word for <current state> and <new state>, e.g., 10, a, state1. State labels are case-sensitive.
• You can use any character for <current symbol> and <new symbol>, or '_' to represent blank (space). Symbols are case-sensitive.
• <direction> should be 'l', 'r' or '*', denoting 'move left', 'move right' or 'do not move', respectively.
• Anything after a ';' is a comment and is ignored.
• The machine halts when it reaches any state starting with 'halt', e.g., halt-reject, halt-accept.
(a) Draw the Turing Machine Diagram of the above machine description: (2 marks)
(b) What are the outputs for the following inputs:
tt00t
t0ttt
00000
_____ (an empty tape) (4 marks)
(c) Using the Turing Machine above as a starting point, design a Turing Machine to perform the 2s complement on an input string of a binary number . Remember that to form the two’s complement, you can first form the 1s complement (inverting the 1s
and 0s) , then simply add 1. (3 marks)
(d) A 2s complement number must always have the same number of binary digits (bits) .
Explain why it is difficult for a Turing Machine to ensure this happens . ( 1 mark)
7. Here are the Circle and Rectangle classes from an in-development drawing program, which lets users create falling shape animations .
public class Circle {
private Point2D _org;
private double _rad;
public Circle(double x, double y, double radius){
_org = new Point2D(x, y);
_rad = radius;
}
public double XOrigin {
get { return _org.getX(); }
set { _org.setX(value); }
}
public double YOrigin {
get { return _org.getY(); }
set { org.setY(value); }
}
public override string ToString(){
return "circle(" + _org.ToString() + ", radius=" + _rad.ToString() + ")";
}
}
public class Rectangle {
private Point2D _org;
private double _x_dim;
private double _y_dim;
public Rectangle(double x_org, double y_org, double x_dim, double y_dim){
_org = new Point2D(x_org, y_org);
_x_dim = x_dim;
_y_dim = y_dim;
}
public void resizeDimensions(double x_dim, double y_dim){
_x_dim = x_dim;
_y_dim = y_dim;
}
public double XOrigin {
get { return _org.getX(); }
set { _org.setX(value); }
}
public double YOrigin {
get { return _org.getY(); }
set { org.setY(value); }
}
public override string ToString(){
return "rectangle(" + _org.ToString() + "," + _x_dim.ToString() + "," +
_y_dim.ToString() + ")";
}
}
(a) Write an abstract class called Shape that contains all code elements common to both
Circle and Rectangle, so that they can both inherit from it . (5 marks)
(b) Rewrite both Circle and Rectangle to be subclasses of your new Shape class . (5 marks)
8 . This question relates to WRAMP instructions and programs .
(a) For each of the three specifications below write one WRAMP instruction that satisfies
the specification:
(i) Subtract 1 from the value stored in register $4.
(ii) Set the value in $5 to 1 (i .e . true) if the value in $4 is greater than or equal to the
value in $3.
(iii) Move the top of the stack “down” by two words . (3 marks, 1 for each part)
(b) Below is an excerpt of WRAMP assembly code and the memory address of each
instruction is given on the left (i .e . 01, 02, 03, … 0C) .
main:
01 addi $3, $0, 0x08
02 subui $sp, $sp, 1
03 sw $3, 0($sp)
04 jal increment
05 addui $sp, $sp, 1
06 addu $3, $0, $1
increment:
07 subui $sp, $sp, 1
08 sw $6, 0($sp)
09 lw $6, 1($sp)
0A addi $1, $6, 0x01
0B lw $6, 0($sp)
0C jr $ra
Regarding this code, answer the following questions:
(i) Which memory address would the program counter point to when the program starts?
(ii) Which line of code pushes the value of register $3 to the top of the stack memory?
(iii) What is the value of $ra when the instruction “jal increment” is executed?
(iv) Which line of code will be executed after the instruction “jr $ra” is executed?
(v) What is the value of $3 when the program finishes? (5 marks, 1 for each part)
(c) The stack memory stores information about the active functions/methods/subroutines of a computer program . Use the code above to discuss what information is stored on the stack , and why this is a poor example of programming . (2 marks)
9 . (a) What would be the output of the following code when the console program is run?
public static void Main()
{
List<int> numbers = new List<int>{10,4,8,11,24,0,-1};
Console.WriteLine (Mystery(numbers, 8));
}
public static int Mystery(List<int> numbers, int num){
if (numbers.Count == 1) {
if (numbers[0] > num) return 1;
return 0;
}
int first_num = numbers[0];
numbers.RemoveAt(0);
if(first_num > num) return 1 + Mystery(numbers, num);
else return Mystery(numbers, num);
}
} (4 marks)
(b) What is the purpose of the mystery method? ( 1 marks)
(c) What would happen if the mystery method was called with an empty list? What change, or changes, needs to be made to the code so that it returns the correct answer
for an empty list . (3 mark)
(d) What would happen if the mystery method was called with the variable numbers equal to null? Write an assert statement for the mystery method to ensure that this does
not occur . (2 marks)
10 . Study the code below that reduces the size of an image held in a 2-dimensional array .
public double[,] ReduceSize (double[,] image, int size)
{
double[,] newimage = new double[size/2, size/2];
double sum;
int x,y;
for (int i = 0; i < size/2; i+=1) {
for (int j = 0; j < size/2; j+=1) {
x = i*2;
y = j*2;
sum = image [x,y] + image [x+1,y] + image [x,y+1] + image [x+1,y+1];
newimage [i,j] = sum/4;
}
}
return newimage;
}
(a) Count the number of elementary operations in this code . Assume that every
assignment, every logical or arithmetic operation (addition, subtraction, comparison, etc .), and every return statement constitutes an elementary operation, and nothing else does . (5 marks)
(b) What is the speed complexity of this code in Big O notation . (1 mark)
(c) A friend suggests that a way to speed up the code is to calculate size/2 at the start and store this in a variable, rather than calculating it every time . Count the new number of elementary operations in this code . (2 marks)
(d) Comment on what you think the speed difference would be . (1 mark)
(e) What is the speed complexity of the code in (c) in Big O notation . (1 mark)
2022-10-24