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 _____.

A
3
B
7
C
11
D
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
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?

A
41
B
63
C
52
D
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
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 ______.

A
5
B
2
C
7
D
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
Question 4
Consider the following C function definition. int f(int x, int y) { for (int i=0; i<y; i++) { x=x+x+y; } return x; } Which of the following statements is/are TRUE about the above function?
A
If the inputs are x=20, y=10, then the return value is greater than 220
B
If the inputs are x=20, y=20, then the return value is greater than 220
C
If the inputs are x=20, y=10, then the return value is less than 210
D
If the inputs are x=10, y=20, then the return value is greater than 220
Question 4 Explanation: 
The function f(int x, int y) takes two integer arguments x and y. It uses a for loop that iterates y times. Inside the loop, the value of x is updated using the expression x = x + x + y. Analyzing Statements: (A) If the inputs are x=20, y=10, then the return value is greater than 220 Let's simulate the function call with x = 20 and y = 10: Iteration 1: x = 20 + 20 + 10 = 50 Iteration 2: x = 50 + 50 + 10 = 110 ... (similar updates in every iteration) Iteration 10 (assuming y = 10): x = ... + ... + 10 = ~210 (The exact value will be slightly higher than 210) Therefore, statement (A) is False. The return value is likely around 210, not greater than 220. (B) If the inputs are x=20, y=20, then the return value is greater than 220 Similar to (A), with y = 20, the final value of x will be significantly higher than 220. Statement (B) is True. (C) If the inputs are x=20, y=10, then the return value is less than 210 As shown in the analysis of (A), the return value is around 210. Statement (C) is False. (D) If the inputs are x=10, y=20, then the return value is greater than 220 With x = 10 and y = 20, the final value of x will be even higher than in (B). Statement (D) is True.
Question 5
What is the output of the following program segment ?
main ( )
{
int count, digit=0;
count=1;
while(digit <=9)
{
printf(​"%d /n”" , ++count);
++digit;
}
}
A
10
B
9
C
12
D
11
Question 5 Explanation: 
The digit starts with 0 and will run 9 more times. It means, the loop will run 10 runs. The count variable we are given pre increment. So, it increments before assigning values.
It prints the result is
2 /n 3 /n 4 /n 5 /n 6 /n 7 /n 8 /n 9 /n 10 /n 11
Question 6
If the following loop is implemented
{
int num=0;
do
{
- - num;
printf (​ ”%d”​ , num);
num++;
} while (num >50)
}
A
the loop will run infinitely many times
B
the program will not enter the loop
C
there will be compilation error reported
D
a run time error will be reported
Question 6 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 7
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).
A
p = 2 k − 1 and 0≤k < m
B
p = 2 k+1 − 1 and 0≤k < m
C
p = 2 k − 1 and 0≤k≤m
D
p = 2 k+1 − 1 and 0≤k≤m
Question 7 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
There are 7 questions to complete.

Access quiz wise question and answers by becoming as a solutions adda PRO SUBSCRIBER with Ad-Free content

Register Now

If you have registered and made your payment please contact solutionsadda.in@gmail.com to get access