+ All Categories
Home > Documents > Note: Please write down how you derive your answers in detail...

Note: Please write down how you derive your answers in detail...

Date post: 23-Jan-2021
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
16
CS135602 Introduction to Information Engineering Midterm Exam 2 (2012/12/03 10:10~12:00) Total 110 points Note: Please write down how you derive your answers in detail even if the question is simple. 1. (20%) Answer the following questions (a) (6%) Please explain the differences among a user-defined data type, an abstract data type and a class. (b) (4%) Please explain privileged instructions. (c) (4%) Please explain the differences between syntax and semantics. (d) (6%) Please explain the differences among an algorithm, a program and a process. Answer: (a) A user-defined data type is description of data organization, whereas an abstract data type includes operations for manipulating data. Class is an abstract data type with extra features: inherit, encapsulated, initial constructor method. user-defined data type:只有 data 的基本資料結構描述。 abstract data type :同時含有資料的表示方法和資料處理運算的資料結構, queue stack (含有 push or pop)。所謂「抽象化」是指使用一種名稱 代表一個複雜的物件,其目的就是指描述主要特徵而隱藏大部份的次要 特徵,可以將所有複雜的物件加以簡化。 class:為 abstract data type 之延伸,多了一些性質如繼承、封裝等。 (b) Privileged instructions are machine language instructions that request activities that general application programs should not be allowed to perform. These instructions can only be executed when the CPU is operating in “privileged mode.” Examples of privileged instructions include requests to change the CPU’s current privilege level and requests to alter registers that control memory access limits. 不允許用戶程序中直接使用的指令稱為「特權指令」,由於這些指令誤用 可能會導致整個系統崩潰,因此這些指令只允許 OS 或某些相關模組使 用。例如:I/O 指令、設置時鐘和置控制暫存器等指令都是特權指令。 (c) Syntax refers to the way something is expressed, whereas semantics refers to what is being expressed. 語法:表達方式(如何表達) 語意:表達的意義內含
Transcript
Page 1: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

CS135602 Introduction to Information Engineering

Midterm Exam 2 (2012/12/03 10:10~12:00) Total 110 points

Note: Please write down how you derive your answers in detail even if the question is simple.

1. (20%) Answer the following questions

(a) (6%) Please explain the differences among a user-defined data type, an

abstract data type and a class.

(b) (4%) Please explain privileged instructions.

(c) (4%) Please explain the differences between syntax and semantics.

(d) (6%) Please explain the differences among an algorithm, a program and a

process.

Answer:

(a) A user-defined data type is description of data organization, whereas an

abstract data type includes operations for manipulating data. Class is an

abstract data type with extra features: inherit, encapsulated, initial constructor

method.

user-defined data type:只有 data的基本資料結構描述。

abstract data type:同時含有資料的表示方法和資料處理運算的資料結構,

如 queue或 stack (含有 push or pop)。所謂「抽象化」是指使用一種名稱

代表一個複雜的物件,其目的就是指描述主要特徵而隱藏大部份的次要

特徵,可以將所有複雜的物件加以簡化。

class:為 abstract data type之延伸,多了一些性質如繼承、封裝等。

(b) Privileged instructions are machine language instructions that request

activities that general application programs should not be allowed to perform.

These instructions can only be executed when the CPU is operating in

“privileged mode.” Examples of privileged instructions include requests to

change the CPU’s current privilege level and requests to alter registers that

control memory access limits.

不允許用戶程序中直接使用的指令稱為「特權指令」,由於這些指令誤用

可能會導致整個系統崩潰,因此這些指令只允許 OS 或某些相關模組使

用。例如:I/O指令、設置時鐘和置控制暫存器等指令都是特權指令。

(c) Syntax refers to the way something is expressed, whereas semantics refers to

what is being expressed.

語法:表達方式(如何表達)

語意:表達的意義內含

Page 2: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

(d) Process is activity of executing a program, and program is static code and

static data, an algorithm is an ordered set of unambiguous, executable steps

that defines a terminating process.

程序:正在機器上執行的程式。

程式:一些固定的 code和 data (演算法以程式碼方式實際表現)。

演算法:定義一個明確的程序的執行步驟(解決問題的具體步驟)。

Page 3: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

2. (15%) (Operating Systems)

