代写Algorithms and Data Structures MOCK EXAM代写留学生Java程序

Algorithms and Data Structures (M)

MOCK EXAM

Question 1

Parts (i) – (ii) refer to the algorithm shown in Box 1. The algorithm takes an array a and searches it for a substring that matches a shorter array b. This uses an auxiliary algorithm that compares two subarrays of equal length.

Main algorithm:

To search the array a[0 n-1] for a subarray that matches b[0 m-1], where m < n:

1. For i = 0, , n-m, repeat:

1.1. Ifa[i i+m-1] matches b[0 m-1], terminate with answer i.

2. Terminate with answer none.

Auxiliary algorithm:

To test whether a[i i+m-1] matches b[0 m-1]:

1. For j = 0, , m-1, repeat:

1.1. Ifa[i+j] ≠ b[j], terminate with answer false.

2. Terminate with answer true.

Box 1 An algorithm to search an array for a matching subarray.

For example:

Here the answer should be 1 (the index of the leftmost matching substring in a).

(i)        What is the average time complexity of the auxiliary algorithm in terms of the length n of the subarray a?

(a) O(n)

(b) O(n2)

(c) O(log n)

(d) O(1)

(ii)       If array a is as above but b is

What answer should the main algorithm give?

(a) 8

(b) 4

(c) -1

(d) none

Parts (iii) - (iv) refer to the Java code shown in Box 2.

public class ListExample {

public static void pListSet (List<Integer> l, Set<Integer> s){

for(Integer i:l) System.out.print (i + " ");

System.out.print ("; ");

for(Integer j:s) System.out.print (j + " ");

}

public static void main(String[] args) {

List<Integer> myList = new ArrayList<Integer>(7);

Set<Integer> mySet = new TreeSet<Integer>();

int temp = 0;

for(int i=0;i<6;i++){

if(i%2==0) temp = 8 - 2*i;

else temp = 10 - 2*i;

myList.add (i,temp);

mySet.add (temp);

}

printListSet (myList,mySet);

}

}

Box 2 Java code for list example

(iii) What is the output from running the main method?

(a) 8 8 4 4 0 0 ; 8 4 0

(b)  8 8 4 4 0 0 ; 0 4 8

(c) 8 4 0 ; 0 4 8

(d)  8 8 4 4 0 0 ; 0 0 4 4 8 8

(iv)      If the line initialising mySet is replaced by the line:

Set mySet = new HashSet();

What would the output be now?

(a)  8 8 4 4 0 0 ; 8 4 0

(b)  It is not possible to predict the order without knowing the hash function used

(c)  8 4 0 ; 0 4 8

(d)   8 8 4 4 0 0 ; 0 0 4 4 8 8

(v)      Let T(n) denote the number  of comparisons required to sort an array ofsize n using the  mergesort  algorithm.  Assuming  that  n-1  comparisons  are  required  to merge two arrays of total length n together, which of the following recursive expressions correctly describes T(n):

(a) T(n) = 2T(n-1) + 1, T(1) = 0

(b) T(n) = 2T(n/2), T(1) = 0

(c) T(n)=2T(n/2) + n-1, T(1) = 0

(d) T(n) = 2T(n/2) + T(n-1), T(1) = 0

(vi)        What are the best-case time and space complexities of the mergesort algorithm?

(a) O(n2) and O(n)

(b) O(n2) and O(2n)

(c) O(n log n) and O(n log n)

(d) O(n log n) and O(n)

(vii)     Which of the following  trees are binary search trees and would result from

inserting the values 9, 4, 7, 5, 1, 3, 8, 11 into an empty Binary Search tree in  the given order (i.e. without balancing)?

(viii)      Which  of  the  following   (unbalanced)  Binary   Search  Trees  would  result  from deleting the value 7 from the search tree depicted as (d) in part (i)?

(ix)        Which of the following trees is an AVL tree that would result from inserting the   values 9, 4, 7, 5, 1, 3, 8, 11 into an empty AVL tree in the given order (i.e. with balancing)?

