c for Looping MCQ
Q1) What is the correct syntax of for loop in C?
- for(initalization; condition; incrementoperation)
{
//statements
} - for(declaration; condition; incrementoperation)
{
//statements
} - for(declaration; incrementoperation; condition)
{
//statements
} - for(initalization;condition;incrementoperation;)
{
//statements
}
Answer:- (A).
Explanations :None
Explanations :None
- for (i = 0; i < n; i++)
for (j = 0; j < n; j += 5) - for (i = 0, j = 0; i < n, j < n; i++, j += 5)
- for (i = 0; i < n;i++){ }
for (j = 0; j < n;j += 5){ } - None of these
Answer:- (B).
Explanations : None.
Explanations : None.
#includeint main() { int i = 0; for (i) { printf("Hello"); } return 0; }
- Know Program
- Know Program is printed infinite times.
- Compiled Successfully, No Output
- Compile-time error
Answer:- (D).
Explanations : We must use the semicolon if we are not passing all three values in the FOR loop. The valid syntax will be:- for (i;;)
Explanations : We must use the semicolon if we are not passing all three values in the FOR loop. The valid syntax will be:- for (i;;)
#includeint main() { int i; for (i=0; i<3; i++); { printf("%d", i); } return 0; }
- 0123
- 01234
- 4
- 3
Answer:- (D).
Explanations : Notice a semicolon is at the end of the for loop. The printf(“%d”, i); is not part of the for loop, it is outside of the loop therefore it will be executed after executing the for loop.
Explanations : Notice a semicolon is at the end of the for loop. The printf(“%d”, i); is not part of the for loop, it is outside of the loop therefore it will be executed after executing the for loop.
#includeint main() { int i; for (i=0; i<=3; i++); { printf("%d", i); } return 0; }
- 0123
- 01234
- 4
- 3
Answer:- (C).
Explanations : It is similar to the previous program but this time for loop will be executed 4 times, and value of “i” becomes 4.
Explanations : It is similar to the previous program but this time for loop will be executed 4 times, and value of “i” becomes 4.
#includeint main() { int i; for (i=5; i>=2; i--); { printf("%d", i); } return 0; }
- 54321
- 5432
- 1
- 2
Answer:- (C).
Explanations :None
Explanations :None
#includeint main() { int i; for (;;) { printf("Know Program"); break; } return 0; }
- Know Program
- Know Program is printed infinite times.
- Compiled Successfully, No Output
- Compile-time error
Answer:- (A).
Explanations :NIn the first iteration, break statement will be executed, which terminates the execution of the loop.
Explanations :NIn the first iteration, break statement will be executed, which terminates the execution of the loop.
#includeint main() { for (;;) { printf("Know Program"); } }
- Know Program
- Know Program is printed infinite times.
- Compiled Successfully, No Output
- Compile-time error
Answer:- (B).
Explanations : This syntax of for loop is an example of infinite loop.
Explanations : This syntax of for loop is an example of infinite loop.
#includeint main() { int i; for (printf("Hello "); printf("Know "); printf("Program ")) { break; } return 0; }
- Hello
- Hello Know
- Compiled Successfully, No Output
- Compile-time error
Answer:- (B).
Explanations :For loop execution process:- initialization, condition checking, statement execution, update. In initialization “Hello” will be printed, in condition checking “Know” will be printed, in the statement execution break will be executed which terminates the loop, and control came out of the loop.
Explanations :For loop execution process:- initialization, condition checking, statement execution, update. In initialization “Hello” will be printed, in condition checking “Know” will be printed, in the statement execution break will be executed which terminates the loop, and control came out of the loop.
#includeint main() { int i = 500; for (; i; i >>=2) { printf("KnowProgram "); } return 0; }
- 2
- 5
- 500
- infinite times.
Answer:- (B).
Explanations :The expression set >>= 2; means set = set >> 2; that is right shift bits of set by 2 (self assigned form of >> bitwise right shift operator. Therefore on each iteration “i” will be divided by 4.
Explanations :The expression set >>= 2; means set = set >> 2; that is right shift bits of set by 2 (self assigned form of >> bitwise right shift operator. Therefore on each iteration “i” will be divided by 4.
Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.