A semaphore S is an integer variable that is accessed only through two standard

atomic operations: Wait and Signal

• Wait(S):

while (S <= 0) {

do_nothing();

}

S = S - 1;

• Signal(S): S = S + 1;

(a) (9%) Observe a railroad crossing, where a semaphore S is used to indicate

whether or not cars can pass the crossing (the critical region in this question).

When trains or cars arrive at the crossing, the code segments in Figure. a and

Figure. b will be executed respectively (that is, Figure. a for each train arrival

and Figure. b for each car arrival) to ensure that trains and cars can pass the

crossing safely. Please complete the code segments by filling in blank_1,

blank_2 and blank_3 with correct answers.

semaphore S = blank_1 ;

if (A train is coming) {

blank_2

blank_3

train_passing();

;

;

// critical region

}

while (S<=0) {

}

wait_for_train();

// wait until S > 0

car_ passing();

Figure. a Figure. b

(b) (6%) Five men sit around a table with five chopsticks to have dinner. A man

can’t have dinner until he gets both the right chopstick and the left chopstick.

Suppose that each man’s behavior follows the code below, and all of them

will start the action at the same time. Please explain in detail why these five

men will suffer from a deadlock.

Page 4: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

semaphore chopstick[5] = {1,1,1,1,1};

for each man i:

do {

wait(chopstick[i]); // wait for the right chopstick

wait(chopstick[(i+1) % 5]); // wait for the left chopstick

dinner();

signal(chopstick[i]); // release the right chopstick

signal(chopstick[(i+1) % 5]); // release the left chopstick

} while (1);

Answer:

(a) blank_1: S = 1

blank_2: Wait(S) (直接寫 S = 0 或 S = S – 1也給對)

blank_3: Signal(S) (直接寫 S = 1 或 S = S + 1也給對)

(b) 大家都拿到右邊的筷子但等不到左邊的筷子造成 deadlock。

Page 5: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

3. (6%) (Algorithms)

According to the following code segment, answer the questions (a) and (b).

Sum ← 0;

K ← 0;

While ( K < N ) do {

K ← K + 1;

Sum ← Sum + K;

}

(a) (3%) Please define a loop invariant for this code segment.

(b) (3%) Please use your loop invariant to verify that when the routine

terminates, sum will indeed be assigned the value 0 + 1 + 2 + … + N.

Answer:

(a) “Sum=1+2+…+K and K less than or equal to N” is true each time the test

for termination is conducted.

(b) Combining “Loop Invariant : K <= N, Sum=1+2+…+N” and “Termination

Condition : K >= N” produces the desired conclusion “Sum=1+2+…+N”,

Because K is initialized at zero and increased by 1 each time through the

loop, its value must ultimately reach that of N.

Page 6: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

4. (14%) (Algorithms)

The bubble sort is a well-known sorting algorithm. It is based on the process of

repeatedly comparing two adjacent names and interchanging them if they are not

in the correct order relative to each other. Suppose that the list has n entries. The

bubble sort would begin by comparing (and possibly interchanging) the entries in

positions n and n–1. Then, it would consider the entries in positions n–1 and n–2,

and continue moving forward in the list until the first and second entries in the list

had been compared (and possibly interchanged). Observe that this pass through

the list will pull the smallest entry to the front of the list. Likewise, another such

pass will ensure that the next to the smallest entry will be pulled to the second

position in the list. Thus, by making a total of n–1 passes through the list, the

entire list will be sorted.

(a) (9%) Please complete the following pseudocode to express a procedure for

sorting a list of names using the bubble sort algorithm.

Procedure sort (List)

N ← the length of list;

(b) (5%) Please analyze the efficiency of your algorithm in terms of the Big-O

notation.

Answer:

(a)

procedure sort ( List )

N ← the length of List;

while ( N is greater than 1 ) do

( J ← the length of List;

while ( J is greater than 1 ) do

( if ( the entry in position J is less than the entry in position J-1 )

Then ( interchange the two entries );

J ← J-1)

N ← N-1)

Page 7: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

(b)

Assume the length of List is n.

For outer loop :

N=n, inner loop will execute n-1 times.

N=n-1, inner loop will execute n-1 times.

N=2, inner loop will execute n-1 times.

Total execute (n − 1)(n − 1) = n2 − 2n + 1 = O(n2)

