Control Flow
Question 1 |
Consider the following C code segment:
For the program fragment above, which of the following statements about the variables i and j must be true after the execution of this program? [!(exclamation) sign denotes factorial in the Solution]

For the program fragment above, which of the following statements about the variables i and j must be true after the execution of this program? [!(exclamation) sign denotes factorial in the Solution]
( j=(x-1)!) ∧ (i>=x) | |
( j = 9!) ∧ (i =10) | |
(( j = 10!) ∧ (i = 10 )) V (( j = (x - 1)!) ∧ (i = x )) | |
(( j = 9!) ∧ (i = 10)) V (( j = (x - 1)!) ∧ (i = x )) |
Question 1 Explanation:
As per code segment,
Step-1: The ith loop will compute from value 1 to 9. If the condition is true, then it computes j=j*i statement.
Step-2: The statement j=j*i is nothing but calculating 9!. If the condition fails automatically while loop will be terminated. It means maximum we can execute 9 values.
Step-3: when x becomes 10 then maximum I value will be 9. So, we can calculate up to 9! And I become 10 then it terminates the condition.
Step-1: The ith loop will compute from value 1 to 9. If the condition is true, then it computes j=j*i statement.
Step-2: The statement j=j*i is nothing but calculating 9!. If the condition fails automatically while loop will be terminated. It means maximum we can execute 9 values.
Step-3: when x becomes 10 then maximum I value will be 9. So, we can calculate up to 9! And I become 10 then it terminates the condition.
Question 2 |
Consider the following pseudo-code
x:=1;
i:=1;
while (x <= 1000)
begin
x:=2^x;
i:=i+1;
end;
What is the value of i at the end of the pseudo-code?
x:=1;
i:=1;
while (x <= 1000)
begin
x:=2^x;
i:=i+1;
end;
What is the value of i at the end of the pseudo-code?
4 | |
5 | |
6 | |
7 |
Question 2 Explanation:
Initialisation: x = 1, i = 1;
Loop: x i
21 2
22 3
24 4
216 5
After this condition becomes false.
Loop: x i
21 2
22 3
24 4
216 5
After this condition becomes false.
Question 3 |
Assume A and B are non zero positive integers. The following code segment


Computes the LCM of two numbers | |
Divides the larger number by the smaller number | |
Computes the GCD of two numbers | |
Finds the smaller of two numbers |
Question 3 Explanation:
→ The above iterative code is computes the GCD of two numbers using euclidean algorithm.
→ The procedure is to subtract smaller number from larger. So that we can reduce larger number then doesn’t change the value of GCD.
→ if we performing number of iterations based on condition the larger of two numbers will end up with GCD.
→ The procedure is to subtract smaller number from larger. So that we can reduce larger number then doesn’t change the value of GCD.
→ if we performing number of iterations based on condition the larger of two numbers will end up with GCD.
Question 4 |
Consider the following C program
How many lines of output does this program produce?

How many lines of output does this program produce?
0-9 lines of output | |
10-19 lines of output | |
20-29 lines of output | |
More than 29 lines of output |
Question 4 Explanation:
→ Since there is one line of output for each loop, we need to determine the number of
times the loop executes. Since i is constant, we need to see the growth of j only.
→ Let the initial value of j be denoted by J1 and the subsequent values by Jn for n= 2, 3,... so that J denotes a progression of j. We see that Jn = 2Jn-1;
→ J1 = 2, which gives Jn = 2n-1;
n = 1, 2, ...
→ The loop will execute as long as i/j=2.0/2n-1 >0.001 which gives n<3log210+2 (or) n<11.97.
→ Thus the loop will execute 11 times which is equivalent to say there will be 11 lines of output.
times the loop executes. Since i is constant, we need to see the growth of j only.
→ Let the initial value of j be denoted by J1 and the subsequent values by Jn for n= 2, 3,... so that J denotes a progression of j. We see that Jn = 2Jn-1;
→ J1 = 2, which gives Jn = 2n-1;
n = 1, 2, ...
→ The loop will execute as long as i/j=2.0/2n-1 >0.001 which gives n<3log210+2 (or) n<11.97.
→ Thus the loop will execute 11 times which is equivalent to say there will be 11 lines of output.
Question 5 |
What will be the output of the following C code?


10 11 12 13 14 | |
10 10 10 10 10 | |
0 1 2 3 4 | |
Compilation error |
Question 5 Explanation:
Step-1: We are initialized i=0 in for loop. It means condition true because it is less than 5.
Step-2: Inside the for loop we are assigning again I value is 10 and printing value i.
Iteration-1: We are printing value 10 then increment by 1
Iteration-2: We are clearing previous value and assigning value 10. Printing value is 10;
Iteration-5: We are clearing previous value and assigning value 10. Printing value is 10
Step-3: output is 1010101010
Step-2: Inside the for loop we are assigning again I value is 10 and printing value i.
Iteration-1: We are printing value 10 then increment by 1
Iteration-2: We are clearing previous value and assigning value 10. Printing value is 10;
Iteration-5: We are clearing previous value and assigning value 10. Printing value is 10
Step-3: output is 1010101010