CS 2505 Final Exam Flashcards

1
Q

Which of the following are valid ways to pass information about an array back to the calling function?
(Mark all that apply)

a. Pass the array as a parameter to the called function and modify the contents directly in the called
function.
b. Return a pointer to a statically allocated array declared within the called function.
c. Return an array from the called function.
d. Return a pointer to a dynamically allocated array declared within the called function.

A

a. Pass the array as a parameter to the called function and modify the contents directly in the called
function.
d. Return a pointer to a dynamically allocated array declared within the called function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which of the following are true of the linker? (Mark all that apply)

a. The linker turns our C source code into assembly code, which is still human readable but closer to
what the computer can read.
b. The linker is responsible for resolving functions used in one file but created in another.
c. The linker creates an executable from object files (.o files).
d. The linker only runs if we have multiple C files.

A

b. The linker is responsible for resolving functions used in one file but created in another.
c. The linker creates an executable from object files (.o files).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

You have been working on a homework that has source code files shapes.c, shapes.h, and sh_main.c
(which has your main() function). Which of the following compile lines would correctly create an
executable called shape_fun, turn on the warnings we have discussed in class, and turn on debugging
symbols?

a. gcc -o shape_fun -Wall -Wextra -g sh_main.c shapes.c
b. gcc -o -Wall -Wextra -g shape_fun sh_main.c shapes.c
c. gcc -o shape_fun -Wall -debug sh_main.c shapes.c shapes.h
d. gcc sh_main.c shapes.c shapes.h -Wall -Wextra -debug -exe shape_fun

A

a. gcc -o shape_fun -Wall -Wextra -g sh_main.c shapes.c

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following is NOT a command to get to your home directory in rlogin?

a. cd
b. cd /home
c. cd /home/<classification>/<PID>
d. cd ~</PID></classification>

A

b. cd /home

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when the following command is run? (You may assume that the files main.c, vector.c,
and vector.h exist in the current working directory.)
tar -cf main.c vector.c vector.h HW2.tar

a. This command is syntactically invalid. You cannot provide the name of the tar file as the final
argument of the command.
b. Only the C files (not the header files) are added into an archive file called HW2.tar. Header file
contents can be inferred from C files, so they do not need to go into tar files.
c. The contents of main.c is overwritten. main.c is now a tar file in the computer’s mind and
contains vector.c and vector.h.
d. An archive file called HW2.tar is created. This file contained uncompressed versions of main.c,
vector.c, and vector.h.

A

c. The contents of main.c is overwritten. main.c is now a tar file in the computer’s mind and
contains vector.c and vector.h.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

You are a member of a shared computer, and you have uploaded a file called clients.xlsx that you
need other people on the computer with the same classification as you to be able to see and modify.
Which command will grant the necessary permissions? You may assume you are in the directory with
the file.

a. pmod clients g+rw
b. chmod g+rw clients
c. pmod group clients.xlsx +rw
d. chmod g+rw clients.xlsx

A

d. chmod g+rw clients.xlsx

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which of the following statements about dynamic memory is FALSE?

a. Dynamic allocations have space reserved for them in memory at runtime.
b. Dynamic memory is garbage collected if we lose all references to it.
c. We can grow or shrink the size of a dynamically allocated array at will.
d. Dynamic memory is used to keep items from disappearing after function calls end.

A

b. Dynamic memory is garbage collected if we lose all references to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many bytes of memory does the following allocation reserve?
double* arr = calloc(10, sizeof(double));

a. 80
b. 40
c. 20
d. 10

A

a. 80

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which step in the compilation process produces assembly code?

a. assembler
b. preprocessor
c. linker
d. compiler

A

d. compiler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Examine the code below. In what situation would input be NULL?
FILE* input = fopen(“my_input_file.txt”, “r”);

a. If my_input_file.txt does not exist anywhere on the computer.
b. If my_input_file.txt is an empty file.
c. If my_input_file.txt does not exist in the current working directory.
d. If my_input_file.txt has both read and execute permissions.

A

c. If my_input_file.txt does not exist in the current working directory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Answer questions 11-13 below using the following directory structure (from HW1). You are
just inside your food_court directory. Assume you are on rlogin. If you were to run the ls
command, you would see drinks and food.

food_court/ <– YOU ARE HERE
|– drinks
| >– coffee.txt
| >– smoothie.txt
| >– soda.txt
|– food
|– mains
| >– burger.txt
| >– pizza.txt
|– sides
>– fries.txt
>– salad.txt
>– smoothie.txt

  1. Which of the following will create a file called taco.txt in the mains directory from your current
    location?
    a. mk taco.txt food/mains
    b. touch ./food/mains/taco.txt
    c. touch taco.txt ./food/mains/
    d. touch /food_court/food/mains/taco.txt
