• No results found

DS-second-exam-04-1-2012-v-c-solution.docx

N/A
N/A
Protected

Academic year: 2020

Share "DS-second-exam-04-1-2012-v-c-solution.docx"

Copied!
5
0
0

Loading.... (view fulltext now)

Full text

(1)

Petra University

(Private Accredited University)

Faculty of Information Technology Department of Computer Science

ارتبلا ةعماج

)ةدمتعم ةصاخ ةعماج(

تامولعملا ايجولونكت ةيلك

بوساحلا ملع مسق

Course Title: Data Structures

601221

:بلاطلا مسا

Instructor Name: Dr Bassam Haddad

Mr. Abed alkareem

Albana

Dr Mohammed Abu Arqoub

Second

Exam

:يعماجلا مقرلا

Date: 04-01-2012

:ةبعشلا

Time: 15:00-16:00

2011-2012 (1)

C

Part I.: 7 Part II.: 13

20

:عومجملا

:ةجردلا

Part I. )7 Points( Multiple Choice Questions. Choose the best answer.

Instructions: Use the Answer Table located at the end of this test part for your answers. Use Capital Letters only for answers ( If none ,write additionally the correct Answer(.

Q1.Consider the following pseudocode:

declare a stack of characters

while ( there are more characters in the word to read ) {

read a character

push the character on the stack }

while ( the stack is not empty ) {

pop a character of the stack write the character to the screen }

What is written to the screen for the input "carpets"?

A. steprac

B. ccaarrppeettss C. serc

D. carpets

E. None=

Q2. In the linked list implementation of the stack class, where does the push method place the new entry on the linked list

?

A. After all other entries that is greater than the new entry. B. After all other entries that is smaller than the new entry. C. At the head

D. At the tail

E. None=

Q3. In the array version of the Stack class, which operations require linear time O(n) for their worst-case behavior?

A. pop

B. push when the stack is below capacity C. is_empty

D. peek

(2)

Q4.What is the value of the postfix expression 6 3 2 4 + - *:

A. Something between 5 and -5 B. Something between 5 and 15 C. Something between -15 and -100 D. Something between -5 and -15

E. Something between 15 and 100

Q5. Here is an infix expression: 4+3*(6*3-12). Suppose that we are using the usual Stack algorithm to convert the expression from infix to postfix notation. What is the maximum number of symbols that will appear on the stack AT ONE TIME during the conversion of this expression?

A. 3

B. 4

C. 1

D. 2

E. 5

Q6. One difference between a queue and a stack is:

A. Queues use two ends of the structure; stacks use only one. B. Stacks use two ends of the structure, queues use only one. C. Queues require linked lists, but stacks do not.

D. Stacks require linked lists, but queues do not.

E. None

Q7. If the characters 'D', 'C', 'B', 'A' are placed in a queue (in that order, first ‘D’ then ‘C’ and so on…), and then removed one at a time, in what order will they be removed?

A. DCAB

B. DCBA

C. ABCD

D. ABDC

E. None=

Q8. Suppose we have a circular array implementation for a queue with one unused location, with ten items in the queue stored at data[2] through data[11]. The current capacity is 42. What does the frontIndex and the backIndex equal?

A. frontIndex=2 and backIndex=11 B. frontIndex=0 and backIndex=42 C. frontIndex=41 and backIndex=0

D. frontIndex=1 and backIndex=11 debends on implementation

E. None=

Q9. If data is a circular array queue of CAPACITY elements, and rear is an index into that array, what is the formula for the index after rear?

A. (rear + 1) % CAPACITY B. rear + (1 % CAPACITY) C. (rear % 1) + CAPACITY D. rear % (1 + CAPACITY)

(3)

Q10. I have implemented the queue with a circular array of size 6, keeping track of front, rear, and counter (the number of items in the array). Suppose front =0, and rear= arraySize-1. What the value of the counter?

A. Counter= arraySize-1.

B. Counter=(front+1)%arraySize C. Counter=0.

D. Counter=arraySize; E. None of the above.

Q11. Consider this method declaration:

void quiz(int i) { if (i > 1) { quiz(i / 2); quiz(i / 2); }

System.out.print("*"); }

How many asterisks are printed by the method call quiz(5)?

A. 7

B. 8

C. 3

D. 4

E. Some other number

Q12. Consider the following method:

public static void test_b(int n) { if (n>0)

test_b(n-2);

System.out.println(n + " "); }

What is printed by the call test_b(4)?

A. 2 4

B. 4 2

C. 0 2 4

D. 0 2

E. 4 2 0

Q13. A recursive method defining the Fibonacci number n 2 , Fabio(n) is A. Fabio(n) =Fabio(n-1) + Fabio(Fabio n-2))

B. Fabio(n) =Fabio(2n) + Fabio(n) C. Fabio(n) =Fabio(n-1) + Fabio(1-n) D. Fabio(n) =Fabio(n-1) + Fabio(n-2)

E. Non=

Q14. In a circular Array implementation for a queue with one unused location, the queue is full when:

A. frontIndex = =((backIndex +3) modulus queue length B. frontIndex equals (backIndex +2) modulus queue Size

C. A+B

(4)

Q1.(4 points ) Implement the following recursive method. Do not use any local variables or

loops.

public static void pattern(int n) // Precondition: n > 0;

// Postcondition: The output consists of lines of integers. The first line // is the number n. The next line is the number 2n. The next line is // the number 4n, and so on until you reach a number that is larger than

// 4242.

/* Example output with n = 840: 840

1680

3360

6720

public static void pattern(int n){ if(n>0)

{

System.out.println(n); if(n<4242)

pat(2*n); }

}

Q2. (5 points)

a( Let S1 and S2 be stacks containing comparable objects. Write at the client

Level a method TheSame that is capable of determining whether S1 and S2 are identical or not (4P)

b( what is the Big-Oh of your method? (1P)

Hint:

1) Any two stacks considered the same, if the number and the content of the objects stored in the two stacks are equal.

2) Let your method returning 1 if the two stacks are the same otherwise 0

Such as

public static int theSame( ArrStack S1, ArrStack S2) {……}

a.

public static int theSame( ArrStack S1, ArrStack S2)

{ int result =1;

while(!s1.isEmpty() && !s2.isEempty())

{ if(s1.pop().compareTo(s2.pop())!=0)

Return 0; // by content

}

if(s1.isEmpty() && s2.isEmpty())

return 1;

else

(5)

b. O(n )

Q3. (4 points) At implementation level implement the following method as a new method

for the Queue –linked based. (Use the usual node definition with member

variables called data and next.)

Public Object getBack( );

// Precondition: frontNode is the head pointer of a queue. // The queue might be empty or it might be non-empty.

// Postcondition: The return value is the object stored at the rear node of the

// queue.

// The queue itself is unchanged.

public Object getBack() { Object back = null;

if (!isEmpty())

back = backNode. data; return back;

} // end getFront

Answer Table of Part I: Note Use only Capital Letters for your answers.

Q

1 2 3 4 5 6 7 8 9 1

0

1

1

1

2

1

3

1

4

Answ

er

A C E C B B B D A A A C D E

References

Related documents

Accessing elements of a structure Initialization of structure data Using pointers to access structures

This workers’ compensation dispute was filed by claimant, Jerry Perez, against his employer, Express Jet, alleging injury to his neck via an accident that occurred on October 21,

○ If BP elevated, think primary aldosteronism, Cushing’s, renal artery stenosis, ○ If BP normal, think hypomagnesemia, severe hypoK, Bartter’s, NaHCO3,

Results suggest that the probability of under-educated employment is higher among low skilled recent migrants and that the over-education risk is higher among high skilled

Spray bottle of parfum de versailles tarif even though i can tell you make the versailles orangerie du château de toilette bottle of civet.. Need to fit tarif shocking stems from

In this PhD thesis new organic NIR materials (both π-conjugated polymers and small molecules) based on α,β-unsubstituted meso-positioning thienyl BODIPY have been

* Corresponding author. E-mail address: [email protected].. Controversial too are the empirical findings on the effectiveness of social learning, once contextual variables and

In animals that recovered one and two weeks following blast injury there was virtually no difference in the average number of parvalbumin positive cells in the visual cortex..