For each of the following questions, be sure to choose not only the correct answer but the *canonically correct* answer. There may be one or more answers which are correct, but some of those answers may not be conventional use.
Question 1 (1 point)
Which of the following will allocate a buffer suitable for 4 integers?
Question 1 options:
int *ptr = (int *)malloc(sizeof(int) * 4);
int *ptr = (int * 4)malloc(sizeof(int));
int *ptr = (int *)malloc(4);
int *ptr = malloc[4];
Question 2 (1 point)
For the following program, which satement best fits as a parameter for the printf() call?
#include
typedef union u_foo {
int iValue;
float fValue;
} Foo;
void main(int argc, char **argv) {
Foo foo;
foo.iValue = 42;
printf("The value of foo is %d\\", );
}
Question 2 options:
foo.fValue
foo.iValue
(&foo)->iValue
(*(&foo)).iValue
Question 3 (1 point)
In the following makefile, what is the target `foo` dependent upon?
all: foo
foo: foo.c
gcc -o foo foo.c bar.c
bar: bar.c
gcc -o bar bar.c
clean:
rm foo bar
Question 3 options:
foo.c and bar.c
bar
all
foo.c
Question 4 (1 point)
For the following header file, which statement best declares a function that takes 2 integer parameters?
#ifndef SOMETHING__H
#define SOMETHING__H
#endif // SOMETHING__H
Question 4 options:
int foo(int a, int b);
extern int foo(int a, int b);
extern int foo(int *a, int *b);
static int foo(int a, int b);
Question 5 (1 point)
For the following program, what line is best applied to release the allocated memory?
#include
int main(int argc, char **argv) {
char *buf = malloc(32);
// assume buf is used appropriately here
}
Question 5 options:
release(buf);
free(buf);
free(32);
delete(buf);
Question 6 (1 point)
In the following program, which statement will properly copy the contents of `foo` to `bar`?
#include
#include
#include
int main(int argc, char **argv) {
char *foo = "This is a statement of intent";
char bar[16];
memset(bar, 0, 16);
}
Question 6 options:
memcpy(bar, foo, 15);
strncpy(bar, foo, 16);
strcpy(bar, foo);
strcpy(foo, bar);
Question 7 (1 point)
For the following program, which statement best fits as a parameter for the printf() call?
#include
typedef struct t_foo {
int value;
} Foo;
void main(int argc, char **argv) {
Foo foo = { 42 };
Foo *pFoo = &foo;
printf("The value of foo is %d\\", );
}
Question 7 options:
pFoo->value
*pFoo->value
pFoo.value
(*pFoo).value
Question 8 (1 point)
For the following program, which statement best fits as a parameter for the printf() call?
#include
typedef struct t_foo {
int value;
} Foo;
void main(int argc, char **argv) {
Foo foo = { 42 };
printf("The value of foo is %d\\", );
}
Question 8 options:
(*foo).value
foo->value
*foo.value
foo.value
Question 9 (1 point)
For the following header file, which line of code is missing?
#ifndef SOMETHING__H
extern int foo;
#endif
Question 9 options:
#define SOMETHING__H
// SOMETHING__H
static int foo();
typedef foo something;
Question 10 (1 point)
What is the size in bytes of the following structure?
struct t_foo {
int bar: 6;
int baz: 5;
int bat: 2;
};
Question 10 options:
12
13
16
2
Question 11 (1 point)
For the following program, what is the best way to get a block of memory on the heap for the variable `foo`?
#include
typedef struct t_foo {
int value;
} Foo;
void main(int argc, char **argv) {
Foo *foo = ;
}
Question 11 options:
malloc(sizeof(t_foo))
malloc(8)
{ 42 }
malloc(sizeof(Foo))
Question 12 (1 point)
For the following program, which line of code will place the contents of the file into the memory for the variable `buf`?
#include
void main(int argc, char **argv) {
FILE *fp = fopen("somefile.txt", "rt");
int fsize = // ASSUME WE GET THE SIZE OF THE FILE HERE
char buf[128];
// MORE PROGRAM CODE HERE
}
Question 12 options:
fread(fp, buf, fsize)
fread(buf, fp, fsize, 1)
fread(fp, fsize, buf, 1)
fread(buf, fsize, 1, fp)
Question 13 (1 point)
For the following program, which line of code will get the size of the file in question?
#include
void main(int argc, char **argv) {
FILE *fp = fopen("somefile.txt", "rt");
fseek(fp, 0, SEEK_END);
fseek(fp, 0, SEEK_SET);
}
Question 13 options:
int fsize = ftell(fp);
int fsize = fp.position;
int fsize = ftell(&fp);
int fsize = fread(fp, 1);
Question 14 (1 point)
For the following program, which is the best type for the variable `next`?
#include
typedef struct t_foo {
int value;
next;
} Foo;
void main(int argc, char **argv) {
Foo foo;
}
Question 14 options:
struct t_foo
Foo
struct t_foo*
Foo *
Question 15 (1 point)
In the following program, what is the value of `*ptr`?
#include
int main(int argc, char **argv) {
int foo[] = { 0, 1, 2, 3, 4 };
int *ptr = &foo;
do {
++ptr;
} while (!ptr);
}
Question 15 options:
foo[1]
2
0
1