ECE340 Final

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

1. Write a FOR loop to sum the values of an array. Assume that all variables are declared. • The name of the array is ArrName. • The size of the array is specified by the constant variable SIZE. • The variable to hold the sum is named sum.

#define SIZE 10 int i; int ArrName[SIZE], sum; for(i = 0; i < SIZE; i++){ sum += ArrName[i]; }

4. Given the array-based list (20, 12, 24, 25), what is the output of ArrayListSearch(list, 25) ? (a) 3 (b) 4 (c) 25 (d) true

(a) 3

7. Which is an abstract data type (ADT)? (a) A list (b) A string (c) A boolean (d) A float

(a) A list

10. Which abstract data type (ADT) is best suited to store the names of all currently available smartphone models? (a) A set (b) An array (c) A linked list (d) A stack

(a) A set

4. Which of the following statements is correct? (a) Every data structure has a specific algorithm to implement a certain operation. (b) Algorithms cannot utilize data structures to store and organize data. (c) The algorithm to append an item in an array is the same as appending an item to a linked list. (d) Data structures define only how data is organized and stored.

(a) Every data structure has a specific algorithm to implement a certain operation.

1. A system call runs in kernel mode only. (a) True (b) False

(a) True

10. Upon termination, all processes in a Unix system transition to Zombie processes. (a) True (b) False

(a) True

5. For a single-processor system, there will never be more than one process in the Running state. (a) True (b) False

(a) True

3. If a queue is implemented as a linked list, an enqueue inserts a new item _____. (a) after the last item (b) before the first item (c) before the last item (d) after the first item

(a) after the last item

13. An algorithm's _____ is the scenario where the algorithm does the minimum possible number of operations. (a) best case (b) best time (c) average case (d) worst case

(a) best case

3. If a stack is implemented as a linked list, then a pop will remove the _____. (a) head node (b) tail node (c) middle node (d) null

(a) head node

2. Which of the following contains the executable code? (a) text section (b) data section (c) heap section (d) stack section

(a) text section

4. If a queue is implemented as a linked list, a dequeue removes _____ node. (a) the head (b) the tail (c) the middle (d) a random

(a) the head

16. The worst-case runtime of an algorithm is _____ number of steps taken to execute a program. (a) the maximum (b) the minimum (c) the average (d) the optimum

(a) the maximum

1. When adding an item to an array-based list with an allocation size equal to the list length, a new array is generally allocated with _____ the current length. (a) twice (b) one more than (c) the same size as (d) one less than

(a) twice

7. An array-based queue's back item index is _____. (a) (frontIndex + length) % allocationSize (b) (frontIndex + length - 1) % allocationSize (c) (frontIndex + length + 1) % allocationSize (d) length - 1

(b) (frontIndex + length - 1) % allocationSize

2. Given the list (24, 36, 48), which is the correct way to add 12 at the start of the list? (a) ArrayListAppend(list, 12) (b) ArrayListPrepend(list, 12) (c) ArrayListInsertBefore(list, 12) (d) ArrayListAppend(list, 0, 12)

(b) ArrayListPrepend(list, 12)

4. The difference between a program and a process is that a program is an active entity while a process is a passive entity. (a) True (b) False

(b) False

14. An algorithm is written to return the first name beginning with "L" in a list of employee names. Which of the following is the algorithm's worst case scenario? (a) The first name in the list begins with "L." (b) No names in the list begin with "L." (c) The last name in the list begins with "L." (d) The first half of the list has names beginning with alphabets before "L," and the second half of the list has names beginning with alphabets after "L."

(b) No names in the list begin with "L."

6. What values are stored in the list numList? numberList() { for (i = 0; i < 10; i++) { if (i % 2 == 0) { numList[i] = i; } } } (a) [1, 2, 3, 4, 5] (b) [0, 2, 4, 6, 8] (c) [2, 4, 6, 8, 10] (d) [1, 3, 5, 7, 9]

(b) [0, 2, 4, 6, 8]

9. Which of the following system calls is used by the parent process to load a new program into the memory space of the child process? (a) fork() (b) exec() (c) wait() (d) exit()

(b) exec()

6. Which of the following data structures is used to manage the processes in Linux? (a) tree (b) linked list (c) hash map (d) stack

(b) linked list

8. A stack abstract data type (ADT) is implemented using a(n) _____ data structure. (a) array (b) linked list (c) heap (d) binary search tree