(x)        Which of the following sequences correspond to a pre-order traversal of the search tree depicted in (c) above?

(a) 8, 5, 3, 1, 4 ,11, 9, 7

(b) 1, 3, 4, 5, 8, 7, 9, 11

(c)  1, 4, 3, 5, 7, 9, 11, 8

(d) 3, 1, 4, 5, 11, 9, 7, 8

Question 2

Parts (i) - (ii) refer to the algorithm shown in Box 3 to change the order of

elements in an array a containing copies of the Strings “red”, “yellow ” and

“green”. Values left and Right denote the smallest and largest indices of  a.

1. Set g to left, set r to left, and set y to left.

2. While r ≤ right, repeat:

If a[r] is “red” :

Increment r.

Else

if a[r] is “green”

if r > g

swap a[r] with a[g] increment g and r

Else

if a[r] is “yellow” :

if r > y

swap a[r] with a[y].

if r > g, swap a[r] with a[g].

Increment y, g, and r.

3. Terminate.

Box 3 Algorithm to reorder elements in an array

(i)  If the algorithm is applied to the array a below:

a = ["green", "yellow", "red", "green", "red"]

What  is the final state of a?

(a)  ["green", "yellow", "red", "green", "red"]

(b)  ["yellow", "green", "green", "red", "red"]

(c)  ["green", "green", "yellow", "red", "red"]

(d)  ["red", "red", "yellow", "green", "green"]

(ii)      What is the time complexity of the algorithm of part (i), given that swapping  two elements of the array is a constant time operation?

(a) O(n2)

(b) O(n)

(c) O(log n)

(d) O(1)

(iii)           Consider the following sorted array:

If linear search is used to locate the string “pear”, what are the strings contained in the first 3 array positions visited?

If required, you may assume that, for an array with smallest and largest indices  left and right respectively, the index approximately at the middle of the array is calculated as the integer part of   (left+right)/2.

(a) “melon”, “raspberry”, “pear”

(b) “satsuma”, “raspberry”, “pear”

(c) “melon”, “pear” (no further positions need to be visited)

(d) “apple”, “banana”, “grape”

(iv)         If binary search is instead used to search the array from part (xiii), what are the strings contained in the first 3 array positions visited?

If required, you may assume that, for an array with smallest and largest indices  left and right respectively, the index approximately at the middle of the array is calculated as the integer part of   (left+right)/2.

(a) “melon”, “raspberry”, “pear”

(b) “satsuma”, “raspberry”, “pear”

(c) “melon”, “pear” (no further positions need to be visited)

(d) “apple”, “banana”, “grape”

(v)          Java’s  generic Iterator interface,  Iterator supports  which  of the  following sets of methods?

(a) hasNext(),next(),remove()

(b) getFirst(),next(),addLast()

(c) hasNext(),getFirst(),remove()

(d) getFirst(),next(),remove()

(vi)          This question involves the deletion of a node from a non-empty doubly linked list L with first node first. Any Node N consists of an element (N.elem) and references to the next node in the list (N.next) and the previous node in the list, (N.pred).

Assuming that del is the node to be deleted, which pair of steps from the following list, when performed in the given order, will delete the node from the list?

(a) A, C

(b) B,D

(c) A,D

(d) C,B

(vii)   Suppose that two binary search trees BST1  and BST2  store sets S1  and S2 respectively, where  S1   and  S2   have  size n. What are the average and worst time complexities respectively for determining if the sets are equal (i.e., they contain the same elements)? You may assume that the average height of a BST containing n elements is O(log n).

(a) O(n log n) and O(n2)

(b) O(n log n) and O(n log n)

(c) O(n2) and O(n2)

(d) O(n2) and O(n log n)

(viii)    Suppose that a new node N is inserted into an AVL tree using standard Binary Search Tree insertion prior to balancing and that k1 is the deepest node in the tree to become unbalanced by the insertion. Let k2 be the right child ofk1 and  k3 the left child ofk2, where k2 and k3 are the next nodes on the path from k1 to N.

