CS140 -- Midterm Exam: Q12 - Q14 (cont)

Question 12: Which of the following pieces of code should go into ``Code fragment 1'' (specify all that are correct):

a. p = &p2;
b. p = (Person *) malloc(sizeof(Person));
c. &p2 = (Person *)malloc(sizeof(Person));
p = &p2;
d. p = parray
e. p = &(parray[0])


Question 13: Which of the following pieces of code should go into ``Code fragment 2'' (specify all that are correct):

a. &p.firstname = is->fields[0];
&p.lastname = is->fields[1];
b. p->firstname = is->fields[0];
p->lastname = is->fields[1];
c. p->firstname = strdup(is->fields[0]);
p->lastname = strdup(is->fields[1]);
d. *p->firstname = strdup(is->fields[0]);
*p->lastname = strdup(is->fields[1]);
e. *p.firstname = strdup(is->fields[0]);
*p.lastname = strdup(is->fields[1]);
f. strcpy(p->firstname, is->fields[0]);
strcpy(p->lastname, is->fields[1]);
g. strcpy(&(p->firstname), is->fields[0]);
strcpy(&(p->lastname), is->fields[1]);


Question 14: Which of the following pieces of code should go into ``Code fragment 3'' (specify all that are correct):

a. p->age = is->fields[2];
b. p->age = strdup(is->fields[2]);
c. p->age = atoi(is->fields[2]);
d. p->age = atoi(strdup(is->fields[2]));
e. sscanf(is->fields[2], "%d", &p.age);
f. sscanf(is->fields[2], "%d", p->age);
g. sscanf(is->fields[2], "%d", &p->age);
h. p->age = malloc(sizeof(int));
sscanf(is->fields[2], "%d", p->age);