



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A midterm exam for the cgs-3460 fall 2007 c programming course taught by manuel e. Bermudez. The exam consists of 5 pages, each with multiple-choice problems related to various aspects of the c programming language. Students are required to attempt all questions, keep their answers brief, and write their name and uf id on all pages.
Typology: Exams
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Maximum Points: 100 Time: 50 minutes
Problem 1 ________ (30 pts.) Problem 2 ________ (20 pts.) Problem 3 ________ (20 pts.) Problem 4 ________ (30 pts.) Total: ________ (100 pts.)
Instructions:
Problem 1. Answer the following questions briefly (30 points)
(a) What is the value of the following expression in C? (1<2)?1:
1
(b) Is int arr[3] = {0,1} a valid initialization of the array arr? If yes, then indicate the values of each element of arr. Otherwise indicate why this is invalid.
0 1 0
(c) Is it necessary for a recursive function to have a return type other than void? Why?
No, because recursion only requires that the function calls itself and nothing beyond that.
(d) Is the following a valid conditional statement in C? Explain why or why not. Assume that a is an integer variable. if(a == 1 && 1 || 0)
Yes, because in C every integer other that 0 is TRUE and 0 is FALSE.
(e) What is the use of the default clause in a switch statement?
If the test variable does not match any of the defined cases, default statements, if present, will be executed and thus unexpected behavior of the program can be avoided.
(f) What is the difference between local and global variables?
Local variables are only available within the current block while global variable are available through out the program.
(g) What is the length of the longest valid string that can be stored in a character array declared as char str[10]? Why?
9 because the 10th^ character would have to be a ‘\0’ for str to have a valid string
(h) If you use a function declared in math.h in your program (named p1.c), what command would you use to compile it?
gcc p1.c -lm
(i) What is the difference between printf("%d",--i) and printf("%d",i--)? You can assume i is an integer.
--i decrements i before printing its value while i-- decrements i after printing its value.
(j) What function can used to find out the amount of space in bytes that is occupied by a variable? sizeof()
Problem 3. There are 4 errors in the following program. Locate them and indicate the necessary corrections. (20 points)
#include
i=n;
do { { printf("%d\n",i); i-- } }while(i>0) }
Problem 4. Write a function (not a complete program) that computes and returns the sum of the elements of an array of integers, using recursion. (30 points)
int sum(int arr[], int c, int n) { if(c>=n) return 0; else return arr[c] + sum(arr,c+1,n); }