You are on page 1of 4

COMPRO2 Long Exam 1

October 8, 2010 (Friday) 1000h 1200h

2nd Term 10-11


Caronongan/Chua/Lim/Salvador

READ BEFORE YOU START!


1. Use only 1 booklet. All answers must be written in your ANSWER SHEET & in ink. Use your booklet for scratch work. Answers
that are ambiguous, multiple, or written in pencil will not be considered. Write legibly.
2. Calculators are not allowed. Turn off all cellular phones. Any form of communications such as use of cellphone, talking with
classmates (even if innocently, like asking the time), borrowing things from classmates, and non-verbal language (including
signing) among others during the exam are considered cheating.
3. Tearing pages from your booklet and other suspicious acts are considered cheating. Cheating in any form is punishable with a
grade of 0.0 for the course and discipline offense.
4. All programs are designed to run using gcc. It is not required to put internal documentation in your code for your answers to
programming problems. Assume all header files have been included.
5. For every instruction not followed, 5 points will be deducted from your total score.
6. Submit your answer sheet and booklet. You can keep the questionnaire.
7. After submission, LEAVE the second floor using the nearest stairs. If caught loitering, you can be charged with a minor offense
and be sent to the Discipline Office.
I. Analysis (40 pts)
A. Debugging (10 pts). The recursive function below is supposed to determine whether the array of characters

letters (with end number of initialized entries) contains a palindrome or not. The function returns 1, if it is
a palindrome. Otherwise, the function returns 0. For lines with errors, indicate the line number and the
corresponding corrected statement. Note that correct statements should not be modified. start refers to
the starting index and end refers to the number of characters in the array.
1
2
3
4
5
6
7
8
9

void isPalindrome (char letters, int start, int end)


{
if (start >= end - 1)
return 1;
else if (letters[start] != letters[end])
return 0;
else
return isPalindrome(letters[end], end 1, start + 1);
}

B. Multiple Choice (10 pts).


1. Which of the following is a valid pointer declaration?
2.
3.
4.

5.
6.
7.
8.
9.
10.

A. int a;
B. int *a;
C. int &a;
D. none of the above
Given int array[5], which of the following will pass the base address of the array as a parameter?
A. function(array[]);
B. function(array[5]);
C. function(array); D. none of the above
Given char str[50];, the following statements properly get an input string except:
A. scanf(%s, &str);
B. scanf(%s, str);
C. gets(str);
D. none of the above
What does strcpy(str1, str2); do?
A. Copies the value of str1 and store to str2
C. Swaps the values of str1 and str2
B. Copies the value of str2 and store to str1
D. none of the above
The library ________ needs to be included for memory allocation and deallocation
A. stdlib.h
B. stdio.h
C. conio.h
D. none of the above
This function returns the length of the input string.
A. strcmp(char str[]); B. strlen(char str[]); C. strcpy(char str[]); D. none of the above
If char array[50] is a string, how many user-input characters can it contain?
A. 48
B. 49
C. 50
D. none of the above
In C, what should be the last character in a string?
A. \0
B. \n
C. null
D. garbage value
It is impossible to input and output strings without string.h
A. True
B. False
If char name[51] contains I LOVE COMPRO2, what does strlen(name); return?
A. 12
B. 14
C. 15
D. 51
Page 1 of 4

C. OBJECTIVES Pointers and Dynamic Memory Allocation (20 pts).


For numbers 1 to 5, make sure that you indicate the type as it is written in C language. For example, do not write integer
pointer, it should be written properly as int * in C language..
1. What is the return type of function malloc()?
2. What is the data type of the parameter of malloc()?
3. What is the return type of function free()?
4. What is the data type of the parameter of function free()?
5. What is the data type of NULL?
For the following problems, assume that we are using as platform the computer and GCC compiler in our computer
laboratory.
6. Write the assignment statement for allocating 5KB of memory for storing elements of double data type. Assume that
the recipient pointer variable was declared as double *ptr.
7. How many double data type elements can be stored based on the memory request in question 6?
8. What is the address of the twentieth element based on question 6? (Note: recall that the address of the first
element corresponds to the return value of malloc()).
9. Assuming a base address of 500600 (in decimal) what is the address (in decimal) of the last element in question 6?
10. Using scanf(), input the value of the tenth element based on the memory request in question 6.
For numbers 11 to 20, assume the following declarations:
char *p1, *p2;
int *p3, *p4;
float *p5, *p6;
double *p7, *p8;
Determine if there is an error in the statements in numbers 11 to 20. Question items are independent from each other.
Choose your answer from among the following choices.
A.
B.
C.
D.
E.

data type mismatch error


illegal index (i.e, index is out of bounds)
operation will result in garbage value
illegal dereference operation
there is no error

