Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Q1: Consider the following C function definition.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Which of the following statements is/are TRUE about the above function?  (2024 SET-1)
(a) If the inputs are x = 20, y = 10, then the return value is greater than 220
(b) If the inputs are x = 20, y = 20, then the return value is greater than 220
(c) If the inputs are x = 20, y = 10, then the return value is less than 210
(d) If the inputs are x = 10, y = 20, then the return value is greater than 220

Ans: (b and d)
Sol: 
Let ti be the value of x after finishing the ith loop. Therefore, the returned value is ty-1.
t0 = 2x + y
t1 = 2t+ y = 2(2x + y) + y = 4x + 3y
t= 2t1 + y = 2(4x + 3y) + y = 8x + 7y
ti = 2i+1 x + (2i+1 -1)y = 2i+1(x + y) - y
Therefore, returned value = ty-1 = 2(x + y) - y
A. When x = 20, y = 10 ⇒ t9 = 210 (30) -10Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)220
B. When x = 20, y = 20 ⇒ t19 = 220(40) - 20 > 220
C. When x = 20, y = 10 ⇒ t9 = 210 (30) - 10Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)210
D. When x = 10, y = 20 ⇒ t19 = 220 (30) - 20 > 220
Answer - B, D

Q2: Consider the following C code. Assume that unsigned long int type length is 64 bits.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The value returned when we call fun with the input 240 is  (2018)
(a) 4
(b) 5
(c) 6
(d) 40
Ans: 
(b)
Sol:

Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
First for loop will make j = 40
Next for loop will divide j value (which is 40 now) by 2 each time until j ≤ 1.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
So, Sum = 5
Correct Answer: B

Q3: Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x, y, q and r are all unsigned int.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x==(y*q+r)?  (2017 SET-2)
(a) (q==r) && (r==0)
(b) (x > 0) && (r==x) && (y > 0)
(c) (q==0) && (r==x) && (y > 0)
(d)(q==0) && (y > 0)
Ans:
(c)
Sol: 
Here, x == (y *q+r) says q = quotient and r = remainder.
To divide a number with repeated subtraction, quotient should be initialized to 0 and should be incremented for each subtraction.
Initially q = 0 r = x.
∴ Initial conditions should be  (q == 0) && (r == x) && (y > 0).

Q4: The following function computes XY for positive integers X and Y.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Which one of the following conditions is TRUE before every iteration of the loop?  (2016 SET-2)
(a) X= ab
(b) (res * a)Y = (res * X)b
(c) XY = res * ab
(d) X= (res * a)b 
Ans: 
(c)
Sol:

Take X = 10, Y = 3
In that case,
Before Iteration 1:
res = 1, a = 10, b = 3
All options are satisfied here.
Iteration 1:

  • while(b! = 0) ⇒ 3! = 0 ✔
  • if(b%2 == 0) ⇒ 3%2 == 0  ✖
  • else
    res = res * a  ⇒ res = 1 * 10 = 10
    b = b -1  ⇒ b = 3 -1 = 2

Before Iteration 2:
res = 10, a = 10, b = 2
option A: XY = a⇒ 103 = 102     
option B: (res * a)Y = (res * X)= (10 * 10)3 = (10 * 10)2    
option C: XY = res * ab  ⇒ 10= 10 * 102      ✔
option D: XY = (res * a)b ⇒ 103 = (10 * 10)2      ✖
Lets see one more iteration to verify option C.
Iteration 2:
res = 10, a = 10, b = 2

  •  while(b!= 0) ⇒ 2! = 0     ✔
  •  if(b%2 == 0) ⇒ 2%2 == 0     ✔
    a = a * a
    = 10 * 10 = 100
    b = b/2
    = 2/2 = 1

Before Iteration 3:
res = 10, a = 100, b = 1
Option C: XY = res * a ⇒103 = 10 * 1001 = 103           ✔
Option C is answer

