Você está na página 1de 17

Final

Exam Review
Fall 2014

Ques5on 1
(10%) Which of the following program fragments will NOT terminate?

(a) int w = 0;
while (w<32)
{w = w+1;}

(b) int w = 1;
while(w>0)
{w = w/4;}

(c) int w = 0;
while(1)
{w = w+1;}

(d) int w = 200;
while(w>0 && w<200)
{w = w-2;}

Answer 1
(a) int w = 0;
while (w<32)
{w = w+1;}

(b) int w = 1;
while(w>0)
{w = w/4;}

(c) int w = 0;
while(1)
{w = w+1;}

(d) int w = 200;
while(w>0 && w<200)
{w = w-2;}

Ques5on 2
(10%) What is stored in the list array aUer the following
code executes?


int j, list[6];
for (j = 0; j < 6; j++)
{
list[j] = 3 * j + 4;
if (j % 3 == 0)
{
list[j] = list[j]-2;
}
}

Answer 2


int j, list[6];
for (j = 0; j < 6; j++)
{
list[j] = 3 * j + 4;
if (j % 3 == 0)
{
list[j] = list[j]-2;
}
}

2, 7, 10, 11, 16, 19

Ques5on 3
(10%) Which of the following statements are correct about the below program?

#include<stdio.h>
int main()
{
int i = 1;
i++;
if(i <= 6)
{






}
return 0;
}

prin^("Happy Thanksgiving\n");
exit(0);
main();


(a) The program prints 'Happy Thanksgiving' 5 5mes
(b) The program prints 'Happy Thanksgiving ' one 5me
(c) The call to main() aUer exit() doesn't materialize.
(d)The compiler reports an error since main() cannot call itself.

Answer 3
#include<stdio.h>
int main()
{
int i = 1;
i++;
if(i <= 6)
{

prin^("Happy Thanksgiving\n");

exit(0);

main();
}
return 0;
}


(a) The program prints 'Happy Thanksgiving' 5 5mes

(b) The program prints 'Happy Thanksgiving ' one Fme


(c) The call to main() aUer exit() doesn't materialize.
(d)The compiler reports an error since main() cannot call itself.

Ques5on 4
(10%) Given the following func5on calls, assuming they are properly declared, what is
the output of g(5)?


int f(int x){
prin^(%d, x);
return x + 3;
}
int g(int x){
prin^(%d, f(x));
return x+2;
}

Answer 4



int f(int x){
prin^(%d, x);
return x + 3;
}
int g(int x){
prin^(%d, f(x));
return x+2;
}

58

Ques5on 5
(10%) What will be the output of the following program :

#include <stdio.h>
int main()
{
int add(int,int);
int a=7,b=13;
prin^("%d", add(add(a,b), add(a,b)));
return 0;
}
int add(a,b)
int a,b;
{
return (a+b);
}


(a) Compile-Time error

(b) 20

(c) 40

(d) None of these

Answer 5
#include <stdio.h>
int main()
{
int add(int,int);
int a=7,b=13;
prin^("%d", add(add(a,b), add(a,b)));
return 0;
}
int add(a,b)
int a,b;
{
return (a+b);
}


(a) Compile-Time error

(b) 20

(c) 40 (d) None of these

Ques5on 6
(10%) What is the output of the following program?
int x = 5;
int func1(int p) {
p = p+x+1;
return p;
}
int func2(int q) {
int x;
x = q + 10;
return 0;
}
int main(void) {
int y;
y = func2(x);
x = func1(y);
prin^(%d \n, x);
}

Answer 6
int x = 5;
int func1(int p) {
p = p+x+1;
return p;
}
int func2(int q) {
int x;
x = q + 10;
return 0;
}
int main(void) {
int y;
y = func2(x);
x = func1(y);
prin^(%d \n, x);
}

Ques5on 7
7. (20%) Write a C Program to nd and replace a specic integer from an array of 5
memory loca5ons.
Your program should ask the user to enter 5 digits and store them in an array.
Next, your program should ask the user to input two integers; the rst integer should
be replaced with the second integer. If the rst integer is not present in the array,
your program should print it.

Your Program output should be as follows:
Enter 5 digits
12
12
23
28
34
Enter 2 integers to nd and replace
12
6
2 integers replaced.
The nal array is: 6 6 23 28 34

Answer 7
#include <stdio.h>
#include <math.h>
int main(){
int a[10];
int i, nd, replace, temp=0;
prin^("enter 5 digits\n");
for (i=0;i<5;i++)

scanf ("%d", &a[i]);
prin^("enter 2 integers to nd and replace");
scanf ("%d %d", &nd, &replace);
for(i=0;i<5;i++){

if(a[i] == nd){
a[i] = replace;
temp = temp+1;
}
}
if (temp == 0)
prin^ ("No integers replaced.");
else
prin^ ("%d integers replaced.",temp);
prin^ ("The nal array is :");
for (i=0;i<10;i++)
prin^ ("%d ", a[i]);
return 0;
}

Ques5on 8
(20%) Write a C program to nd the sum of the squares of all the even numbers within
the given range. Your program should ask the user to input the lower and the upper
range and it should calculate the summa5on of squares of all the even numbers within
the specied range.
For example if the lower range is 10 and the upper range is 20 then your program
should calculate
sum = 102 + 122 + 142 + 162 + 182 + 202

Your Program output should be as follows:
Please input the lower range
10
Please input the upper range
12
The sum is 244

Answer 8
#include <stdio.h>
#include <math.h>
int main (){
int i,sum=0,l_limit, u_limit;
prin^ ("please input the lower range\n");
scanf ("%d", &l_limit);
prin^ ("please input the upper range\n");
scanf ("%d", &u_limit);
for (i=l_limit;i<=u_limit;i++){
if (i%2 == 0){
sum = sum+ i*i;
}
}
prin^("the sum is %d", sum);
}

Você também pode gostar