Control Flow

Question 1
Assume A and B are non zero positive integers. The following code segment
A
Computes the LCM of two numbers
B
Divides the larger number by the smaller number
C
Computes the GCD of two numbers
D
Finds the smaller of two numbers
       Programming-for-Output-Problems       Control Flow       ISRO-2018
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.
Question 2
Consider the following C program How many lines of output does this program produce?
A
0-9 lines of output
B
10-19 lines of output
C
20-29 lines of output
D
More than 29 lines of output
       Programming-for-Output-Problems       Control Flow       ISRO-2018
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 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 3
What will be the output of the following C code?
A
10 11 12 13 14
B
10 10 10 10 10
C
0 1 2 3 4
D
Compilation error
       Programming       Control Flow       ISRO-2017 May
Question 3 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
Question 4
What does the following program do when the input is unsigned 16-bit integer?
A
It prints all even bits from num
B
It prints all odd bits from num
C
It prints binary equivalent of num
D
None of the above
       Programming       Control Flow       ISRO-2017 May
Question 4 Explanation: 
Step-1: Take n value initially 14(any number we can take but we are taken 14)
Step-2: 14<16
Step-3: It will print 00000000 00001110
Question 5
The complexity of the program is
A
O(log n)
B
O(n2)
C
O(n2 log n)
D
O(n log n)
       Programming       Control Flow       ISRO-2017 December
Question 5 Explanation: 
Question 6
Consider the following segment of C-code: The number of comparisons made in the execution of the loop for any n > 0 is:
A
⌊log 2n⌋*n
B
n
C
⌊log 2n⌋
D
⌊log 2n⌋+1
       Programming-for-Output-Problems       Control Flow       ISRO-2016
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 2n⌋+1
⌊log 26⌋+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);
}
}
A
8
B
9
C
10
D
11
       Programming-for-Output-Problems       Control Flow       ISRO CS 2014
Question 7 Explanation: 
Iteration-1:
while (1.000000 > 0.001)
{
j=2.0
sum=0+1.000000;
printf("%f\n",sum); /* It will print 1.000000 */
}
Iteration-2: 1.500000
Iteration-3: 1.750000
Iteration-4: 1.875000
Iteration-5: 1.937500
Iteration-6: 1.968750
Iteration-7: 1.984375
Iteration-8: 1.992188
Iteration-9: 1.996094
Iteration-10: 1.998047
Iteration-11: 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
a syntax error
B
cartrt
C
catrat
D
catratratratrat...
       Programming       Control Flow       Nielit Scientist-B CS 2016 march
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...
Question 9
Output of following program
#include
int main()
{
int i=5;
printf("%d%d%d", i++,i++,i++);
return 0;
}
A
765
B
567
C
777
D
compile dependent
       Programming       Control Flow       Nielit Scientist-B IT 22-07-2017
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.
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
A
4
B
8
C
9
D
6720
       Programming-for-Output-Problems       Control Flow       ISRO CS 2015
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
Iteration-2:
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
Iteration-3: 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
Iteration-4:
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
Iteration-5:
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
Iteration-6:
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:
A
0101010101
B
0111111111
C
0000000000
D
1111111111
       Programming-for-Output-Problems       Control Flow       ISRO CS 2015
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)
Question 12
The following program fragment prints
int i=5;
do
{
putchar(i+100);
printf("%d",i--);
}
while(i);
A
i5h4g3f2el
B
14h3g2f1e0
C
An error message
D
None of the above
       Programming       Control Flow       Nielit Scientific Assistance IT 15-10-2017
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.
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?

A
370
B
385
C
380
D
55
       Programming       Control Flow       JT(IT) 2018 PART-B Computer Science
Question 13 Explanation: 

Step-1: if i=1, j is also will execute 1 time because it is purely dependent to the variable i.
Sep-2: 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?

A
It returns the remainder of d2/d1, with the same sign as d1
B
It returns the remainder of d2/d1, with the same sign as d2
C
It returns the remainder of d1/d2, with the same sign as d1
D
It returns the remainder of d1/d2, with the same sign as d2
       Programming       Control Flow       JT(IT) 2018 PART-B Computer Science
Question 14 Explanation: 
Mod operations: fmod(d1,d2)
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;
}
A
10 times
B
Not even once
C
Repeated forever
D
Only once
       Programming       Control Flow       KVS 22-12-2018 Part-B
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?
A
Both the loops are repetitive in nature
B
Boh are conditional loops
C
Both will be executed at least once
D
Both are terminated when the condition become false
       Programming       Control Flow       KVS 22-12-2018 Part-B
Question 16 Explanation: 
Do-while 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);
A
i5h4g3f2el
B
14h3g2f1e0
C
An error message
D
None of the above
       Programming       Control Flow       Nielit Scientific Assistance CS 15-10-2017
Question 17 Explanation: 
Step-1: putchar(105) will print the ASCII equivalent of 105 is ' i '.
Step-2: The printf statement prints the current value of i=5 because we are given post decrement.
Step-3: 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=Y-1;
}
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?
A
Sets Z to be the product X*Y
B
Sets Z to be the sum X+Y
C
Sets Z to be the absolute value of Y
D
Sets Z to be the value of Y
       Programming       Control Flow       KVS DEC-2017
Question 18 Explanation: 
Assume X=3 and Y=3
So, it satisfying condition while(Y>0)
Iteration-1: while(3>0)
Z=0+3⇒ Z=3
Y=3-1⇒ Y=2
Iteration-2: while(2>0)
Z=3+3=6
Y=2-1=1
Iteration-3:while(1>0)
Z=6+3
Y=1-1=0
Iteration-4 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 ← X-Y
else
Y← Y-X
endif
end while
print(X)
What is the code doing?
A
It computes the GCD of two numbers
B
It computes the LCM of two numbers
C
It finds the smallest of two numbers
D
It divides the largest number by the smaller
       Programming       Control Flow       KVS DEC-2017
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.
Question 20
What is the following program segment doing?
main()
{
int d=1;
do
{
printf(“%d”\n”,d++);
}while(d<=9);
}
A
Adding 9 integers
B
Adding integers from 1 to 9
C
Displaying integers from 1 to 9
D
No output
       Programming       Control Flow       KVS 30-12-2018 Part B
Question 20 Explanation: 
The code consists of do-while 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.
There are 20 questions to complete.
PHP Code Snippets Powered By : XYZScripts.com
error: Content is protected !!