0% found this document useful (0 votes)
219 views8 pages

Final

This document contains a final exam for a computer engineering course. It includes 14 multiple choice questions related to C programming concepts like loops, arrays, functions, pointers, and more. It also provides code snippets and asks students to identify outputs, errors, or modifications needed to fix errors. The questions cover a wide range of fundamental and advanced C programming topics that would be expected on an exam for an undergraduate computer engineering course.

Uploaded by

jocansino4496
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)
219 views8 pages

Final

This document contains a final exam for a computer engineering course. It includes 14 multiple choice questions related to C programming concepts like loops, arrays, functions, pointers, and more. It also provides code snippets and asks students to identify outputs, errors, or modifications needed to fix errors. The questions cover a wide range of fundamental and advanced C programming topics that would be expected on an exam for an undergraduate computer engineering course.

Uploaded by

jocansino4496
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/ 8

Dept. of Computer Eng. Fall 2009 CEng 230 Final Examination Jan.

12, 2010 75 Minutes


EXAM GROUP A
Student Name & Surname Student ID

Use the below program to answer next 2 questions.

int days_in_feb(int y) {
if(y % 100 == 0) return 28;
if(y % 4 == 0) return 29;
return 28;
}
int dyear ( int day , int month, int year
){
int day_of_year ;
day_of_year = day ;
for( ; month > 0; month--)
switch ( month ) {
case 11: case 9 :
case 6: case 4 :
day_of_year += 30; break;
case 10: case 8 :
case 7: case 5 :
case 3: case 1 :
day_of_year += 31; break;
case 2:
day_of_year += days_in_feb( year
);
}
return day_of_year ;
}

1. What is the value returned by dyear(1,3,2009)?
a) 1 b) 32 c) 60 d) 91 e) 30

2. There is an error in this program. Which of the below
corrections would fix this error?

a) switch (month) switch(month -1)
b) for(; (month-1)>0 ; month--) for(; (month-1) > 0 ;)
c) return 28 return ( (month==2)? 28:29)
d) switch (month) switch(month +1)
e) There is no error.

3. What is the value of y after below code segment is
executed?

int x1 = 7, x2 = 10, x3 = 8.1, y ;
y = x3>x1 x2<=10 ;

a) 0
b) 1
c) 1.0
d) 0.0
e) Invalid assignment




4. What is the output of the below code?
#include <stdio.h>
int fun (int ix){
ix = ix * 2 ;
printf ("%d " , ix);
return ix ;
}
int main (void)
{ int x = 3 ;
printf ("%d ", x);
printf ("%d\n", fun(x));
return 0 ;
}

a) 3 6 6
b) 3 3 3
c) 3 6 3
d) 3 6 9
e) 3 3 6

5. What is the output of the below program with input
3
#include<stdio.h>
int fonksiyon ( int gln ) {
if( gln <= 1 )
return 1;
else
return (gln*fonksiyon(gln 1));
}
int main ( void ) {
int k,N,f;
puts(Enter an integer: );
scanf ( %d , &N );
f= fonksiyon(N);
printf(Result is=%d\n, f );
return 0 ;
}
a) 1 b) 2 c) 3 d) 4 e)5

6. What is the mistake in the below switch statement?
double a= 1.25;
switch(a){
case 1.1: case 1.2:
x= x*x; y= x; break;
case 1.3: y= x*x;
case 2.1: case 3.3:
y= sqrt(x); break;
case 3.5: printf(Error ); break;
default: y= x+1;

a) case 1.3 has no break
b) default doesnt have printf
c) a = 1.25 is not matched.
d) switch works only with integers
e) case 3.5 doesnt need break
7. How can you code the below condition in C?
x and y are both greater than z
a) a) x > z && y > z
b) b) x && y > z
c) c) (x && y) > z
d) d) (x | | z ) > z
e) e) x > z | | y > z

8. If a=1 and b=20 what is the output of the below code
segment?
if(a=10){
if(b == 20)
printf(*** ); }
else printf(*****) ;

a) ***
b) *****
c) *** *****
d) No Output is produced
e) * * * * * * * * *

9. What is the effect of the following code segment?
int i;
for (i=1;i <21;i++)
if (i%3 == 0)
printf(%4d,i);