Consider the following actions:

A: single right rotation about k1

B: single left rotation about k1

C: single right rotation about k2

D: single left rotation about k2

Which sequence of actions when performed in the given order will balance the tree?

(a) C, A

(b) B

(c) C, B

(d) D, A

(ix)     Suppose that in part (viii), k3 were the right child ofk2. Which of the actions A-D, when performed in the given order will balance the tree?

A: single right rotation about k1

B: single left rotation about k1

C: single right rotation about k2

D: single left rotation about k2

(a) C, A

(b) B

(c) C, B

(d) D, A

(x)    Which of the following statements is not true?

(a) the Queue ADT is a special case of the List ADT

(b) the Stack ADT is a special case of the List ADT

(c) the Stack ADT is a special case of the Queue ADT

(d) A Java ArrayList is one of several implementations ofthe List ADT

Question 3

(i)    Suppose that we use a closed bucket hash table H of size 32 to store a set of 4567 words. The load factor of H is:

(a) 32/4567

(b)  4567 - 32

(c)  32 - 4567

(d)  4567/32

(ii)     If a hash table is used to represent a Set of integers, which of the following statements about the hash function is true?

(a) The hash function should always leave some buckets empty

(b) The hash function should ensure that  data is spread evenly and thinly throughout the table

(c) The hash function should always ensure that a prime number of buckets are used

(d)  The hash function should always be applied to the first few digits of the inputs

(iii)      Consider a hypothetical university in which each student-number is a string of 4  decimal digits. The student records are to be stored in a closed-bucket hash-table of size 7, in which the keys are student-numbers. Assume the hash function is:

hash(k) = remainder on division by 7 of the last digit of k

So for example, hash(0001) = 1 and hash(3457) = 0. Starting with an empty hash- table, the following  student-numbers are added to the hash table: 8001, 7008, 0002, and 1443. What is the number of comparisons required when the resulting hash-table is searched for the student-number 4158?

(a) 2

(b) 1

(c) 0

(d) 4

Parts (iv)-(vi) refer to the maps map1 and map2 shown in Box 4.

Box 4 Maps map1 and map2

Box 4 shows two maps used to store details of library books.  In each map the keys denote the book code, and the values the book title.

(iv)      Suppose that some Java code involves the instantiation of both maps as empty

HashMaps with initial size 20 and their population with the data above, followed by the following code fragment:.

map1.putAll(map2);

System.out.println(map1);

What is output when the code is run?

(a)  {5=The Little Prince, 9=Pollyanna, 10=The Gruffalo, 16=The Borrowers}

(b) {2=[Desert Island, Jane Eyre], 5=The Little Prince,

7=[Snow White, The BFG], 9=Pollyanna,10=The Gruffalo, 16=The Borrowers}

(c) {2=Jane Eyre, 5=The Little Prince, 7=The BFG,

9=Pollyanna,10=The Gruffalo, 16=The Borrowers}

(d) {2=Desert Island, 5=The Little Prince, 7=Snow White, 9=Pollyanna,10=The Gruffalo, 16=The Borrowers}

(v)          If the Java code fragment  in question (iv) is replaced by:

map1.remove(16,map1.get(16));

System.out.println(map1);

what is output when the full code is run?

(a)  There is a syntax error, as the method get(int) is undefined for the type Map

(b) An exception is thrown as map1 has no entry with key 16

(c) {} (the empty map)

(d) {2=Desert Island, 7=Snow White, 9=Pollyanna, 10=The Gruffalo}

(vi)      If the code fragment were instead replaced with:

for(String title:map1.values()) System.out.print(title+", ");

what is output when the full code is run?

(a) there is an error - we cannot iterate over map1.values

(b) there is an error - there is no Map method values()

(c)  Desert Island, Snow White, Pollyanna, The Gruffalo,

(d) Snow White, Pollyanna, The Gruffalo, Desert Island,

(vii)     Which of the following would we use to declare a class named A with a single generic type?

(a) public class A { ... }

(b) public class A { ... }

(c) public class A(E) { ... }

