CS181 Reveiw Question Final Exam
CS181 Reveiw Question Final Exam
# Q A
1 What is the output of the following: x = 7, y = 4
int x = 1, y = 3;
x == x + y;
if(x = y)
x+=++y;
printf("x= %d, y = %d", x , y);
2 What is the output of the following program PROGRAMMING
int main()
{
int i = 0;
char c, char str[] = "programming";
while(str[i])
{
putchar (toupper(str[i]));
i++;
}
return(0);
}
16 Error correction
int x = 3;
if ( X >= (x*2-2)) if ( x >= (x*2-2))
{
printf("%d\n", x);
}
17 Error correction
int main{
int c = 4, d = 5;
d = func2(c); d = func2(c, d);
return 0;
}
19 Errors Correction
FILE *fp;
fp = fopen( file.txt, "r"); fp = fopen("file.txt", "r");
printf(&fp, "%s", str); printf(&fp, "%s", str);
fclose(fp);
20 Error Correction:
#include <stdio.h>
static a = 10; static int a = 10;
void total(int *p) void total(int *p);
int main(){
int b[] = {1, 2, 3};
int *m = b[0]; int *m = &b[0];
total(m[]); total(m);
printf("%d",*m);
return 0;
}
void total(int *p){
p += a; *p += a;
}
21 The following program prints the sum of the last three numbers of a int lastThree(int *s, int m){
given array. write the lastThree function that takes the array and int sum;
print the sum of the last 3 numbers for(int i=m-1; i >= m-3 ; i-- ){
int main(void) { sum += s[i];
int a[7] = {3, 7, 8, 9, 3, 5 ,1}; }
int k = lastThree (a, 7); return sum;
return 0;
}
#include <stdio.h>
22 Write a program that declare an array with the following ten integer
numbers int sum (int *s, int m);
int main(void) {
3, 5, 7, 4, 8, 3, 9, 3, 5
int a[10] = {3, 5, 7, 4, 8, 3, 9, 3, 5};
and then print out the sum of the second, forth, and sixth elements. int x= sum (a, 10);
Use a function that takes the array and returns the sum. printf("\n%d\n", x);
return 0;
int z = 0;
if(i == 1 || i== 3 || i == 5)
z += s[i];
return z;
}
23 Write a C function that receives two numbers and return the sum of void sum (int *s, int *m, int *q){
the numbers between them (exclusive). Consider that the function is int k = 0;
called by reference from the main().
for(int i = *s; i <= *m; i++){
k += i;
}
*q = k;
}
24 Write C program to take letters from the user until the letter 'c' is #include <stdio.h>
entered int main()
{
char ch, s[20];
int i=0;
while(ch != 'c'){
printf("Enter a character\n");
scanf("%c", &ch);
s[i] = ch;
i++;
}
printf("entered letters = %s\n", s);
return 0;
}
25 The following program prints out the number of letters in a given int letterCount(char *msg){
word. Write the letterCount function that takes the word and return int m = 0;
the number of letters.
int i;
int main(void) { for( i = 0; msg[i] != '\0'; i++){
char *str = "Programming"; m++;
int n= letterCount(str); }
26 What is the value of x after executing the following code lines: Answer: 8
int x = 8; Explaination:
if (!(x != 1) && x>=10) x++; x != 1 : 1 (True)
!(x != 1) = !(1) : 0
x>=10 : 0
0 && 0 : 0 (False)
31 How to open the file.txt in the following line of a code in the read fp = fopen("file.txt", "r");
mode:
fp = fopen("file.txt", "a");
33 If the fn function in the following code has been rewritten to receive int t = fn(b);
the b array, how to rewrite the following function call accordingly:
or
int a = 5, b[]={2,4};
int t = fn(&b[0]);
int t = fn(a);
34 Rewrite the following variable declaration so that the variable is auto char var1;
defined as a an automatic variable
char var1;
37 Write the proper function prototype of the following function int func (char m);
call:
char z;
int k;
k = func (z);
38 How to use rand() to generate random integer numbers from 2 to ? Answer: 2 + rand() %7
Explanation:
rand() %7: generates numbers from 0 to 6
2 + rand() %7:
add 2 to both ends ( 0+2 = 2 , 6+2 = 8)
So 2 + rand() %7 will print random
numbers from 2 to 8
39 Write the function prototype of the a function total that receives a double total (float x);
floating-point argument, and returns a double result