a) It prints out the multiples of 3 from 3 to 21
b) It prints out the multiples of 3 from 1 to 18
c) It prints out the odd numbers from 3 to 18
d) It prints out the multiples of 3 from 3 to 18
e) It prints out the odd numbers from 3 to 21

10. What are the values of k and m after the following
segment has been executed?
k=1;
for (m=2; m< 9; m +=2)
k += 3;

a) k=7 m=9
b) k=9 m=9
c) k=12 m=10
d) k=13 m=9
e) k=13 m=10

11. What is the output of the following program segment?
k=0;
do {
printf(*);
k++;
} while (k++ <5);

a) *
b) **
c) ***
d) ****
e) *****
12. How many lines are printed out after executed the
following program segment?

for (i=0; i< 10; i +=2)
for (j=8; j!=1; --j)
for (k=3; k<=6 ; k++)
printf(*\n);

a) 128 b) 140 c) 160 d) 105 e) 120

13. Which of the following expressions does the program
segment below compute in F?

F=0;
for (i=2 ;i<=n; ++i) {
term = i * (i -1);
F += term;
}

a) 11 + 22 +33 +44 + .+ nn
b) 1! + 2! + 3! + .+ n!
c) 1 + 1(2 +2(3 + 3(..+(n-1)((n-1)))))
d) 1*2 + 2*3 + 3*4 + ..+ (n-1)*n
e) 1+2+3+(n-1)+n

14. What should be the condition in the do-while loop
below, which allows to input pair of integers until it
reaches a pair in which the first integer is multiple of
the second?

do {
scanf(%d%d,&a,&b);
} while (.);

a) a % b == 0
b) a % b != 2
c) a % b != 0
d) a % b == 2
e) a != b && a > b

15. Let Y, Z, and S denote any C expressions. Then,
for ( ; Y; Z) S; is equivalent to:

a) do { Z; if (Y) S } while (Y);
b) do { S; Z; } while (!Y);
c) do { if (Y) S; Z; } while (Y);
d) if (Y) S; do { S; Z; } while (Y);
e) do { S; Z; } while (Y); S;








16. What is the equivalent do-while re-implementation of
the following program segment?

sum=0;
for (i=1; i<n; i++) {
sum += i*3;
if (i % 2) i++;
}
printf(%5d\n,sum);

a) sum = 0;
i=1;
do {
sum += i*3;
i +=2;
} while (i<n);

b) sum = 0;
i=0;
do {
sum += i*3;
if (i %2 == 0)
i++;
} while (i<n);
c) sum = 0;
i=1;
do {
sum += i*3;
if (i %2 == 1)
i++;
} while (i<n);
d) sum = 0;
i=0;
do {
sum += i*3;
if (i %2 == 0)
i +=2;
} while (i<n);
e) sum = 0;
i=1;
do {
sum += i*3;
if (i %2 == 1)
i +=2;
} while (i<=n);



17. Which of the following program segments causes an
infinite loop?

a) s=10;
while (s--)
printf(*);
b) for (i=3; i!=10 ; i +=2)
printf(*);
c) x=0;
do {
x++;
for (i=x;i<x*x;i++)

printf(*);
} while (x<20);
d) for (x=1; x!=10; ++x)
for (y=x;y != -10; y--
)
printf(*);
e) i=1;
while (i > 10) {
printf(*);
i -=3;
}




18. What is the effect of the following program segment
assuming that:
Numbers is an array of integers of size n,
The entries of the array Numbers are in increasing order
(sorted).
item, flag, ust, alt and i are integer variables.

flag=0;
alt=0;
ust=n-1;
i=(alt+ust)/2;
while (!flag && alt <=ust) {
if (Numbers[i] == item) {
flag=1;
} else if (Numbers[i] > item)
ust=i-1;
else
alt=i+1;
if (!flag) i=(ust+alt)/2;
}
if (flag) printf(%d,i);
else printf(-1);

a) It searches the array for the value of item and assigns its
value to the middle position of the array.
b) It searches the array for the value of item, and then
displays its position in the array if it is found, or -1
otherwise.
c) It reverses the elements of the array and print out the
last item reversed if the operation is successful or -1
otherwise.
d) It finds the position of the maximum value in the array
and displays it if it is equal to item, or -1 otherwise.
e) It searches the array for the value of item, compares its
position with the first and the last element of the array, the
finally it displays the position