(b) linked list

2. In a linked list, each node stores a _____ the next node. (a) copy of (b) pointer to (c) child of (d) memory of

(b) pointer to

5. Appending an element in an array involves increasing the array's _____. (a) data type (b) size (c) value (d) item

(b) size

5. If a stack is implemented as a linked list, which XXX would replace the missing statement? StackPop(stack) { XXX ListRemoveAfter(stack, null) return headData } (a) headData = null (b) headData = stack (c) headData = stack⇢tail⇢data (d) headData = stack⇢head⇢data

(d) headData = stack⇢head⇢data

11. Identify the operation that is NOT constant time. (a) if (x > y) return x (b) num = arr[i] arr[i + 1] = num + 1 (c) for (i = 0; i < 10; i++) { sum += num2 } (d) i = 0 while (i < listSize) { sum = sum + i i++ }

(d) i = 0 while (i < listSize) { sum = sum + i i++ }

6. An array-based queue's enqueue operation must resize if _____. (a) frontIndex == 0 (b) frontIndex == allocationSize - 1 (c) length == maxLength (d) length == allocationSize

(d) length == allocationSize

6. What data value does a bounded stack data structure require that an unbounded stack data structure does not? (a) allocationSize (b) array (c) length (d) maxLength

(d) maxLength

4. If a stack is implemented as a linked list, then an empty stack will have _____. (a) non-null head and tail pointers (b) a null head and non-null tail pointer (c) a non-null head and null tail pointer (d) null head and tail pointers

(d) null head and tail pointers

6. In a singly-linked list with 1 element, the tail pointer ____ and the next pointer of the head node ____. (a) points to the last node, points to the first node (b) is null, is null (c) is null, points to the head node (d) points to the head node, is null

(d) points to the head node, is null

2. If a stack is implemented as a linked list, then a push will be a(n) _____ operation. (a) replace (b) insert (c) append (d) prepend

(d) prepend

9. Which abstract data type (ADT) is most suitable to store a list of perishable products such that the product with the nearest expiry date is removed first? (a) A deque (b) A linked list (c) A queue (d) A priority queue

(d) A priority queue

7. Identify the correct way to insert 52 at the end of the following list: (12, 24, 36, 48). (a) ListPrepend(list, node 52) (b) ListInsertAfter(list, node 52) (c) ListInsert (tail, node 52) (d) ListAppend(list, node 52)

(d) ListAppend(list, node 52)

12. The Big O notation of the algorithm 7 + 12N + 3N^2 is _____. (a) 7 (b) 12N (c) 3N^2 (d) N^2

(d) N^2

17. The best case runtime complexity of an algorithm that searches an array of size N is _____ (a) O(N) (b) O(N - 1) (c) O(N / 2) (d) O(1)

(d) O(1)

7. Which of the following cases could force a process to be removed from the CPU? (a) I/O request (b) fork a child (c) interrupt or time slice expired (d) all of the above

(d) all of the above

3. Identify the correct sequence for inserting an item in a linked list. (a) 1. Shift higher-indexed items. 2. Create a list node for a new item. 3. Assign a pointer to point to the new item. (b) 1. Shift higher-indexed items. 2. Assign a pointer to point to a new item. 3. Create a list node for the new item. (c) 1. Create a list node for a new item. 2. Assign a pointer of the new item to point to the next item. 3. Update the pointer of the previous node to point to the new node. (d) 1. Create a list node for a new item. 2. Update the pointer of the previous node to point to the new node. 3. Assign a pointer of the new item to point to the next item.

(c) 1. Create a list node for a new item. 2. Assign a pointer of the new item to point to the next item. 3. Update the pointer of the previous node to point to the new node.

2. Given the queue myData 12, 24, 36 (front is 12) What is the result of the following operations? Enqueue(myData, 48) Enqueue(myData, 60) Dequeue(myData) print(Peek(myData)) print(IsEmpty(myData)) (a) 12 false (b) 24 true (c) 24 false (d) 12 true

(c) 24 false

7. An array-based stack's data array is [ 10, 83, 19, 42, 56 ]. If the stack's top item is 42, then the stack's length is _____. (a) 1 (b) 2 (c) 4 (d) 5

(c) 4

1. Given a stack myData: 34, 56, 78, 12, 66 (top is 34) What is the output after the following operations? Push(myData 43) Pop(myData) Pop(myData) print(Peek(myData)) Pop(myData) print(Peek(myData)) (a) 43 34 (b) 34 56 (c) 56 78 (d) 12 66

