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>
int main(){
int x=2,y=3;
if(x< x++||y< y++)
printf(“%d %d”,x,y);
else
printf(“else block”);
return 1;
}

  1. else block
  2. 3,  4
  3. 3, 3
  4. 2, 4

Answer :

Ans : 1; Condition fails because x<x++ means 2<2

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

  1. 1,0,-1
  2. infinite loop
  3. Compile time error
  4. -1

Answer :

Ans : 4; Condition checks but will not increase the value of i in condition

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

  1. b=2
  2. b=3
  3. b=4
  4. infinite loop

Answer :

Ans : 2; followed by while loop there is a semi-colon; hence loop executes with empty body and prints final b value

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

  1. 5
  2. 5 to 11
  3. 6 to 10
  4. blank

Answer :

Ans : 4; When if conditiona fails with i value 5, else block break statement executes and loop will be terminated.

What will be the output of following code?
#include<stdio.h>
main()
{
int i=-1 , j=1;
do
{
do
{
j++;
}while(j++ <= 3);
i++;
}while(i++ < 3);
printf(” i : %d \n j : %d\n”,i,j);
}

  1. 4, 3
  2. 5, 4
  3. 5, 9
  4. 4, 6

Answer :

Ans : 3; Values of i and j increases everytime when condition checks

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

  1. 2, 4, 6, 8
  2. 3, 5, 7, 9
  3. 2, 5, 8, 11
  4. Blank output

Answer :

Ans : 4; Every time condition fails in the if-block

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

  1. 15
  2. 20
  3. 6
  4. 26

Answer :

Ans : 2; 

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

  1. 1
  2. infinite times 1
  3. 1,0,-1,-2 …..
  4. Compile time error

Answer :

Ans : 1; condition is true only once in the loop and expression-1 value –i prints

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

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

Answer :

Ans : 3; Continue statemen omits printf statement only in the 6th iteration

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

  1. 5, 54, 543, 5432, 54321
  2. 5, 44, 333, 2222, 11111
  3. 5, 45, 345, 2345, 12345
  4. None of the above

Answer :

Ans : 3; 

Control Statements – Technical – Part 3

Share this