19. What will be printed for b in the following program
segment?

int a[10], b, *pa, i;
for (i=10; i>0; i--)
a[i-1]=i*i;
pa = a;
b= *(pa+9) / *(a+2);
printf (%d, b);

a) 0 b) 11 c) 8 d) 12 e) 16







Use below program to answer next 3 questions.

#include <stdio.h>
int f(int b);
int g(int c);
int x=5;

void main (void) {
int a;
a= (f(g(x))-g(f(x)))/x;
/*** first_ printf ***/
printf(%d, x);
/* second_printf */
printf(%d, a);
}
int f (int b) { return b*b;}
int g (int c) {return x+++c;}

20. What will be printed for variable x by the first_printf
statement?

a) 0 b) 1 c) 5 d) 7 e) 10

21. What will be printed for variable a by the
second_printf statement?

a) 1 b) 5 c) 7 d) 8 e) 10

22. If the body of function g is replaced by the following
statement
return c+++c;
What will be printed for variable-a by the second printf
statement?

a) 0 b) 5 c) 7 d) 8 e) 10

23. Which of the fallowing definition is equivalent to the
following code segment?
do {
sayac+=2;
} while (a || b );

a) sayac+=2;while (!(a&& b)) sayac+=2;
b) while(a||b) sayac+=2;
c) sayac+=2;while (!(!a&& !b)) sayac+=2;
d) if(a||b) sayac+=2;
e) sayac+=2;if(a||b) sayac+=2;

24. What would be the value of x, after execution the code
below?
int i,j,x=16;
for(i=0;i<5;i+=2)
for (j=4;j>i;j--) x+=i-j;

a) 16 b) 3 c) 23 d) 5 e) -5


Use the following data to answer next 2 questions.
You are asked to write a function to compute the main
diagonal sum and secondary diagonal sum for any 2-
dimensional square matrix of size NxN. For instance; if the
following 3x3 array is given
A =

i h g
f e d
c b a

main_diagonal_sum = a+e+i,
secondary_diagonal_sum= c+e+g

int DiagonalSum (int *ary, int diag, int
size) {
/* ary: the array
diag: (0: main diagonal, 1: secondary
diagonal)
size: size of the 2-d square array, N */

int i, sum=0;
for(i=size-1; i>=0; i--)
switch(diag) {
case 0: /* statement_1 */
break;
case 1: /* statement_2 */
break;
}
return sum;
}

25. Which of the following statements would you choose
to replace statement_1 ?

a) sum+=ary[i][i] b) sum+=ary[i][size-i-1]
c) sum+=ary[size*(i-1)+i]
d) sum+=ary[i-size][i] e) None of them

26. Which of the following statements would you choose
to replace statement_2 ?

a) sum+=ary[i][i] b) sum+=ary[(size-1)*i+size-1]
c) sum+=ary[size-i][i]
d) sum+=ary[i-size][i] e) None of them

27. Determine the output of the below program?
#include <stdio.h>
void f1(int *x, int y) {
int t;
t=*x; *x=y; y=t;
}
int main() {
int a=0, b=0, c=0, d=0, e=0, f, g, h;
a=10; b=20;
f1(&a,b);
printf("%d %d\n",a,b);
}

a) 10 10 b) 10 20 c) 20 10 d) 20 20 e) 0 0
28. What is printed by below program?
: represents one space character

#include <stdio.h>
int MeaningfulVariable(int x, int *y);
int NamesAreBetter(int *x, int y);

void main(void) {
int a=1, b=2, aa;
aa=MeaningfulVariable(a+b, &b);
printf ("\n %d %d %d", a, b, aa);
NamesAreBetter(&b, a);
printf("\n %d %d %d", aa, b, a);
}
int MeaningfulVariable(int p, int *q) {
*q=NamesAreBetter(q, p)*p;
p++;
return *q;
}
int NamesAreBetter(int *p, int q){
*p=*p*q;
q++;
return *p;
}

a) 18118 b) 122
18118 221
c) 112 d) 11012
221 12101
e) 11818
18181

29. What would be the output of below code?
int i,g[2][3]={2,4,6,1,3};
for (i=0;i<2;i++) g[i][i+1]+=3; g[1][2] -
=g[1][0];
printf(%d, g[1][2]);

