Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Output of C programs | Set 35 (Loops)

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

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, both the expression before and after “,” operator will be evaluated but the expression right will be returned, i.e. the loop will be ended if the condition, j < 10 becomes false.

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.


My Personal Notes arrow_drop_up
Last Updated : 07 Sep, 2018
Like Article
Save Article
Similar Reads