[go: up one dir, main page]

0% found this document useful (0 votes)
88 views9 pages

CS181 Reveiw Question Final Exam

The following summarizes the key information from the document: 1. The document provides 20 review questions (Q1-Q20) about C programming concepts like loops, functions, arrays, pointers, etc. along with their expected outputs. 2. It also provides 20 error correction questions (Q16-Q20) where some code snippets have errors and the corrections are needed. 3. The review questions and error corrections cover a wide range of fundamental and advanced C programming topics to help students prepare for an exam.

Uploaded by

raghd .
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views9 pages

CS181 Reveiw Question Final Exam

The following summarizes the key information from the document: 1. The document provides 20 review questions (Q1-Q20) about C programming concepts like loops, functions, arrays, pointers, etc. along with their expected outputs. 2. It also provides 20 error correction questions (Q16-Q20) where some code snippets have errors and the corrections are needed. 3. The review questions and error corrections cover a wide range of fundamental and advanced C programming topics to help students prepare for an exam.

Uploaded by

raghd .
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

CS181 Review Questions

# 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);
}

3 What is the output of the following: *****


for (int i = 5; i >= 1; i--){ ****
for (int c = 1; c <= i; c++) ***
printf("*"); **
printf("\n"); *
}

4 What is the output of the following code: C=6


int c = 3;
c = 4 – c * 2;
if(c = 5) c++;
else c--;
printf("c = %d", c);

5 What is the output of the following code:


Int m[4]={0}; third element=12
for(int i=0; i <=3 ; i++)
m[i]+=i+10;
printf("third element=%d",m[2]);
6 What is the output of the following codes
int func1(int *a, int *b); a = 12, b = 6
int main (){ x = 12, y = 2
int x = 3, y = 5;
y = func1(&x,&y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
int func1(int *a, int *b){
*b = *a + *a ;
printf("a = %d, b = %d\n", *a+=9, *b);
return *a / *b++;
}

7 What is the output of the following codes 6 14 15 6 15


int x = 6, y = 8;
int *p= &y, *q= &x;
int *k = p;
*p = x + *q*2 - *k/2;
printf("%d %d ", x, y);
printf("%d %d ", ++*p, *q);
printf("%d ", *k);

8 What is the output of the following code: !!=


int i, j; !=!
for (i = 1 ; i < 5 ; i{ )++ =!!
for (j = 3 ; j >= 1 ; j)-- !!!
if (j != i)
printf;)" !"(
else
printf;)" ="(
printf("\n;)"
}

9 What is the output of the following code: Result = -1


int a = -1, b = 4, c = 0;
c = a++ - b-- * ++c;
printf("Result = %d\n", (c - 1) * (a + 2));
10 What is the output of the following code: 2: Even
int m[ ] = {2, 7, 10}, m1 = 0, m2;
for (; m1 < 3 ; m1++){
m2 = m[m1];
if(m2 % 2 == 1) break;
else printf("%d: Even\n", m2);
}

11 What is the output of the following code: 4


int c = 2;
int *b = &c;
*b = *b * *b ;
printf("%d", c);

12 What is the output of the following 30


int y[3]= {10, 20, 30};
int *k = y;
printf("y[2] = %d", *(k+2) );

13 What is the output of the following Answer: 24


int x = 2, y = 6; Explanation:
int z = 3 * ++x + y * 3 / 2 + y++; 3*++x = 3*3 = 9
printf("%d\n", z); Y*3/2 = 6*3/2 = 18/2 = 9
Y++ = 6 (increment y after
executing the assignment to z)
Z = 9+9+6 = 24

14 What is the output of the following codes 3 4 9


int arr[10] = {2,7,5,2,7,9,4,5,2,3};
int count[10] = {0};
for (int i = 0; i < 10; i++){
++count[arr[i]];
}
for (int i = 0; i < 10; i++){
if (count[i] == 1)
printf("%d ", i);
}
15 What is the output of the following codes 2
int x=0; 7
while(x < 7){ 13
switch (x){ 6
case 0:
printf("%d\n", x+=2);
case 1:
printf("7\n");
case 2:
x++;
break;
case 3:
printf("%d\n",x+10);
case 4:
x = 6;
break;
default:
x = x*2;
printf("%d\n",x/2);
}
}

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

printf("%d %d\n", c , d);


return 0;
}
void func2(int x, int y){ int func2 (int x, int y){
return (x++ * --y);
}
18 Error correction
#include <stdio.h>
int main() {
int ARR[9] = {2, 5, 7, 3, 8, 12, 1, 4, 10, 9}; int ARR[10]
int OAR = {0}, EAR[10] = [0]; int OAR[10] = {0}, EAR[10] = {0};
int i = 1, j = 0, k = 0, n = 10;
do
{
if (ARR[n-i] % 2 == 0)
{
EAR[j] = ARR{n-i}; EAR[j] =ARR[n-i];
j++;
}
else
{
OAR[k] = ARR n-[i]; OAR[k] = ARR [n-i];
k++;
}
i++;
}while(i <= 10) }while(i <= 10);
printf("The elements of OAR are \n");
for (i = 0; i < j; i++)
{
printf("%d\n", &OAR[i]); printf("%d\n", &OAR[i]);
}
printf("The elements of EAR are \n");
for (i = 0; i < k; i++)
{
printf("%d\n", EAR[]); printf("%d\n", EAR[i]);
}

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;

printf("result = %d", k); }

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 sum (int *s, int m){

int z = 0;

for(int i = 0; i < m; i++){

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); }

printf("\n%d", n); return m;


}
return 0;
}

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)

27 What is the value of x after executing the following: 8


int x = 1;
x = sqrt(16) * ++x;
28 What will the following function will return: Char
char f(char x)

29 How many times the following loop will be executed: 4


for(int i = 1; i<9 ; i+=2){
}
30 Modify the following function prototype for the function to take an int nf (int *x, int *y, char *s)
additional argument as a string
int nf(int *x, double y)

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");

32 Rewrite the following while loop into a for loop int x = 3, i = 1;


int x = 3, i = 2; for(i = 2; i <= 8; i++){
while(i <= 8){ x += i;
x += i; printf("%d\n", x);
printf("%d\n", x); }
i++;
}

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;

35 2. How to write to the standard output file? fputs("Hello", stdout);

36 5. How to write to a file fprintf( fptr, "%d%s%f", a, n , b );

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

40 The following would generate random numbers between ……. 4–7


4 + ( rand( ) % 4);

41 If s = "Hello", the correct print statement would be …….. printf( "%s", s );

42 What is the output of the following code: www


char *s = "www.qu.edu.sa"; qu
int c = 1; edu
while( *s != '\0'( sa
{ count = 4
if(*s == '.'){
if(*(s + 1) != '\0');
c++;
printf("\n");
}else
printf("%c", *s);
s++;
}
printf("\ncount = %d", c );

You might also like