Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP104 Revision

More Exam-style questions

This is based on parts of the 2001 0657 . 104B exam, converted to C# and MASM where appropriate .  Questions on irrelevant topics, such as pointers, have been removed .  The four extra questions at the end were revision questions from 2005.

1.             What is the output of the following program when called with   >Test 6 5 2 3  ?

static int c;

static void procB (int b)

{

int a;

a = b - 1;

b = c - 2;

c = b + 3;

Console .WriteLine("ProcB " + a + " " + b + " " + c);

}

static void procA (ref int a)

{

int b, c;

c = a + 1;

b = c + 2;

a = b + 3;

Console .WriteLine("ProcA " + a + " " + b + " " + c);

}

static int main(string[] args)

{

int a, b;

a = Convert.ToInt32(args[1]);

b = Convert.ToInt32(args[2]);

c = Convert.ToInt32(args[3]);

procB(a);

Console.WriteLine("main " + a + " " + b + " " + c);

procA(ref b);

Console.WriteLine("main " + a + " " + b + " " + c);

procB(c);

Console.WriteLine("main " + a + " " + b + " " + c);

}

2.     (a)    Define an appropriate C# class to store information about 50 feature films, where the information stored in an object for each film is as follows:

• title

year of release

 classification (one of G, PG, M, R18)

 running time in minutes

(b)   Write a method in your Film class that is passed a year as a parameter, and

returns true if the film was released before the year, or false if it was released after the year given .

(c)    Write a static method called time_difference which determines if the mean running  time of feature films has increased, decreased or remained the same after a given    year .  The method will have two arguments: an array of film objects, and a year .       The method will compute the mean running time of all films released before the        given year, and the mean running time of all films released after the given year .        The return value will be the difference between these mean times .  If mean running times have decreased the return value will be negative, if they have increased it will be positive, and it will be zero if there is no difference .

3.     (a)   Write the code to implement the following method prototype:

public int find_smallest(int[] a);

When passed an array a  of integers, the function will search for the smallest value within a, returning the smallest value found . Your code should include contract      information (i .e . uses of Contract .Requires and Contract .Ensures) whenever               possible and appropriate .

(c)    Write code to implement the following function prototype:

public int lowercase_count(string s);

When passed a string s the function will return a count of the number of characters in s that are lowercase letters .  Your code should include contract information (i .e .   uses of Contract .Requires and Contract .Ensures) whenever possible and                        appropriate .

4.     (a)    State De Morgans laws .

(b)   Draw a circuit for an AND gate which uses only OR and NOT gates .

(c)    Simplify the following Boolean expression .  Show each step in the process .

a  +  a  •  b

The “Fast Fix” software company was having difficulty with the speed of one of their most important pieces of software .  They traced the problem to their sorting                procedure .  It took too long to run .  Careful analysis showed that the number of       comparison steps performed when sorting N items was

N  / 2

and that on average each comparison took  100 microseconds to perform .  The         timing of other operations in the sort was insignificant .  Two programmers Alpha Smith and Beta Jones were contracted to write improved versions of the sort .       Smith rewrote the sort routine in machine code and managed to reduce the time    taken to perform each comparison by a factor of 100 (each comparison now took  1 microsecond) .  The number of comparisons required was unchanged .  Jones             decided to replace the algorithm with one which required

N log2(N)

comparison steps .  The new algorithm required a change to the comparison                 instructions which increased the time for each comparison to 200 microseconds .      The managers of Fast Fix tested the work of the two programmers by sorting sets of 100 items using each of the new routines .  The fastest new routine was used in         their software product .

(a)      What were the results of the test, and which new routine was chosen for use?

(b)      In everyday use, the software product was required to sort sets of 100,000

items .  How long would the sorting part of its work take using

i)             The original algorithm

ii)            Smiths new algorithm

iii)          Jonesnew algorithm

(c)      Write down the time complexity of each of the three algorithms using the big O’ notation .

(d)      Had the managers made the correct choice?  What choice should the             managers have made if their product was only required to sort sets of 1000 items .

This question relates to instructions and programs for the MASM computer .  The instruction set for MASM is as follows

Mnemonic

Function

mov  O1, O2

move O2 to O1

cmp  O1, O2

Compare the values in O1 and O2 and set flags

jge  lab

If  flags  set  to  greater  than  or  equal  to  then  jump  to command at lab

jmp  lab

Unconditional jump to command at lab

sub  O1, O2

O1 = O1 – O2

add  O1, O2

O1 = O1 + O2

Assume that memory address  10 initially contains the value  12, memory address 14 initially contains the value 4, and memory address  18 initially contains the      value – 6.

(a)   What values will be stored in memory locations  10,  14 and  18 after the following

MASM code has been executed?

mov   eax, 10

add    eax, 4

push   eax

mov    [eax], eax

(b)   Write down the sequence of values stored in memory locations Q and S as the

following MASM code fragment is executed

main:

mov

mov

eax,

ebx,

[S]

[Q]

HERE:

dec

eax

 

 

cmp

jge

eax,

END

0

 

mov

sub

mov

jmp

[S],

ebx,

[Q],

HERE

eax

[R]

ebx

END:

ret

 

 

.data:

Q:     dd        21

R:     dd        5

S:     dd        4

T:    dd       1

(d)   Write down a C# statement (or set of statements) that is equivalent to the MASM code in (c) .

 

