## 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?

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 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

→ J

n = 1, 2, ...

→ The loop will execute as long as i/j=2.0/2

→ 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_{n-1};→ J

_{1}= 2, which gives J_{n}= 2^{n-1};n = 1, 2, ...

→ The loop will execute as long as i/j=2.0/2

^{n-1}>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:

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

Question 4 |

What does the following program do when the input is unsigned 16-bit 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:

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

Step-2: 14<16

Step-3: 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:

There are 5 questions to complete.