0% found this document useful (0 votes)
1K views8 pages

Looping Questions and Answers With Explanation For Written Test Exam and Interview in C Programming Language

The document contains 15 coding questions and answers about loops in C programming. Key topics covered include do-while loops, for loops, continue statements, break statements, and infinite loops. The questions test a variety of looping concepts like initialization conditions, increment/decrement operations, and loop termination conditions. Sample outputs are provided for each code snippet to demonstrate how the loops will execute.

Uploaded by

Ishan Jawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views8 pages

Looping Questions and Answers With Explanation For Written Test Exam and Interview in C Programming Language

The document contains 15 coding questions and answers about loops in C programming. Key topics covered include do-while loops, for loops, continue statements, break statements, and infinite loops. The questions test a variety of looping concepts like initialization conditions, increment/decrement operations, and loop termination conditions. Sample outputs are provided for each code snippet to demonstrate how the loops will execute.

Uploaded by

Ishan Jawa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Looping questions in c and answers

Looping questions and answers with explanation for


written test exam and interview in c programming
language

(1)

What will be output of following c code?

#include<stdio.h>
extern int x;
int main(){
    do{
        do{
             printf("%o",x);
         }
         while(!-2);
    }
    while(0);
    return 0;
}
int x=8;

OUTPUT=10

(2)
What will be output of following c code?
        
#include<stdio.h>
int main(){
    int i=2,j=2;
    while(i+1?--i:j++)
         printf("%d",i);
    return 0;
}

output: condition is true so first statement executes.here (i=2) - -i gives 1.


(3)
What will be output of following c code?

#include<stdio.h>
int main(){
    int x=011,i;
    for(i=0;i<x;i+=3){
         printf("Start ");
         continue;
         printf("End");
    }
    return 0;
}

output: start start start

(4)What will be output of following c code?

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

EXPLANATION :
HERE I & J TAKE VALUE AS I=2,J=2
SO - -i=1, j++=3.
It will print 1,3.
(5)What will be output of following c code?

#include<stdio.h>
int main(){
    static int i;
    for(++i;++i;++i) {
         printf("%d ",i);
         if(i==4) break;
    }
    return 0;
}

OUTPUT: 2 , 4.

(6)What will be output of following c code?

#include<stdio.h>
int main(){
    int i=1;
    for(i=0;i=-1;i=1) {
         printf("%d ",i);
         if(i!=1) break;
    }
    return 0;
}

OUTPUT: -1

(7)What will be output of following c code?


#include<stdio.h>
int main(){
    for(;;) {
         printf("%d ",10);
    }
    return 0;
}

EXPLANATION: IT IS AN INFINITE LOOP,SO IT WILL CONTINUOUSLY PRINT 10.

(8)What will be output of following c code?


        
#include<stdio.h>
int r();
int main(){
    for(r();r();r()) {
         printf("%d ",r());
    }
    return 0;
}
int r(){
    int static num=7;
    return num--;
}

OUTPUT: 5 , 2

(9)What will be output of following c code?


        
#include<stdio.h>
#define p(a,b) a##b
#define call(x) #x
int main(){
    do{
         int i=15,j=3;
         printf("%d",p(i-+,+j));
    }
    while(*(call(625)+3));
    return 0;
}

OUTPUT:11

(10)

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

OUTPUT: I=6.
IF FOR LOOP HAVING NO SEMICOLON(;) IT WILL PRINT 0,1,2,3,4,5.

(11)What will be output of following c code?

#include<stdio.h>
int i=40;
extern int i;
int main(){
    do{
         printf("%d",i++);
    }
    while(5,4,3,2,1,0);
    return 0;
}

EXPLANATION:
here int i=40 is declared above main, the compiler will treat as extern storage class,so with in
program it’s value is 40.i++ is post increment so it prints 40.

(12)What will be output of following c code?

#include<stdio.h>
char _x_(int,...);
int main(){
    char (*p)(int,...)=&_x_;
    for(;(*p)(0,1,2,3,4); )
         printf("%d",!+2);
    return 0;
}
char _x_(int a,...){
    static i=-1;
    return i+++a;
}

OUTPUT=0

(13)What will be output of following c code?

#include<stdio.h>
int main(){
    int i;
    for(i=10;i<=15;i++){
         while(i){
             do{
                 printf("%d ",1);
                 if(i>>1)
                      continue;
             }while(0);
             break;
         }
    }
    return 0;
}

EXPLANATION:
FROM I=10 TO 15, THE LOOP WILL REPEAT 6 TIMES,SO IT WILL PRINT 1 1 1 1 1 1
6 TIMES BECAUSE IT IS SATISFYING THE CONDITION (I>>1).

(14)How many times this loop will execute?

#include<stdio.h>
int main(){
    char c=125;
    do
         printf("%d ",c);
    while(c++);
    return 0;
}

EXPLANATION
IT WILL PRINT 125 126 127 -128 -127 UNTIL IT COMES TO 0,BECAUSE OF WHILE(C++);
STATEMENT.

(15)What will be output of following c code?


        
#include<stdio.h>
int main(){
    int x=123;
    int i={
         printf("c" "++")
    };
    for(x=0;x<=i;x++){
         printf("%x ",x);
    }
    return 0;
}
Output: C++ 0 1 2 3.

You might also like