Page 8: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

5. (10%) (Algorithms)

Please complete the following function in terms of a recursive structure to derive

𝐶𝑚𝑛 (assume m<=n). ( Hint : 𝐶𝑚

𝑛 = 𝐶𝑚𝑛−1 + 𝐶𝑚−1

𝑛−1 )

int C(int n,int m) //return the value of 𝐶𝑚𝑛

{

if (m==1)

else if (m==n || m==0)

else

}

Answer:

(a) return n;

(b) return 1;

(c) return C(n-1, m) + C(n-1, m-1);

(a)

(b)

(c)

Page 9: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

6. (7%) (Data Abstractions)

(a) Suppose a queue implemented in a circular fashion is in the state shown in

the diagram below. Draw a diagram showing the structure after the letters G

and R are inserted, three letters are removed, and the letters D and P are

inserted.

(b) What error occurs in part (a) if the letters G, R, D, and P are inserted before

any letters are removed?

Answer:

(a)

(b) The size of the queue would exceed the space allotted to it.

Page 10: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

7. (8%) (Data Abstractions)

The following table represents a portion of a linked list in a computer’s main

memory. Each entry in the list consists of two cells: The first contains a letter of

the alphabet; the second contains a pointer to the next list entry.

(a) Please alter the pointers so that the letter N is no longer in the list.

(b) Please replace the letter N with the letter G and alter the pointers so that the

new letter appears in the list in its proper place in alphabetical order.

Address Contents

30 J

31 38

32 B

33 30

34 X

35 46

36 N

37 40

38 K

39 36

40 P

41 34

Page 11: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

Answer:

(a) With N removed

Address Contents

30 J

31 38

32 B

33 30

34 X

35 46

36 N

37 40

38 K

39 40

40 P

41 34

(b) After inserting G

Address Contents

30 J

31 38

32 B

33 36

34 X

35 46

36 G

37 30

38 K

39 40

40 P

41 34

Page 12: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

8. (7%) (Data Abstractions)

Assume we use a binary tree to store a list of symbols in alphabetical order.

According to the procedure below, please show the update process of the binary

tree step by step if the list “V, S, T, X, Y, Z, R, U, W” is provided as the input.

procedure Insert(Tree, NewValue){

if(root pointer of Tree = NIL){

(set the root pointer to point to a new leaf containing

NewValue)

}

else(execute the block of instructions below that is

associated with the appropriate case){

case 1: NewValue = value of root node

(Do nothing)

case 2: NewValue < value of root node

if(left child pointer of root node = NIL)

then (set that pointer to point to a new leaf node

containing NewValue)

else (apply the procedure Insert to insert NewValue

into the subtree identified by the left child

pointer)

case 3: NewValue > value of root node

if(right child pointer of root node = NIL)

then (set that pointer to point to a new leaf node

containing NewValue)

else (apply the procedure Insert to insert NewValue

into the subtree identified by the right

child pointer)

}

}

Page 13: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

Answer:

Step 1: Step 2: Step 3:

Step 4: Step 5:

Step 6: Step 7:

Step 8: Step 9:

Page 14: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

9. (8%) (Programming Languages)

Complete the two rules at the end of the Prolog program below so that the

predicate mother (X, Y) means “X is the mother of Y” and the predicate father

(X, Y) means “X is the father of Y.”

female (carol).

female (sue).

male (bill).

male (john).

parent (john, carol).

parent (sue, carol).

mother (X, Y):-

father (X, Y):-

Answer:

mother (X, Y):- parent (X, Y) , female (X).

father (X, Y):- parent (X, Y) , male (X).

Page 15: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

10. (15%) (Programming Languages)

(a) (5%) Please draw a parse tree for the string abaaba based on the syntax

diagram below.

(b) (10%) Please design a syntax diagram for Context 2 below to ensure that

these two syntax diagrams together can correctly describe strings of the

form anb

ma

n, where n and m are both positive integers. (For example,

aabbaa and aabbbaa are possible sentence patterns)

Page 16: Note: Please write down how you derive your answers in detail …wmnet.cs.nthu.edu.tw/Course/IIE_2012/quiz/midterm2... · 2013. 5. 23. · CS135602 Introduction to Information Engineering

Answer:

(a)

(b)


Recommended