(c) 56 78

11. Which abstract data type (ADT) is suited to check whether a given string is a palindrome? (a) An array (b) A linked list (c) A deque (d) A queue

(c) A deque

9. Which of the following statements is true with reference to an algorithm's runtime? (a) The runtime of an algorithm is independent of the speed of the processor. (b) The runtime of an algorithm is analyzed in terms of nanoseconds. (c) A single algorithm can execute more quickly on a faster processor. (d) The runtime of an algorithm is independent of the input values.

(c) A single algorithm can execute more quickly on a faster processor

12. Which of the following is correct for a list ADT? (a) A list can be implemented in a programming language only using the LinkedList ADT. (b) A list can print or remove an element only from the beginning of the list. (c) An element can be found and removed from the end of the list. (d) A list's behavior is similar to that of a queue.

(c) An element can be found and removed from the end of the list.

5. Identify the correct algorithm to append an item in an array. (a) ArrayListAppend(list, newItem) { if (list ⇢allocationSize == list⇢ length) ArrayListResize(list, list⇢length * 1) list⇢array[list⇢size] = newItem list⇢length = list⇢length + 2 } (b) ArrayListAppend(list, newItem) { if (list == list⇢length) ArrayListResize(list, list⇢length * 2) list⇢array[list⇢length] = newItem list⇢length = list⇢length + 3 } (c) ArrayListAppend(list, newItem) { if (list⇢allocationSize == list⇢length) ArrayListResize(list, list⇢length * 2) list⇢array[list⇢length] = newItem list⇢length = list⇢length + 1 } (d) ArrayListAppend(list, newItem) { if (list⇢length == list⇢lastItem) ArrayListResize(list, list⇢length * 0) list⇢array[list⇢length] = newItem list⇢length = list⇢length + 1 }

(c) ArrayListAppend(list, newItem) { if (list⇢allocationSize == list⇢length) ArrayListResize(list, list⇢length * 2) list⇢array[list⇢length] = newItem list⇢length = list⇢length + 1 }

10. Which of the following is a constant time operation? (a) Finding the minimum value in an unsorted array (b) Finding the occurrence of a string in a sorted array (c) Finding the sum of two numbers input by the user (d) Concatenating two strings entered by the user

(c) Finding the sum of two numbers input by the user

15. What is the runtime complexity notation for the following algorithm? LowNum(listOfAgeGroups, listSize, myAge) { for (ctr = 0; ctr < listSize; ++ctr) { if (listOfAgeGroups[ctr] == myAge){ return ctr } } } (a) O(N^2 ) (b) O(1) (c) O(N) (d) O(N·logN)

(c) O(N)

10. What is the typical runtime for insertion sort for singly-linked lists? (a) O(N) (b) O(N ⋅logN) (c) O(N^2) (d) O(N (N - 1))

(c) O(N^2)

13. The Big O notation of the composite function 5N^3 + O(N^2 ) is _____. (a) O(5N^3 ) (b) O(N^3 + N^2 ) (c) O(N^3 ) (d) O(N^5 )

(c) O(N^3 )

5. Given a Queue implemented as a linked list, identify the correct dequeue algorithm. (a) QueueDequeue(queue) { tailData = queue tail data ⇢ ⇢ ListRemoveAfter(queue, null) return tailData } (b) QueueDequeue(queue) { headData = queue head data ⇢ ⇢ ListRemoveAfter(queue, queue head) ⇢ return headData } (c) QueueDequeue(queue) { headData = queue head data ⇢ ⇢ ListRemoveAfter(queue, null) return headData } (d) QueueDequeue(queue) { tailData = queue tail data ⇢ ⇢ ListRemoveAfter(queue, queue tail) ⇢ return tailData }

(c) QueueDequeue(queue) { headData = queue head data ⇢ ⇢ ListRemoveAfter(queue, null) return headData }

8. In the ListInsertAfter function for singly-linked lists, the curNode parameter points to _____ node. (a) the head (b) the tail (c) any existing (d) the middle

(c) any existing

8. Which of the following system calls is used by the parent process create a child process? (a) abort() (b) wait() (c) fork() (d) exec()

(c) fork()

3. Which of the following contains memory allocated by malloc()? (a) text section (b) data section (c) heap section (d) stack section

