Vous êtes sur la page 1sur 5

CS 11

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.)

ANSWER: Indeterminable, since we don’t know the values stored in c and d.

1
4. The segment of C source code below aims to print the reverse of a string contained in the variable str var :

for (<A>; <B>; i--) {


printf("%c", str_var[i])
}

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;

printf("Enter a number >");


scanf(<A>);

if ((num_input % 2) == 0) {
<B>
} else {
<C>
}

printf("%d is %s", num_input, odd_or_even)

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)

9. Given the C statement below.

int x = !1 + 6 / 4 % 2 - 7 * 10;

What is the value of x? (1 pt.)


ANSWER: -69
10. What does the C code segment below intend to do? (2 pts.)

int x, y;

printf("Enter a non-negative integer >");


scanf("%d,"&x);

for (y = 1; 123; x--) {


if (x <= 1) {
break;
} else {
y *= x;
}
}
printf("%d", y);

ANSWER: It computes and displays the factorial of the integer input.


11. Which of the following is an invalid C statement? Write only the letter corresponding to your choice. (1 pt.)
(a) char x = 69;
(b) y *= ++x; // y and x are both of type int
(c) string_variable = "I am a string"; // string_variable is a char array of size 50
(d) (x == 0)?printf("Hi!"):--x; // x is of type int
ANSWER: (c)
12. How would you rectify the invalid C statement in item 11 to still perform its intended effect? (1 pt.)
ANSWER: Either instantiate the value upon declaration of the variable or use strcpy.

3
13. The segment of C source code below aims to invert the ordering of the contents of the array named int_array:

int int_array[10] = {1,2,3,4,5,6,7,8,9,0};


int i = 0, j = 9, temp;

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;

Part 2 - Problem Solving


For this part, you have to design algorithms to solve the problems given. You may use narratives (list the steps by
numbers), flowcharts, pseudocodes, or even C codes to present your algorithm. In designing your solutions, assume
correct input unless stated otherwise in the problem. Each problem is worth 10 pts.
1. Create an algorithm that will generate the nth Fibonacci number, where n ≥ 0. A Fibonacci number Fn is
defined as follows:

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:

(a) Get input from user for the value of n.


(b) Declare and initialize 3 variables to represent three consecutive Fibonacci numbers, say
Fn ← 0, Fn−1 ← 1, Fn−2 ← 0.
(c) Declare and instantiate another variable to control looping, say i ← 1.
(d) If n = 1, then set Fn ← 1.
(e) If i < n, go to the next step. Otherwise, go to the last step.
(f) Set Fn ← Fn−1 + Fn−2 .
(g) Set Fn−2 ← Fn−1 .
(h) Set Fn−1 ← Fn .
(i) Set i ← i + 1.
(j) Go back to step (e).
(k) The value of Fn represents the nth Fibonacci number.

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

Vous aimerez peut-être aussi