Académique Documents
Professionnel Documents
Culture Documents
Exam 1
General Instructions
• Answer the items completely. Show your solutions when asked.
• Write as legibly as possible. Illegible or unreadable answers and solutions may not merit any points.
• Refrain from making unnecessary motions and sounds during the exam. Any suspicious behavior will be dealt
with accordingly.
• Direct all questions to the proctor.
• If you need to go to the CR, hand your questionnaire, answer sheet, and scratch paper to the proctor before
heading out. Only one person at any given time is allowed to go out.
• Once you’re done with the exam (one way or the other), place your scratch papers and the questionnaire inside
your blue book.
Part 1 - Objective-Type
1. Which of the following is an invalid identifier? Write only the letter corresponding to your choice. (1 pt.)
(a) double
(b) num_123
(c) Y70
(d) All of the above
(e) None of the above
ANSWER: (a)
2. Will the following C source code compile (i.e., produce an executable file)? (1 pt.):
#include <stdio.h>
main() {
int c, d;
int x = c + d * d;
printf("%d", x);
}
ANSWER: Yes
3. If in item 2 the code will NOT compile, why so? If the code will compile, what will be the output? (1 pt.)
1
4. The segment of C source code below aims to print the reverse of a string contained in the variable str var :
Supply the missing parts of the code represented by <A> and <B> to make it work as intended. (1 pt. each)
ANSWERS:
<A>: i = strlen(str_var) - 1
<B>: i >= 0 OR <B>: i != 0 OR <B>: i > -1
5. Create a C code segment that behaves similarly as the code shown in item 4, but this time using the while
expression to iterate over the elements of the str var array. Assume string length is at least 1. (2 pts.)
ANSWER: The following is just a version of potentially many correct answers to the problem:
i = strlen(str_var) - 1;
while (i >= 0) {
printf("%c", str_var[i])
--i;
}
6. Create a C code segment that behaves similarly as the code shown in item 4, but this time using the do-while
statement to iterate over the elements of the str var array. Assume string length is at least 1. (2 pts.)
ANSWER: The following is just a version of potentially many correct answers to the problem:
i = strlen(str_var) - 1;
do {
printf("%c", str_var[i])
--i;
} while (i >= 0);
7. The segment of C source code below aims to display whether an integer input (to be stored in the variable
num_input is even or odd:
char odd_or_even[5];
int num_input;
if ((num_input % 2) == 0) {
<B>
} else {
<C>
}
Supply the missing parts of the code represented by <A>, <B>, and <C> to make it work as intended. (1 pt.
each) Here’s a sample output:
2 is even.
5 is odd.
ANSWERS:
<A>: "%d", &num_input
<B>: strcpy(odd_or_even, "even");
<C>: strcpy(odd_or_even, "odd");
2
8. If the value of an int-typed variable is true, what could be its value? Write only the letter corresponding to
your choice. (1 pt.)
(a) 123
(b) -1
(c) 1
(d) All of the above
(e) None of the above
ANSWER: (d)
int x = !1 + 6 / 4 % 2 - 7 * 10;
int x, y;
3
13. The segment of C source code below aims to invert the ordering of the contents of the array named int_array:
while(i < j) {
temp = int_array[i];
<A>
<B>
++i;
--j;
}
Supply the missing parts of the code represented by <A> and <B> to make it work as intended. (1 pt. each)
ANSWERS:
<A>: int_array[i] = int_array[j];
<B>: int_array[j] = temp;
F0 = 0
F1 = 1
Fn = Fn−1 + Fn−2 , n ≥ 2
For this problem, ask for user input for the value of n.
ANSWER: The following is just a version of potentially many correct answers to the problem:
4
2. Suppose you have an array of integers of size n named int array containing n non-negative integers. Here, let’s
assume that n ≥ 2. Create an algorithm that will store the contents of int array into another array of integers
of size n named sorted int array but with the entries sorted from lowest to highest value, i.e. the first element
of the array is the smallest among the entries, the second element is the second smallest value, and so on, while
the last element is the largest among the values.
ANSWER: The following is just a version of potentially many correct answers to the problem:
Let’s assume that the indexing scheme of an array is the same as in C, i.e. first element is at index 0 while last
element is at index n - 1. Furthermore, let’s assume that int array has already been declared and initialized
with a set of values, and that the sorted int array has already been declared. The direction we want to go
here is to find the maximum value in int array at any given step, “remove” it from the array, and then place
it in the rightmost “unoccupied” index of sorted int array. We also take advantage of the fact that the values
are non-negative, so “removing” an element in int array is done by assigning to the index occupied by that
element a negative integer. The algorithm proceeds as follows
(a) Declare and instantiate 2 variables to control looping, say i ← 0 and j ← n − 1.
(b) Declare 2 variables representing the maximum value found so far plus the index where it was found, say
max value and max value index.
(c) If j ≥ 0, go to the next step. Otherwise, go to the last step.
(d) Set max value ← −1 and max value index ← −1.
(e) Set i ← 0.
(f) If i < n, go to the next step. Otherwise, go to step (j).
(g) If int array[i] > max value, then set max value ← int array[i] and max value index ← i.
(h) Set i ← i + 1.
(i) Go back to step (f).
(j) Set sorted int array[j] ← max value.
(k) “Remove” max value from int array by setting int array[max value index] ← −1.
(l) Set j ← j − 1.
(m) Go back to step (c).
(n) The contents of sorted int array now contains the sorted values, from lowest to highest, of int array