(c) heap section

1. Which data type is best suited to store the names and grades of students? (a) array (b) graph (c) record (d) stack

(c) record

3. Given the array-based list (20, 30, 40, 50), what will the new array look like after the following operations? ArrayListPrepend(list, 10) ArrayListSearch(list, 25) ArrayListRemoveAt(list, 3) ArrayListSearch(list, 40) (a) 20, 30, 40, 50, 10 (b) 10, 20, 40, 50 (c) 10, 20, 25, 40, 50 (d) 10, 20, 30, 50

(d) 10, 20, 30, 50

1. Given the queue myData 12, 24, 48 (front is 12) What will be the queue contents after the following operations? Enqueue(myData, 72) Dequeue(myData) (a) 12, 24, 72 (b) 12, 48, 72 (c) 24, 48, 12 (d) 24, 48, 72

(d) 24, 48, 72

1. Which Unix command is used to display the current directory?

pwd

3. Write the Unix command to change the access rights for a file named myfile.c. The access rights for the file are specified below: • Owner: Read, Write, and Execute • Group: Read and Write • Others: Read only.

chmod 764 myfile.c

6. Which Unix command is used to copy a file?

cp

2. Write the command to compile a program named myprog.c and generate an executable file named myprog

gcc myprog.c -o myprog

7. Which Unix command is used to search the input for a given pattern?

grep

3. Write the code to dynamically allocate memory for an array containing 100 integers, and assign the values 1 through 5 to the first five elements of the array. • Write the statement to declare a pointer to an integer. • Write the statement to dynamically allocate memory for the array. • Assign the value 1 to the first element of the array (using the pointer variable). • Increment the pointer variable to point to the next element of the array. • Assign the value 2 to the second element of the array (again, using the pointer variable). • Repeat for the values, 3, 4, and 5.

int *pointerToInt; pointerToInt = (int*)malloc(100 * sizeoff(int)); *pointerToInt = 1; pointerToInt = pointerToInt + 1; *pointerToInt = 2; pointerToInt += 1; *pointerToInt = 3; pointerToInt++; *pointerToInt = 4; ++pointerToInt; *pointerToInt = 5;

3. Write the Search() function for the dynamic array-based list. The prototype for the function is: int Search(ArrayList *list, int item);

int Search(ArrayList *list, int item) { int i = 0; for(i = 0; i < list->length; i++) { if (list->array[i] == item) { return i; } } return -1; }

3. Which Unix command is used to list the contents of a directory?

ls

8. Which Unix command is used to display the built-in manual page for a Unix command?

man

2. Which Unix command is used to create a new directory?

mkdir

5. Which Unix command is used to remove a file?

rm

1. Write the command to log into Zeus (the Linux cluster). Include the proper name of the server.

ssh userid@zeus.(cec/vse).gmu.edu

4. Which Unix command is used to create a file?

touch

1. Create and initialize a list implemented as a dynamic array. • Write the structure definition for a list of integers implemented using a dynamic array. • Allocate memory for an array containing 4 elements (i.e. integers). • Initialize each element of the array to 0. • Initialize each of the other members of the structure to their appropriate values.

typedef struct ArrayList { int *array; /* Dynamically Allocated Array */ int length; /* Number of Ints in the ArrayList */ int allocationSize; /* Number of Indices in the Array */ } ArrayList; ArrayList *list = NULL; list = (ArrayList*)malloc(sizeof(ArrayList)); list->array = (int*)malloc(4 * sizeof(int)); list->array[0] = 0; list->array[1] = 0; list->array[2] = 0; list->array[3] = 0; list->length = 0; list->allocationSize = 4;

2. Write the structure definition for the structure type PERSON. The structure includes: • A character array named lastName of length 20. • An integer variable named age. • A double variable named salary.

typedef struct person { char lastName[20]; int age; double salary; } person_t;

2. Write the Append() function for the dynamic array-based list. The prototype for the function is: void Append(ArrayList *list, int newItem);

void Append(ArrayList *list, int newItem) { list->array[list -> length] = newItem; list->length = list->length + 1; } Note: this function does not resize the array when the length equals the allocation size


Ensembles d'études connexes

Health Information Systems Midterm

View Set

Prep-U Ch. 60 - Intro to the Musculoskeletal System

View Set

ap spanish ap classroom unit 3 midterm

View Set

Biology: Topic 6: Earth's Early History

View Set