Output of C programs | Set 35 (Loops) - GeeksforGeeks

07 Sep, 2018
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 5b) syntax errorc) 5 10d) 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 5b) syntax errorc) 0 0d) 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 loopb) Numbers will be displayed until the signed limit of short and throw a run time errorc) Numbers will be displayed until the signed limit of short and program will successfully terminated) 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 timesb) GeeksQuiz is printed 9 timesc) GeeksQuiz is printed 7 timesd) 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.000000b)2.000000c)Compile time errord)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.
victer
C-Loops & Control Statements
C-Output
Program Output
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Output of Java program | Set 18 (Overriding)
Output of Java Program | Set 11
Output of Java programs | Set 13 (Collections)
Output of C++ programs | Set 34 (File Handling)
Different ways to copy a string in C/C++
Output of Java Program | Set 3
Runtime Errors
Output of Java program | Set 28
Output of Java program | Set 5
Output of Java Programs | Set 12