PROGRAM LOOPS
A program loop is a fundamental programming structure that allows a
block of code to be executed repeatedly. This repetition continues until a
specific condition is met, enabling efficient execution of repetitive tasks
and complex algorithms. There are several types of loops,
including for, while, and do-while loops, each with its own characteristics
and use cases. Understanding how loops work is crucial for effective
programming.
1. Which of the following is NOT a type of loop in most programming
languages?
a) for loop b) while loop c) do-while loop d) if-else loop
2. What is the primary purpose of a loop in programming?
a) To terminate the program execution.
b) To execute a block of code repeatedly.
c) To declare variables.
d) To define functions.
3. In a while loop, the loop continues to execute as long as the condition
is: a) True b) False c) Equal to zero d) Not equal to zero
4. What is the role of the break statement within a loop?
a) To continue to the next iteration of the loop.
b) To terminate the loop immediately.
c) To skip the current iteration of the loop.
d) To define a new loop.
5. What is the function of the continue statement in a loop?
a) To terminate the loop immediately.
b) To skip the current iteration and move to the next one.
c) To define a new loop.
d) To exit the program.
6. Which loop is guaranteed to execute at least once, even if the
condition is initially false?
a) for loop b) while loop c) do-while loop d) None of the above.
7. What is an infinite loop?
a) A loop that executes only once.
b) A loop that continues executing forever, because the condition is
never met.
c) A loop that is used to debug other loops.
d) A loop that is always empty.
8. Which loop is best suited for iterating a known number of times?
a) while loop b) do-while loop c) for loop
d) Any loop can be used, but the for loop is most convenient.
9. What is the purpose of the loop control variable in a for loop?
a) To store the output of the loop.
b) To determine the number of iterations of the loop.
c) To define the loop's condition.
d) To declare new variables inside the loop.
10. What is the difference between ++i and i++ in a loop?
a) There is no difference.
b) ++i increments the value before using it, while i++ increments the
value after using it.
c) i++ increments the value before using it, while ++i increments the
value after using it.
d) ++i increments the value by two, while i++ increments it by one.
Printf & Scanf – Question and Answers
1. What is the output of this program?
#include <stdio.h>
int main()
{
printf("variable! %d", x);
return 0;
}
A. variable! X B. variable! followed by a junk value
C. Compile time error D. variable!
2. What is the output of this program?
#include <stdio.h>
int main()
{
int main = 3;
printf("%d", main);
return 0;
}
A. 3 B. Compile time error
C. Run time error D. give garbage value
3. What is the output of this program? if input a is 1 and b is 2
#include <stdio.h>
int main()
{
int a, b;
printf("%d", scanf("%d %d",&a,&b));
return 0;
}
A. 1 B. 2 C. runtime error D. compile time error
4. What is the output of this program?
#include <stdio.h>
void main()
{
printf("hello\rworld\n");
printf("hello\b\b\bworld\n");
}
A. hello B. World C. Helloworld D. None of the mentioned
helloworld heworld hellold
5. What is the output of this program?
#include <stdio.h>
int main()
{
int i;
i = printf("letsfindcourse");
i = printf("%d ", i);
printf("%d ", i);
return 0;
}
A. letsfindcourse 15 3 B. letsfindcourse 14 2
C. letsfindcourse 14 3 D. Compilation Error
6. Output of this statment is :
printf ( "%d" , printf ( "hello" ) );
A. Syntex Error B. hello5
C. gives garbage value D. print hello and terminates
7. What is the output of this program?
#include <stdio.h>
# define scanf "%s Find best course "
main()
{
printf(scanf, scanf);
return 0;
}
A. %s Find best course Find best course
B. %s Find best course %s Find best course
C. Invalid Syntex
D. Run time error
8. Which statment is true about the given code ?
#include <stdio.h>
int main()
{
printf("%d", main);
return 0;
}
A. Goes in infinite loop B. Gives Address of function main.
C. Gives garbage value D. Compilation Error
9. What is the output of this program?
#include <stdio.h>
int main()
{
int i;
i = 1, 2, 3;
printf("%d", i);
return 0;
}
A. 1 B. 2 C. 3 D. Invalid Syntax
10. Comment on the given statment:
scanf("%d",i);
A. Will execute without any error B. Will give Segmentation fault
C. Will give Compilation Error D. None of the above
11. What is the output of this program?
#include <stdio.h>
int main()
{
int x = 1, y = 2;
printf("%d", x, y);
return 0;
}
A. 1 B. 2 C. Compilation Error D. Garbage Value
12. What is the output of this program?
#include <stdio.h>
int main()
{
int x = 1, y = 2;
printf("%*d", x, y);
return 0;
}
A. 1 B. 2 C. Compilation Error D. Garbage Value
13. What is the output of this program?
#include <stdio.h>
int main()
{
char str[25];
printf(" %d ",printf("c-letsfind"));
return 0;
}
A. 10 c-letsfind B. 9 c-letsfind C. c-letsfind 9 D. c-letsfind 10
14. What is the output of this program?
#include <stdio.h>
# define loop while(true)
int main()
{
loop;
printf("c-letsfind");
return 0;
}
A. program never ends B. c-letsfind C. Compilation error D. None
15. What is the output of this program?
#include <stdio.h>
int main()
{
printf("%d", 5.00);
return 0;
}
A. Compilation error B. Garbage value C. 5 D. 0
16. What is the output of this program?
#include <stdio.h>
int main()
{
printf("%d",5.25);
return 0;
}
A. Compilation error B. Garbage value C. 5 D. 0
17. What is the output of this program ?
#include <stdio.h>
int main()
{
int a = 3;
printf("%d");
return 0;
}
A. 3 B. Compilation error C. Garbage value D. Runtime error
18. What is the output of this program ?
#include <stdio.h>
int main()
{
char *ptr = "Hello World";
printf(ptr+2);
return 0;
}
A. pointer cannot be initialized B. lo World
C. Runtime error D. llo World
19. What is the output of this program 32 bit c compiler ?
#include <stdio.h>
int main()
{
int a = 1;
printf("%d %p",a,a);
return 0;
}
A. Runtime error B. 1 00000001 C. 1 1 D. %p is not a format specifier
20. What is the output of this program ?
#include <stdio.h>
static struct student
{
int a;
int b;
}
struct_var{2,3};
int main()
{
printf("%d %d",struct_var.a,struct_var.b);
return 0;
}
A. Runtime Error B. Improper representation of structure variable
C. Compilation error D. 2 3
21. What is the output of the following code?
int main() {
int i=1;
i=2+2*i++;
printf("%d",i);
return 0;
}
A. 2 B. 3 C. 4 D. 5
22. What is the output of the following code?
main ( )
{
int i;
i=1;
i=i+2*i++;
printf( "%d" , i);
}
A. 2 B. 3 C. 4 D. 5
23. What is the output of this program?
main()
{
int i;
i = 2+3, 4>3, 1;
printf( "%d" , i);
}
A. 1 B. 4 C. 3 D. 5
24. What is the output of this program?
main ()
{
int i=5;
printf( "%d %d %d " , i,i<<2,i>>2);
}
A. 5 20 1 B. 5 1 20 C. 5 20 20 D. 5 1 1
25. What is the output of this program?
main ( )
{
int i=5;
printf( "%d %d %d " , i,i<<2,i<<2);
}
A. 5 20 1 B. 5 1 20 C. 5 20 20 D. 5 1 1
26. What is the output of this program?
int main()
{
int i=5;
printf( "%d %d %d " , i,i<2,i>2);
return 0;
}
A. Compilation error B. Garbage value C. 5 0 1 D. 5 1 0
27. What is the output of this program ?
int main()
{
int i=5;
printf( "%d %d %d " , i,i&&2,i||2);
return 0;
}
A. 5 1 1 B. Compilation error C. Garbage value D. 5 0 1
28. What is the output of this program ?
int main()
{
int i=5;
printf( "%d %d
" , i,i|2);
return 0;
}
A. 5 7 B. 5 1 C. Garbage value D. 5 0
29. What is the output of this program 32 bit c compiler ?
int main()
{
int i=1;
i=i+i*i++;
printf("%d",i);
return 0;
}
A. 2 B. 3 C. 4 D. 5
30. What is the output of this program ?
int main()
{
int i=-2;
i=i+i*i++;
printf("%d",i);
return 0;
}
A. -2 B. 1 C. -1 D. -2
1. Which of the following is NOT a type of loop in many programming
languages (like C, Java, Python)?
a) for loop b) while loop c) do-while loop d) until loop
2. What is the main purpose of a loop in programming?
a) To execute a block of code once
b) To repeat a block of code until a certain condition is met
c) To terminate the program
d) To declare variables
3. In a while loop, when is the condition checked?
a) Before the first iteration b) After each iteration
c) Both before the first iteration and after each subsequent iteration
d) The condition is not checked in a while loop
4. What does the break statement do within a loop?
a) It skips the current iteration and continues with the next one.
b) It terminates the loop entirely and continues with the next statement
after the loop.
c) It has no effect on the loop. d) It causes a syntax error.
5. What does the continue statement do within a loop?
a) It terminates the loop entirely.
b) It skips the current iteration and continues with the next one.
c) It has no effect on the loop. d) It causes a syntax error.
6. Which type of loop is best used when the number of iterations is
known beforehand? a) while loop b) do-while loop
c) for loop d) It doesn't matter which loop is used.
7. In a do-while loop, when is the condition checked?
a) Before the first iteration b) After each iteration
c) Both before the first iteration and after each subsequent iteration
d) The condition is not checked in a do-while loop
8. What is an infinite loop?
a) A loop that executes a fixed number of times.
b) A loop that has no exit condition and will run forever (or until the
program is terminated).
c) A loop that executes only once. d) A loop that is always valid.
program is terminated).
9. What will be the output of the following Python code?
Python
count = 0
while count < 5:
print(count)
count += 1
a) 0 1 2 3 4 b) 1 2 3 4 5 c) 0 1 2 3 4 5 d) None
10. Which of the following is a correct syntax for a for loop in Python?
a) for i in range(10)b) for i = 0 to 10
c) for i in 10 d) for i : 10
Looping statements Question 1:
When a break statement is encountered inside a loop, the loop
is immediately ________ and the program control resumes at
the _____________ following the loop.
1. terminated, next statement 2. paused, same statement
3. terminated, same statement 4. paused, next statement
Option 3 : terminated, same statement
Looping statements Question 1 Detailed Solution
The Java break statement is used to break loop or switch statement. It
breaks the current flow of the program at specified condition. In case of
inner loop, it breaks only inner loop. We can use Java break statement
in all types of loops such as for loop, while loop and do-while loop.
ANSWERS:
1. d) if-else loop. if-else statements are used for conditional execution,
not for looping.
2. b) To execute a block of code repeatedly.
3. a) True.
4. b) To terminate the loop immediately.
5. b) To skip the current iteration and move to the next one.
6. c) do-while loop.
7. b) A loop that continues executing forever, because the condition is
never met.
8. c) for loop.
9. b) To determine the number of iterations of the loop.
10. b) ++i increments the value before using it, while i++ increments the
value after using it.
ANSWER
1.Ans : C
Explanation: It will give compile time error since x is not declared.
2.Ans : A
Explanation: A C program can have same function name and same variable
name.
3. Ans : B
Explanation: In C, scanf returns the number of inputs it has successfully read.
4. Ans : B
Explanation: "" for a backspace, means if u print it, cursor will print and
come back 1 character . ' ' is used for carriage return to come 1 line back or in
start of line.
5. Ans : C
Explanation: In C, printf() returns the number of characters successfully
written on the output. Here letsfindcourse contain 14 character therefore i =
14 and then i is printed now again value of printf is assign but this time it
print 3 since it contain 3 character %,d and space.
6. Ans : B
Explanation: Firstly inner printf executes and will print hello now this return
the number of character printed that is 5 and this will be printed by outer
printf . Hence output is hello5
7. Ans : A
Explanation: printf statement will become printf("%s Find best course ", "%s
Find best course ");
8. Ans : B
Explanation: Name of the function is actually a pointer variable to the
function and prints the address of the function.
9. Ans : A
Explanation: Associativity of comma operator is from left to right, but =
operator has higher precedence than comma operator.Therefore the statement
i = 1, 2, 3 is treated as i = 1 .
10. Ans : B
Explanation: A segmentation fault occurs when a program attempts to access
a memory location that it is not allowed to access, or attempts to access a
memory location in a way that is not allowed.So as we can see segmentation
fault occurs because program attempts to access a memory location that it is
not allowed to access.
11. Ans : A
Explanation: None
12. Ans : B
Explanation: None
13. Ans : D
Explanation: Inner printf() will print first and then outer printf will display
the length of the inner printf's string.
14. Ans : C
Explanation: error: 'true' undeclared (first use in this function)
15. Ans : B
Explanation: The output is garbage value.
16. Ans : B
Explanation: 5.25 is neitheir a pure integer number(5) nor impure integer
number(5.00).
17. Ans : C
Explanation: As there is no declaration of variable a in printf function, a
standard C compiler printf some garbage value as an output. This program is
tested under Dev cpp and in standard online c compiler.
18. Ans : D
Explanation: In printf, initial address of *ptr is incremented by 2 , thus it
neglect He
19. Ans : B
Explanation: %p is a format specifier to print a pointer in printf or sprintf. For
64 bit c compiler, the output will be 1 0x1
20. Ans : C
Explanation: It will give compilation error.
21. Ans : C
Explanation: For instance, this compiler gives 4 as the answer.
22. Ans : C
Explanation: 4 is the output of the following code.
23. Ans : D
Explanation: 5 is the output of this program.
24. Ans : A
Explanation: << and >> operation applied on the value of "i" and we get the
output.
25. Ans : C
Explanation: << operation applied on the value of "i" and we get the output.
26. Ans : C
Explanation: The code will successfully compiled and give output 5 0 1
27. Ans : A
Explanation: The code will successfully compiled and give output 5 1 1.
28. Ans : A
Explanation: The code will successfully compiled and give output 5 7.
29. Ans : C
Explanation: The code will successfully compiled and give output 4.
30. Ans : B
Explanation: The code will successfully compiled and give output 1.
1. d) until loop
2. b) To repeat a block of code until a certain condition is met
3. c) Both before the first iteration and after each subsequent iteration
4. b) It terminates the loop entirely and continues with the next
statement after the loop.
5. b) It skips the current iteration and continues with the next one.
6. c) for loop
7. b) After each iteration
8. b) A loop that has no exit condition and will run forever (or until the
9. a) 0 1 2 3 4
10. a) for i in range(10)