7.     (a)    Draw a circuit diagram for a circuit with two inputs which produces the result  1 when both inputs are the same and 0 when they are not .

(b)   Write down the truth table for the following circuit diagram .

A

B

 

Out 2

Out 1

(c)    Suggest more appropriate labels for the inputs and outputs that reflect the purpose of the circuit and a name for the circuit itself.

(d)   Draw a high-level circuit diagram for a clocked R-S flip-flop and describe its purpose .

8.     (a)   What would be the output of the following program?

static void mystery (int[] a, int c, int d)

{

if (c > d) return;

Console .Write(a[d] .ToString() + " ");

mystery(a, c, d-1);

}

static void Main(string[] args)

{

int[] b = new int[] {4,2,1,7,6,3,5,8};

mystery(b, 0, 7);

Console .WriteLine();

}

(b)   What would be a more appropriate name for the mystery method?

(c)    A user has entered  n integers into an array   a.  Write a method array_sorter            which searches the array for the smallest element, swaps it with the element in the first position in the array and then calls itself recursively to sort the last n- 1               elements of the array .

9.     (a)    Convert the following decimal (base  10) numbers to their binary (base 2) equivalents

(i)        103 (ii)      287

(b)   Convert the following binary (base 2) numbers to their decimal (base  10)

equivalents

(i)        1111011 (ii)       1010101

(c)    What is the range of integers that can be stored in 8 bits using the following binary number representations? Express your answer in decimal .

(i)       sign-magnitude form (ii)      2’s complement

(d)   Convert the following signed decimal (base  10) numbers to

(i)       8-bit sign-magnitude form binary numbers (ii)      8-bit 2’s complement binary numbers

-50

+31

10.           The following declarations occur in a C# program

string filename = “data.dat;

BinaryReader infile;

(a)   Write a segment of C# code to open the file data .datfor input using the infile

object .  Include code to catch any exceptions that might occur, for example, the file did not open correctly .

(b)   The file "data .dat" contains orders that a greengrocer receives for produce . Each

order consists of some number of items .  Each item takes the form of a weight in    kilograms (a double) and a type of produce (a string) .  Write a short sequence of C# statements to read in the file and count how many items there are ordered in the    file .  Print the number of items in a textbox called numItems before ending .  Make   sure that you detect when the end of the data has been reached .

11.  (a)    Perform the following binary additions

(i)           01100111                                    (ii)              10011111 + 01011100                                                    + 00100001

(b)   Using 2's complement representation where necessary, perform the following

arithmetic using binary representation of the decimal values

(i)       25- 16 (ii)       16-25

(c)    Convert the following unsigned binary values to octal (base 8)

(i)        11011011 (ii)      01100010

(d)   Convert the following hexadecimal (base  16) values to binary

(i)       B6 (ii)      FA

12.  (a)    Describe the difference between the following two data structures

string[] theseAllblacks = {

"Christian Cullen", "Ron Cribb","Jonah Lomu",

"Andrew Mehrtens", "Justin Marshall", "Tana Umaga",

"Todd Blackadder"

};

List<string> thoseAllblacks = new List<string>();

thoseAllblacks.Add("Christian Cullen");

thoseAllblacks.Add("Ron Cribb");

thoseAllblacks.Add("Jonah Lomu");

thoseAllblacks.Add("Andrew Mehrtens");

thoseAllblacks.Add("Justin Marshall");

thoseAllblacks.Add("Tana Umaga");

thoseAllblacks.Add("Todd Blackadder");

(b)   What is the purpose of an assertion in a C# program?

(c)    Given the following program fragment

Contract.Assert(0 <= i && i < 7);

Console.WriteLine(theseAllblacks[i]);

what will happen if the value of i is

(i)

(ii)      7

extra .     A computer game records information about each of the players who use it .  Player

information consists of: the player’s name, his/her high score to date, and the      difficulty setting that the player plays the game on (easy, medium, hard or super- hard) .

(a)   Write a class definition for player information, using an enumerated type to

represent the player’s difficulty level, along with a constructor to create a new     player information object, which takes the player’s name as the parameter .  The default high score is 0 and the default level is easy .

(b)   Write a method for updating the high score of a player information structure .  It

should take the new high score as a value parameter .  However, the new high score should only be assigned to the structure if it is less than  100,000 .  If it exceeds         100,000, the high score should be reset to 0 and the player’s level should increase  by  1 instead .

extra .     You have to write a method to count the number of xcharacters in a string .  For

example, hhxxdxa” passed to the method would return 3.  Here is a recursive definition:

“The number of x’s in a string is defined as  1 if the first character in the string is an x, or 0 if the first character in the string is not an x, PLUS the number of x’s in the remainder of the string .”

Turn this recursive definition into a C# recursive method .

extra .     Write a C# console application that takes the names of two files as command line

arguments, opens the named files as input text files, reads and compares them line by line, and writes to the console  true” if the contents of the two files are              identical,  false” under any other circumstance (including, for example,                 situations where files can’t be opened) . The following is an example of how this          application might be called:

H:>compare first.txt second.txt

extra .     A circuit takes four inputs and has two outputs .  The purpose of the circuit is to

encode the inputs into a binary number representing the input line that is on . Here is the truth table:

INPUTS

OUTPUTS

0001

00

0010

01

0100

10

1000

11

(Assume there can never be any other inputs other than those shown .)

Draw a circuit diagram for a circuit with four inputs which produces this result .