Day 4-Looping Statements (Question+Answer)
Day 4-Looping Statements (Question+Answer)
a) break
b) exit(0)
c) abort()
d) terminate
Answer: a
2. What will be the correct syntax for running two variables for loop simultaneously?
a)
for (j = 0; j < n; j += 5)
b)
c)
Answer: b
3. Which for loop has range of similar indexes of ‘i’ used in for (i = 0;i < n; i++)?
#include <stdio.h>
int main()
short i;
printf("%d\n", i);
b) Numbers will be displayed until the signed limit of short and throw a runtime error
c) Numbers will be displayed until the signed limit of short and program will successfully
terminate
d) This program will get into an infinite loop and keep printing numbers with no errors
Answer: c
#include <stdio.h>
void main()
int k = 0;
for (k)
printf("Hello");
c) Nothing
d) Varies
Answer: a
#include <stdio.h>
void main()
int k = 0;
printf("Hello");
c) Nothing
d) Varies
Answer: a
#include <stdio.h>
void main()
double k = 0;
printf("Hello");
}
Answer: b
#include <stdio.h>
void main()
double k = 0;
printf("%lf", k);
a) 2.000000
b) 4.000000
c) 3.000000
Answer: c
#include <stdio.h>
int main()
int i = 0;
for (; ; ;)
printf("After loop\n");
b) Infinite loop
c) After loop
d) Undefined behaviour
Answer: a
#include <stdio.h>
int main()
int i = 0;
for (i++; i == 1; i = 2)
printf("After loop\n");
b) After loop
d) Undefined behaviour
Answer: a
int main()
while ()
printf("After loop\n");
b) After loop
d) Infinite loop
Answer: c
#include <stdio.h>
int main()
do
while (0);
printf("After loop\n");
a) In while loop
b)
In while loop
after loop
c) After loop
d) Infinite loop
Answer: b
#include <stdio.h>
int main()
int i = 0;
do {
i++;
a)
In while loop
In while loop
In while loop
b)
In while loop
In while loop
Answer: a
#include <stdio.h>
int main()
int i = 0;
while (i < 3)
i++;
a) 2
b) 3
c) 4
d) 1
Answer: c
#include <stdio.h>
void main()
int i = 2;
do
printf("Hi");
} while (i < 2)
b) Hi Hi
c) Hi
d) Varies
Answer: a
#include <stdio.h>
void main()
int i = 0;
while (++i)
printf("H");
a) H
d) Varies
Answer: b
#include <stdio.h>
void main()
int i = 0;
do
printf("Hello");
} while (i != 0);
a) Nothing
c) Hello
Answer: c
#include <stdio.h>
void main()
int i = 0;
i++;
printf("hi\n");
while (i < 8)
{
i++;
printf("hello\n");
Answer: d
a) for
b) while
c) do-while
Answer: d
20. How many times while loop condition is tested in the following C code snippets, if i is
initialized to 0 in both the cases?
while (i < n)
i++;
————-
do
i++;
a) n, n
b) n, n+1
c) n+1, n
d) n+1, n+1
Answer: d
#include <stdio.h>
int main()
int i = 0;
while (i = 0)
printf("True\n");
printf("False\n");
c) False
d) Compiler dependent
Answer: c
#include <stdio.h>
int main()
int i = 0, j = 0;
i++;
j++;
a) 5, 5
b) 5, 10
c) 10, 10
d) Syntax error
Answer: c
23. Which loop is most suitable to first perform the operation and then test the condition?
a) for loop
b) while loop
c) do-while loop
Answer: c
#include <stdio.h>
int main()
int a = 0, i = 0, b;
{
a++;
continue;
a) 2
b) 3
c) 4
d) 5
Answer: d
#include <stdio.h>
int main()
int a = 0, i = 0, b;
a++;
if (i == 3)
break;
a) 1
b) 2
c) 3
d) 4
Answer: d
a) do-while
b) if-else
c) for
d) while
Answer: b
27. Which keyword is used to come out of a loop only for that iteration?
a) break
b) continue
c) return
Answer: b
#include <stdio.h>
void main()
int i = 0, j = 0;
if (i > 1)
break;
printf("Hi \n");
a) Hi is printed 5 times
b) Hi is printed 9 times
c) Hi is printed 7 times
d) Hi is printed 4 times
Answer: a
#include <stdio.h>
void main()
int i = 0;
int j = 0;
if (i > 1)
continue;
printf("Hi \n");
}
}
a) Hi is printed 9 times
b) Hi is printed 8 times
c) Hi is printed 7 times
d) Hi is printed 6 times
Answer: b
#include <stdio.h>
void main()
int i = 0;
if (i < 4)
printf("Hello");
break;
c) Hello
Answer: c