0% found this document useful (0 votes)
7 views18 pages

Computer 12 CH12MCQs

The document provides a comprehensive overview of loop constructs in C programming, including types of loops, their structure, and usage. It contains fill-in-the-blank questions, multiple-choice questions, true/false statements, and programming exercises related to loops. Additionally, it includes sample code snippets for various loop implementations.

Uploaded by

itxxali44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Computer 12 CH12MCQs

The document provides a comprehensive overview of loop constructs in C programming, including types of loops, their structure, and usage. It contains fill-in-the-blank questions, multiple-choice questions, true/false statements, and programming exercises related to loops. Additionally, it includes sample code snippets for various loop implementations.

Uploaded by

itxxali44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

[Ch#12] Loop Constructors 355 Computer Science Part-II

Q.1 Fill in the blanks:


1. There are three types of loops in C.
2. The loop condition controls the loop repetition.
3. In do while loop, first the body of loop is executed and the test condition is
checked.
4. nested loop means a loop within the body of another loop.
5. The goto statement performs an unconditional transfer of control to the named
label.
6. Repetition of statement in a program is called loop.
7. There are three expressions in for loop statement.
8. The body of while loop executes only if the specified condition is true.
9. Increase in the level of nesting increases the complexity of the nested loop.
10. A label is meaningful only to a goto statement.
Q.2 Choose the correct option:
1. How many types of loop structures are in C?
(a) 3 (b) 2
(c) 4 (d) 5
2.  is a loop statement:
(a) if (b) while
(c) if-else (d) switch
3. The loop which never ends is called:
(a) for loop (b) nested loop
(c) infinite loop (d) All
4.  loop structures are available in C language:
(a) do-while (b) while
(c) for (d) All of these
5.  is called counter loop:
(a) do-while (b) while
(c) for (d) Both (a) & (b)
[Ch#12] Loop Constructors 356 Computer Science Part-II
6. Which one is not a loop structure
(a) switch (b) for
(c) while (d) do-while
7. A variable whose value controls the number of iterations is known as _________.
(a) Variable (b) Control Variable
(c) Loop Variable (d) Loop Control Variable
8. In while loop, the loop control variable always initialized _________.
(a) Before the loop starts (b) Inside the loop body
(c) After the loop ends (d) Outside the program
9. In while loop, the increment / decrement statement are placed _________.
(a) Before the loop starts (b) Inside the loop body
(c) After the loop ends (d) Outside the program
10.  structure executes the body of loop at least once:
(a) do-while (b) while
(c) for (d) None
11. The body of loop comes after the test condition in:
(a) do-while (b) while
(c) for (d) Both (b) and (c)
12. The while loop is also called:
(a) conditional loop (b) wend loop
(c) counter loop (d) Attribute
13. The body of loop comes before the test condition in:
(a) do-while (b) while
(c) for (d) None
14. Semicolon is placed at the end of condition in:
(a) while loop (b) do-while loop
(c) switch (d) All of these
15. The do-while loop ends with a ____
(a) } (b) )
(c) , (d) ;
16. The _____ loop will execute at least even the condition is false
(a) while (b) do-while
(c) for (d) All of above
[Ch#12] Loop Constructors 357 Computer Science Part-II
17. The _____________ of for loop executed only once in the first iteration
(a) Loop condition (b) Increment / decrement
(c) Initialization expression (d) All of above
18.  is related to loop structures:
(a) Body of loop (b) Loop control variable
(c) Condition (d) All of these
19.  structure is used when programmer does not know is advance the
number of repetition of loop?
(a) do-while (b) for
(c) while (d) Both (a) and (c)
20. Loop with in a loop is called
(a) Loops (b) Multiple Loops
(c) Many Loops (d) Nested Loops
21. What is the final value of x when the code int x; for(x=0;x<10;x++){} is run
(a) 10 (b) 9
(c) 1 (d) 0
22. Examine the following code and tell output
int count=1; while(count<5){ printf(“%d”,count); }
(a) 1234 (b) 12345
(c) 111111….. (d) 234
23. Examine the following code and tell output
int count=-2; while(count<3){ printf(“%d”,count); count+ = 1;}
(a) -2-11234 (b) -2-1123
(c) -3-4-5-6-7 (d) -2-1012
24. One execution of the body of loop is called:
(a) iteration (b) duration
(c) cycle (d) Text
25. What will be the value of ‘x’ after executing for(x=1 ; x<15;x++); ?
(a) 14 (b) 1
(c) 16 (d) 15
26. How many times does the loop get iterated?
for(i=0;i=10;i+=2) printf(“Hi\\n”);
(a) 10 (b) 2
(c) 5 (d) None of Above
[Ch#12] Loop Constructors 358 Computer Science Part-II
27. In a group of nested loops, which loop is executed the most number of times?
(a) The outermost loop (b) The innermost loop
(c) All loops at the same number of times (d) Cannot be determined
28. What will be value of c after the execution of following statements?
c=-8; do{c++;}while(c>=5);
(a) -8 (b) -6
(c) -7 (d) -9
29.  is used to move the control to the start of loop body:
(a) continue (b) break
(c) switch (d) None
30.  can be used to terminate the loop::
(a) terminate (b) break
(c) stop (d) exit
31. A special value that terminates the loop is called:
(a) terminate value (b) sentinel value
(c) control value (d) end value
32. The for loop contains three expressions: initialization, test, and:
(a) Character (b) Float
(c) increment/decrement (d) All
33. What will be the output of following code?
int x=5,c=0; If(x%2==1){for(;c<=5;c++) printf(“%d”,c);}
(a) 0123456 (b) 012345
(c) 6 (d) Error Message
34. Which for loop will counts from 0 to 5?
(a) for(int c=0; c<=6;c++) (b) for(c=0; c<5; c++);
(c) for(c=0; c<=5; c++) (d) for(int c=0; c<7; c++)
35. What will be the value of ‘x’ after executing the following code?
int x = 5;
while (++x > 5)
break;
printf(“%d”,x);

(a) 5 (b) 6
(c) 0 (d) None
[Ch#12] Loop Constructors 359 Computer Science Part-II
36. What will be the output of the following code segment?
int n = 5;
while (n > 5)
n * = 10;
printf(“%d”, n );
(a) 5 (b) 10
(c) 50 (d) 0
37. What will be the output of the following code segment?
int m = 10, n = 100;
do
{
m = m + (n ++);
m ++ ;
}while (m < 10);
printf(“%d”, m );
(a) 10 (b) 110
(c) 111 (d) 112
38. What will be the output of the following code segment?
int x = 0;
for (x = 100; x = = 200; x ++);
printf(“%d”, x );
(a) 101 (b) 100
(c) 201 (d) 200
39. What will be the output of the following code segment?
int n = 0
for (;;)
{
if (n = = 10) break;
n++;
}
printf(“%d”, n );
(a) 0 (b) 10
[Ch#12] Loop Constructors 360 Computer Science Part-II
(c) 11 (d) 12
40. What is the final value of x when the code intx; for(x=0; x<10; x++) is run?
(a) 10 (b) 9
(c) 0 (d) 1
Q.3 Write T for true and F for false statements.
1. There is no difference between while and do-while loop. (F)
2. The body of while loop may or may not execute. (T)
3. The do-while loop always executes at least once. (T)
4. The var++ is an example of prefix increment operator. (F)
5. The condition of an infinite loop never becomes true. (F)
6. Initialization expression is option in for loop. (T)
7. The for(i = 1; i < = 10; i++); is an infinite loop. (F)
8. Loop is a decision making construct. (F)
9. A while loop can not be used in the body of a for loop. (F)
10. In type casting, a variable of one type behaves as the variable of (T)
another type temporarily.
Programs using loops:
Q4. To print and find the sum of 10 natural numbers (1+2+3+ -----) using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n,sm=0;
for(n=1;n<=10;n++)
{
printf(“\n %d”,n);
sm=sm+n;
}
printf(“\n The sum of 10 natural numbers = %d”,sm);
}
Q5. Print and find the sum of 10 numbers which are multiple of 5 (5+10+15+ -----
--) using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n, sm=0;
for(n=5;n<=50;n+=5)
[Ch#12] Loop Constructors 361 Computer Science Part-II
{
printf(“\n %d”,n);
sm=sm+n;
}
printf(“\n The sum of numbers = %d”, sm);
}
Q6. Program: Print and find the sum of 10 natural numbers (1+2+3+ ---------)
Using While loop
Answer:
#include<stdio.h>
void main ( )
{
int n=1,sm=0;
while(n<=10)
{
printf(“\n %d”,n);
sm=sm+n;
n++;
}
printf(“\n The sum of 10 natural numbers = %d”,sm);
}
Q7. Print and find the sum of 15 even numbers (2+4+6+ -----) using do-while loop
Answer:
#include<stdio.h>
void main ( )
{
int n=2,sm=0;
do
{
printf(“\n %d”,n);
sm=sm+n;
n+=2;
}
while(n<=30);
printf(“\n The sum of 15 even numbers = %d”,sm);
[Ch#12] Loop Constructors 362 Computer Science Part-II
}
Q8. Print the table of any number using for loop
Answer:
#include<stdio.h>
void main ( )
{
int n,c;
printf(“\n Enter table Number “);
scanf(“%d”, &n);
for(c=1;c<=10;c++)
{
printf(“\n %d x %d = %d”, n, c , n*c);
}
}
Q9. Print the table of any number using while loop
Answer:
#include<stdio.h>
void main ( )
{
int n,c;
printf(“\n Enter table Number “);
scanf(“%d”, &n);
c=1;
while(c<=10)
{
printf(“\n %d x %d = %d”, n, c ,n*c);
c++;
}
}
Q10. Print the Table using do-while loop
Answer:
#include<stdio.h>
void main ( )
{
int n, c;
[Ch#12] Loop Constructors 363 Computer Science Part-II
printf(“\n Enter table Number “);
[Ch#12] Loop Constructors 364 Computer Science Part-II

scanf(“%d”, &n);
c=1;
do
{
printf(“\n %d x %d = %d”, n , c, n*c);
c++;
}
while(c<=10);
}
Q11. Print the factorial of a number using do while
Answer:
#include<stdio.h>
void main ( )
{
int n,c=1;
printf(“\n Enter any Number 0 to 7 “);
scanf(“%d”, &n);
do
{
c=c*n;
n--;
}
while(n>=1);
printf(“\n The Factorial of given number = %d “, c);
}
Q12. Print the factorial of a number using for loop
Answer:
#include<stdio.h>
void main ( )
{
[Ch#12] Loop Constructors 365 Computer Science Part-II

int n,c=1,k;
printf(“\n Enter any Number 0 to 7 “);
scanf(“%d”, &n);
for(k=n;k>=1;k--)
{
c=c*k;
}
printf(“\n The Factorial of given number = %d “, c);
}
Q13. Trace the output of the program:
Answer:
Program Output ( � denotes blank spaces)
k=0; loop iteration = 6
while(k<=5) ( k=0,1,2,3,4,5)
{ k=0 ; output � �0 � 10
printf(“%3d%3d\n”, k , 10-k); k=1 ; output � �1 � �9
k++; k=2 ; output � �2 � �8
}
k=3 ; output � �3 � �7
k=4 ; output � �4 � �6
k=5 ; output � �5 � �5
Q14. Trace the output of the program
Answer:
Program Output ( _ denotes blank spaces)
j=10; loop iteration = 5
for(int i=1;i<=5; + + i) (i=,1,2,3,4,5)
{ i=1 ; output 1 _ 10
printf(“%d %d\n”, i, j); i=2 ; output 2 _ 8
j - = 2; i=3 ; output 3 _ 6
} i=4 ; output 4 _ 4
i=5 ; output 5 _ 2
[Ch#12] Loop Constructors 366 Computer Science Part-II
Q15. Trace the output of the program assuming m=3,n=5
Answer:
Program Output
for(k=1;k<=n; ++k) *
{ **
for(j=0; j<k; ++j) ***
{ ****
printf(“*”); *****
}
printf(“\n”);
}
Q16. Trace the output of the program assuming m=3,n=5
Answer:
Program Output
for(k=n;k>=0; --k) ***
{ ***
for(j=m; j>0; --j) ***
{ ***
printf(“*”); ***
}
printf(“\n”);
}
Q17. Correct the following code according to the instructions: Insert braces where
they are needed and correct errors if any. The corrected code should accept
five integers and should display their sum.
Answer:
incorrect code correct code
count = 0; int count=0, sum=0, next_num;
while(count<=5); count = 0;
count += 1; while(count<=5)
printf(“next number >”); {
scanf(“%d”, &next_num); count += 1;
next_num += sum; printf(“next number >”);
printf(“%d numbers were added; \n”); scanf(“%d”, &next_num);
printf(“their sum is %d. \n”, sum); sum += next_num;
}
printf(“%d numbers were added; \n”,
count);
[Ch#12] Loop Constructors 367 Computer Science Part-II
printf(“their sum is %d. \n”, sum);
Q18. Rewrite the following code segment using do-while loop.
Answer:
Code with do-while statement
sum=0; int sum=0,odd=1;
for(odd = 1; odd<n; odd = odd+2) do
sum = sum + odd; {
printf(“sum of the positive odd numbers less sum = sum + odd;
than %d is %d\n”,n, sum); odd = odd+2;
}
while(odd<=n);
printf(“sum of the positive odd numbers less
than %d is %d\n”,n, sum);
Q19. Prime Number
Answer:
#include<stdio.h>
void main ( )
{
int n, k;
printf(“\n Enter any Number :>>>> “);
scanf(“%d”, &n);
k=2;
while (k<=n-1)
{
if(n%k==0)
{
printf(“\n %d is not A Prime Number “, n);
break;
}
k++;
}
if (k == n)
[Ch#12] Loop Constructors 368 Computer Science Part-II
printf(“\n The Number is prime = %d “, n);
}
Q20. Print first 10 Natural Numbers, their squares and their cubes and calculate
the sum of these three series using for loop.
S1=1+2+3+-----+10, S2=12+22+32+-----+102, S3=13+23+33+-----+103
Answer:
#include<stdio.h>
void main( )
{
int i, s1=0,s2=0,s3=0;
for(i=1;i<=10;i++)
{
printf(“\n %d \t %d \t %d”,i,i*i,i*i*i);
s1=s1+I;
s2 = s2 + (i*i);
s3 = s2 + (i*i*i);
}
printf(“\ns1= %d \t s2=%d \t s3=%d”, s1,s2,s3);
}
Q21. Write a program that produces the following output
0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
Answer:
#include<stdio.h>
void main ( )
{
int i, j;
for (j=0;j<=5;j++)
{
for(i=0;i<=j;i++)
printf(“%d\t”,i);
[Ch#12] Loop Constructors 369 Computer Science Part-II
printf(“\n”);
}
}
Q22. Write a program that produces the following output
0 1
1 2
2 4
3 8
4 16
5 32
6 64
Answer:
#include<stdio.h>
#include<math.h>
void main ( )
{
float j;
for (j=0;j<=6;j++)
printf(“\n%4.0f\t\t%4.0f”, j, pow(2,j) );
}
OR
#include<stdio.h>
void main ( )
{
int j, i = 1;
for (j=0;j<=6;j++)
{
printf(“\n%d\t%d”, j, i);
i *= 2;
}
}
Q23. Input and output of an array using scanf( )
Answer:
#include<stdio.h>
void main( )
{
int a[5],i;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
[Ch#12] Loop Constructors 370 Computer Science Part-II
}
for(i=0;i<=4;i++)
printf(“\n Array Element at %d index = %d ”,i, a[i]);
}
Q24. Input array elements and calculate sum
Answer:
#include<stdio.h>
void main( )
{
int a[5],i, s=0;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
s += a[i];
}
printf(“\n Sum of all Array Elements = %d ”, s);
}
Q25. Input array elements and sorting of an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, temp, j;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element ”);
scanf(“%d”, &a[i]);
}
for(i=1;i<=4;i++)
for(j=0;j<=3;j++)
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
[Ch#12] Loop Constructors 371 Computer Science Part-II
}
for(i=0;i<=4;i++)
printf(“\n Array Elements in order are : %d”, a[i]);
}
Q26. Input array elements and find largest number in an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, max;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element”);
scanf(“%d”, &a[i]);
}
max = a[0];
for(i=1;i<=4;i++)
if(a[i]>max)
max=a[i];
printf(“\n Largest Number in Array Elements = %d ”, max);
}
Q27. Input array elements and find smallest number in an array.
Answer:
#include<stdio.h>
void main( )
{
int a[5], i, min;
for(i=0;i<=4;i++)
{
printf(“\n Enter Array Element”);
scanf(“%d”, &a[i]);
}
min=a[0];
for(i=1;i<=4;i++)
if(a[i]<min)
min=a[i];
[Ch#12] Loop Constructors 372 Computer Science Part-II
printf(“\n Smallest Number in Array Elements = %d ”, min);
}

You might also like