(d) public class A(E, F) { ... }

(viii)    Which of the following methods is not in the Collection interface?

(a)        clear()

(b)       isEmpty()

(c)        size()

(d)       getSize()

(ix)      If list is a LinkedList that contains 1 million int values and A and B are the following fragments of code:

A:

for (int i = 0; i < list.size(); i++)

sum += list.get(i);

B:

for (int i: list)

sum += i;

Which of the following statements is true?

(a) Code fragment A runs faster than code fragment B

(b)  Code fragment B runs faster than code fragment A

(c)  Code fragment A runs as fast as code fragment B

(d) It is impossible to tell which of A or B is faster without knowledge of the size of the input integers

(x)       What is the output from the following code?

import java.util.*;

public class Test {

public static void main(String[] args) {

Set set = new HashSet();

set.add(new A());

set.add(new A());

set.add(new A());

set.add(new A());

System.out.println(set); }

}

class A  {

int r = 1;

public String toString() {

return r + "";

}

public int hashCode() {

return r;

}

}

(a)  [1]

(b)  [1, 1]

(c)   [1, 1, 1]

(d)  [1, 1, 1, 1]




热门主题

课程名

mktg2509 csci 2600 38170 lng302 csse3010 phas3226 77938 arch1162 engn4536/engn6536 acx5903 comp151101 phl245 cse12 comp9312 stat3016/6016 phas0038 comp2140 6qqmb312 xjco3011 rest0005 ematm0051 5qqmn219 lubs5062m eee8155 cege0100 eap033 artd1109 mat246 etc3430 ecmm462 mis102 inft6800 ddes9903 comp6521 comp9517 comp3331/9331 comp4337 comp6008 comp9414 bu.231.790.81 man00150m csb352h math1041 eengm4100 isys1002 08 6057cem mktg3504 mthm036 mtrx1701 mth3241 eeee3086 cmp-7038b cmp-7000a ints4010 econ2151 infs5710 fins5516 fin3309 fins5510 gsoe9340 math2007 math2036 soee5010 mark3088 infs3605 elec9714 comp2271 ma214 comp2211 infs3604 600426 sit254 acct3091 bbt405 msin0116 com107/com113 mark5826 sit120 comp9021 eco2101 eeen40700 cs253 ece3114 ecmm447 chns3000 math377 itd102 comp9444 comp(2041|9044) econ0060 econ7230 mgt001371 ecs-323 cs6250 mgdi60012 mdia2012 comm221001 comm5000 ma1008 engl642 econ241 com333 math367 mis201 nbs-7041x meek16104 econ2003 comm1190 mbas902 comp-1027 dpst1091 comp7315 eppd1033 m06 ee3025 msci231 bb113/bbs1063 fc709 comp3425 comp9417 econ42915 cb9101 math1102e chme0017 fc307 mkt60104 5522usst litr1-uc6201.200 ee1102 cosc2803 math39512 omp9727 int2067/int5051 bsb151 mgt253 fc021 babs2202 mis2002s phya21 18-213 cege0012 mdia1002 math38032 mech5125 07 cisc102 mgx3110 cs240 11175 fin3020s eco3420 ictten622 comp9727 cpt111 de114102d mgm320h5s bafi1019 math21112 efim20036 mn-3503 fins5568 110.807 bcpm000028 info6030 bma0092 bcpm0054 math20212 ce335 cs365 cenv6141 ftec5580 math2010 ec3450 comm1170 ecmt1010 csci-ua.0480-003 econ12-200 ib3960 ectb60h3f cs247—assignment tk3163 ics3u ib3j80 comp20008 comp9334 eppd1063 acct2343 cct109 isys1055/3412 math350-real math2014 eec180 stat141b econ2101 msinm014/msing014/msing014b fit2004 comp643 bu1002 cm2030
联系我们
EMail: 99515681@qq.com
QQ: 99515681
留学生作业帮-留学生的知心伴侣!
工作时间:08:00-21:00
python代写
微信客服:codinghelp
站长地图