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);
} Subtract small number from large number
Computer GCD of the given numbers
Computer LCM of the given numbers
The loop will run infinitely
[ 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: 2 What 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;
}
5
6
7
8
[ 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: 3 If we want to execute loop statement at least one time even if the condition false initially then we use which loop?
for
while
do while
It is not possible
[ 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: 4 In C language the break statement can be used for?
Exit from function.
Exit from if statement.
Exit from file.
Exit from loop.
[ 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: 5 Looping is a process in which set of statements are executed __________ for a finite or infinite number of times.
Commonly
Repeatedly
Mutually
Theoretically
[ 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: 6 Consider 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?
2^n and 2^n
2^(n-1) and 2^(n-1)
2^(n-1)-1 and 2^(n-1)
2^n and 2^(n-1)
[ Option C ]
Q: 7 Which of the following C statement is syntactically correct?
for(;;);
for(;);
for();
for(,);
[ 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: 8 In a loop of C program, ____________ statement immediately exits a loop, skipping the rest of the body of the loop.
if
else
continue
break
[ 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: 9 Two loops in nesting form and we use break statement inside inner loop. When break statement encounter then
Exit from outer loop only
Exit from inner loop only
Exit from both inner and outer loop
Exit from program
[ 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.
Q: 10 What will be the output of following code?
int main()
{
int i = 1;
printf(“%d”, i);
for(i = 0; i < 3; i++)
if (i == 1)
printf(“%d”, i);
return 0;
}
13
11
1
112
[ Option B ]
Q: 11 Complete the following loop to print even numbers from 2 to 10:
#include<stdio.h>
int main()
{
int i;
for(i=2;i<=10;____)
printf(“%d”,i);
return 0;
}
i+=1
i+=2
i++
i+2
[ Option B ]
The loop starts with i = 2 and should print even numbers up to 10. To achieve this, we need to increase i by 2 in every iteration. Hence, using i+=2 ensures the loop prints 2 4 6 8 10.
Q: 12 What will be the output of following code?
int main()
{
int i = 1;
printf(“%d”, i);
for(i = 0; i < 3; i++);
if (i == 1)
printf(“%d”, i);
return 0;
}
13
11
1
112
[ Option C ]
Q: 13 To exit from infinite loop use
(1) continue statement.
(2) goto statement.
(3) break statement.
(4) return statement.
(1) (2) (3) (4) are correct
(2) (3) (4) are correct
(1) (3) (4) are correct
(3) (4) are correct
[ Option B ]
An infinite loop can be exited using break, goto, or return, but not by using continue, since continue keeps the loop running.
Q: 14 In _________ control loop the test condition is checked before the executing the body of loop.
Exit control loop
Entry control loop
do while loop
Both (a) and (b)
[ Option B ]
The loops are used to repeat a set of statements multiple times. In loops, the test condition can be checked either before or after executing the loop’s body.
When the condition is checked before the loop body runs, it is called an entry-control loop. In this type of loop, the statements inside the loop will execute only if the condition is true. If the condition is false, the loop body will not run even once. The while and for loop are example of entry-control loop.
In an exit-control loop, the loop body runs first, and the condition is checked after execution. So, the loop runs at least once. The do-while loop is an example of exit-control loop.
Q: 15 Which of the following statement can only use inside any loop is?
goto
continue
break
case
[ Option B ]
In C language, some statements are designed to work only within loops. These statements control the execution of loop iterations.
The continue is used only inside loops. It skips the remaining statements of the current iteration and moves to the next iteration.
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.