TCS Recursion and Iteration Questions

1.function main()
{
integer a=5,b=7
switch(a)
{
case 5 :print “I am 5”
break
case b:print “I am not 5”
break
default:print “I am different”
}
}
a.I am 5
b.I am not 5
c.I am different
d.Error
Answer:d
Explanation: Break doesn’t have -> ;

2.Ashima wants to print a pattern which includes checking and changing a variables value iteratively She decides to use a loop/condition Which of the following options should she use such that the body of the loop/condition is executed atleast once whether the variable satisfies the entering condition or not?
a.For Loop
b.While Loop
c.Do While Loop
d.Switch Case
Answer:c

3.The construct “if (condition) then A else B” is for which of the following purposes? 1) 2) 3) 4)
a.Decision-Making
b.Iteration
c.Recursion
d.Object Oriented Programming
Answer:a

4.Ravi and Rupali are asked to write a program to sum the rows of 2X2 matrices stored in the array A. Ravi writes the following code (Code A): for n = 0 to 1 sumRow1[n] = A[n][1] + A[n][2] end Rupali writes the following code (Code B): sumRow1[0] = A[0][1] + A[0][2] sumRow1[1] = A[1][1] + A[1][2] Comment upon these codes (Assume no loop­ unrolling done by compiler):
a.Code A will execute faster than Code B.
b.Code B will execute faster than Code A
c.Code A is logically incorrect.
d.Code B is logically incorrect.
Answer:b

5.Integer a =40, b =35, c=20, d =10 Comment about the output of the following two statements •
Print a*b/c-d
Print a*b/(c-d)
Comment about the output of the following two statements
a.Differ by 80
b.Same
c.Differ by 50
d.Differ by 160
Answer:a

6.What is the output of the following pseudo code? Int a =456,b,c,d=10; b=a/d; c=a-b; print c;
a.411.4
b.411
c.410.4
d.410
Answer: b

7.Ashima wants to print a pattern which includes checking and changing a variables value iteratively She decides to use a loop/condition Which of the following options should she use such that the body of the loop/condition is executed atleast once whether the variable satisfies the entering condition or not?
a.For Loop
b.While Loop
c.Do While Loop
d.Switch Case
Answer:c

8.The construct “if (condition) then A else B” is for which of the following purposes?
a.Decision-Making
b.Iteration
c.Recursion
d.Object Oriented Programming
Answer:a

9.Function main() { Integer i=0.7 Static float m=0.7 If(m equals i) Print(“We are equal”) Else If(m>i) Print(“I am greater”) Else Print(“I am lesser”)
a.We are equal
b.I am greater
c.I am lesser
d.This code will generate an error
Answer:d