a) 2 b) -1 c) 3 d) -4 e) 0

30. What would be the value of y, after execution the code
below?
int x,y=32;
for(x=2;x<11;x+=4) y+=10+30/x-2*6;

a) 26 b) 176 c) 14 d) Error (dividing to 0) e) 49

31. What do we get first, if we try to execute the code
below?
int d,y=1;
for(d=0;d<4;d++) {
if(d=2) y--;
y/=I;}

a) Run time error (dividing by 0)
b) y=0
c) Run time error (Infinite loop)
d) Compiling error (syntax error)
e) y=13
32. What are the values of X and Y (with initial values
of Ali and Veli respectively) after function call
funny(X,Y)?

void funny(char *s, char *t) {
char *p;
p=t;
while(*t++)
p++;
while(*s)
*(p++)=*(s++);
*p=*s;
}

a) X=AliVeli, Y=Veli
b) X=Veli, Y=Ali
c) X=Ali, Y=Ali
d) X= Ali, Y=VeliAli
e) X=Veli, Y=Veli

33. What is the output of the following program?
: represents one space character.

int UpperTriangleSum(int a[][4], int n);
void Transpose(int a[][4], int n);
void main(void) {
static int
ary[4][4]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,1
4,15};
register int i, j;
auto int sum=0;

printf("\n %d", UpperTriangleSum(ary, 4));
Transpose(ary,4);
printf("%d", UpperTriangleSum(ary, 4));
}
int UpperTriangleSum (int a[][4], int n) {
register int i, j;
auto int sum=0;
for(i=0; i<n;i++)
for(j=i+1; j<n;j++)
sum+=a[i][j];
return sum;
}
void Transpose(int a[][4], int n){
register int i,j;
auto int temp;
for(i=0; i<n; i++)
for(j=0; j<n; j++)
{
temp=a[i][j];
a[i][j]=a[j][i];
a[j][i]=temp;
}
}

a) 3060 b) 3030 c) 6030
d) 60 e) 30
30 30
34. Which statements should be written for the (1) and (2)
respectively?
int i=2,j=0,tut;
scanf("%d",&tut);
do{ if(tut==2||tut==1) { j=1; break; }
if((1)...) { j=1;break;}
i++;}
while((2)...);
if (j==1) printf("the number %d is not a
prime number",tut);
else printf("the number %d is a prime
number",tut);}

(1) (2)
a) tut/i==0 i<=tut
b) tut/i==0 i<tut
c) tut%i==0 i<=tut
d) tut%i==0 i<tut
e) tut%i==1 i<=tut


35. What would be the value of w, after execution the code
below?
int i=1,w=5;
do{
if(i==2) w--;
w++;i++;}
while (i>5);

a) 8 b) 12 c) 5 d) 9 e) 6

36. What would be the value of z, after execution the code
below?
int j,i,z=0;
for (j=0;j<5;j++)
for (i=1;i<3;++i) {
if(j==2) break;
z+=2; }

a) 8 b) 16 c) 12 d) 10 e) 14

37. Which one is an incorrect declaration and initialization
of the C string s1?

a) char s1[]= BLUE;
b) char s1[5]= BLUE;
c) char s1[4]= BLUE;
d) char s1[5]= {`B`,`L`,`U`, `E`,\0};
e) None. All of them are correct.

38. What will be the value of string pointed by newptr ?
char s[10]= Green;
char *ptr1=Blue;
newptr=strncpy(s,ptr1,2);

a) Blue b) Green c) Bleen
d) Grue e) GrBlue

39. What will be the output of below segment?
char s[10]= Green;
char *newptr= *s+1;
printf(%c, *newptr);

a) Green
b) reen
c) G
d) H
e) r

40. Determine the output of below program?
#include <stdio.h>
int z=100;
void f3(int *x, int *y) {
int z=50;
*x=*y; *y=z;
}
int main() {
int a=0, b=0, c=0, d=0, e=0, f, g, h;
a=10; b=20;
f3(&a,&b);
printf("%d %d %d\n",a,b,z);
}

a) 10 20 0
b) 10 20 10
c) 10 20 20
d) 10 20 30
e) 20 50 100

