ITSC 2181 FINAL EXAM REVIEW
Explain the four states of a process' lifecycle in detail
-Ready: process is able to run, but is currently idle -Running: process is acknowledged by CPU and is actively running executing instructions -Blocked: process is waiting for some event to happen so it can run -Exiting: process has completed its execution, but has not been removed from the system yet
Assume that a struct named employee has already been defined, as below: struct employee{ int id; float salary; }; Write the code to do the following: 1) Declare an array of 10 elements of type employee struct 2) Initialize values for the first two employees, Pick any values that make sense
//Declare an array of 10 elements struct employee array[10]; //Initialize values for the first two employees array[0].id = 32; array[0].salary = 4000.50; array[1].id = 40; array[1].salary = 43000.50;
Write the code to do the following: A) Defines a struct Person_t. This struct contains two members: 1) The first member should be a string of sufficient length, to contain the name of a person 2) The second member should be an integer value containing ther person's age b) Define two struct variables person1 and person2 1) Initialize the two struct variables with suitable values, The first person should be called Betsy. Betsy should be 42 years old 2) The second person should be named after yourself and should be as old as yourself
//Define a struct Person_t with required members struct Person_t{ char name[20]; int age; }; //Define two struct variables person1 and person2 struct Person_t person1 = {"Betsy", 42}; struct Person_t person2 = {"Jada", 24};
Create a struct variable for a the following struct and assign on value for all members struct myStructure{ int myNum; char myLetter; char myString[30]; };
//declare struct variable called "a" and initialize all members struct myStructure a = {32, 'A', "Josh"};
Assume you have three integers a, b, and c. Write the code to find and print the smallest of these three numbers.
//declare variable int smallest = 0; //determine smallest if( a < b && a < c){ smallest = a;} else if( b < a && b < c){ smallest = b;} else if( c < a && c < b){ smallest = c;} //display smallest printf("%d", smallest);
A C structure is also knowns as a ____ defined datatype A. User B. Basic C. Data D. Complex
A
A parent process can pause until the completion of its child process by using the ____ system call A. wait() B. stop() C. exit() D. fork()
A
Each function call in a program has its own separate and isolated set of local variables stored in its activation frame A. True B. False
A
Evaluate the following: !(1 && !(0 || 1)) A. True B. False
A
If you wanted to add read and write permissions for the owner on a file named test.txt, which command would you use? A. chmod u+wx B. chmod o+wx C. chmod on+wx D. chmod u=wx
A
In the CPU, the ____ executes instructions, and the ____ guides the execution of these instructions A. Processing Unit, control Unit B. Control unit, Processing unit
A
Micro architecture is the circuitry for what? A. A specific ISA B. RAM C. CPU D. ALU
A
Multiple processes may be in the ready state at the same time. A. True B. False
A
One of the differences between CISC and RISC is that A. CISC uses more complex instructions B. RISC instructions execute slower than CISC C. RISC is more common desktop computers D. RISC instructions have more variety in terms of functionality
A
Per the definition of a computer system we discussed in class, which of the following is NOT an example of a computer system? A. Microcontroller B. Cell phone C. Tablets D. Smart watches
A
Suppose that you want to navigate directly to the root directory from whichever directory you are currently in. Provide the Linux/Unix command A. cd / B. cd .. C. cd ~ D. cd root
A
Suppose that you want to set read and write permissions only for the owner of a file named i_am_editable within the current directory. What command would you use? A. chmod 600 i_am-editable B. chmod 640 i_am-editable C. chmod 760 i_am-editable D. chmod 444 i_am-editable
A
Suppose you want to list all files and directories, including hidden ones, in the current directory. Provide the Linux/Unix command A. ls -a B. ls -l C. ls -h D. ls
A
The _____ command can be used to view current processes in the system and see which ones are taking up the most system resources A. top B. second C. bottom D. middle
A
The call stack follows a last in, first out (LIFO) order, meaning that the most recently called functions is the first to be executed and removed from the stack A. True B. False
A
What are the two built in array operators in C? A. sizeof and address of (&) B. sizeof and size C. address of (&) and length
A
What best describes the difference between jal (jump and link) and branch control flow instructions? A. jal is used for function calls, while branch instructions just lead the execution of a program to another portion of the program B. jal is used for function returns, while branch instructions just lead the execution of a program to another portion of the program C. They are both used for the same purpose and are interchangeable D. jal stores the function's arguments in registers while branch instructions do not
A
What is assembly as mentioned in the context of computer systems? A. A human readable form of machine code B. A hardware component inside the cpu C. A new programming language D. A physical gathering of computers
A
What is the job of the instruction register in the control unit, which is part of the CPU? A. Store the address of the current instruction being executed B. Store the address of all previously executed instructions C. Keep the address or the instruction that will be executed next
A
What is the largest number that can be represented with 5 digits in base 16 A. (FFFFF)7 = 1048575 B. (EEEEE)_7 = 978670 C. (DDDDD)_6 = 908765 D. (99999)_6 = 629145
A
When using the scanf function, what is the format specifier to read a single character from the console? A. %c B. %d C. %f
A
Which of the following is a valid way to declare a two dimensional array? A. int anarray[20][20]; B. array anarray[20][20]; C. int array[20][20]; D. char array[20];
A
Which of the following is the Boolean operator for logical-and? A. && B. & C. |& D. ||
A
Will this code generate an error? int main(){ int nums[5]; printf("%d", nums[8]); return 0} A. No, but the program will run with a warning B. Yes, and the program will also throw a warning C. Yes, IndexOutOfBounds will be generated
A
After the following line of code is executed, what would array represent? You may choose multiple answers A. The base address of the array B. The memory address of the first element in the array C. The memory address of the entire array D. Pointers cannot be made to arrays
A & B
What commands will show file contents and number the lines in the output? (Multiple answers) A. cat-b B. cat-a C. cat D. cat -l
A & B
What happens when the call to malloc() fails? You may choose multiple answers A. Not enough heap memory is available B. The malloc() function call returns "NULL" C. The program will return a compiler error D. Memory will be set aside in the stack instead of heap, which would be too full
A & B
Which of the following are true regarding the fork() system call and the exec() family of system Calls? (multiple answers) A. fork() creates a new process which is a copy of the parent process B. exec() replaces the current process with a different one C. exec() and fork() both return -1 when successfully run D. both exec() and fork() take in 2 parameters
A & B
What value is stored in x6 after the following RISC-V instruction is assembled? srai x6, x5, 1 A. -53 B. -52 C. -52.5 D. 11025
A (round down!)
In a couple of sentences, describe the differences between a caller and a callee function?
A caller function starts the execution of the callee (like main()) and the callee function does something for the caller function like computing values or printing statements
Explain, in detail, what a system call is, and how a user can use system calls to their advantage.
A system call is a temporary switch from user mode to kernel mode to complete a specific task. Users can use system calls to their advantage by being able to execute privileged tasks/instructions briefly that they usually wouldn't be able to perform or have access to in user mode, then the mode switches back to user mode.
Which of the following include the roles of the GDB debugger? You may pick multiple answers A. Can help with debugging large code bases B. Only work for high level coding languages, such as Java or Python C. Helps you visualize code line by line instead of as a cohesive program D. One of the useful options for debugging code written in C
A,C,D
What are the three kinds of RISC-V instructions we covered in our lectures?
Arithmetic, Memory Load and Store, and Control Transfer
After running the following command "gcc filename.c" what is the resulting output? A. An output file contains the assembly code which was translated into the source code written in C B. An output file that contains the binary code which was translated into the source code written in C C. A file that needs to be compiled again to run the code
B
How many times is a do while loop guaranteed to run? A. 0 B. 1 C. Infinitely D. It varies depending on the body of the loop
B
In Unix based systems, a child process gets the same ID as its parent, since it is a copy of the parent process. True or false. A. True B. False
B
Operands in assembly are what ____ are in high level programming languages, and instructions in assembly are what _____ are in high level programming A. Variables, functions B. Variables, operations, C. Operations, variables D. Data, text
B
Processes start executing as soon as they enter the ready state A. True B. False
B
Referring to the struct mentioned in the previous question, assumed the date struct contains a field Called day. Assign the value 10 to the field day using the dates pointer A. dates.day = 10; B. dates->day = 10; C. dates = 10; D. day.dates = 10;
B
Strings in C are terminated with___ A. \1 B. \0 C. \\0
B
To print a string value in a printf statement, you don't need to use a format specifier A. True B. False
B
What best describes an offset, as we have studied? A. The different indices in an array B. The memory location of data relative to another location C. The different memory addresses of each element in an array D. None
B
What is the output of the following code? #include <stdio.h> void fun(int *ptr) { *ptr = 30; } int main() { int y = 20; fun(&y); printf("%d", y); return 0; } A. 20 B. 30 C. Compiler Error D. Runtime Error
B
Which of the following C functions dynamically allocates a block of memory? A. realloc B. malloc C. dyn_alloc D. free
B
Which of the following C functions dynamically allocates a block of memory? A. realloc B. malloc C. dyn_alloc D. free E. new
B
Which of the following accesses a variable in structure b? A. b->var; B. b.var; C. b-var; D. b>var;
B
Which of the following is NOT a function of the Operating System? A. Controls and manages the underlying hardware components of the computer B. Permanently stores program and data C. Enables users to easily run and interact with programs D. Implements policies and mechanisms to allow multiple programs to run simultaneously
B
Which of the following is NOT a service provided by the Operating System? A. File management B. Word processing C. Program execution management D. Error management
B
Which of the following is not an important requirement a computer must have to support function calls of a program? A. Proper hardware instructions for function calls and returns B. Predefined methods in the ".data" portion of the program C. A mechanism for track of stack memory D. Specific registers for argument passing and returning values during and after function calls
B
Which of the following lines is a correct way to use the function scanf? A. int i = 0; scanf("%d", i); B. char s[10] = "1234567"; scanf("%3s", s); C. int i = 0; int *p = &i; scanf("%d", *p); D. char c; scanf("%c", c);
B
In a RISC-V program, which register would you use to manipulate the stack after function calls and returns? A. tp B. gp C. sp D. immediate
C
What command would you use to add read and write permissions for everyone on the file study guide.txt A. chmod u+rw studyguide.txt B. chmod e+rw studyguide.txt C. chmod o+rw studyguide.txt D. chmod o=w studyguide.txt
C
What happens during the deciding step of instruction execution? A. The memory unit reads bytes and sends them back in the control unit B. The memory unit writes the value of the instruction to memory at yhe specified address C. The control unit breaks down the bits of the instruction and fetches operant values from registers, then passes the decoded instruction to the processing unit
C
What is the output of the following code? #include<stdio.h> void main(){ int a = 1, b = 0, c = 1, d; d = ++a; c = - -b; b = ++d; a = - - c; printf("%d %d %d %d", b, a, c, d); A. -2 3 3 -2 B. -2 3 -2 3 C. 3 -2 -2 3 D. 3 3 -2 -2
C
What is the purpose of a register in the Von Neumann architecture? A. A communication channel for binary values B. A er manner method of storage in the CPU C. a small, fast unit of storage used to hold program data and instructions
C
What is typically obscured by a heavyweight fan in both desktop and laptop computers? A. Secondary storage devices B. Power supply C. CPU D. RAM
C
What will be the size of the following structure? #include <stdio.h> struct temp { int a[10]; char p; }; A. 5 B. 11 C. 41 D. 44
C
Which of the following is a complete function? A. int funct(); B. void funct(int) {printf("Hello"); C. int funct(int x) { return x=x + 1;} D. void funct(x) {printf("Hello"); }
C
Which of the following is a correct statement about processes? A. A program can have only one process associated with it at any given time B. A process is just another name for a program C. The operating system uses processes to manage concurrently running programs
C
Which of the following is a properly defined struct? A. struct {int a;} B. struct a_struct {int a;} C. struct a_struct {int a;}; D. struct a_struct int a;
C
Which of the following is an advantage of using pre-built registers in assembly programming A. Registers are stored in hardware which makes them B. There is only one variable type associated with registers C. Data and instructions stored in them are faster to access than that in memory D. The large number of 64 available register in RISC-V makes writing code easier
C
Match the debugger command with its corresponding description A. break: Sets a breakpoint in a program B. list: Shows the source code around pause point or specified point C. next: Executes the current line of code in the program and pauses D. quit: Ends the debugger session and exits the tool
Correctly matched
Match the part of program memory with its description Code: where function instructions are stored Data: where global variables are stored Heap: where dynamically allocated memory is stored Stack: where local variables and function parameters are stored
Correctly matched
A pointer variable can be A. Passed to a function as an argument B. Changed within a function C. Returned by a function D. All of the above
D
If the following values were the result of the condition of an if state tn, which would evaluate to true? A. 1 B. 66 C. -1 D. All of the above
D
The _____ executes program Instructions to carry out tasks. These instructions and the necessary data for them stored in _____ A. CPU, ALU B. CPU, Control Unit C. CPU, Processing Unit D. CPU, RAM
D
True or false: it is possible to have more than one main function in one program
False
In a couple of sentences, explain the main idea behind a load-store architecture such as RISC-V
In RISC-V, load and store allows specific memory addresses to be used for arithmetic operations like add, sub, etc. Load and store architecture in RISC-V allows the access to memory and being able to access the information stored in that address or register by loading it and to storing works similarly by allowing users to determine what's stored in registers makes them accessible for loading from memory
Explain the concept behind multi-core processing. What is a core, and why is it useful for computers to have multiple cores?
Multi-core processing is multiple processes going on at once and a core is individual processing units within a CPU that can execute instructions independently. It is useful for computers to have multiple cores because it allows tasks to run simultaneously, enhancing multitasking and improving overall system performance
Explain in your own words the different between pass by reference and pass by value
Pass by reference is when a copy/reference is passed instead of passing the actual value. Pass by values is passing the actual value instead of making a copy to reference to. This means that pass by reference won't change the value since it's only referring to the copy, but when pass by value is used, the value will be changed if it is changed since it's not a reference
Given two unsigned binary numbers, A and B, of the same length, add them together and write The result in binary form A = 1011 B = 0010
Steps: (Note: 1 + 1 = 0 and carry a 1 & 1 + 1 + 1 = 1 and carry a 1) 1011 0010 —----- 1101 Answer: 1101
True or false: there is no bound checking, which means an array "numbers" with a captor 5 elements, numbers[6] is a legal statement
True
Translate the following code in C to RISC-V assembly code. Assume only integer array A is stored in memory, and assume its base address is in register x16. The value of i is stored in register x5 for(i = 0; i < 200; i++){ A[i] = A[i] + A[i+1] + A[i-1]; }
add x5, x5, 0 #perform i + 0 and store in register 5 addi x7, 0l 200 #register 7 has 200 Loop: bge x5, x7, exit #if i >= 200, then exit the loop slli x8, x5, 2 #calculate offset for A[i] add x9, x8, x16 #add base + offset to get A[i] addi x10, x8, 4 #offset + 4 for A[i+1] and store in register 11 add x11, x10, x16 #add base + offset to get A[i+1] addi x12, x8, -4 #offset for A[i-1] add x13, x12, x16 #add base + offset to get A[i-1] add x20, x9, x11 #perform A[i] + A[i+1] add x21, x20, x13 #perform A[i] + A[i+1] + A[i-1] sw x21, 0(x9) #store results at memory location of A[i] Exit:
The sizeof operator returns the total amount of memory allocated to an array in ___
bytes
The program ___begins in the main function
execution
Write the C code to display the numbers from 100 to 50 in decrements of 10 using a for or a while loop (don't include main function)
for(int i = 99; i >= 51; i -= 10) { printf("%d", i); }
Values that are used in function definition are called ____ arguments, while values used in the function call are known as _____ arguments
formal, actual
Dynamically allocated or heap memory must be explicitly deallocated when its no longer needed by a program. What C function is used to deallocate memory?
free()
Write a function named time_calc, that does the following: 1) Accepts two parameters: long time_minutes, int *hours 2) Calculates the total number of hours in time_minutes. Note that this variable stores a total number of minutes. Note also that *hours is a pointer to an integer value 3) The results are stored using pointers, so the results can be used by the calling program
int time_calc (long time_minutes, int *hours){ int hour = *hours; //if minutes are below 60 minutes, then hours is 0 if (time_minutes < 60){ *hours = 0; } //if minutes are equal to or more than 60, then calculate hours else if (time_minutes >= 60){ *hours = time_minuters/60; } //return the number of hours return *hours;
Translate the following code in C to RISC-V assembly code. Assume all values are integers and the f is in x19, g in x20, h in x21, i in x22, and j in x23 if (i == j) f = g + h; else f = g - h;
loop: bne x22, x23, else #if i != j, then go to else add x19, x20, x21 #perform f = g + h else: sub x19, x20, x21 #perform f = g - h
Translate the following code in Cto RISC-V assembly code. Assume the variables stored in memory are all integers, and assume the following: the memory addresses of a, b, and i are in x11, x12, and x15, respectively. The values of a, b, and i are store in x1, x2, and x5, respectively a = b * 33 + i * 2;
lw x2, 0(x12) #load b from memory lw x5, 0(x15) #load i from memory slli x6, x2, 5 #multiply b * 2^5 = b * 32 addi x7, x6, 1 #now contains g * 33 slli, x10, x5, 1 #multiply i * 2^1 = i * 2 add x20, x7, x10 #perform b * 33 + i * 2 and store in register 11 sw x20, 0(x11) #store results at memory location of a
Provide the Linux/Unix command to rename the file vacation.png to paris.png
mv vacation.png paris.png
In C programming, you are given three variables with specific types: student of type char, age of type int, and balance of type float. Using printf function, how would you write the code snippet to display the contents of these three variables on the same line of the console, separated by commas?
printf("%c, %d, %f", student, age, balance);
In C, using printf function, what would be the code snippet to display the contents of an integer variable named total_customers?
printf("%d", total_customers);
When dynamically allocating memory for an array, we should always use the _____ operator To calculate the amount of space needed for the array
sizeof
Declare a pointer to a structure of type date called dates
struct date *dates;
Translate the following C code to RISC-V assembly. Use the following information in your answer: the memory addresses of array B, variable b, and variable i are in registers x17, x12, and x15. The values of variables b and i are in registers x2 and x5 i = 0 while (B[i] != b){ i++; }
sw x0, 0(x15) #store 0 at memory location of i lw x5, 0(x15) #load i slli x6, x5, 2 #calculate offset add x7, x6, x17 #base + offset gets B[i] lw x2, 0(x12) #load b loop: beq x7, x2, exit #if B[i] == b, then exit addi x5, x5, 1 #increment i sw x5, 0(x15) #store i at memory location of i beq x0, x0, loop #unconditional jump exit:
An array in C is a collection of data that are of the same____
type
The malloc function allocates a block of memory and returns a _____
void pointer
Translate the following code in C to RISC-V assembly code. Assume the variables are in memory, they are all integers, and assume the following: A is in x16, B in x17, the memory address of a is in x11, and the value of a is stored in x1 A[10] = B[10] + a; A[20] = A[20] + B[20]; A[30] += B[30];
~ A[10] = B[10] + a here: lw x1, 40(x17) #load B[10] lw x2, 0(x11) #load a add x6, x1, x2 #add B[10] to a and store in register 6 sw x6, 40(x16) #store results of B[10] + a into memory location of A[10] ~ A[20] = A[20] + B[20] here: lw x1, 80(x16) #load A[20] lw x2, 80(x17) #load B[20] add x8, x1, x2 #perform A[20] + B[20] and store in register 8 sw x8, 80(x16) #store the result at the memory location of A[20] ~ A[20] = A[20] + B[20] here: lw x1, 120(x16) #load A[30] lw x2, 120(x17) #load B[30] add x1, x1, x2 #perform A[30] += B[30] sw x1, 120(x16) #store result at memory location of A[30]
Using ONLY the add, sub, and slli instruction to convert the following C statements to the corresponding RISC-V assembly. Assume that the variables f, g, and j are integers assigned to registers x1, x2, and x3 respectively f = f * 128 g += 10; j -= g; f += g + j;
~ f = f * 128 here: slli x1, x1, 7 #perform f - f * 128 ~ g += 10 here: addi x2, x2, 10 #perform g += 10 ~ j -= g here: sub x4, x3, x2 #perform j-g and store in register 4 sub x3, x3, x4 #here is j -= g ~ f += g + j here: add x8, x2, x3 #perform g + j and store in register 8 add x1, x1, x8 #perform f += g + j
Convert the binary value of 0b1110000 from two's complement to a decimal value
~Steps: - First, look at leftmost bit which is 1 meaning leftmost bit is negative -Second, (-1 * 2^7) + (1 * 2^6) + (1 * 2^5) + (0 * 2^4) + (0 * 2^3) + (0 * 2^2) + (0 * 2^1) + (0 * 2^0) = -128 + 64 + 32 + 0 + 0 + 0 + 0 + 0 = -32 Answer: -32
Perform a 3 bit left shift on the value 0b1001110001111000 A. 0b0001001110001100 B. 0b1110001111000000 C. 0b1111001110001111 D. 0b1001110001111000
~Steps: - Given: 1001110001111000 - First, want to take the first 3 numbers in 1001110001111000 & add it to the right side 1001110001111000 - 1 doesn't matter, were just adding 0's only so this is the result below - 1110001111000000 (3 0's were add to the right side (not 1 bc that just tells if value is negative)) Answer: B
Convert the decimal 245 to a binary number
~Steps: 245/2 = 122.5 → 1 122/2 = 61 → 0 61/2 = 30.5 → 1 30/2 = 15 → 0 15/2 = 7.5 → 1 7/2 = 3.5 → 1 3/2 = 1.5 → 1 2/1 = 2 → 0 ½ = 0.5 → 1 (stop at ½) Answer: 0b01110101
Convert 336 from decimal to hexadecimal
~Steps: 336/16 = 21 → 336 - (16 * 21) = 0 21/16 = 1.313 → 21 - (16 * 1) = 5 1/16 = 0.063 → 1 - (16 * 0) = 1 Answer: 0x150