A

b. touch ./food/mains/taco.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Answer questions 11-13 below using the following directory structure (from HW1). You are
just inside your food_court directory. Assume you are on rlogin. If you were to run the ls
command, you would see drinks and food.

food_court/ <– YOU ARE HERE
|– drinks
| >– coffee.txt
| >– smoothie.txt
| >– soda.txt
|– food
|– mains
| >– burger.txt
| >– pizza.txt
|– sides
>– fries.txt
>– salad.txt
>– smoothie.txt

  1. You decide to remove the mains directory. You run this command:
    rmdir ./food/mains/
    The command fails. What is the most likely reason the command failed?

a. The directory contains files. It must be empty to remove it in Linux.
b. rmdir will only work with an absolute path, not a relative path.
c. rmdir must be called from within the directory you want to remove.
d. The command does not know if you want to remove food or mains.

A

a. The directory contains files. It must be empty to remove it in Linux.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Answer questions 11-13 below using the following directory structure (from HW1). You are
just inside your food_court directory. Assume you are on rlogin. If you were to run the ls
command, you would see drinks and food.

food_court/ <– YOU ARE HERE
|– drinks
| >– smoothie.txt
| >– soda.txt
|– food
|– mains
| >– burger.txt
| >– pizza.txt
|– sides
>– fries.txt
>– salad.txt
>– smoothie.txt

  1. The menu is getting a bit of a rewrite. coffee.txt needs to be renamed to latte.txt. Which of the
    following commands will accomplish the menu modification? (Assume this command will be run from
    the drinks directory.)

a. rn coffee.txt latte.txt
b. rn latte.txt coffee.txt
c. mv latte.txt coffee.txt
d. mv coffee.txt latte.txt

A

d. mv coffee.txt latte.txt

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

You are working with a text file that contains the following text:
16,Senger5Cao
You do not care about anything in that file except the two numbers, but those two numbers could be
anything. How would you properly format an fscanf statement to read those 2 numbers into int x and
int y?

a. fscanf(in, “%x,Senger%yCao\n”);
b. fscanf(in, “%d,Senger%dCao\n”, &x, &y);
c. fscanf(in, “%d %d\n”, &x, &y);
d. fscanf(in, “%d,Senger%dCao\n”, x, y);

A

b. fscanf(in, “%d,Senger%dCao\n”, &x, &y);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Examine the following code. Assume function() is called from main() at some point, and assume
that all necessary include statements are at the top of the file.

void function()
{
for (int i; i < 10; i++)
{
printf(“%d “, (i * i) + 1);
}
}

When this code is compiled and run, what is displayed on the terminal?

a. 1 2 5 10 17 26 37 50 65 82
b.
1
2
5
10
17
26
37
50
65
82
c. indeterminate
d. %d %d %d %d %d %d %d %d %d %d
4

A

c. indeterminate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which of the following would be the correct way to dynamically allocate for an array of 10 Mesa structs (as
declared in Project 1)?

a. struct Mesa* mesa_arr = malloc(11 * sizeof(struct Mesa));
b. struct Mesa mesa_arr[10];
c. Mesa* mesa_arr = (Mesa) malloc(10 * sizeof(Mesa));
d. struct Mesa
mesa_arr = (struct Mesa*) malloc(10 * sizeof(struct Mesa));

A

d. struct Mesa* mesa_arr = (struct Mesa*) malloc(10 * sizeof(struct Mesa));

13
Q

include <stdio.h></stdio.h>

What happens when you run the following code on rlogin?\

void pointer_value(int* p)
{
printf(“The value stored at my
pointer is: %d\n”, *p);
}

int main()
{
int* ptr = 0;
pointer_value(ptr);
return 0;
}

a. Compilation error (no executable is created, so we can’t run it)
b. Since pointers are addresses, the value will display in hex, not decimal
c. The value 0 is printed after the given string
d. Segmentation fault

A

d. Segmentation fault

14
Q

The following code is intended to join two strings together into one. What is the problem with it?
char* one = “apple”;
char* two = “banana”;
char* join = calloc(strlen(one) + strlen(two), sizeof(char));
strcat(join, one);
strcat(join, two);
printf(“%s\n”, join); //should print “applebanana”

a. You cannot add values within the arguments of calloc().
b. The dynamic allocation does not have room for the null-terminator.
c. Statically-declared strings must use array syntax, not pointer syntax.
d. strcat() must already have one string in it to be used.

A

b. The dynamic allocation does not have room for the null-terminator.

