Search Placementyogi.com
 

Loops in C objective Questions with answer

  :: Topics ::

1

What is the output of the following program?

#include<stdio.h>

void main()
{
	int i=1,j=1;
	while (++i < 10)
		printf("%d ",i);
		printf("\n");
	while (j++ < 10)
		printf("%d ",j);
}
A.

1 2 3 4 5 6 7 8 9 10

1 2 3 4 5 6 7 8 9 10

B.

1 2 3 4 5 6 7 8 9 

1 2 3 4 5 6 7 8 9 10

C.

2 3 4 5 6 7 8 9

2 3 4 5 6 7 8 9 10

D.

2 3 4 5 6 7 8 9 

1 2 3 4 5 6 7 8 9 10

See Answer & Explanation Lets Discuss
Correct answer is : C
Explanation

 

We know that in a pre-increment operator the value is incremented first and then used, while 

in post-increment operator the current value is used first and then incremented.

Now,

In the first loop,

The incremented value is compared with 10 and is printed.

initially i is 1, the control goes to the condition ( ++i < 10) in while loop, the value of i is incremented and i becomes 2 and 2< 10 is TRUE, 

So, the incremented value 2 is printed. Please Note that 1 is not printed here as the value of i has already become 2 at the time of printing.

Similarly, 

When the value of i is 2 , 3 is printed.

value of i     printed value

        1-----------2

        2------------3

        3------------4

        4------------5

        .  ------------ .

        .  ------------ .

        8------------9

        9------------The loop exits as ++i becomes 10.

 

So, 10 is not printed.

 

In the Second loop,

The current value of j is compared with 10 and the incremented value is printed.

 

So, initially j=1 and  j<10 is TRUE , Hence the printf statement is executed, by this time the value of j has become 2.

Hence 2 is printed.

value of j     printed value

        1------------2

        2------------3

        3------------4

        4------------5

        .  ----------  .

        .  ----------  . 

        8------------9

        9------------10

        10------------ Loop Exits,

 

Since ++j is 11 which is greater than 10, the condition fails and the control comes out of the loop.

 

NOTE: The statement printf("\n"); is not inside the while loop.

 
 
2

What is the output of the following program?

void main()
{
	int c=1;
	while(c <100)
		printf("%d",c);
		c++;
}
A.

prints the numbers from 1 to 99.

B.

Infinite Loop

C.

Compilation Error

D.

prints the numbers from 1 to 100

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

 

Inside the body of  while loop, as there is no parenthesis ,there is only one statement i.e. the printf statement.

The statement c++ lies outside the  while loop and is never executed.

Correct program would be: 

while(c <100)
	{
		printf("%d ",c);
		c++;
	}


output: 1 2 3 4 . . . 99

 

If the c++ is placed before printf statement as shown below:

while(c <100)
	{
		c++;
		printf("%d ",c);	
	}

The output will be: 2 3 4 . . . 99 100

Because, when c is 1 , it increments value of c to 2 and then prints 2, and so on .

when c becomes 98 -> 99 is printed and when c is 99 -> 100 is printed. 

c is now 100 and  the control comes out of the loop

 

 
3

What is the output of the following program?

void main()
{
	char c=1;
	while(c <=128)
	{
		printf("%d -> %c",c,c);
		c++;
	}
}
A.

prints the ASCII table.

B.

Infinite Loop

C.

Compilation Error

D.

None of the Above.

See Answer & Explanation Lets Discuss
Correct answer is : B
Explanation

Remember c is of 1 byte i.e. 8 bits.

The maximum value we can have with 8 bits is 2^8 -1 = 255 i.e. 0 to 255.

But since a character variable has sign too the maximum value is 2^7 -1 = 127

The range of a character variable is  0 to 127 and then the next value becomes -128 instead of 128. 

i.e. 0 1 . . . 127 -128 -127 . . . -1 .

Now, in the given program the value of 'c' goes till 127 and printf statement is executed for c=127.

The c is incremented and it becomes 127 + 1 = -128 which is again less than 128 ! Damn!! Damn!!

This keeps on happening every time  and results in an infinite loop.

 
4

What is the output of the following program?

#include<stdio.h>
void main()
{
        int i=0;
        while(i <10);             // line 5
        {
                printf("%d ",i);  // line 7
                i++;              // line 8
        }
}
A.

1 2 3 4 5 6 7 8 9

B.

0 1 2 3 4 5 6 7 8 9

C.

Compilation Error

D.

Infinite Loop

See Answer & Explanation Lets Discuss
Correct answer is : D
Explanation

Notice the semi-colon(;) at the end of while statement in line 5.

while(i <10);

The while loop has no statement to execute when the condition is TRUE so i is always 0 

and the while loop runs infinitely.

Please Note : The line 7 and line 8 are never executed.

The Question might look very simple , but keeping it in mind will avoid unnecessary waste of time while 

you are actually programming.

 
5

What is the output of the following program?

void main()
{
	int i;
	for(i=1;i++ <= 5;printf("%d \n",i))
	printf("Inside the loop : ");
}
A.

Inside the loop: 2

Inside the loop: 3

Inside the loop: 4

Inside the loop: 5

Inside the loop: 6

 
B.

Inside the loop: 1

Inside the loop: 2

Inside the loop: 3

Inside the loop: 4

Inside the loop: 5

 
C.

2 Inside the loop:

3 Inside the loop:

4 Inside the loop: 

5 Inside the loop:

6 Inside the loop:

 
D.

1 Inside the loop:

2 Inside the loop:

3 Inside the loop:

4 Inside the loop: 

5 Inside the loop:

 
See Answer & Explanation Lets Discuss
Correct answer is : A
Explanation

Step 1. At first i is initialized to 1.

Step 2: Now the condition i++ < 5 is checked, Here i is currrently 1 and the value is incremented only after use.

So,The condition is TRUE and the printf statement "Inside the loop" is executed.

Step 3: The control then enters the increment section of for loop, it contains -> printf("%d \n",i).

        The value of i had been incremented earlier in the condition section and hence 2 is printed.

Thus, when i = 2 next time and condition i++ <=5 is TRUE, 3 is printed.

This goes on till last time when i=5 as i++ would still be less than and equal to 5 and 6 is printed.

Next time i is 6 and 6++ <=5 is FALSE and the loop exits.

 
 
 

Banner to LogicGuns.com

Hide Page Information

Loop control in C objective questions with answer and explanation, questions on while loop ,do-while loop, infinite loop , continue, break, inside loop

Objective Questions on Loops in C for placement written round and campus selection

This section contains objective questions and explanations on loop structures in C. The explanations are written in detail to help freshers and experienced graduates to prepare for written round of placement.

How to practice Objective Questions on for loop in C for placement written round and campus selection

Practice the objective questions on loops in C

Where can I get objective Questions on loops in C?

placementyogi provides a range of objective questions on loop in C like for loop,while loop, do while loop in C that will help candidates in getting IT jobs .

How to solve C Loop Control Structures problems?

You can easily solve all kind of questions based on C loop Control Structures by practicing the exercises (including shortcut methods to solve problems) given below.