Final
Final
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!