Classical

C Programming MCQ - Decision Making & Looping

46:  

Consider the program fragment

j = 2;
while ((i % j) ! = 0)
j = j + 1;
if (j < i) printf ("%d", j);


If i >= 2, then the value of j, will be printed only if

A.

i is prime

B.

j does not divide i

C.

j is odd

D.

i is nor prime

 
 

Option: D

Explanation :


47:  

Consider the following program fragment

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

A.

 9030

B.

 27000

C.

3000

D.

none of the above 

 
 

Option: A

Explanation :

a + b +c  3 will be 0 if a + b+c is a multiple of 3. This will happen in one of the following ways. All three - a, b, and c are multiples of 3. This can only happen if a, b, and c take one of the 10 values. - 3 , 6, 9 , ..... , 30, independent of one another. So, there are 10 x 10 x 10 = 1000 ways this can happen. Another possibility is that a, b, and c all leave a remainder 1 so that a + b + c is evenly divisible by 3. Considering all the different possibilities and adding. we get 9000. That will be the integer that gets printed.




Suggest an improvement