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()
{
if(!!0)
printf(“Hi\n”);
printf(“Hello\n”);
return 0;
}

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

Answer :

Ans : 1; Only first statement comes under if-block that will not execute as condition fails

What will be the output of following code?
#include<stdio.h>
int main()
{
int a=5,b=10,c=1;
if(a||b == c)
{
printf(“if block”);
break ;
}
else
printf(“else block”);
return 0;
}

  1. if block
  2. else block
  3. Compile time error
  4. Blank output

Answer :

Ans : 3; We cannot place break statement inside if block.

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

  1. Blank output
  2. 1
  3. 1 to 10
  4. 1 to 9

Answer :

Ans : 2; condition fails for the first time in the loop after priniting the value 1

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

  1. Hi, Hello
  2. Hi
  3. Hello, Bye
  4. Hi , Bye

Answer :

Ans : 4; Prints ‘Hi’ as Condition is true and outside statement ‘Bye’ prints

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

  1. 0
  2. 30,25,20,15,10,5
  3. prints 0 infinite times
  4. compile time error

Answer :

Ans : 1; 30%5 value is 0 prints and the value will be stored into i, hence second time loop will not repeat.

What will be the output of following code?
#include<stdio.h>
int main(){
int a=-3,b=-2;
if(++a=++b)
printf(“if block”);
else
printf(“else block”);
return 1;
}

  1. If block
  2. Error
  3. else block
  4. None of the above

Answer :

Ans : 2; Error : L value required in the expression ++a=++b (a=a+1=b=b+1)

What will be the output of following code?
#include<stdio.h>
int main()
{
if(4<!20)
printf(“block-1”);
else if(!3 > 0)
printf(“block-2”);
else if(5 > !50)
printf(“block-3”);
return 1;
}

  1. block-1
  2. block-2
  3. block-3
  4. block-1 and block-3

Answer :

Ans : 3; First two conditions fails

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

  1. 1 to 10
  2. 1 to 6
  3. 1,2,3,4,5,6,6,6,6,6
  4. None of the above

Answer :

Ans : 1; Inner while loop condition increase the j value either in ture or false case, every time when it checks

What will be the output of following code?
#include<stdio.h>
int main(){
int n=3;
if(printf(“hello %d”,n>=5)-4)
while(1)
break;
else;
return 1;
}

  1. Compile time error
  2. hello 0 prints infinite times
  3. hello 3 , hello 3 , hello 3
  4. hello 0

Answer :

Ans : 4; Expression n>=5 returns 0, so prints ‘hello 0’ and while loop will break as soon as it has started

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

  1. if block
  2. a=-1
  3. Compile time error
  4. a=0

Answer :

Ans : 3; Misplaced else, We need to specify if-statements in a block.

Control Statements – Technical – Part 2

Share this