This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1
Assume that p and q are non-zero positive integers, the following program segment will perform—
while(p!=0)
{
if(p>q)
p=p-q;
else
q=q-p;
printf(“%d”,p);
}Option D
The program is trying to reduce the numbers p and q by repeatedly subtracting the smaller one from the bigger one. This looks like the Euclidean method for finding the GCD, but there is a mistake in the loop condition. The loop only checks while(p != 0), so it keeps running as long as p is not zero.
When q becomes zero (which will always happen before p does), the subtraction stops changing the value of p because p = p - 0 will always give the same p. This means the program gets stuck, and the loop keeps running forever without ending.
So, instead of calculating the GCD, this program will go into an infinite loop. The correct way would be to stop when either p or q becomes zero, i.e., use while(p != 0 && q != 0).
Q: 2What will be the output of the following C code in GCC compiler?
#include<stdio.h>
int main()
{
int a = 5, i = 0;
for(;i<3;a++,i++){}
printf(“%d”,a++);
return 0;
}
Option D
The loop executes 3 times, incrementing both a and i in each iteration. After the loop ends, the value of a becomes 8. The statement printf("%d", a++); prints the current value of a, which is 8. Since it is a post-increment, a is increased to 9 after printing, but this updated value is not displayed.
Q: 3If we want to execute loop statement at least one time even if the condition false initially then we use which loop?
Option C
Loops are used to repeat a block of code. The main difference between loops is when the condition is checked.
In exit-control loop the body is executed at least once, even if the condition is false at the beginning.
Q: 4In C language the break statement can be used for?
Option D
In C language, the break statement is a control statement used to immediately stop execution of certain program blocks. It is mainly used to come out of a loop or a switch statement when a specific condition is met.
Q: 5Looping is a process in which set of statements are executed __________ for a finite or infinite number of times.
Option B
Looping is a process in which a set of statements are executed repeatedly for a finite or infinite number of times, until a specific condition is met.
This repetition allows programs to automate tasks and efficiently handle large amounts of data or repeated actions.
Q: 6What is the control flow graph for the following code fragment?
while (a>b)
{
b=b-1;
b=b*a;
}
c=a+b
Option A
Q: 7What is the output of the following C program?
#include<stdio.h>
void main()
{
int n;
for (n=9;n!=0;n--)
printf("%d ",n--);
}
Option C
The loop variable n is decremented twice in each iteration, once inside printf(n--) and once in the for loop (n--). When n reaches 1, it becomes 0 and then −1, so the condition n != 0 is always true afterward. Since n never becomes 0 again, the loop runs infinitely.
Q: 8Consider the code segment:
#include<stdio.h>
#include<unistd.h>
int main()
{
int i,n;
for(i=1;i<n;i++)
{
if(fork() == 0)
printf(“CHILD ”);
}
printf(“OS ”);
return 0;
}
Number of times CHILD and OS are printed?
Option C
Q: 9What will be the output of the following program segment?
int i=0;
int sum=0;
for(i=1;i<11;i++)
{
sum=sum+i;
}
printf(sum);
Option A
The program initializes sum to 0 and starts a for loop with i=1, where the condition i < 11 ensures that the loop runs from 1 to 10. During each iteration, the value of i is added to sum, gradually calculating the total sum of numbers from 1 to 10.
Iteration Process:
| Value of ‘i' | sum=sum+i | Updated sum |
|---|---|---|
| 1 | 0+1 | 1 |
| 2 | 1+2 | 3 |
| 3 | 3+3 | 6 |
| 4 | 6+4 | 10 |
| 5 | 10+5 | 15 |
| 6 | 15+6 | 21 |
| 7 | 21+7 | 28 |
| 8 | 28+8 | 36 |
| 9 | 36+9 | 45 |
| 10 | 45+10 | 55 |
After the loop ends, sum = 55. So, printf(sum); prints 55.
Q: 10In the following C program, how many times will the string "Suraku Academy" be printed?
#include <stdio.h>
void main()
{
int i=1024;
for (; i ; i>>=1)
printf("Suraku Academy");
}
Option B
In C, any non-zero value is treated as true in a loop condition. Here, i is initialized to 1024. The loop condition is simply i, so the loop runs as long as i is not zero.
In each iteration, i>>=1 performs a right shift, which divides i by 2. Thus, the values of i become 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, and 1. That makes 11 non-zero values, so "Suraku Academy" is printed 11 times. When i becomes 0, the loop terminates.
Q: 11Output of the given C code fragment is ____________.
int n=0;
while((n--)!=0) n++;
printf(“%d”,n);
Option B
In C language, the post-decrement operator (n--) first uses the current value of the variable and then decreases it by 1. Also, in a while loop, the condition is checked before each iteration.
Initially, n = 0. In the condition (n--) != 0, the current value of n (0) is first compared with 0, which results in false, and then n is decremented to -1. Since the condition is false, the loop body is not executed even once. Finally, printf("%d",n); prints the updated value of n, which is -1.
Q: 12Which of the following C statement is syntactically correct?
Option A
The syntax of for loop is for(initialization; condition; increment). All three expressions are optional, but the semicolons are mandatory.
The for(;;); is valid. All expressions are empty, but semicolons are present. This creates an infinite loop with an empty body.
Q: 13In a loop of C program, ____________ statement immediately exits a loop, skipping the rest of the body of the loop.
Option D
In C programming, the break statement is used to immediately exit a loop (for, while, or do-while), skipping the remaining statements in the loop body. When the break statement is encountered, control transfers to the statement immediately following the loop body.
for(int i = 1; i <= 8; i++) {
if(i == 4) {
break; // exits the loop when i equals 4
}
printf("%d ", i);
}
Output: 1 2 3
Q: 14Find the output of the given C code fragment.
for(i=2;i<20;i+=5);
printf(“%d”,i);
Option C
In C language, a semicolon (;) immediately after a loop makes it an empty loop, meaning the loop runs completely without executing any body.
| Step | Value of i | Condition i < 20 | Updation of i |
|---|---|---|---|
| Initial | 2 | 2 < 20 : True | i += 5 |
| 1 | 7 | 7 < 20 : True | i += 5 |
| 2 | 12 | 12 < 20 : True | i += 5 |
| 3 | 17 | 17 < 20 : True | i += 5 |
| 4 | 22 | 22 < 20 : False | Loop Terminates |
Q: 15Two loops in nesting form and we use break statement inside inner loop. When break statement encounter then
Option B
When loops are nested, it means one loop is written inside another loop. The break statement is used to terminate the loop in which it appears. Remember, break affects only the nearest loop.
In nested loops, when a break statement is encountered inside the inner loop, it exits only the inner loop, and control returns to the outer loop.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.