Control Flow
Question 1 
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 1 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 2 
Consider the following C program
How many lines of output does this program produce?
09 lines of output  
1019 lines of output  
2029 lines of output  
More than 29 lines of output 
Question 2 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 J_{n} for n= 2, 3,... so that J denotes a progression of j. We see that J_{n} = 2J_{n1};
→ J_{1} = 2, which gives J_{n} = 2^{n1};
n = 1, 2, ...
→ The loop will execute as long as i/j=2.0/2^{n1} >0.001 which gives n<3log_{2}10+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 J_{n} for n= 2, 3,... so that J denotes a progression of j. We see that J_{n} = 2J_{n1};
→ J_{1} = 2, which gives J_{n} = 2^{n1};
n = 1, 2, ...
→ The loop will execute as long as i/j=2.0/2^{n1} >0.001 which gives n<3log_{2}10+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 3 
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 3 Explanation:
Step1: We are initialized i=0 in for loop. It means condition true because it is less than 5.
Step2: Inside the for loop we are assigning again I value is 10 and printing value i.
Iteration1: We are printing value 10 then increment by 1
Iteration2: We are clearing previous value and assigning value 10. Printing value is 10;
Iteration5: We are clearing previous value and assigning value 10. Printing value is 10
Step3: output is 1010101010
Step2: Inside the for loop we are assigning again I value is 10 and printing value i.
Iteration1: We are printing value 10 then increment by 1
Iteration2: We are clearing previous value and assigning value 10. Printing value is 10;
Iteration5: We are clearing previous value and assigning value 10. Printing value is 10
Step3: output is 1010101010
Question 4 
What does the following program do when the input is unsigned 16bit integer?
It prints all even bits from num  
It prints all odd bits from num  
It prints binary equivalent of num  
None of the above 
Question 4 Explanation:
Step1: Take n value initially 14(any number we can take but we are taken 14)
Step2: 14<16
Step3: It will print 00000000 00001110
Step2: 14<16
Step3: It will print 00000000 00001110
Question 5 
The complexity of the program is
O(log n)  
O(n^{2})  
O(n^{2} log n)  
O(n log n) 
Question 5 Explanation:
Question 6 
Consider the following segment of Ccode:
The number of comparisons made in the execution of the loop for any n > 0 is:
⌊log _{2}n⌋*n  
n  
⌊log _{2}n⌋  
⌊log _{2}n⌋+1 
Question 6 Explanation:
Explanation:
Let us consider n=6, then
1<=6 (correct)
2<=6 (correct)
4<=6 (correct)
8<=6 (False)
4 comparisons required
Option A:
⌊log n⌋+1
⌊log 6⌋+1
3+1=4 (correct)
Option B:
n=6 (False)
Option C:
⌊log n⌋
⌊log 6⌋=3 (False)
Option D:
⌊log _{2}n⌋+1
⌊log _{2}6⌋+1=2+1=3 (False)
Let us consider n=6, then
1<=6 (correct)
2<=6 (correct)
4<=6 (correct)
8<=6 (False)
4 comparisons required
Option A:
⌊log n⌋+1
⌊log 6⌋+1
3+1=4 (correct)
Option B:
n=6 (False)
Option C:
⌊log n⌋
⌊log 6⌋=3 (False)
Option D:
⌊log _{2}n⌋+1
⌊log _{2}6⌋+1=2+1=3 (False)
Question 7 
How many lines of output does the following C code produce?
#include<stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
while (i/j > 0.001)
{
j+=j;
sum=sum+(i/j);
printf("%f\n", sum);
}
}
#include<stdio.h>
float i=2.0;
float j=1.0;
float sum = 0.0;
main()
{
while (i/j > 0.001)
{
j+=j;
sum=sum+(i/j);
printf("%f\n", sum);
}
}
8  
9  
10  
11 
Question 7 Explanation:
Iteration1:
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf("%f\n",sum); /* It will print 1.000000 */
}
Iteration2: 1.500000
Iteration3: 1.750000
Iteration4: 1.875000
Iteration5: 1.937500
Iteration6: 1.968750
Iteration7: 1.984375
Iteration8: 1.992188
Iteration9: 1.996094
Iteration10: 1.998047
Iteration11: 1.999023
The program will terminate after 11th iteration. So, it print 11 lines.
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf("%f\n",sum); /* It will print 1.000000 */
}
Iteration2: 1.500000
Iteration3: 1.750000
Iteration4: 1.875000
Iteration5: 1.937500
Iteration6: 1.968750
Iteration7: 1.984375
Iteration8: 1.992188
Iteration9: 1.996094
Iteration10: 1.998047
Iteration11: 1.999023
The program will terminate after 11th iteration. So, it print 11 lines.
Question 8 
Output of the following loop is
for(putchar('c');putchar('a');putchar('r'))
putchar('t');
a syntax error  
cartrt  
catrat  
catratratratrat... 
Question 8 Explanation:
Syntax of ‘for loop’
for(Initialization; Condition; value modification/updation)
As per the above question,
→ Initialization part given by putchar('c)
→ Condition part check by putchar('a')
→ Value modification/updation part given by putchar('r')
→ Inside the for loop we are given putchar('t’)
→ The sequence of the for loop first initialization, second condition, executing inside body of for loop and value modification.
So, it prints catratratratrat...
for(Initialization; Condition; value modification/updation)
As per the above question,
→ Initialization part given by putchar('c)
→ Condition part check by putchar('a')
→ Value modification/updation part given by putchar('r')
→ Inside the for loop we are given putchar('t’)
→ The sequence of the for loop first initialization, second condition, executing inside body of for loop and value modification.
So, it prints catratratratrat...
Question 9 
Output of following program
#include
int main()
{
int i=5;
printf("%d%d%d", i++,i++,i++);
return 0;
}
#include
int main()
{
int i=5;
printf("%d%d%d", i++,i++,i++);
return 0;
}
765  
567  
777  
compile dependent 
Question 9 Explanation:
● Multiple increment or decrement of same variable in one expression or one statement is compiler dependent.
● The output will depend upon the way the compiler developed.
● The output will depend upon the way the compiler developed.
Question 10 
Consider the following program fragment
i=6720;
j=4;
while (i%j==0)
{
i=i/j;
j=j+1;
}
On termination j will have the value
i=6720;
j=4;
while (i%j==0)
{
i=i/j;
j=j+1;
}
On termination j will have the value
4  
8  
9  
6720 
Question 10 Explanation:
Initial values of i and j are 6720 and 4 respectively
Iteration 1:
i=6720
j=4
i%j==0 which is 6720%4 ==0 condition is true then
i=i/j=6720/4=1680
j=j+1=5
Iteration2:
i=1630
j=5
i%j==0 which is 1680%5 ==0 condition is true then
i=i/j=1630/5=336
j=j+1=6
Iteration3: i=336
j=6
i%j==0 which is 336%6 ==0 condition is true then
i=i/j=336/6=56
j=j+1=7
Iteration4:
i=56
j=7
i%j==0 which is 56%7 ==0 condition is true then
i=i/j=56/7=8
j=j+1=8
Iteration5:
i=8
j=8
i%j==0 which is 8%8 ==0 condition is true then
i=i/j=8/8=1
j=j+1=9
Iteration6:
i=1
j=9
i%j==0 which is 1%9 ==0 condition is false.
At this iteration, the “j” value is 9.
Iteration 1:
i=6720
j=4
i%j==0 which is 6720%4 ==0 condition is true then
i=i/j=6720/4=1680
j=j+1=5
Iteration2:
i=1630
j=5
i%j==0 which is 1680%5 ==0 condition is true then
i=i/j=1630/5=336
j=j+1=6
Iteration3: i=336
j=6
i%j==0 which is 336%6 ==0 condition is true then
i=i/j=336/6=56
j=j+1=7
Iteration4:
i=56
j=7
i%j==0 which is 56%7 ==0 condition is true then
i=i/j=56/7=8
j=j+1=8
Iteration5:
i=8
j=8
i%j==0 which is 8%8 ==0 condition is true then
i=i/j=8/8=1
j=j+1=9
Iteration6:
i=1
j=9
i%j==0 which is 1%9 ==0 condition is false.
At this iteration, the “j” value is 9.
Question 11 
The for loop
for (i=0; i<10; ++i)
printf("%d", i&1);
prints:
for (i=0; i<10; ++i)
printf("%d", i&1);
prints:
0101010101  
0111111111  
0000000000  
1111111111 
Question 11 Explanation:
The loop will execute for 10 times, from “i” value 0 to 9
“&” is bitwise AND operator.
For a given i=0 , the value is 0&1 is 0000 & 0001 which is 0
i=1, 0001 & 0001 which is 1
i=2 , 0010 & 0001 which is 0
i=3 0011 & 0001 which is 1
i=4 , 0100 & 0001 which is 0
i=5 0101 & 0001 which is 1
i=6 , 1100 & 0001 which is 0
i=7 0111 & 0001 which is 1
i=8 , 1000 & 0001 which is 0
i=9 1001 & 0001 which is 1
So the output is 0101010101(from i=0 to 9)
“&” is bitwise AND operator.
For a given i=0 , the value is 0&1 is 0000 & 0001 which is 0
i=1, 0001 & 0001 which is 1
i=2 , 0010 & 0001 which is 0
i=3 0011 & 0001 which is 1
i=4 , 0100 & 0001 which is 0
i=5 0101 & 0001 which is 1
i=6 , 1100 & 0001 which is 0
i=7 0111 & 0001 which is 1
i=8 , 1000 & 0001 which is 0
i=9 1001 & 0001 which is 1
So the output is 0101010101(from i=0 to 9)
Question 12 
The following program fragment prints
int i=5;
do
{
putchar(i+100);
printf("%d",i);
}
while(i);
int i=5;
do
{
putchar(i+100);
printf("%d",i);
}
while(i);
i5h4g3f2el  
14h3g2f1e0  
An error message  
None of the above 
Question 12 Explanation:
Here, i=5 and putchar(i+100) it means actually putchar(5+100) ⇒ putchar(105);
It will print the ASCII equivalent of 105 which is lower case ' i '. The printf statement prints the current value of i. i.e. 5 and then decrements it. So, h4 will be printed in the next pass. This continues until ' i ' becomes 0, at which point the loop gets terminated.
It will print the ASCII equivalent of 105 which is lower case ' i '. The printf statement prints the current value of i. i.e. 5 and then decrements it. So, h4 will be printed in the next pass. This continues until ' i ' becomes 0, at which point the loop gets terminated.
Question 13 
Consider the following algorithm:
Algorithm Anand(n) { sum=0; For i=1 to n do { For j=1 to i do { sum=sum+i; } } }
What will be the output of this algorithm for n=10?
370  
385
 
380  
55 
Question 13 Explanation:
Step1: if i=1, j is also will execute 1 time because it is purely dependent to the variable i.
Sep2: The sum variable is perform addition how many time jth loop executes with the difference of i value.
Question 14 
What is the return value of the C library function fmod(d1,d2), where d1 and d2 are integer numbers?
It returns the remainder of d2/d1, with the same sign as d1  
It returns the remainder of d2/d1, with the same sign as d2  
It returns the remainder of d1/d2, with the same sign as d1
 
It returns the remainder of d1/d2, with the same sign as d2

Question 14 Explanation:
Mod operations: fmod(d1,d2)
Take d1 = 10 and d2 = 20
d1/d2 = 10/20 = 10
So, option C is correct.
Take d1 = 10 and d2 = 20
d1/d2 = 10/20 = 10
So, option C is correct.
Question 15 
Consider the following code segment in C++. How many times the for loop will be repeated?
int main()
{
int f=1;
for(;f;)
cout<<”f=”<<f++<<”\n”;
Return 0;
}
int main()
{
int f=1;
for(;f;)
cout<<”f=”<<f++<<”\n”;
Return 0;
}
10 times  
Not even once  
Repeated forever  
Only once 
Question 15 Explanation:
There is no termination condition for the loop and value “f” incremented every time. So it will execute infinite number of times.
Question 16 
In the context of while loop do while loop in C++, which of the following is not true?
Both the loops are repetitive in nature  
Boh are conditional loops  
Both will be executed at least once  
Both are terminated when the condition become false 
Question 16 Explanation:
Dowhile will execute at least one time while loop will execute minimum of zero times.
Question 17 
The following program fragment prints
int i=5;
do
{
putchar(i+100);
printf("%d",i);
}while(i);
int i=5;
do
{
putchar(i+100);
printf("%d",i);
}while(i);
i5h4g3f2el  
14h3g2f1e0  
An error message  
None of the above 
Question 17 Explanation:
Step1: putchar(105) will print the ASCII equivalent of 105 is ' i '.
Step2: The printf statement prints the current value of i=5 because we are given post decrement.
Step3: The same process will perform until it fail the condition.
Step2: The printf statement prints the current value of i=5 because we are given post decrement.
Step3: The same process will perform until it fail the condition.
Question 18 
Consider the following code segment
if(Y<0)
{
X=X;
Y=Y;
}
Z=0;
while(Y>0)
{
Z=Z+X;
Y=Y1;
}
Assume that X,Y and Z are integer variables, and that X and Y have be initialized. Which of the following best describes what this code segment does?
if(Y<0)
{
X=X;
Y=Y;
}
Z=0;
while(Y>0)
{
Z=Z+X;
Y=Y1;
}
Assume that X,Y and Z are integer variables, and that X and Y have be initialized. Which of the following best describes what this code segment does?
Sets Z to be the product X*Y  
Sets Z to be the sum X+Y  
Sets Z to be the absolute value of Y  
Sets Z to be the value of Y 
Question 18 Explanation:
Assume X=3 and Y=3
So, it satisfying condition while(Y>0)
Iteration1: while(3>0)
Z=0+3⇒ Z=3
Y=31⇒ Y=2
Iteration2: while(2>0)
Z=3+3=6
Y=21=1
Iteration3:while(1>0)
Z=6+3
Y=11=0
Iteration4 while(0>0) fails.
So,It Sets Z to be the product X*Y=3*3=9
So, it satisfying condition while(Y>0)
Iteration1: while(3>0)
Z=0+3⇒ Z=3
Y=31⇒ Y=2
Iteration2: while(2>0)
Z=3+3=6
Y=21=1
Iteration3:while(1>0)
Z=6+3
Y=11=0
Iteration4 while(0>0) fails.
So,It Sets Z to be the product X*Y=3*3=9
Question 19 
Assume X and Y are non zero positive integers. Consider the following pseudo code
fragment:
while X< >Y do
if X > Y then
X ← XY
else
Y← YX
endif
end while
print(X)
What is the code doing?
fragment:
while X< >Y do
if X > Y then
X ← XY
else
Y← YX
endif
end while
print(X)
What is the code doing?
It computes the GCD of two numbers  
It computes the LCM of two numbers  
It finds the smallest of two numbers  
It divides the largest number by the smaller 
Question 19 Explanation:
Let X=3 and Y=5
1 _{st} pass : X=3 and Y=2
2 _{nd} pass : X=1 and Y=2
3 _{rd} pass : X=1 and Y=1
Write(X), which writes 1. Which is nothing but GCD of 3 & 5.
1 _{st} pass : X=3 and Y=2
2 _{nd} pass : X=1 and Y=2
3 _{rd} pass : X=1 and Y=1
Write(X), which writes 1. Which is nothing but GCD of 3 & 5.
Question 20 
What is the following program segment doing?
main()
{
int d=1;
do
{
printf(“%d”\n”,d++);
}while(d<=9);
}
main()
{
int d=1;
do
{
printf(“%d”\n”,d++);
}while(d<=9);
}
Adding 9 integers  
Adding integers from 1 to 9  
Displaying integers from 1 to 9  
No output 
Question 20 Explanation:
The code consists of dowhile loop in which action performs first and later condition checking.
In the printf() statement, d++ means first it will display d value and increment the d value later condition checking.So the integer values 1 to 9 will be printed.
In the printf() statement, d++ means first it will display d value and increment the d value later condition checking.So the integer values 1 to 9 will be printed.
There are 20 questions to complete.