wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Quiz 4 Review

Total questions: 27

Worksheet time: 14mins

Name
Class
Date
1.

What is the order in which C programs are compiled?

a)

Preprocessor -> Compiler -> Assembler -> Linker

b)

Compiler -> Assembler ->Preprocessor -> Linker

c)

Linker -> Preprocessor -> Compiler -> Assembler

d)

Preprocessor -> Linker -> Assembler -> Compiler

2.

What is true about the preprocessor?

a)

Replaces macros with their expansions

b)

Checks program for syntax errors

c)

Is only used for #include

d)

Is disabled by default

3.

What does N evaluate to?

a)

10

b)

67

c)

24

d)

11

4.

Which of the following statements about sizeof is FALSE?

a)

sizeof array variables is the same as sizeof(void*) since array variables decay to pointers

b)

sizeof always uses bytes as units

c)

sizeof can take both variables and types as arguments

d)

sizeof a struct might not be the sum of sizeof each member within the struct

5.

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?

a)

Anything can happen (Undefined Behavior)

b)

Compiler will error

c)

You get a runtime out of bounds error

d)

You will get a 0 or NULL byte

6.

Assuming the size of an int is 4 bytes, what will the following print?

a)

40

b)

4

c)

8

d)

10

7.

Which of the following is a valid way to find the length of an array?

a)

Sizeof(arr) / sizeof(arr[0])

b)

Sizeof(arr)

c)

Arr.len()

d)

Len(arr)

8.

Will it compile if Bianca uses printf’s %f specifier on integers?

a)

Yes, but you won’t get the expected results

b)

Yes, the compiler will implicitly convert the int to a float

c)

No, the compiler will error since you are passing in something of the wrong type

d)

No, you get a runtime error inside printf when it sees the passed in value is an integer

9.

In the following program, where are a, b, and c located respectively?

a)

Data, data, stack

b)

Stack, data, stack

c)

Data, stack, stack

d)

Data, heap, stack

10.

What is the type of p in this declaration?
int **p;

a)

Pointer to int

b)

Pointer to pointer to an int

c)

Array of ints

d)

Function pointer

11.

Pointer arithmetic — If int *p is at address 0x1000 and sizeof(int) = 4, what is p + 3?

a)

Address 0x100C

b)

Address 0x1012

c)

Address 0x100A

d)

Address 0x1004

12.

Pointer vs array: When p points to an array, which statement is true?

a)

p[i] is the same as *(p + i)

b)

p[i] is always faster than *(p + i)

c)

*(p + i) is invalid

d)

p[i] only works with char arrays

13.

Given the code, what does *p evaluate to?

a)

The address of x

b)

10

c)

The pointer p

d)

Undefined behavior

14.

Char pointer initialization: What is the issue with this code?

a)

Nothing is wrong

b)

You cannot modify string literals

c)

You cannot assign a char to char*

d)

s must be declared as an array

15.

Henry wrote this and has no clue how C works. What will this code print if I printed s2?

a)

hello(and then gibberish)

b)

hello

c)

(gibberish and then)hello

d)

Error

16.

Consider the code snippet that is run inside a function: Which statement is correct about where memory is allocated?

a)

char *p = "Andy"; allocates memory for the string on the stack

b)

char name[] = "Other Andy"; allocates memory for the string on the stack

c)

Both allocate memory on the heap

d)

Both allocate memory in global data of memory

17.

What is the correct typedef version for defining a struct named Student with a single int field id?

a)

typedef Student struct { int id; };

b)

typedef struct Student { int id; } Student;

c)

typedef { int id; } Student;

d)

struct Student typedef { int id; };

18.

Rick has no clue what code he just wrote. What does sp[1].a refer to?

a)

The first struct’s a field

b)

The a field inside the second struct in the array

c)

The address of arr

d)

A pointer to struct S

19.

Why do we need malloc in C?

a)

To allocate memory at run time when the required size is not known at compile time

b)

To automatically free memory when a function returns and prevent memory leaks

c)

To allocate variables in the global data section at runtime so they persist throughout the entire program

d)

To access variables more efficiently as using pointers is faster than calculating offsets in stack memory

20.

At runtime, local variables are stored on the stack and disappear when the function returns. Dynamically allocated memory remains allocated until ____.

a)

it is explicitly freed with free()

b)

another call to malloc overwrites it

c)

the pointer that references it goes out of scope

21.

Where is data we malloc?

a)

Heap

b)

Global Data

c)

User Stack

d)

System Space

22.

Which of the following correctly malloc’s an array of 12 ints pointed to by a variable myNums?

a)

int *myNums = malloc(sizeof(int) * 12);

b)

int *myNums = malloc(48);

c)

int *myNums = malloc(sizeof(int), 12);

d)

int myNums[12] = malloc(sizeof(int) * 12);

23.

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)

a)

We need 18 bytes for each bag!

b)

We need 67 bytes for each bag!

c)

We need 20 bytes for each bag!

d)

We need 36 bytes for each bag!

24.

Select all that is true about calloc:

a)

Calloc takes 2 arguments, the number of elements and the size of each element.

b)

Calloc initializes all values to 0

c)

Calloc takes 1 argument, the size of memory to be allocated

d)

Calloc does not initialize, we need to explicitly update the values

25.

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?

a)

No, arr is never updated to the temp pointer

b)

Yes, the program uses a temporary pointer so that if realloc fails, the original pointer and its data is preserved

c)

No, when we free temp the previous data pointed to by arr is also freed

d)

Yes, the call to realloc directly updates the size of data that arr points to, and temp handles error checking for a failed realloc

26.

Given a linked list of items, does the following correctly free the list and why?

a)

No, by freeing the next_item pointer we lose access to the rest of the list

b)

No, we also need to free the data in the struct pointed to by curr

c)

No, we never check if *head == NULL, which can lead to a segmentation fault when we try to dereference it

d)

Yes, we free all deallocated memory, and the pointer is accurately updated

27.

Refer to the C code shown in the image. Eric thinks there might be errors. Which lines could go wrong?

a)

Line 41: Node *curr = *head;

b)

Line 44: Node *new_Node = malloc(sizeof(Node));

c)

Line 51: curr->next = new_node;

d)

None – this program will work as intended in all cases