Font size
WorksheetsQuiz 4 Review
Total questions: 27
Worksheet time: 14mins
What is the order in which C programs are compiled?
Preprocessor -> Compiler -> Assembler -> Linker
Compiler -> Assembler ->Preprocessor -> Linker
Linker -> Preprocessor -> Compiler -> Assembler
Preprocessor -> Linker -> Assembler -> Compiler
What is true about the preprocessor?
Replaces macros with their expansions
Checks program for syntax errors
Is only used for #include
Is disabled by default
What does N evaluate to?
10
67
24
11
Which of the following statements about sizeof is FALSE?
sizeof array variables is the same as sizeof(void*) since array variables decay to pointers
sizeof always uses bytes as units
sizeof can take both variables and types as arguments
sizeof a struct might not be the sum of sizeof each member within the struct
Ronit is doing crazy things and made an array int arr [20]. Then he tried to access arr[21]. What happens when you access an array outside its specified size?
Anything can happen (Undefined Behavior)
Compiler will error
You get a runtime out of bounds error
You will get a 0 or NULL byte
Assuming the size of an int is 4 bytes, what will the following print?
40
4
8
10
Which of the following is a valid way to find the length of an array?
Sizeof(arr) / sizeof(arr[0])
Sizeof(arr)
Arr.len()
Len(arr)
Will it compile if Bianca uses printf’s %f specifier on integers?
Yes, but you won’t get the expected results
Yes, the compiler will implicitly convert the int to a float
No, the compiler will error since you are passing in something of the wrong type
No, you get a runtime error inside printf when it sees the passed in value is an integer
In the following program, where are a, b, and c located respectively?
Data, data, stack
Stack, data, stack
Data, stack, stack
Data, heap, stack
What is the type of p in this declaration?
int **p;
Pointer to int
Pointer to pointer to an int
Array of ints
Function pointer
Pointer arithmetic — If int *p is at address 0x1000 and sizeof(int) = 4, what is p + 3?
Address 0x100C
Address 0x1012
Address 0x100A
Address 0x1004
Pointer vs array: When p points to an array, which statement is true?
p[i] is the same as *(p + i)
p[i] is always faster than *(p + i)
*(p + i) is invalid
p[i] only works with char arrays
Given the code, what does *p evaluate to?
The address of x
10
The pointer p
Undefined behavior
Char pointer initialization: What is the issue with this code?
Nothing is wrong
You cannot modify string literals
You cannot assign a char to char*
s must be declared as an array
Henry wrote this and has no clue how C works. What will this code print if I printed s2?
hello(and then gibberish)
hello
(gibberish and then)hello
Error
Consider the code snippet that is run inside a function: Which statement is correct about where memory is allocated?
char *p = "Andy"; allocates memory for the string on the stack
char name[] = "Other Andy"; allocates memory for the string on the stack
Both allocate memory on the heap
Both allocate memory in global data of memory
What is the correct typedef version for defining a struct named Student with a single int field id?
typedef Student struct { int id; };
typedef struct Student { int id; } Student;
typedef { int id; } Student;
struct Student typedef { int id; };
Rick has no clue what code he just wrote. What does sp[1].a refer to?
The first struct’s a field
The a field inside the second struct in the array
The address of arr
A pointer to struct S
Why do we need malloc in C?
To allocate memory at run time when the required size is not known at compile time
To automatically free memory when a function returns and prevent memory leaks
To allocate variables in the global data section at runtime so they persist throughout the entire program
To access variables more efficiently as using pointers is faster than calculating offsets in stack memory
At runtime, local variables are stored on the stack and disappear when the function returns. Dynamically allocated memory remains allocated until ____.
it is explicitly freed with free()
another call to malloc overwrites it
the pointer that references it goes out of scope
Where is data we malloc?
Heap
Global Data
User Stack
System Space
Which of the following correctly malloc’s an array of 12 ints pointed to by a variable myNums?
int *myNums = malloc(sizeof(int) * 12);
int *myNums = malloc(48);
int *myNums = malloc(sizeof(int), 12);
int myNums[12] = malloc(sizeof(int) * 12);
Iris started a cat abandonment crisis, and we need to rescue them! What is then size of the data in rescue message that will be sent? In our cat rescue memory, sizeof(char) = 1, sizeof(int) = 4, sizeof(ptr) = 8, sizeof(Cat) = 12. (Ignore Padding)
We need 18 bytes for each bag!
We need 67 bytes for each bag!
We need 20 bytes for each bag!
We need 36 bytes for each bag!
Select all that is true about calloc:
Calloc takes 2 arguments, the number of elements and the size of each element.
Calloc initializes all values to 0
Calloc takes 1 argument, the size of memory to be allocated
Calloc does not initialize, we need to explicitly update the values
Given a pointer arr that points to a char array of 5 bytes, does the following correctly reallocate the array to 10 bytes, and why?
No, arr is never updated to the temp pointer
Yes, the program uses a temporary pointer so that if realloc fails, the original pointer and its data is preserved
No, when we free temp the previous data pointed to by arr is also freed
Yes, the call to realloc directly updates the size of data that arr points to, and temp handles error checking for a failed realloc
Given a linked list of items, does the following correctly free the list and why?
No, by freeing the next_item pointer we lose access to the rest of the list
No, we also need to free the data in the struct pointed to by curr
No, we never check if *head == NULL, which can lead to a segmentation fault when we try to dereference it
Yes, we free all deallocated memory, and the pointer is accurately updated
Refer to the C code shown in the image. Eric thinks there might be errors. Which lines could go wrong?
Line 41: Node *curr = *head;
Line 44: Node *new_Node = malloc(sizeof(Node));
Line 51: curr->next = new_node;
None – this program will work as intended in all cases
