#include <stdio.h>
int main() {
int i = 0;
label1:
printf("%d ", i);
i++;
if (i < 3) {
goto label1;
}
return 0;
}
Correct Answer: 0 1 2
Explanation: The `goto label1;` statement creates a loop that prints the values of `i` from 0 to 2, then exits when `i` becomes 3.
Correct Answer: Yes
Explanation: A label can be defined after the `goto` statement in the code. The compiler allows forward declarations of labels.
#include <stdio.h>
int main() {
int num = 1;
goto label;
num = 2;
label:
printf("%d\n", num);
return 0;
}
Correct Answer: 1
Explanation: The `goto label;` statement transfers control to the `label:` statement, skipping the assignment `num = 2;`. Therefore, `num` remains 1 and is printed.
#include <stdio.h>
int main() {
int i = 0;
while (1) {
if (i == 5)
goto end;
printf("%d ", i);
i++;
}
end:
printf("End\n");
return 0;
}
Correct Answer: 0 1 2 3 4 End
Explanation: The loop runs indefinitely until `i` equals 5. When `i` becomes 5, control is transferred to the `end` label, printing "End".
#include <stdio.h>
int main() {
int x = 10;
if (x < 15) {
printf("x is less than 15\n");
goto end;
}
printf("x is greater than or equal to 15\n");
end:
printf("End of program\n");
return 0;
}
What will be the output if this program is executed?
Correct Answer: x is less than 15 End of program
Explanation: The condition `x < 15` is true, so the code inside the if block is executed, printing "x is less than 15". Then, the control jumps to the `end` label, printing "End of program".
#include <stdio.h>
int main() {
int i = 0;
while (i < 5) {
if (i == 3) {
goto end;
}
printf("%d ", i);
i++;
}
end:
printf("End\n");
return 0;
}
Correct Answer: 0 1 2 3 End
Explanation: The loop iterates until `i` equals 3. When `i` becomes 3, control is transferred to the `end` label, printing "End".
#include <stdio.h>
int main() {
int x = 1;
if (x) {
goto label;
}
x++;
label:
printf("%d\n", x);
return 0;
}
Correct Answer: 2
Explanation: The condition `if (x)` is true since `x` is non-zero. Control jumps to the `label` where `x` is incremented before being printed.
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
goto end;
}
printf("x is not greater than 5\n");
end:
printf("End of program\n");
return 0;
}
What will be the output if this program is executed?
Correct Answer: x is greater than 5 End of program
Explanation: The condition `x > 5` is true, so the code inside the if block is executed, printing "x is greater than 5". Then, control jumps to the `end` label, printing "End of program".
#include <stdio.h>
int main() {
int i = 0;
loop:
printf("%d ", i);
i++;
if (i < 3) {
goto loop;
}
return 0;
}
Correct Answer: 0 1 2
Explanation: The `goto loop;` statement creates a loop that prints the values of `i` from 0 to 2, then exits when `i` becomes 3.
#include <stdio.h>
int main() {
int num = 1;
if (num == 1) {
goto label;
}
num = 2;
label:
printf("%d\n", num);
return 0;
}
Correct Answer: 1
Explanation: The `goto label;` statement transfers control to the `label:` statement, skipping the assignment `num = 2;`. Therefore, `num` remains 1 and is printed.
#include <stdio.h>
int main() {
int x = 0;
while (x < 3) {
if (x == 2) {
goto end;
}
printf("%d ", x);
x++;
}
end:
printf("End\n");
return 0;
}
What will be the output if this program is executed?
Correct Answer: 0 1 2 End
Explanation: The `goto end;` statement transfers control to the `end:` label when `x` is 2, so 0, 1, 2, and "End" are printed.
#include <stdio.h>
int main() {
int a = 5;
int b = 10;
if (a < b) {
printf("a is less than b\n");
goto label;
}
printf("a is greater than or equal to b\n");
label:
printf("Label reached\n");
return 0;
}
Correct Answer: a is less than b Label reached
Explanation: Since `a` is less than `b`, the code inside the `if` block executes, printing "a is less than b" and then jumping to the `label`, where "Label reached" is printed.
#include <stdio.h>
int main() {
int i = 0;
label1:
printf("%d ", i);
i++;
if (i < 3) {
goto label1;
}
return 0;
}
Correct Answer: 0 1 2
Explanation: The `goto label1;` statement creates a loop that prints the values of `i` from 0 to 2, then exits when `i` becomes 3.
#include <stdio.h>
int main() {
int x = 1;
if (x) {
goto label;
}
x++;
label:
printf("%d\n", x);
return 0;
}
Correct Answer: 2
Explanation: Since `x` is non-zero, the control jumps to the `label` where `x` is incremented before being printed.
#include <stdio.h>
int main() {
int x = 10;
if (x > 5) {
printf("x is greater than 5\n");
goto end;
}
printf("x is not greater than 5\n");
end:
printf("End of program\n");
return 0;
}
What will be the output if this program is executed?
Correct Answer: x is greater than 5 End of program
Explanation: The condition `x > 5` is true, so the code inside the if block is executed, printing "x is greater than 5". Then, control jumps to the `end` label, printing "End of program".