41. Determine the output of the above program?
#include <stdio.h>
int z=100;
void f4(int x, int y) {
z=x+y;
}
int main() {
int a=0, b=0, c=0, d=0, e=0, f, g, h;
a=10; b=20; z=50;
f4(a,b);
printf("%d %d %d\n",a,b,z);
}

a) 10 0 0
b) 10 20 10
c) 10 20 20
d) 10 20 30
e) 20 50 100









42. Determine the output of below program?
#include <stdio.h>
int main() {
int A[N]={0,1,2,3,4,5,6,7,8,9};
int i,j,k,t;

for (i=0,j=N-1;i<j;i++,j--)
{
t=A[i]; A[i]=A[j]; A[j]=t;
}
for (i=0;i<N;printf("%d ",A[i]),i++);
printf("\n");
}

a) 0 1 2 3 4 5 6 7 8 9
b) 9 1 7 3 5 4 6 2 8 0
c) 1 2 3 4 5 6 7 8 9 10
d) 9 8 7 6 5 4 3 2 1 0
e) 1 3 5 7 9 8 6 7 8 9

43. Determine the output of the below program?
#include <stdio.h>
int main() {
int A[N]={1,2,1,2,1,2,1,2,1,2};
int i,j,k,t;

for (i=0,j=N-1;i<j;i+=2,j-=2)
{
t=A[i]; A[i]=A[j]; A[j]=t;
}
for (i=0;i<N;printf("%d ",A[i++]));
printf("\n");
}

a) 2 2 2 2 2 1 1 1 1 1
b) 2 1 2 1 2 1 2 1 2 1
c) 1 2 1 2 1 2 1 2 1 2
d) 1 1 1 1 1 2 2 2 2 2
e) 1 2 3 4 5 6 7 8 9 0

44. Determine the output of the below program?
#include <stdio.h>
int main() {
int A[N]={0,1,2,3,4,5,6,7,8,9},
B[N]={1,3,5,7,9,8,6,4,2,0};
int i,j,k,t;

for (i=0;i<N;i++) {
if (A[i]>B[i]) printf("%d ",A[i]);
else printf("%d ",B[i]);
}
printf("\n");
}

a) 1 3 5 7 9 8 6 7 8 9
b) 0 2 4 6 8 9 7 7 8 9
c) 1 3 5 7 9 8 6 4 2 0
d) 1 2 3 4 5 6 7 8 9 10
e) 0 1 2 3 4 5 6 7 8 9
45. Determine the output of the below program?

#include <stdio.h>
#define M 5
int main() {
int A[M][M]=

{{0,1,2,3,4},{5,6,7,8,9},{10,11,12,13,14},{1
5,16,17,18,19},{20,21,22,23,24}};
int i,j,k,t,rc;

rc=2;
for (i=0;i<M;i++)
{
t=A[rc][i]; A[rc][i]=A[i][rc];
A[i][rc]=t;
}
for (i=0;i<M;i++,printf("\n"))
for (j=0;j<M;printf("%d
",A[i][j]),j++);
printf("\n");
}

a) 4 1 2 3 0
5 8 7 6 9
10 11 12 13 14
15 18 17 16 19
24 21 22 23 20

b) 0 1 2 3 4
5 6 7 8 9
14 13 12 11 10
15 16 17 18 19
20 21 22 23 24
c) 0 1 22 3 4
5 6 17 8 9
10 11 12 13 14
15 16 7 18 19
20 21 2 23 24
d) 0 1 22 3 4
5 6 17 8 9
14 13 12 11 10
15 16 7 18 19
20 21 2 23 24

e) 0 1 10 3 4
5 6 11 8 9
2 7 12 17 22
15 16 13 18 19
20 21 14 23 24

















THERE ARE 5 BONUS QUESTIONS.
YOUR EXAM WILL BE GRADED OVER 40 QUESTIONS.
NO WRONG ANSWER CANCELS ANY CORRECT ANSWER.

DO NOT FORGET TO FILL IN ALL THE INFORMATION
IN THE OPTICAL FORM!
OTHERWISE YOUR EXAM WILL NOT BE GRADED!
WRITE YOUR NAME ON BOTH OF THE EXAM
BOOKLET AND THE OPTICAL FORM!


Student ID your unique 7-digit ID number
Course ID 5710230
Sect no need to fill
Exam Type A
Test Number 2



AFTER YOU COMPLETE, CHECK AGAIN!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy