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
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 5
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 5 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 6
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 6 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.
Question 7
Consider the following pseudo-code fragment, where a and b are integer variables that have been initialized: /* Pre-conditions: (a>1∧ a<b) */ /* Assume that overflow never occurs */ int x=0; int p=1; while (p<b) { p=p*a; x=x+1; } When the while loop terminates, what will be the value of x in terms of a and b?
A
ab
B
ba
C
⌊logab⌋ /* ⌊ ⌋ means floor */
D
⌈logab⌉ /* ⌈ ⌉ means ceil */
E
None of the above
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