Write the capital letter of your choice as answer for this problem.
11.
p1 = 0;
/* zero */
12.
p3 = 10;
13.
p8 = p7 = NULL;
14.
p6 = p4 = NULL;
15.
*p4 = malloc(sizeof(int) * 5);
For numbers 16 to 20, assume that the first statement is correct. Determine if there is something wrong with the second
statement.
16. p2 = NULL;
*p2 = 'A';
17.

p1 = malloc(sizeof(char) * 10);
strcpy(p1, "COMPRO2");

18.

p3 = malloc(sizeof(int) * 10);
*(p3 - 1) = 10;

19.

p8 = malloc(sizeof(double) * 8);
scanf("%lf", &p8[8]);

20.

p6 = malloc(sizeof(float) * 10);
p7 = p6 + 9;

Page 2 of 4

II. Tracing (15 points). Answer the questions below based on the given program.
void func2 (int arrInt[],int nOp)
{
int i,nTemp;
switch (nOp)
{
case 0:
nTemp = arrInt[4];
for (i = 4;i >= 1;i--)
arrInt[i] = arrInt[i-1];
arrInt[0] = nTemp;
break;
case 1:
nTemp = arrInt[0];
for (i = 0;i < 4;i++)
arrInt[i] = arrInt[i+1];
arrInt[4] = nTemp;
}
}
void func1(int arrInt[],int nNum,int nOp)
{
int i;
for (i = 0;i < nNum;i++)
func2(arrInt,nOp);
}
int main()
{
int arrInt[5] = {35, 2, -1, 123, 99};
func1(arrInt,2,0);
func1(arrInt,3,1);
func1(arrInt,(arrInt[0]%10) + 2,0);
return 0;
}

1.) What will be the contents of arrInt after executing func1(arrInt,2,0); in main()? (5 points)
2.) What will be the contents of arrInt after executing func1(arrInt,3,1); in main()? (5 points)
3.) What will be the contents of arrInt after executing func1(arrInt,(arrInt[0]%10) + 2,0); in
main()? (5 points)
III. Programming with Strings. (15 pts)
Write a function toUpperCase( ). Given an input string, the function must turn all lower case letters in the string to
upper case. Using brute force will not merit full points. Hint: the ASCII values of A and a are 65 and 97 respectively.
Note that the string may contain other characters aside from lower case letters.The function prototype is as follows:
void toUpperCase(char string[]);
IV. Programming with Arrays. (15 pts)
Write the function displayBest() which displays the name of the student with the highest exercise average. Refer to
the definitions, function prototype, and descriptions below:
#define MAX_Student 22
#define MAX_Exer
10
typedef

char string30[31];

void displayBest(int grades[][MAX_Exer], int student, int exer, string30 names[]);


grades refers to the list of exercise scores of the students in the class. Note that it is possible that it may not be fully filled
with values. Thus, the number of students and number of exercises is given to signify which are the initialized elements in
Page 3 of 4

the 2-D array. The parameter student refers to the number of initialized elements in both the arrays grades and names.
exer refers to the number of initialized columns in the 2-D array grades. names refers to the array of strings containing
the students names. Assume that the student whose name is in index n of names will also have his list of grades in row
index n of grades (where n refers to any integer value not exceeding the number of students). Also assume that the
averages are unique.
V. Programming with Pointers. (20 pts)
Assume that we are given a list of characters. The base address of the list is stored in a character pointer variable ptr,
and the number of elements is stored in integer variable n. We are interested in implementing the body of function
void Relocate(char *ptr, int origloc, int newloc);
which will relocate an existing character in the list from one memory location (index origloc) to another memory location
(index newloc). Other elements will be relocated accordingly by moving the affected characters towards the direction of
lower indices.
We also assume that origloc and newloc are values within the range 0 to n-1, and that origloc < newloc. There is no
need to test for these conditions within the Relocate() function.
For example, let the original list of characters be as follows. The numbers indicate the indices corresponding to each
character element.
C
O
M
P
R
O
2
0

After calling Relocate(ptr, 0, 1) using the original list, the new list will be:
O

After calling Relocate(ptr, 1, 3) using the original list, the new list will be:
C

After calling Relocate (ptr, 4, 6) using the original list, the new list will be:
C

We consider two different algorithms that will achieve the relocation requirements.
Algorithm 1.
1. Let i be a local integer variable. Copy origloc to i.
2. Swap the element stored in index i with the element stored in index i + 1.
3. Increment i.
4. Repeat steps 2 and 3 as long as i is less than newloc.
Algorithm 2.
1. Let ch be local character variable. Copy the element in index origloc to ch.
2. Let i be a local integer variable. Copy origloc to i.
3. Copy the character in index i + 1 as the value of element in index i.
4. Increment i.
5. Repeat steps 3 and 4 as long as i is less than newloc.
6. Copy ch as the value of the element in index newloc.
Your task is to implement the body of the function following these algorithms. Two restrictions apply:
a. you must access the elements in the list using pointer dereferencing (DO NOT USE array indexing)
b. you can only declare/use two local variables named ch and i following the algorithms above

Page 4 of 4