Control-Statement
Question 1 |
Consider the following C program:
#include <stdio.h> int main() { int a[] = {2, 4, 6, 8, 10} ; int i, sum = 0, *b = a + 4 ; for (i = 0; i < 5; i++) sum = sum + (*b - i) - *(b - i) ; printf ("%d\n", sum) ; return 0 ; }
The output of the above C program is _____.
3 | |
7 | |
11 | |
10 |
Question 1 Explanation:
///////////////////////////////////PROGRAM
#include
int main()
{
int a[] = {2,4,6,8,10};
int i, sum = 0, *b = a+4;
for(i=0; i<5; i++)
{ printf("*b, (*b-i): %d , %d\n",*b, (*b-i) );
printf("*(b-i): %d\n",*(b-i) );
printf("sum = %d + %d - %d\n",sum, (*b-i),*(b-i));
sum = sum + (*b-i) - *(b-i);
printf("sum = %d\n", sum);
}
printf("%d\n", sum);
return 0;
}
//////////////////////////////OUTPUT
*b, (*b-i): 10 , 10
*(b-i): 10
sum = 0 + 10 - 10
sum = 0
*b, (*b-i): 10 , 9
*(b-i): 8
sum = 0 + 9 - 8
sum = 1
*b, (*b-i): 10 , 8
*(b-i): 6
sum = 1 + 8 - 6
sum = 3
*b, (*b-i): 10 , 7
*(b-i): 4
sum = 3 + 7 - 4
sum = 6
*b, (*b-i): 10 , 6
*(b-i): 2
sum = 6 + 6 - 2
sum = 10
10
#include
int main()
{
int a[] = {2,4,6,8,10};
int i, sum = 0, *b = a+4;
for(i=0; i<5; i++)
{ printf("*b, (*b-i): %d , %d\n",*b, (*b-i) );
printf("*(b-i): %d\n",*(b-i) );
printf("sum = %d + %d - %d\n",sum, (*b-i),*(b-i));
sum = sum + (*b-i) - *(b-i);
printf("sum = %d\n", sum);
}
printf("%d\n", sum);
return 0;
}
//////////////////////////////OUTPUT
*b, (*b-i): 10 , 10
*(b-i): 10
sum = 0 + 10 - 10
sum = 0
*b, (*b-i): 10 , 9
*(b-i): 8
sum = 0 + 9 - 8
sum = 1
*b, (*b-i): 10 , 8
*(b-i): 6
sum = 1 + 8 - 6
sum = 3
*b, (*b-i): 10 , 7
*(b-i): 4
sum = 3 + 7 - 4
sum = 6
*b, (*b-i): 10 , 6
*(b-i): 2
sum = 6 + 6 - 2
sum = 10
10
Question 2 |
Consider the following C program:
#include <stdio.h> int r() { static int num=7 ; return num-- ; } int main () { for (r(); r (); r()) printf ("%d", r()); return 0 ; }
Which one of the following values will be displayed on execution of the programs?
41 | |
63 | |
52 | |
630 |
Question 2 Explanation:
///////////////////////////PROGRAM
#include
int r()
{
int x;
static int num=7;
x =num--;
printf("num--: %d\n",x);
return x;
}
int main()
{
for(r(); r(); r())
{
printf("%d\n", r());
}
return 0;
}
//////////////////////////////OUTPUT
num--: 7
num--: 6
num--: 5
5
num--: 4
num--: 3
num--: 2
2
num--: 1
num--: 0
#include
int r()
{
int x;
static int num=7;
x =num--;
printf("num--: %d\n",x);
return x;
}
int main()
{
for(r(); r(); r())
{
printf("%d\n", r());
}
return 0;
}
//////////////////////////////OUTPUT
num--: 7
num--: 6
num--: 5
5
num--: 4
num--: 3
num--: 2
2
num--: 1
num--: 0
Question 3 |
Consider the following C program:
#include <stdio.h> int main () { float sum = 0.0, j = 1.0, i = 2.0; while (i/j > 0.0625) { j = j + j; sum = sum + i/j; printf ("%f \n", sum); } return 0; }
The number of times the variable sum will be printed, when the above program is executed, is ______.
5 | |
2 | |
7 | |
10 |
Question 3 Explanation:
///////////////////////////////// PROGRAM
#include
int main()
{
float sum= 0.0, j=1.0, i=2.0;
while(i/j > 0.0625)
{
j = j+j;
sum = sum+i/j;
printf("%f\n",sum);
}
return 0;
}
//////////////////////////////////OUTPUT
1.000000
1.500000
1.750000
1.875000
1.937500
#include
int main()
{
float sum= 0.0, j=1.0, i=2.0;
while(i/j > 0.0625)
{
j = j+j;
sum = sum+i/j;
printf("%f\n",sum);
}
return 0;
}
//////////////////////////////////OUTPUT
1.000000
1.500000
1.750000
1.875000
1.937500
Question 4 |
If the following loop is implemented
{
int num=0;
do
{
- - num;
printf ( ”%d” , num);
num++;
} while (num >50)
}
{
int num=0;
do
{
- - num;
printf ( ”%d” , num);
num++;
} while (num >50)
}
the loop will run infinitely many times | |
the program will not enter the loop | |
there will be compilation error reported | |
a run time error will be reported |
Question 4 Explanation:
It will run infinite number of times because the --num variable is keep on reducing amount and num++ variable is keep on increasing amount. So, it never violate condition.
Question 5 |
Consider the following pseudo-code fragment, where m is a non-negative integer that has been initialized:
p=0;
k=0;
while(k < m)
        p = p + 2k;
        k=k+1;
