click below
click below
Normal Size Small Size show me how
CECS 325 Quiz 4 Gold
| Question | Answer |
|---|---|
| In a multi-threaded C++ program, why would you use a mutex? | To protect shared resources from concurrent access |
| What is the difference between int* arr and int arr[] in function parameters? | There is no difference; both are treated as pointers |
| Consider the following code: int x = 5; int* ptr = &x; *ptr = 10; After execution, what is the value of x? a) 5 b) 10 c) Undefined d) Compiler error | 10 |
| In your threaded bubble sort program, total_swaps is a global variable. Why is a mutex needed when updating it? | To ensure only one thread updates it at a time |
| Which of the following is stored on the heap? a) Global variables b) Local variables in a function c) Dynamically allocated arrays using new d) Function parameters | Dynamically allocated arrays using new |
| What is the output of the following code? #include <iostream> using namespace std; void swap(int &x, int y) { int temp = x; x = y; y = temp;} int main() { int a = 10, b = 20; swap(a, b); cout << a << ',' << b << endl; return 0; } | 10,20 |
| Which function would you use to copy a null-terminated character array? a) strcpy(dest, src); b) strncpy(dest, src); c) strlen(dest); d) Both a and b | Both a and b |
| Which statement about threads in C++ is true? a) Threads automatically synchronize without any additional code b) Each thread gets its own stack, but shares the heap c) Threads cannot access global variables d) Threads cannot be joined | Each thread gets its own stack, but shares the heap |
| What is the difference between the stack and heap? | Stack is local memory with automatic cleanup; heap requires manual allocation and deletion |
| In your project, why did you pass int* start and int size to each thread for sorting? | To ensure threads operate on separate sections of the array |
| int showElement(int A[], int index) { cout << A[index]; } Which of the answers will print the same correct output? a) cout << *(A + index); b) cout << &A[index]; c) cout << *A + index; d) cout << &A + index; | cout << *(A + index); |
| Assume the following C++ code segment: struct student{ string name; int age; char grade; }; student s1, *sptr; sptr = &s1; s1.name = "MasterGold"; What code would you use to set MasterGold's age to 49? | sptr->age = 49; |
| Assume the same C++ code as above. What code would you use to set MasterGold's grade to 'A'? a) The correct answer is not listed b) sptr.grade = 'A'; c) s1->grade = 'A'; d) MasterGold->grade = 'A'; | The correct answer is not listed. You would use s1.grade = 'A'; or sptr->grade = 'A'; |
| In which C++ library is strlen() found? a) <cstring> b) The correct answer is not listed. c) <strlen> d) <string> | <cstring> |
| Consider the following valid C++ function: void func(char * ptr) { cout << ptr; } What is the passing mode used in the function to pass the parameter ptr? a) pass by reference b) pass by touchdown c) pass by linkage d) pass by value | pass by value |
| What is another name for a "null terminated character array"? a) The correct answer is not listed b) container c) word d) C-string | C-string |
| char name[15] = "PonyExpress"; char * cptr = name; name = "cowboy"; cout << name; What is wrong here? | You can't assign "cowboy" to name |
| The function pthread_create() has how many parameters? a) 4 b) 1 c) 2 d) 3 | 4 |
| Consider the following C++ code segment: char word[10] = "matrix"; cout << strlen(word); What will be printed to the screen? a) 10 b) matrix c) The correct answer is not listed d) 6 | 6 |
| The function pthread_create() allows a program to have multiple threads running concurrently. a) True b) False | True |
| What is the time complexity of the following C++ code segment? for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cout << i + j << endl; } } a) O(N) b) O(log N) c) O(N^2) d) O(N log N) | O(N^2) |
| What is the worst-case time complexity of Bubble Sort for an array of size N? a) O(N) b) O(N log N) c) O(N^2) d) O(log N) | O(N^2) |
| What is the best-case time complexity of Bubble Sort if the array is already sorted? a) O(N) b) O(N^2) c) O(log N) d) O(1) | O(N) |
| What is the time complexity of accessing an element in a C++ array by index? a) O(N) b) O(log N) c) O(1) d) O(N^2) | O(1) |
| Which of the following Big-O notations best describes the time complexity of merging two sorted arrays of size N each? a) O(N) b) O(N^2) c) O(log N) d) O(N log N) | O(N) |
| Consider the mysort.cpp project using multiple threads to sort segments of an array. If each segment is of size N/S, and there are S segments, what is the Big-O time complexity for sorting all segments using Bubble Sort in the worst case? | O(N^2) |
| In your mysort.cpp project, the bubble sort function needs ____ parameters. | 3 |
| When sorting a vector, how many parameters does the bubblesort() function need? | 1 |
| How many parameters does the bubbleSort() function need? | 2 |
| What is the expected growth rate of bubbleSort algorithm? | O(N^2) or O(N*N) |
| Which sorting algorithm is the fastest of those listed: - Bubble sort - Insertion sort - Selection sort | The listed algorithms are all about the same because they are all n-squared sort |
| What Linux command is used to generate information about memory usage? | Free |
| int* ptr = new int; *ptr = 42; What is true about the variable ptr? | ptr stores the address of a dynamically allocated integer on the heap |
| int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr; cout << *(ptr + 2) << endl; What is printed to the screen? a) 10 b) 20 c) 30 d.)40 | 30 |
| Consider a variable declared as static int count = 0; inside a function. Which statement is correct? a) count is reset to 0 every time the function is called. b) count retains its value between function calls but is not visible outside the function | count retains its value between function calls but is not visible outside the function |
| In the bubble() function, the variable num_swaps is local. Which of the following is true? a) Its value persists between calls to bubble() b) Each thread has its own separate num_swaps count. c) It must be declared as static to count swaps correctly | Each thread has its own separate num_swaps count |
| Which of the following statements about the global variable total_swaps in your threaded sort program is correct? a. It is only accessible inside the bubble() function b. It retains its value for the entire program and can be accessed by all threads | It retains its value for the entire program and can be accessed by all threads. |
| Why is a mutex used in the threaded bubble() function in your sort program? | To prevent multiple threads from updating total_swaps at the same time. |
| Which of the following statements correctly locks and unlocks a mutex? a) locker.unlock(); locker.lock(); b) locker.lock(); // critical section locker.unlock(); c) lock(locker); d) mutex.locked(); mutex.unlocked(); | locker.lock(); //critical section locker.unlock(); |
| What could happen if total_swaps was incremented by multiple threads without using a mutex? a. Nothing, it would still count correctly. b. The program would crash immediately. c. The total swap count could be incorrect due to race conditions. | The total swap count could be incorrect due to race conditions |
| In your program, mutex locker; is declared globally. Why? | So all threads can access the same mutex to coordinate access to total_swaps |