Control-Statement

Question 1
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 1 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 2

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 2 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 3

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 3 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 4

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 4 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 5
Given the pseudocode below for the function remains(), which of the following statements is true about the output, if we pass it a positive integer n > 2?
int remains(int n)
{
int x = n;
for (i=(n-1);i>1;i--) { x = x i;
}
return x;
}
A
Output is always 0
B
Output is always 1
C
Output is 0 only if n is NOT a prime number
D
Output is 1 only if n is a prime number
E
None of the above
Question 6
Which of the following control structures of C is always executed at least once?
A
for loop
B
while loop
C
do-while loop
D
All the above
Question 6 Explanation: 
do-while loop in C is always executed at least once, because in this control structure first the loop is executed and then condition is checked ,which is not the case in for loop or while loop.
Question 7
Consider the following pseudo code
I=0; J=0; K=8;
while(I {
J=J+1;
while(J {
if(J {
temp=x[I])
x[I] = x[J];
x[J]=temp;
}
} // end of while-2
I=I+1;
} // end of while-1
The cyclomatic complexity of the above is
A
3
B
2
C
4
D
1
Question 7 Explanation: 
Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code.
The complexity M is then defined as
M = E − N + 2P,
where
E = the number of edges of the graph.
N = the number of nodes of the graph.
P = the number of connected components.
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