Practice Final Exam - Programming (Dec 16, 2024)

Document Details

Uploaded by Deleted User

Carleton University

2024

Tags

C programming memory management system calls programming exam

Summary

This document contains a practice final exam in C programming. The exam covers topics such as memory management, system calls, and different aspects of C programming in the context of the course virtual machine environment.

Full Transcript

# PRACTICE Final Exam (Weekly Quizzes, Wooclaps, Etc.) - Results ## Attempt 15 of Unlimited Written Dec 16, 2024 10:35 PM - Dec 16, 2024 10:38 PM ## Attempt Score 49/49-100% Overall Grade (Highest Attempt) 49/49-100% ## Question 1 **1/1 point** **This is an example of the "fill-in-the-blank" mu...

# PRACTICE Final Exam (Weekly Quizzes, Wooclaps, Etc.) - Results ## Attempt 15 of Unlimited Written Dec 16, 2024 10:35 PM - Dec 16, 2024 10:38 PM ## Attempt Score 49/49-100% Overall Grade (Highest Attempt) 49/49-100% ## Question 1 **1/1 point** **This is an example of the "fill-in-the-blank" multiple choice examples you can expect to find on the exam.** **The first two questions both relate to the same code.** ```c main() return Complement Appropr ``` **Question:** In Blank 1 on our course virtual machine environment, which line of code will initialize a variable of size 4 bytes that will be interpreted using two's complement during arithmetic operations? - int var = -42; - char var = -42; - int "var-42 - float var= 42; - No single answer is correct (multiple answers correct or none of them are) **Feedback** Yes! By default int is signed, and on our machine, uses 4 bytes. ## Question 2 **1/1 point** **This is an example of the "fill-in-the-blank" multiple choice examples you can expect to find on the exam.** **The first two questions both relate to the same code.** ```c main() return 0; Complement Appropriat ``` **Question:** In Blank 2 when run on our course virtual machine environment, which line of code will correctly setup a pointer to point to the same value as 'var' which was initialized in the previous question? - int "par = &var; - int "par = "var; - char *par = &var; - char "par "var; - float *par = &var; - float *par - *var; **There are multiple / no correct answers.** ## Question 3 **1/1 point** **SCENARIO:** You are writing some videogame networking code. The code has to be able to rapidly send and receive data from many different computers over the internet very rapidly. If the data is lost or received out of order, okay you already planned around that. you just add a small number and timestamp to each piece of data the players send to keep track of ordering and extrapolate where players will be based on the direction they were previously moving to give best guesses of the position to other players. The data should be coming in so fast it shouldn't get in the way. What protocol is most likely being used in this scenario to handle communication across different hosts? - TCP (Stream Sockets) - UDP (Datagram Sockets) - Signals - Pipes ## Question 4 **1/1 point** **When building, Compilation refers to the process which:** - Takes in many source code files and combines them into one object code file - Combines multiple libraries / object files into executable code - Takes in source code file and outputs one object file - Takes in source code and translates this into executable code **Feedback** Review Chapter 1.3 for information about compiling and building. ## Question 5 **1/1 point** **MULTI-SELECT:** Why might we need to use a system call, either directly or indirectly using a library function? Select ALL correct options. **Note: You will not be asked to select more than one option on an exam or midterm test in this course** - We want to compare the values of two primitive data types - We want to have branching logic in our program - We want to interact with devices like mouse, keyboard - We want to change the value of stack-allocated variables - We want to interact with different processes - We want to print data to the screen - We want to create an array on the stack ## Question 6 **1/1 point** **Given the statements:** ```c int count; while ((count-1)++ 1-5); ``` **the above while statement contains how many operators (note: the '()' here are not considered:** - 1 - 2 - 3 - 4 **Feedback** Review: Page 20, Operators. ## Question 7 **1/1 point** **Given code snippets A and B:** **A:** ```c int count while (count 5) ( if (count ++count/ ``` **B:** ```c int count; for (count=0; count < 5; ++count) { if (count 3) break; ``` **Which of the following statements is true:** - Neither A nor B causes an infinite loop - Both A and B causes an infinite loop - B causes an infinite loop, but A does not - A causes an infinite loop, but B does not ## Question 8 **1/1 point** **The following can be executed directly by the computer:** - object code, machine code, and source code - machine code and object code - object code only - machine code only **Feedback** Review: Chapter 1.3, writing your first C code. ## Question 9 **1/1 point** **Which of the following snippets of code best demonstrates good practice when using scanf() to read in a user-provided file name in order to avoid potential buffer overflows, assuming the file name will not contain spaces and that more user input will be collected later?** - char filename[20]; scanf("%19s", filename); while (getchar() != '\n'); - char filename[20]; scanf("%20s", filename); while (getchar() != '\n'); - char filename[20]; scanf("%s", filename); if (strlen(filename) > 20) { filename [19] = "\0'; 1 while (getchar() != "\n"); - char filename[20]; scanf("%20s", filename): scanf("\n"); - char filename[20]; scanf("%19s", filename); scanf("\n"); - All options will produce a compile-time error - All options will produce a runtime error **Feedback** Review Chapter 1.5 to review the best practices for accepting user input. ## Question 10 **1/1 point** **Given the statement:** ```c 0xc7 0b00010001; ``` **what does the following statement output: printf("%d\n", b),** - 74 - -74 - 181 - 182 **Feedback** The default bit model, when neither 'signed nor 'unsigned is used is 'signed', meaning the binary will be interpreted as having positive or negative values, and will print as a negative integer if it is negative and a positive integer if it is positive. Use "Two's Complement", described in Chapter 2, to get the magnitude of a negative value (i.e., a "signed" value which leads with a one). ## Question 11 **1/1 point** **Given the statement:** ```c unsigned char 0x9d + 80; ``` **what binary sequence is stored in memory for variable a:** - 0001 0010 - 1110 1101 - 0001 0001 - 1110 1110 **Feedback** Review Chapter 2 to check your understanding of unsigned values, representing different bases in your C source code, and conversions from hex to binary and decimal to binary. The first step is to get everything in a common base (in this problem, base 16+ base 10, final result in base 2) ## Question 12 **1/1 point** **Given the statements:** ```c unsigned char b; 0263; 2 ``` **what does the following statement output: printf("%d\n", b);** - 207 - -52 - 14 - 204 **Feedback** Begin by converting the values into binary, then perform a left shift on the values, then convert the resulting binary into decimal to display it. Make sure to note which bit model is being used! ## Question 13 **1/1 point** **Given the statements:** ```c -109; >> 2 ``` **the following statement output: printf ("%d\n", b);** - 0-36 - 220 - 228 - -28 **Feedback** Right-shifting a signed value is technical "implementation defined", meaning the compiler can choose how to handle the bits here, but in our class we use the standard that right-shifting a signed value will fill the new gaps with the most-significant bit. Review Chapter 2.2 for information and examples. ## Question 14 **1/1 point** **Which value would correctly replace X in the code below to reserve exactly enough space required for the value initialized on line 3 and explains it correctly?** ```c main() rock[X] "stone"; return 0; ``` - 6; but this assumes we add the null terminator afterwards - 8; to hold a pointer since"" returns a pointer to the read-only segment - 5; but we really should reserve 6 and assign the next one to a null pointer - 6; because "stone" is already null terminated ## Question 15 **1/1 point** **Given the program:** ```c 01 int main() 02 03 char arr[32], 04 strcpy(arr, "Hello world"); 05 printf("%d\n", arr); 06 ``` **it is printed out execution:** - the ASCII value of the first character in the array - the address of the array - a runtime error occurs - the numeric value of the first character in the array **Feedback** Chapter 2.4: Strings and Char Arrays ## Question 16 **1/1 point** **Given the statements:** ```c 01 char strl [32]; 02 char str2[32]; 03 04 strcpy(strl, "Hello world"); 05 strcpy(str2, "Hello world"); 06 07 if (strl str2) 08 printf("test #1\n"); 09 10 if (strcmp(strl, str2) 0) 11 printf("test #2\n"); 12 ``` **What do lines (7) and (10) do:** - line (7) compares string contents and line (10) compares string addresses - both lines (7) and (10) compare string contents - line (7) compares string addresses and line (10) compares string contents - both lines (7) and (10) compare string addresses ## Question 17 **1/1 point** **Given a statically allocated array of structure pointers, the following is true:** - the array of pointers is stored in the heap, and the structure data may be stored in the call stack or heap - the array of pointers is stored in the heap, and the structure data must be stored in the heap - the array of pointers is stored in the call stack, and the structure data must be stored in the heap - the array of pointers is stored in the call stack, and the structure data may be stored in the call stack or heap ## Question 18 **1/1 point** **Given the code below:** ```c fool(int x) -42; int main() ( int z 50; fool(&z); ``` **Which statement best describes the argument that is passed into the parameter x?** - The address to the integer value 50 - The value of the integer variable z. - The integer variable z - The address to the integer variable z **Feedback** This address is a value, but its value is an address, not an integer. ## Question 19 **1/1 point** **Given the statements:** ```c 01 int arr[8], 02 03/ Initialization of content is present, 04 but not shown */ 05 06 printf("%d\n", "arr+5); 07 printf("%d\n", (arr+5)); 08 ``` **What does line (06) print out:** - the content of arr [5] - the address of axx [0] incremented by 5 bytes - the content of arr[0] incremented by 5 - the address of arr [5] ## Question 20 **1/1 point** **Given the statements:** ```c 01 int arr[8], 02 03 Initialization of arr content is present, 04 but not shown */ 05 06 printf("%d\n", "arr+5); 07 printf("%d\n", (arr+5)); 08 ``` **What does line (07) print out:** - the content of arr [5] - the address of arr[5] - the content of arr[0] incremented by 5 - the address of arx [0] incremented by 5 bytes ## Question 21 **1/1 point** **Given the following structure definitions and start of main function:** ```c struct customer { 02 unsigned int id; 04); unsigned char name [32]; 05 struct account { 06 struct customer holder; int balance_dollars; 08 09}; 10 int main() { int balance_cents; 11 struct customer eashan; 12 struct account account; ``` **Assuming all required headers and definitions are included, which of the following pieces of code within the main function correctly assigns a name to the struct customer variable eashan?** - eashan->name "Eashan S."; - strcpy(eashan.name, "Eashan S."); - strcpy(eashan->name, "Eashan S."); - eashan "Eashan S."; ## Question 22 **1/1 point** **Given the following structure definitions and start of main function:** ```c 01 struct customer { unsigned int id; 03 unsigned name [32]; 04); 05 struct account { 06 struct customer holder; 07 int balance_dollars; int balance_cents; 09}; 10 int main() { 11 struct customer eashan; 12 struct account account; ``` **Assuming all required headers and definitions are included, which of the following pieces of code correctly initializes the new struct account account?** - account.holder eashan; - account->holder = &eashan; - account->holder eashan; - account.holder = &eashan; ## Question 23 **1/1 point** **Given the following structure definitions and start of main function:** ```c 01 struct customer { 02 04); unsigned int id; unsigned char name [32]; 05 struct account { 06 struct customer holder; 07 int balance_dollars; int balance_cents; 09); 10 int main() { 11 struct customer eashan; 12 struct account account; ``` **Assuming all required headers and definitions are included and the struct account account has been correctly initialized, which of the following pieces of code correctly assigns the id of the account's holder field?** - account->holder.id 42; - account.holder.id 42; - account->holder->id 42; - account.holder->id 42; ## Question 24 **1/1 point** **Which one of the following storage classes is most guaranteed to store its memory in the DATA segment, based on our discussions in class?** **(Note: This is a refresher from our topics on Storage Classes during Slides 07 - Areas of Memory, page 265 of the course notes)** - automatic - register - static ## Question 25 **1/1 point** **Given the following code:** ```c var) ( foo_var_a foo_var b main() int foo(main_var_a); return 0; ``` **In which segment is the value for the variable main_var_a stored?** - Stack Segment - Code Segment - Heap Segment - Data Segment ## Question 26 **1/1 point** **Given the following code:** ```c global void foo(int param var) ( int foo var a 20 foo_var_b main() { int main_var_a foo(main_var_a) return @j ``` **In which segment is the value for the variable foo_var_astored?** - Heap Segment - Data Segment - Code Segment - Stack Segment ## Question 27 **1/1 point** **Given the following code:** ```c foo(int param var static oo_var_ main() ( foo(main_var_e); return 0; ``` **In which segment is the value for the variable param_var stored?** - Code Segment - Data Segment - Stack Segment - Heap Segment ## Question 28 **1/1 point** **Given the following function implementation:** ```c void foo3 (int **x) malloc(sizeof(int)); 99; ``` **Calling the function, what can we most accurately say about the argument passed into the parameter x?** - the value of the argument is and integer pointer variable - the value of the argument is the address of an integer pointer variable - the value of the argument is an integer value - the value of the argument is the address of an integer variable ## Question 29 **1/1 point** **Given the program:** ```c 01 int main() 02 03 int *ptrl, *ptr2; 04 int n = 10; 05 06 ptrl malloc (5 sizeof(int)); 07 08 *ptrl 09 ptrl [1] 20; 10 11 ptr2 ptrl + 27 12 ptr2 [1] 70, 13 14 // free (*ptrl); 15 // free (ptrl); 16 // free(n); 17 // free (ptr2); 18 1 ``` **Which of the deallocation statements, if un-commented, would result in a working program with no memory leaks:** - line (14) only - line (15) only - lines (15) and (16) - lines (15) and (17) ## Question 30 **1/1 point** **Given the following function implementations:** ```c oid fool (int *x) 77; void foo2 (int *x) malloc(sizeof(int)); 80; void foo3 (int **x) malloc(sizeof(int)); 99; ``` **Which function(s) will definitely result in a memory leak, no matter what code is written in the calling function:** - both functions foo2 () and foo3 () - function foo2 () only - function fool () only - function f003 () only ## Question 31 **1/1 point** **Given the following three versions of the copyList() function that copies data from one linked list to a different list without any unnecessary code:** **VERSION #1:** ```c pyList(NodeType **dest, ``` **VERSION #2:** ```c NodeType currDestlode currcode currin ``` **VERSION #3:** ```c curr deType NodeType "prevflode NULL; (curriode 1 MLL) ( NodeType "neudlode malloc(sizeof(NodeType)); currflode->data; NULL; if (prevllode NULL) ( dest neudlode; else { prevtiode ``` **Which one(s) perform a correct copy operation:** - Version #1 - Version #2 - Version #3 - none of the other options **Feedback** We have successfully allocated new memory for all of our nodes and copied the previous structure data into the newly allocated memory, such that "dest will point to brand new memory containing the same data as our 'src'. ## Question 32 **1/1 point** **Given a stack-allocated array of structure pointers, the following is true:** - the array is stored in the heap, and the structure data must be stored in the heap - the array is stored in the heap, and the structure data may be stored in the call stack or heap - the array is stored in the call stack, and the structure data may be stored in the call stack or heap - the array is stored in the call stack, and the structure data must be stored in the heap. ## Question 33 **1/1 point** **Given a dynamically allocated array of structures, the following is true:** - the array is stored in the heap, and the structure data may be stored in the call stack or heap - the array is stored in the heap, and the structure data must be stored in the heap - the array is stored in the call stack, and the structure data must be stored in the heap - the array is stored in the call stack, and the structure data may be stored in the call stack or heap ## Question 34 **1/1 point** **Which of these functions will correctly initialize data for a student structure on the heap and then provide it a value for its GPA field, updating the 'stu parameter to correctly point to the new memory?** - ```c void gpa) ( malloc(sizeof(struct Student)); ini udent(st ``` - ```c struct Student) malloc(sizeof( Student)); ("stu). udent gpa) { (struct Student") malloc(sizeof(struct Student")); ("stu).gpa Init ``` - ```c tudent "stu, float gpe) { ("stu) (struct Student") malloc(sizeof(struct Student)); Student gpa) ( ``` - ```c Astu Student") malloc(sizeof(struct Student)); ``` ## Question 35 **1/1 point** **Given the following code, where should we free each of the pointers? You may select multiple answers** ```c calloc(10, sizeof(StudentType)); capacity 10; // Make a bunch of additions until is 10... StudentType "temporary calloc(20, sizeof(StudentType)); class = temporary; free(class) on line 04 free(class) on line 07 free(class) on line 09 class should never be freed. free(temporary) on line 07 free(temporary) on line 10 temporary should never be freed ``` ## Question 36 **1/1 point** **Where is the integer value for the 'data' field of each structure stored?** ```c de struct NodeB Model struct NodeA struct Nod nodeD. malloc(sizeof(struct NodeA)); malloc(sizeof(struct NodeB)); malloc(sizeof(int)); malloc(sizeof(struct NodeB)); ``` - Heap: None Stack: nodeA, nodeB, nodeC, nodeD - Heap: nodeA Stack: nodeB, nodeC, nodeD - Heap: nodeA, nodeB Stack: nodeC, nodeD - Heap: nodeA, nodeB, nodeC Stack: nodeD - Heap: nodeA, nodeB, nodeC, nodeD Stack: None ## Question 37 **1/1 point** **The shell command gee o pl fl.c f2.cdoes the following:** - it performs assembling only - it performs compilation and linking - it performs compilation only - it performs linking only ## Question 38 **1/1 point** **The shell command gcc c f1.c does the following:** - it performs linking only - it performs compilation only - it performs compilation and linking - it performs assembling only ## Question 39 **1/1 point** **A Makefile with all the correct dependencies will re-compile:** - all the source files - only the source files that have changed - only the object files that have changed - all the object files ## Question 40 **1/1 point** **A Makefile with all the correct dependencies will re-link:** - all the source files - only the object files that have changed - only the source files that have changed - all the object files ## Question 41 **1/1 point** **During the compile step of building a program, how many source files are used as input?** - The compile step does NOT use any source files as input. - The compile step takes in ONE source file. - The compile step takes in TWO source files. - The compile step takes in MANY source files (all source files in the program). ## Question 42 **1/1 point** **A multi-process system must also be:** - distributed across multiple hosts - none of the other options - multi-threaded - organized as client-server ## Question 43 **1/1 point** **To send a small numeric value between two processes on the same host, we may use:** - sockets only - semaphores only - sockets or signals - signals only ## Question 44 **1/1 point** **To send a small numeric value between two processes on different hosts, we may use:** - semaphores only - signals only - sockets or signals - sockets only ## Question 45 **1/1 point** **Threads do not share:** - the code segment - the function call stack - the heap ## Question 46 **1/1 point** **The data segment stores:** - static variables - parameter variables - automatic variables - statically allocated variables ## Question 47 **1/1 point** **Which of the following is true of the fork() system call?** - It waits for a process to complete before continuing to run instructions - It creates an entirely new process with a new process ID - It replaces the current process' memory with a new program - It creates a new control flow within the process which called fork() ## Question 48 **1/1 point** **TCP is considered to be....** - More reliable and slower than UDP. - More reliable and faster than UDP. - Less reliable and slower than UDP. - Less reliable and faster than UDP. ## Question 49 **1/1 point** **In the course VM, the standard output stream is:** - line buffered - unbuffered - none of the other options - block buffered

Use Quizgecko on...
Browser
Browser