TCS Questions on Loops

1. The output of the code below is
#include
int a;
void main()
{
if (a)
printf(“Hello”);
else
printf(“world”);
}
a) Hello
b) World
c) compile time error
d) none of the mentioned
Answer:b

2. The output of the code below is
#include
void main()
{
int a = 5;
if (true);
printf(“hello”);
}
a) It will display hello
b) It will throw an error
c) No Output
d) Depends on Compiler
Answer:b

3. The output of the code below is
#include
void main()
{
int a = 0;
if (a == 0)
printf(“hi”);
else
printf(“how are u”);
printf(“hello”);
}
a) hi
b) how are you
c) hello
d) hihello
Answer:d

4. The following code ‘for(;;)’ represents an infinite loop. It can be terminated by.
a) break
b) exit(0)
c) abort()
d) all of the mentioned
Answer:a

5. The correct syntax for running two variable for loop simultaneously is.
a) for (i = 0; i < n; i++) for (j = 0; j < n; j += 5) b) for (i = 0, j = 0;i < n, j < n; i++, j += 5) c) for (i = 0; i < n;i++){} d) for (j = 0; j < n;j += 5){} Answer:b 6. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)? a) for (i = n; i>0; i–)
b) for (i = n; i >= 0; i–)
c) for (i = n-1; i>0; i–)
d) for (i = n-1; i>-1; i–)
Answer:d

7. The output of this C code is?
#include
void main()
{
int x = 0;
for (x < 3; x++) printf(“Hello”); } a) Compile time error b) Hello is printed thrice c) Nothing d) Varies Answer:a 8. The output of this C code is? #include
void main()
{
double x = 0;
for (x = 0.0; x < 3.0; x++) printf(“Hello”); } a) Run time error b) Hello is printed thrice c) Hello is printed twice d) Hello is printed infinitely Answer:b 9. The output of this C code is? #include
int main()
{
do
printf(“Inside while loop “);
while (0);
printf(“Outside loop\n”);
}
a) Inside while loop
b) Inside while loop
Outside loop
c) Outside loop
d) Infinite loop
Answer:b

10. The output of this C code is?
#include
int main()
{
int i = 0;
do {
i++;
printf(“Inside while loop\n”);
} while (i < 3); } a) Inside while loop Inside while loop Inside while loop b) Inside while loop Inside while loop c) Depends on the compiler d) Compile time error Answer:a 11. Which of the following cannot be used as LHS of the expression in for (exp1 ;exp2 ; exp3) ? a) Variable b) Function c) typedef d) macros Answer:d 12. Which keyword can be used for coming out of recursion? a) break b) return c) exit d) Both break and return Answer:b 13. The keyword ‘break’ cannot be simply used within: a) do-while b) if-else c) for d) while Answer:b 14. Which keyword is used to come out of a loop only for single iteration? a) break b) continue c) return d) none of the mentioned Answer:b 15. The output of this C code is? #include
void main()
{
int i = 0;
if (i == 0)
{
printf(“Hello”);
break;
}
}
a) Hello is printed infinite times
b) Hello
c) Varies
d) Compile time error
Answer:d