C Technical Interview Questions – Control Statements

These C Technical Interview Questions post providing basic, intermediate and Hard level examples in C Control Statements. The Practice of these examples makes you good in C programming.


What will be the output of following code?
#include<stdio.h>
void main()
{
int i;
for(i=1 ; i<=10 ; i++)
{
if(i==6)
break;
printf(“i : %d\n”,i);
}
}

  1. 1 to 5
  2. 1 to 6
  3. Only 6
  4. 6 to 10

Answer :

Ans : 1 ; When break statement executes, the for loop execution will be terminated.

What will be the output of following code?
#include<stdio.h>
main()
{
int s=0;
while(s++ <= 10)
{
if(s<4 && s<8)
continue;
printf(“s : %d\n”,s);
}
}

  1. 1,2,3,8,9,10
  2. 4 to 10
  3. 5 to 11
  4. 4 to 11

Answer :

Ans : 4 ; continue statement will not exeucte from s=4.

What will be the output of following code?
#include<stdio.h>
int main()
{
int i=1;
while(i++);
printf(“%d”,i);
return 1;
}

  1. infinite loop
  2. 0
  3. 1
  4. compile time error

Answer :

Ans : 3 ; There is a semi-colon at the end of while loop, hence while loop executes with empty body and condition fails when i=0, but prints 1 because of post increment

What will be the output of following code?
#include<stdio.h>
void main()
{
while(1){
while(1){
printf(“Outer\t”);
break;
}
printf(“Inner\n”);
}
}

  1. Outer, Inner
  2. 1 time Outer, Infinite times Inner
  3. Infinite times it prints Outer and Inner
  4. None of the above

Answer :

Ans : 3 ; When break statement breaks the inner loop, it continues with outer loop exeuction

What will be the output of following code?
#include<stdio.h>
int main()
{
if(1)
printf(“Hi\n”);
printf(“Welcome\n”);
else
printf(“Hello\n”);
return 0;
}

  1. Hi, Welcome
  2. Welcome, Hello
  3. Hi, Hello
  4. Compile time error

Answer :

Ans : 4 ; Mis-placed else-block in the definition of if-block.

What will be the output of following code?
#include<stdio.h>
int main()
{
int a=30,b,c;
if(a>=40)
b=30 ;
c=20 ;
printf(“%d”,c);
return 0;
}

  1. 20
  2. 30
  3. 0
  4. Compile time error

Answer :

Ans : 1 ; Condition fails, hence if-block will not execute, c=20 is the outside statement, hence it executes

What will be the output of following code?
#include<stdio.h>
void main()
{
int i,j;
for(i=1 ; i<=5 ; i++)
for(j=1 ; j<=5 ; j++)
if(i==j)
break;
printf(“i:%d\tj:%d\n”,i,j);
}

  1. i=6 , j=6
  2. i=6 , j=5
  3. i=5 , j=5
  4. i=5 , j=6

Answer :

Ans : 2 ; When j=5 inner loop breaks, but i value in the outer loop increases hence i=6

What will be the output of following code?
#include<stdio.h>
void main(){
int a=5,b=10;
if(++a||++b)
printf(“%d %d”,a,b);
else
printf(“Java2Python”);
}

  1. 5 and 11
  2. Java2Python
  3. 6 and 11
  4. 6 and 10

Answer :

Ans : 4 ; Logical operator doesn’t execute if the first expression is true, hence ++b will not be executed.

What will be the output of following code?
#include<stdio.h>
void main()
{
int i=0;
while(++i<=10)
{
printf(“%d\n”,i++);
continue;
++i ;
}
}

  1. 1,3,5,7,9
  2. 2,4,6,8,10
  3. 2,5,8
  4. Compile time error

Answer :

Ans : 1 ; The statement ++i followed by continue statement will not be executed.

What will be the output of following code?
#include<stdio.h>
int main()
{
int a=3,b=3;
while(–a && –b);
printf(“%d,%d\n”,a,b);
return 0;
}

  1. 1, 1
  2. 0, 0
  3. 0, 1
  4. 2, 2

Answer :

Ans : 3 ; It prints final values of a and b once while loop has failed

Control Statements – Technical – Part 4

Share this