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.
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.