Sei sulla pagina 1di 5

Extra Final Exam Prep Exercises

Q1. Find the error in each of the following program segments and correct the errors.
a. int x[5]={1,2,3,4,5};
for(int i=0;i<=5;i++)
printf(%d,x[i]);
b. float
a[3][4]={1.1,2.0,3.3,4.5,5.3,6.7,7.6,8.0,9.1,10.2,11.4,12
.4,13.6,14.9};
c. void h(int m,int n)
{int q;
q=m%n;
return q;
}
d. while(d>0)
printf(%d,d--);
e. for(int k=10;k>5;k++)
printf(%d,k*2);

Q2. Determine the output of the following 5 C program segments below. You must show all
the output.
a. int x[4][3]={{1,2},5,6,8,7,9,10,11,14};
int y[4][3]={1,2,5,6,8,7,9,10,11,14};
for(int a=0;a<=3;a++)
for(int b=0;b<=2;b++)
{printf(%d,x[a][b]);
printf(%d,y[a][b]);}

b. char s1[20]=End of semester;


char s2[20]=2011-2012_
char s3[20]=;
char s4[20]=End of smtr
printf(%s,strcat(s2,s1));
printf(%s,strncpy(s3,s2,13));
printf(%d,strcmp(s1,s4));

c. int n=1, c=0, a[5]={9,4,12,1,8};


while(n<5)
{if (a[n]<a[0])
c++;
++n;
}
printf(%d,c);
d. temp = 5687;
rev = 0;
do {
digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}while (temp > 0);
printf(Rev is %d,rev);
if (val == rev)
printf("equal\n");
else
printf("not\n");

e. void f();
int main()
{
f();
f();
return 0;
}
void f(int l)
{static int l=1;
++l;
printf((l is %d,l);
l++;
printf(l is %d,l);
printf(l is %d,l++);
printf(l is %d,l++);
printf(l is %d,l);
}

Q3:
Given the following, answer the questions below:
#define MAX 50
int a[MAX], i,temp;
i. What is the effect of this program segment?
for

(i = 0; i < MAX / 2;
temp = a[i];
a[i] = a[MAX - i - 1];
a[MAX - i - 1] = temp;

++i) {

}
(a)
(b)
(c)
(d)
(e)

Arranges the elements of array a in ascending order.


Counts the number of elements of array a greater than its first element.
Reverses the numbers stored in the array.
Puts the largest value in the last array position.
None of the above.

ii. What is the effect of the following program segment?


for

(a)
(b)
(c)
(d)
(e)

iii.

(i = 0; i < MAX - 1;
if (a[i] > a[i + 1]) {
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}

Arranges the elements of array a in ascending order.


Counts the number of elements of array a greater than its first element.
Reverses the numbers stored in the array.
Puts the largest value in the last array position.
None of the above.

What is the effect of the following program segment?


temp = 0;
for (i = 1; i < MAX;
if (a[i] > a[0])
++temp;

(a)
(b)
(c)
(d)
(e)

++i)

++i)

Arranges the elements of array a in ascending order.


Counts the number of elements of array a greater than its initial element.
Reverses the numbers stored in the array.
Puts the largest value in the last array position.
None of the above.

iv.

What is the maximum valid subscript value for array a?

(a)
(b)
(c)
(d)
(e)

0
49
50
a[50]
none of the above

v. What is the minimum valid subscript value for array a?


(a)
(b)
(c)
(d)
(e)

0
1
any negative number
There is no minimum.
none of the above

Q4. Trace the following program and write down the exact output of this program in the box
shown.
#include <stdio.h>
#define ROWS 4
#define COLS 4
int multi[ROWS][COLS];
int main(void)
{
int row, col;
for (row = 0; row < ROWS; row++)
{
for (col = 0; col < COLS; col++)
{
multi[row][col] = row*col;
}
}
for (row = 0; row < ROWS; row++)
{
printf("\n");
for (col = 0; col < COLS; col++)
printf("%d ",multi[row][col]);
printf("\n");
}
return 0;
}

Q5. Give the output of the following C program:


#include<stdio.h>
#define SIZE 7
void fun1(int [ ], int);
int main()
{ int i;
int array1[SIZE] = {2, 3, 4, 5, 6};
fun1(array1, array1[2]);
for (i = 0; i<SIZE; i++)
printf(%\nd, array1[i]);
return 0;
}
void fun1(int array1[], int len)
{ int i;
for (i = 0; i<len; i++)
array1[i] += 10;
array1[len-1] += 20;
}

Potrebbero piacerti anche