Search Placementyogi.com
 

Tough MCQ on loops in C objective Questions

  :: Topics ::

1

What will be the output of the following program?

#include<stdio.h>

int main()
{
    int i=2,j=2;  
    while(i+1?--i:j++) 
       printf("i=%d j=%d",i,j);
    return 0;
}
A.

1,2

B.

1,2
0,2

C.

1,2
0,2
-1,2

D.

1,2
0,2
0,3

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

Consider the while condition: (i+1? --i:j++)

It makes use of ternary operator.

i=2, j=2 initially.

i+1 = 3 which being a Non Zero Value is TRUE.

So --i is executed and  i becomes 2-1 = 1. This is the actual value that decides whether the while condition is TRUE or FALSE.

since --i = 1 is a Non Zero Value , 

i=1 j=2 is printed.

In the Second Iteration

i=1 and j=2.

In the condition (i+1? --i:j++)

i+1 = 2 is TRUE and the value of i is 1 , --i =0 which is FALSE, 

So the while condition Fails and nothing is printed.

 

Hence, option A is the correct Answer. 

 
 
2

What will be the output of the following 2 programs?

#include<stdio.h>
void main()
{
        int i;
        for(i=1,printf("Intialization");printf("\nCondition"),i++ <= 5;printf("%d",i))
                printf("\nInside the loop\n");
}
Program 2:
#include<stdio.h>
void main()
{
        int i;
        for(i=1,printf("Initialization");i++ <= 5,printf("\nCondition");printf("%d",i))
                printf("\nInside the loop :");
}
A.

Program 1:

Initialization

Condition

Inside the loop:2

Condition

Inside the loop:3

Condition

Inside the loop:4

Condition

Inside the loop:5

Condition

Inside the loop:6

Program 2: 

Infinite Loop

B.

Program 1:

Infinite Loop

 

Program 2:

Initialization

Condition

Inside the loop:2

Condition

Inside the loop:3

Condition

Inside the loop:4

Condition

Inside the loop:5

Condition

Inside the loop:6

C.

Infinite Loop in both the programs

D.

Compilation Error

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

The condition has post-increment, Hence values printed are from 2 to 6.

First program has the i++ <= 5 at the end in condition section , Hence the loop terminates properly

after printing 6.

 

The Second program has printf at the end in condition section, which returns the number of characters printed.

In this case it is Non-Zero i.e. TRUE. So, The loop becomes Infinite.

 
3

What is the output of the following program?

#include<stdio.h>

void main()
{
		int i=1;
		for(;i++;)
			printf("%d",i);
}
A.

Infinite Loop.

B.

Compiler Dependent output, but no Infinite Loop.

C.

Compilation Error

D.

1 to 65535

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

The Initialization and increment section is blank here, but the condition section itself increments the value of i.

Initially, i =1 and i++ being post-increment is checked if it is Equal to 0 or not before incrementing the value.

The condition statement can be written as ( i++ != 0)

So 1++ in condition prints 2, 

2++ prints 3 and so on. 

at one point the value of i is 32767 and is incremented by 1 , it becomes -32768 as i is a signed integer.

The condition is still satisfied , i keeps on incrementing by 1 until it becomes equal to -1.

-1++ prints 0 and i becomes 0.

This time the condition FAILS and the control comes out of the loop.

If it is run on a 16 bit compiler, It will print :

2 3 4 5 ... 32767 -32768 -32767 ... -1 0.

 
4

What is the output of the following program?

#include<stdio.h>
int main()
{
	int i,j;
	i=j=2,3;       // line 5
	while(--i&&j++)
	printf("%d %d\n",i,j);
	printf("%d %d\n",i,j);
	return 0;
}
A.

1 3

0 3

B.

1 3

0 4

C.

2 4

1 5

0 5

D.

Compilation Error

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

 The below statement:

      i=j=2,3;

The precedence of assignment operator is higher than comma operator. Hence the statement becomes

i=j=2

In the While loop, --i is --2 = 1 which is a Non-Zero number and hence the j++ is also executed.

The print statement prints i=1(decremented value) and j=3(incremented value).

Loop round 2:

THe value of --i is 0, Now we know that in case of Logical AND operator if the first expression is FALSE, 

then short circuiting is done and the whole statement is taken as FALSE. Here j++ is not executed.

So the loop terminates.

Now the current value of i is 0 and j is still 3.

Hence, The final output is:

1 3

0 3

 

 
 

Banner to LogicGuns.com

Hide Page Information

Tough, Difficult, Tricky Loops in C objective questions with answer and explanation, questions on while loop ,do-while loop, infinite loop , continue, break, inside loop

Tough, Difficult, Tricky Objective Questions on Loops in C for placement written round and campus selection

This section contains Tough, Difficult, Tricky 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.