Output of C programs | Set 35 (Loops)

Short question based on c loops

1. What will be the output of the following code?

#include <stdio.h>
int main()
{
	int i = 0, j = 0;
	while (i<5,j<10)
	{
		i++;
		j++;
	}
	printf("%d %d", i, j);
}

options :
a) 5 5
b) syntax error
c) 5 10
d) 10 10

Answer: d

Explanation : Here “, ” is equal to “OR” condition. If either of condition will be true the loop will execute.

2. What will be the output of the following code?


#include <stdio.h>
int main()
{
    int i = 0, j = 0;
    while (i<5 & j<10)
    {
        i++;
        j++;
    }
    printf("%d %d", i, j);
}

options :
a) 5 5
b) syntax error
c) 0 0
d) 10 10

Answer: a

Explanation : The loop will execute only if both the conditions will be true.

3. What will be the output of the following code?


#include <stdio.h>

int main()
{
    short i;
    for (i = 1; i> 0; i++)
        printf("%d\n", i);
}

options :
a) The control won’t fall into the for loop
b) Numbers will be displayed until the signed limit of short and throw a run time error
c) Numbers will be displayed until the signed limit of short and program will successfully terminate
d) This program will get into an infinite loop and keep printing numbers with no errors

Answer: c

Explanation : It will display all the elements less than 32768 as the range of short is till 32767.

4. What will be the output of the following code?


#include <stdio.h>

void main()
{
    int i = 0, j = 0;
    for (i = 0; i < 5; i++) 
    {
        for (j = 0; j < 1;)
        {
            break;
        }
        printf("GeeksQuiz \n");
    }
}

options :
a) GeeksQuiz is printed 5 times
b) GeeksQuiz is printed 9 times
c) GeeksQuiz is printed 7 times
d) GeeksQuiz is printed 4 times

Answer : a

Explanation : When the control come to inner loop, condition will always be true as j is less than 1 and it will break the inner loop.

5. What will be the output of the following code?


#include <stdio.h>

void main()
{
    double k = 0;
    for (k = 0.0; k < 3.0; k++);
    printf("%lf", k);
}

options :
a)0.000000 1.000000 2.000000
b)2.000000
c)Compile time error
d)3.000000

Answer : d

Explanation : Printf is a separate instruction as it is not included within the loop. Notice the semicolon “;” after the for loop. After the execution of loop printf statement executes.

This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.