C Loop Control Structures Set 1

Ques: 1 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int a=2,b=2;
    while(a+3?--a:b++)
   	printf("%d %d/n",a,b);
    return 0;
}
A. 1 2
B. 0 2
C. infinite loop
D. 2 2
Description
Answer: A
first iteration: a+3=5 which is true, --a=1 hence while(1), a=1 b=2 gets printed. Next iteration: a+3=1+3=4 which is true, hence --a=0, while(0) and loop gets terminated.

Ques: 2 What will be the output of the following program?
#include<stdio.h>
extern int x;
int main(){
    do{
        do{
            printf("%x",x);
        }
        while(!-2);
    }
    while(0);
    return 0;
}
int x=23;
A. 17
B. Error
C. true 11
D. 14
Description
Answer: A
Here variable x is extern type. So it will search the definition of variable x which is present at the end of the code. So x =23. There are two do-while loops in the above code. do-while executes at least one time even if the condition is false. So program control reaches at printf statement and prints hexadecimal 17 which is equal to decimal number 23.

Note: %x is used to print the number in hexadecimal format.

In inner do- while loop while condition is ! -2 = 0

In C zero means false. Hence program control comes out of the inner do-while loop. In outer do-while loop while condition is 0. That is again false. So program control will also come out of the outer do-while loop.


Ques: 3 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int x=022,i;
    for(i=0;i<x;i+=5){
        printf("geek ");
        continue;
        printf("bix");
    }
    return 0;
}
A. geek geek geek bix
B. geekbix
C. geek geek bix
D. geek geek geek geek
Description
Answer: D
022 is octal number. Its equivalent decimal value is 18.

So, x = 18

Program control reaches for loop

first iteration i<18

geek gets printed due to continue statement program again goes to for loop and i increses by 5,

second iteration: i=5<18

geek gets printed and code works so on.


Ques: 4 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i,j;
    i=j=2,4;
    while(--i&&j++)
        printf("%d %d",i,j);
    return 0;
}
A. 2 4
B. 1 4
C. 1 3
D. 1 5
Description
Answer: C
consider expression: i=j=2,4 assignment operator has higher priority than comma operator hence i and j gets value 2

Ques: 5 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i,j;
    i=j=2,4;
    while(--i++&&j++)
        printf("%d %d",i,j);
    return 0;
}
A. infinite loop
B. 2 3
C. Error
D. 1 3
Description
Answer: C
--i++ results into --(2) which flashes lvalue required error.

Note that postfix increament has higher priority than prefix decreament


Ques: 6 What will be the output of the following program?
#include<stdio.h>
int main()
{
    static int j;
    for(++j;++j;j++) {
        printf("%d ",j);
        if(j==4) 
        break;
    }
    return 0;
}
A. 3 4
B. Garbage Garbage
C. 2 3
D. 2 4
Description
Answer: D

Ques: 7 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=1;
    for(i=0;i=-1;i=1) {
        printf("%d ",i);
        if(i!=1) break;
    }
    return 0;
}
A. infinite loop
B. 0
C. -1
D. Compile time error
Description
Answer: C
Initial value of i=1

First iteration:

For loop initial value: i = 0

For loop condition: i = -1 . Since -1 is non- zero number. So loop condition true. Hence printf function will print value of variable i i.e. -1

Since variable i is not equal to 1. So, condition is true. Due to break keyword program control will come out of the for loop.


Ques: 8 What will be the output of the following program?
#include<stdio.h>
int main()
{
    char j=1;
    while(j<=255)
    {
        printf("%d",j);
        j++;
    }
    return 0;
}
A. 1234.....255
B. 1234.....127
C. 1234.....128
D. infinite loop
Description
Answer: D
here the culprit is range of char data type

Ques: 9 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=1;
    while(i++<=1)
    while(i++<=2)
    printf("%d",i);
    return 0;
}
A. infinite loop
B. 3
C. 3 6
D. Error
Description
Answer: B

Ques: 10 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=1;
    while(i++<=1);
    while(i++<=2);
    printf("i=%d",i);
    system("pause");
    return 0;
}
A. 1
B. 2
C. 3
D. 4
Description
Answer: D
Note that every loop is terminated but it will check for condition twice

Ques: 11 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=2;
    do
    while(i++<=2);
    printf(“i=%d�,i);
    return 0;
}
A. 3
B. 4
C. 5
D. Error
Description
Answer: D
 The above code shows error because compiler reads the program as
do
{
while(i++<=2)
}
printf("%d",i);

and every do must have a while which is absent here.


Ques: 12 What will be the output of the following program?
#include<stdio.h>
int main()
{
    int i=2;
    do;
    while(i++<=2);
    printf("%d",i);
    return 0;
}
A. 3
B. 4
C. 5
D. Error
Description
Answer: B


comments powered by Disqus