15
Q

In the model of the von Neumann machine we have been using in class, what components live on the CPU?

Select all that apply if you feel that there are multiple answers.
a. registers
b. RAM
c. program counter
d. ALU

A

a. registers
d. ALU

16
Q

You are working on rlogin. rlogin is a little-endian system. You are storing the number 0x15B47F at memory
address 0x234. What is stored at 0x236?

a. 0x7F
b. 0x15
c. nothing relevant to this number
d. 0xB4

A

b. 0x15

17
Q

The following is a function to perform a deep copy of an array. However, the const modifiers on the function
parameters are causing the function to not perform correctly. Which line or lines of the function will throw
errors when this function is compiled?

int* deep_copy(int* const src, int* const dest, const int len)
{
int i = 0; //1
while ( i < len ) //2
{
dest[i] = *src; //3
src++; //4
i++; //5
}
return dest;
}

a. line 2
b. both lines 3 and 4
c. line 3
d. line 4

A

d. line 4

18
Q

I see the following line of assembly:
addq %rdx, -8(%rbp)
Which parts of this command tell me the size of the data I am working with? Select all that apply if you feel
that there are multiple answers.

a. The -8
b. The r in %rdx
c. The use of the parentheses
d. The q suffix

A

b. The r in %rdx
d. The q suffix

19
Q

Imagine we had a datatype called an int10_t which consisted of 10 bits (instead of our normal 8, 16, etc.).
Assume this datatype behaves the same way as all the other integer types from the inttypes.h library. What
range of values could our int10_t store?

a. 0 to 1023
b. 0 to 1024
c. -512 to 511
d. -1024 to 1023

A

c. -512 to 511

20
Q

What is printed by the following code?

int val = 0x10;
int* p = &val; //assume p is memory address 0x123
int** p2 = &p; //assume p2 is memory address 0x456

**p2 = 0x11;
*p = 0x54;
*p2 = 0x33;

printf(“val: 0x%x, p: 0x%x, p2: 0x%x \n”, val, p, p2);

a. val: 0x54, p: 0x54, p2:0x33
b. val: 0x10, p: 0x123, p2: 0x456
c. val: 0x54, p: 0x33, p2: 0x456
d. val: 0x10, p: 0x54, p2: 0x33

A

c. val: 0x54, p: 0x33, p2: 0x456

21
Q

How many bytes of memory (if any) are leaked from this function? Assume all necessary libraries are included.

void func()
{
double* x = malloc(5 *
sizeof(double));
for (int i = 0; i < 5; i++)
{
x[i] = i;
printf(“x[i] = %lf\n”, x[i]);
}
x = NULL;
free(x);
}

a. 20 bytes
b. 0 bytes
c. 40 bytes
d. 5 bytes

A

c. 40 bytes

22
Q

What is problematic about the code below? You may assume there is one name per line in the input file and that the names do not contain spaces.

char* get_name(FILE* in)
{
char buffer[100];
fscanf(in, “%s\n”, buffer);
char* name = calloc(strlen(buffer) + 1, sizeof(char));
name = buffer;
return name;
}

a. name goes out of scope after this block, so the identifier cannot be used elsewhere.
b. Pointers cannot be returned from functions.
c. The string stored into name is not properly null-terminated.
d. The contents of buffer is not correctly transferred into name

A

d. The contents of buffer is not correctly transferred into name

23
Q

Which of the following will return the value of bits b7 through b4 (the bolded bits below) from the starting
number, x, shifted to the lowest 4 bit positions? Select all that apply if you feel that there are multiple answers.

  • initially:
    – int x = b31. . . b11b10b9b8 b7b6b5b4 b3b2b1b0
  • after bitwise operations:
    – int x = 0. . . 0000 b7b6b4b5
    – //lowest four bits of an int – should hold a value between 0 and 15

a.
x = x &laquo_space;24;
x = x & 0xF0000000;
return x;

b.
x = x & 0xF0;
x = x&raquo_space; 4;
return x;

c.
x = x&raquo_space; 4;
x = x & 0x0000000F;
return x;

d.
x = x & 0xFF;
x = x&raquo_space; 4;
return x;

A

B, C, D

24
Q

What is the binary representation of -19 as an int8_t?

a. 0001 0011
b. 1110 1101
c. 0110 1101
d. 1001 0011

A

b. 1110 1101

25
Q

What does the 0 return value from the main() function represent in C?

a. False. Something went wrong during execution of the program.
b. The number of remaining function calls.
c. Typical execution and termination of the program.
d. NULL. By the end of the program execution, the stack should be NULL.

A

c. Typical execution and termination of the program.