Upload
lea-dela-rama
View
19
Download
0
Embed Size (px)
Citation preview
CS 12 – Computer Programming 1 Midterm Examination
int another(int a, int b) { int i, j; j = 0; for (i = a; i <= b; i++) j = j + i; return j; }
What is the output of each of the following program segments? Assume that x, y, and k are int variables. a. x = 10; printf(“%i\n”, secret(x)); b. x = 5; y = 8; printf(“%i\n”, another(x, y)); c. x = 10; k = secret(x); printf(“%i %i\n”, x, k); d. x = 5; y = 8; printf(“%i\n”, another(y, x));
C. [14pts]Consider the following function prototypes: int test(int, char, double, int); double two(double, double); char three(int, int, char, double);
Answer the following questions. a) How many parameters does the function test have? What is the type of the function
test ? _____________________________________________________b) How many parameters does function two have? What is the type of function two?
_____________________________________________________c) How many parameters does function three have? What is the type of function
three ? _____________________________________________________d) How many actual parameters are needed to call the function test?
_____________________________________________________
I. Fill in the blanks.
__________________1) There are two types of userdefined functions: the __________ and
__________________2) ____________________________3) [2pts] The general syntax for a userdefined function
prototype is:
__________________4) The function heading and the body of the function are called the ________________________
__________________5) A ____________ is the function heading without the body of the function
__________________6) [2pts] The general syntax for a userdefined function definition is:
__________________7) In an array, if there are fewer initial values than the array size, the excess elements are initialized to ___________.
__________________8) A null character is represented as ___________.__________________9) As an alternative to assignment operator in initializing
values to a string identifier, the string function __________ is used.
__________________10) [2pts]The general syntax for a userdefined function call:
__________________11) _Comparing strings using relational operators is not allowed. The string function __________ is used as an alternative
__________________12) The string function that returns the number of characters in a string is called __________
CS 12 – Computer Programming 1 Midterm Examination
II. True/False. _____1) Even if a function has no parameters, you still need the empty
parentheses in both the function heading and the function call._____2) A function cannot return a value of type array _____3) A return statement returns only one value_____4) Aggregate operations are not allowed on arrays and strings _____5) Array subscript should be always of type int and cannot have any other
variable type_____6) As parameters to functions, arrays are passed by reference only _____7) Elements of a onedimensional array are arranged in the form of a list _____8) Identifiers, array names and function names should follow the same
naming conventions _____9) In a function call statement, when passing an array as an actual
parameter,you use only its name _____10) In a function call, you specify only the actual parameter, not its data
type._____11) In C, an array index always starts with 0 _____12) Individual array components can be passed as parameters to functions _____13) Strings and Character arrays are null terminated _____14) The associativity rule applies when two operations are on the same
precedence level_____15) The initialization of a variable in a declaration is optional _____16) The return statement can appear anywhere in a function _____17) Userdefined functions will not be executed unless called._____18) When the program executes, the execution always begins with the first
statement in the function main._____19) When writing the function prototype, you do not have to specify the
variable name in the parameter list._____20) You can place a declaration statement in any part of the program before
the line that uses the identifier you are going to declare.
III. Applications A. [8pts]
void One(int A, int B, int& C) {int D;A = 10; B = 11; C = 12; D = 13;
}void Two(int A, int B, int& D) {
int C=0;}void main() {
int A,B,C,D;A = 5; B = 6; C = 7; D = 8;One(A,B,D);Two(A,B,C);
}
a) What are the values of variables a, b, c and d in function one? b) What are the values of variables a, b, c and d in Main after
execution c) What are the values of variables a, b, c and d in function two? d) What are the values of variables a, b, c and d in Main after
executing function two?
B. [8pts] Consider the following functions: int secret(int x){ int i, j; i = 2 * x; if (i > 10) j = x / 2; else j = x / 3; return j 1; }
B C DA
CS 12 – Computer Programming 1 Midterm Examination
e) What is the type of each actual parameter, and in what order should you use these parameters in a call to the function test? _____________________________________________________
f) [3pts] Write a statement that prints the value returned by the function test with the actual parameters 5, 5, 7.3, and 'z'._____________________________________________________
g) [3pts] Write a statement that prints the value returned by function two with the actual parameters 17.5 and 18.3, respectively._____________________________________________________
h) [3pts] Write a statement that prints the next character returned by function three. (Use your own parameters.) _____________________________________________________
D. [5pts] Show the output of the following program: #include <stdio.h>
int mystery(int);
int main() { int n; for (n = 1; n <= 5; n++)
printf(“%i\n”, mystery(n)); return 0;
}
int mystery(int k) {
int x, y; y = k; for (x = 1; x <= (k 1); x++) y = y * (k x); return y; }
CS 12 – Computer Programming 1 Midterm Examination
E. [10pts] Consider the function headings: void funcOne(int alpha[], int size);
Int funcSum(int x, int y); void funcTwo(const int alpha[], int beta[]);
and the declarations: int list[50]; int aList[60]; int num; Write C statements that do the following: a) [3pts]Call the function funcOne with the actual parameters, list and 50,
respectively. _____________________________________________________
b) [2pts]Print the value returned by the function funcSum with the actual parameters, 50 and the fourth component of list, respectively._____________________________________________________
c) [2pts]Print the value returned by the function funcSum with the actual parameters, the thirtieth and tenth components of list, respectively.
_____________________________________________________d) [3pts]Call the function funcTwo with the actual parameters, list and aList,
respectively. _____________________________________________________
F. [10 pts]Given the declaration: char str1[21]; char str2[21]; a) [2pts]Write a statement that stores "Sunny Day" in str1.
_____________________________________________________b) [2pts]Write a statement that stores the length of str1 into the int variable length.
_____________________________________________________c) [2pts]Write a statement that copies the value of name into str2.
_____________________________________________________d) [4pts]Write code that outputs str1 if str1 is less than or equal to str2, and
otherwise outputs str2. _____________________________________________________
CS12 – Computer Programming 1
MIDTERM EXAMINATION2nd Semester 20112012
Name: Course: Year: Block:
Date of Examination: _____________
SCORES/number of itemsI. __________ out of 15II. __________ out of 20 III. A. __________ out of 8
B. __________ out of 8 C. __________ out of 14 D. __________ out of 5 E. __________ out of 10 F. __________ out of 10
Total Score: out of 80