Q5: The following function computes the maximum value contained in an integer array p[] of size n (n>=1).
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The missing loop condition is  (2016 SET-1)
(a) a !=n
(b) b !=0
(c) b > (a+1)
(d) b !=a
Ans: (d)
Sol: Given in the question itself that we start comparing the contents of an array from a[0] and a[n - 1] (converging from both side) then condition must be till both meet at a point and that point will be a = b.
Hence loop condition should be a! = b.
Option C fails for n = 2, p = [1, 2].

Q6: Consider the following pseudo code, where x and y are positive integers.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The post condition that needs to be satisfied after the program terminates is  (2015 SET-1)

(a) {r = qx + y ∧ r < y}
(b) {x = qy + r ∧ r < y}
(c) {y = qx + r ∧ 0 < r < y}
(d) {q + 1 < r - y ∧ y > 0}
Ans:
(b)
Sol: 
The loop terminates when r < y. So, r < y is one post condition.
In each iteration q is incremented by 1 and y is subtracted from r. Initial value of r is x.
So, loop iterates x/y times and q will be equal to x/y and  r = x%y ⇒ x = qy + r;

Q7: Consider the following pseudo code. What is the total number of multiplications to be performed?  (2014 SET-1)
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)

(a) Half of the product of the 3 consecutive integers
(b) One-third of the product of the 3 consecutive integers.
(c) One-sixth of the product of the 3 consecutive integers
(d) None of the above.
Ans:
(c)
Sol: 
Total number of multiplications
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Therefore, correct answer would be (C).

Q8: Consider the following C function in which size is the number of elements in the array E:                  Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The value returned by the function My X is the  (2014 SET-1)
(a) maximum possible sum of elements in any sub-array of array E.
(b) maximum element in any sub-array of array E.
(c) sum of the maximum elements in all possible sub-arrays of array E.
(d) the sum of all the elements in the array E.
Ans: 
(a)
Sol: 
Answer is (A) maximum possible sum of elements in any sub-array of array E.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)

Q9: Consider the following segment of C-code:
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The number of comparisons made in the execution of the loop for any n > 0 is:  (2007)
(a) [logn] + 1
(b) n
(c) [log2n]
(d) [log2n] + 1
Ans: 
(d)
Sol: 

Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
We have to count those comparisons which happens during the execution of the loop and so the exit comparison must also be a part. So, the correct answer should be [log2 n] +2.
Since this is not in the choices we can assume that the question setter excluded the exit comparison and so the answer should be [log2 n] +1.
Option D.

Q10: Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n+m] be another integer array.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Which of the following condition(s) hold(s) after the termination of the while loop?  (2006)

(a) Only (I)
(b) Only (II)
(c) either (I) or (II) but not both
(d) neither (I) nor (II)
Ans: (d)
Sol: The while loop adds elements from a and b (whichever is smaller) to c and terminates when either of them exhausts. So, when loop terminates either i = n or j = m.
Suppose i = n. This would mean all elements from array a are added to c => k must be incremented by n. c would also contain j elements from array b. So, number of elements in c would be n + j and hence k = n + j.
Similarly, when j = m, k = m + i.
Hence, option (D) is correct. (Had k started from -1 and not 0 and we used + + k inside loop, answer would have been option (C))

Q11: Consider line number 3 of the following C-program.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Identify the compiler's response about this line while creating the object-module:  (2005)
(a) No compilation error  
(b) Only a lexical error
(c) Only syntactic errors
(d) Both lexical and syntactic errors
Ans: 
(a)
Sol: 
C language allows only certain words in it- these are called tokens. If we input any invalid tokens it causes lexical error.
eg:
44a44
causes lexical error as in C as an alphabet cannot come in between digits.
Syntactic error is caused by bad combination of tokens. For example, we cannot have a constant on the left hand side of an assignment statement, a for loop must have two expressions inside () separated by semi colon etc.
In the given question, line 3 won't cause a lexical error or syntactic error. The statement will be treated as a function call with three arguments. Function definition being absent will cause link time error, but the question asks only for compile-time errors. So, (a) must be the answer.
PS: Implicit function declaration was removed from C99 standard onwards. As per current standard, we should not use a function without declaration. Still, we cannot guarantee "compilation error"- just expect compiler warnings in C. In C++ this should produce a compilation (semantic) error. The output of compiling the above code using different standards are given below:
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)