end while
Which of the following is a loop invariant for the while statement?
(Note: a loop variant for a while statement is an assertion that is true each time guard is evaluated during the execution of the while statement).
p=0;
k=0;
while(k < m)
        p = p + 2k;
        k=k+1;
end while
Which of the following is a loop invariant for the while statement?
(Note: a loop variant for a while statement is an assertion that is true each time guard is evaluated during the execution of the while statement).
p = 2 k − 1 and 0≤k < m | |
p = 2 k+1 − 1 and 0≤k < m | |
p = 2 k − 1 and 0≤k≤m | |
p = 2 k+1 − 1 and 0≤k≤m |
Question 5 Explanation:
For k=0, P=0+2 0 =1
k=1, P=1+2 1 =3
k=2,P=3+2 2 =7
k=3,P=7+2 3 =15
Only the option-3 satisfied to the loop variant.
Ex: m=2, The loop will execute for 3 times for k value is 0,1 and 2.
Then options-3 gives P=1,P=3 and P=7 for the k values 0,1 and respectively
k=1, P=1+2 1 =3
k=2,P=3+2 2 =7
k=3,P=7+2 3 =15
Only the option-3 satisfied to the loop variant.
Ex: m=2, The loop will execute for 3 times for k value is 0,1 and 2.
Then options-3 gives P=1,P=3 and P=7 for the k values 0,1 and respectively
Question 6 |
Assume that x and y are non-zero positive integers. What does the following program segment perform ?
while (x!=0)
{
if (x>y)
x = x-y;
else
y=y-x;
printf(“%d”,x);
}
Computes LCM of two numbers | |
Computes GCD of two numbers | |
Divides large number with small number | |
Subtracts smaller number from large number |
Question 6 Explanation:
Let X=3 and Y=5
1st pass : X=3 and Y=2
2nd pass : X=1 and Y=2
3rd pass : X=1 and Y=1
Write(X), which writes 1.
Which is nothing but GCD of 3 & 5.
1st pass : X=3 and Y=2
2nd pass : X=1 and Y=2
3rd pass : X=1 and Y=1
Write(X), which writes 1.
Which is nothing but GCD of 3 & 5.
Question 7 |
Consider the following program segment :
d=0;
for(i=1; i<31, ++i)
for(j=1; j<31, ++j)
for(k=1; k<31, ++k)
if ((i+j+k)%3)= = 0);
d = d + 1;
printf(“%d”, d);
The output will be
d=0;
for(i=1; i<31, ++i)
for(j=1; j<31, ++j)
for(k=1; k<31, ++k)
if ((i+j+k)%3)= = 0);
d = d + 1;
printf(“%d”, d);
The output will be
9000 | |
3000 | |
90 | |
2700 |
Question 7 Explanation:
Step1: Each for loop will execute 30 times, then total number of iterations are 30*30*30=27000,
But value “d” is incremented only factors of 3 only.
Step2: For the values i=1, j=1 then
k=1,4,7,10,13,16,19,22,25 and 28(10 times)
i=1 and j=2 then
k=3,6,9,12,15,18,21,24,27,and 30(10 times)
i=1 and j=3 then
K=2,5,8,11,14,17,20,23,26,29(10 times)
Then for i=1 value there will be 30*10 =300 times.
Finally for all “i” values from 1 to 30 is
= 300*30
= 9000
But value “d” is incremented only factors of 3 only.
Step2: For the values i=1, j=1 then
k=1,4,7,10,13,16,19,22,25 and 28(10 times)
i=1 and j=2 then
k=3,6,9,12,15,18,21,24,27,and 30(10 times)
i=1 and j=3 then
K=2,5,8,11,14,17,20,23,26,29(10 times)
Then for i=1 value there will be 30*10 =300 times.
Finally for all “i” values from 1 to 30 is
= 300*30
= 9000
There are 7 questions to complete.