Consider the following C code segment: #include main() { int i, j , x ;…

2018

Consider the following C code segment:

#include 
main()
{
    int i, j , x ;
    scanf("%d", &x);
    i = 1 ; j = 1;
    while ( i< 10 ) {
          j = j * i;
          i = i + 1;
          if (i == x) break ;
          }
 }

For the program fragment above, which of the following statements about the variables i and j must be true after execution of this program? [!(exclamation) sign denotes factorial in the answer]

Answer: D. (( j = 9!) ? (i = 10)) V (( j = (x - 1)!) ? (i = x ))Initially: i = 1 j = 1 Inside the loop: j = j * i; i = i + 1;So, j stores factorial values. The loop can terminate in two ways: Case 1: Break condition…

  1. A.

    ( j = (x - 1 )! ) ? (i >= x)

  2. B.

    ( j = 9!) ? (i =10)

  3. C.

    (( j = 10!) ? (i = 10 )) V (( j = (x - 1)!) ? (i = x ))

  4. D.

    (( j = 9!) ? (i = 10)) V (( j = (x - 1)!) ? (i = x ))

Attempted by 372 students.

Show answer & explanation

Correct answer: D

Initially:

i = 1
j = 1

Inside the loop:

j = j * i;
i = i + 1;

So, j stores factorial values.

The loop can terminate in two ways:

Case 1: Break condition executes

If i == x, the loop breaks after incrementing i.

At that time:

i = x
j = (x - 1)!

Case 2: Loop ends normally

If x >= 10, the break condition never occurs.

The loop runs until i = 10, and:

j = 9!
i = 10

Therefore, the correct statement is:

((j = 9!) ? (i = 10)) V ((j = (x - 1)!) ? (i = x))

Hence, Option D is correct.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…