Q12: What does the following algorithm approximate? (Assume m >1, e > 0).  (2004)
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
(a) logm
(b) m2
(c) m1/2
(d) m1/3
Ans:
(c)
Sol: 
By putting y = m/x into x = (x + y)/2
x = (x + m/x)/2
⇒ 2x2 = x2 + m
⇒ x = m1/2
We can also check by putting 2 or 3 different values also.
Correct Answer: C

Q13: Consider the following C program
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The program computes  (2004)
(a) x + y using repeated subtraction
(b) x mod y using repeated subtraction
(c) the greatest common divisor of x and y
(d) the least common multiple of x and y
Ans: 
(c)
Sol: 
It is an algorithm for gcd computation.
Here, while loop executes until m = n.
We can test by taking any two numbers as m, n.
Answer will be (C).

Q14: Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let n = d1d2...dm.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
The loop invariant condition at the end of the ith iteration is:  (2004)
(a) n = d1d2...dm-i and rev = dmdm-1...dm-i+1
(b) dm-i+1...dm-1dm or rev = dm-i...d2d1
(c) n ≠ rev
(d) n = d1d2...dm or rev = dm...d2d1
Ans: 
(a)
Sol: 
A loop invariant is something that hold at the start of a loop, across each iteration (inside an iteration it can change but before the iteration ends original condition must be true) and at the end also. So, we can check for the satisfiability of the condition at the loop header for start of the loop, for each iteration and also at the exit.
Here, in each iteration the right most digit of n, is moving to the right end of rev. So, answer is (A). i.e. the 2 conditions given in (A) choice are true on entry to loop, after each iteration (not necessarily during an iteration), and at end of loop.

Q15: Consider the following C function.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
For large values of y, the return value of the function f best approximates  (2003)

(a) xy
(b) ex
(c) In(1 + x)
(d) xx
Ans: (b)
Sol: A simplified version of the given program can be written as:
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
We can take p = 1, s = 1 initialization outside of for loop because there is no condition checking in for loop involving p, s.
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)
Hence, option B is answer.

The document Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Previous Year Questions: Loop - Programming and Data Structures - Computer Science Engineering (CSE)

1. What is a loop in computer science?
Ans. A loop in computer science is a programming construct that allows a set of instructions to be executed repeatedly based on a condition or a set of conditions.
2. What are the types of loops commonly used in programming?
Ans. The common types of loops used in programming are for loops, while loops, and do-while loops.
3. How do for loops differ from while loops in programming?
Ans. In a for loop, the loop control variable is initialized, checked for a condition, and updated within the loop syntax itself, while in a while loop, the condition is checked before the execution of the loop.
4. What is the difference between a while loop and a do-while loop in programming?
Ans. In a while loop, the condition is checked before the execution of the loop, while in a do-while loop, the condition is checked after the execution of the loop, ensuring that the loop runs at least once.
5. How can loops help in optimizing code in programming?
Ans. Loops help in reducing redundancy by allowing the same set of instructions to be executed multiple times without the need for writing them repeatedly, thus optimizing the code and making it more efficient.
119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

mock tests for examination

,

Semester Notes

,

study material

,

shortcuts and tricks

,

Sample Paper

,

Free

,

Summary

,

Exam

,

Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)

,

MCQs

,

pdf

,

Previous Year Questions with Solutions

,

Objective type Questions

,

Viva Questions

,

Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)

,

ppt

,

past year papers

,

practice quizzes

,

video lectures

,

Previous Year Questions: Loop | Programming and Data Structures - Computer Science Engineering (CSE)

,

Extra Questions

,

Important questions

;