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>
main()
{
int i=6;
switch(i)
{
default : i+=2;
case 0 : i=2;
case 1 : i++;
case 2 : i+3;
case 3 : ++i;
break;
}
printf(“i : %d\n”,i);
}

  1. 5
  2. 4
  3. 7
  4. 8

Answer :

Ans : 4 ; Case-2 expression will not modify i value

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

  1. Compile time error
  2. Blank output
  3. Infinite loop
  4. 1 to 65535

Answer :

Ans : 2 ; Condition fails for the first time using i++ expression

What will be the output of following code?
#include<stdio.h>
main()
{
signed char ch = 120;
while(++ch)
{
printf(“%d\t”,ch);
}
}

  1. infinite loop
  2. 121 to 127
  3. 121 to 127,-128,-127,….-1
  4. 121 to 127, -128

Answer :

Ans : 3 ; Once it reaches upper limit, it starts printing values from lower limit until false value 0 

What will be the output of following code?
#include<stdio.h>
int main()
{
int i=3;
switch(i)
{
case 1 : printf(“Hello\n”);
case 2 : printf(“Hi\n”);
case 3 : continue;
default : printf(“Bye\n”);
}
return 0;
}

  1. Compile time error
  2. Blank output
  3. Bye
  4. Bye infinite times

Answer :

Ans : 1 ; ‘continue’ statement cannot be placed inside switch statement directly.

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

  1. 7,911,13
  2. 7 to 13
  3. 4 to 10
  4. Compile time error

Answer :

Ans : 1 ; 

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

  1. Infinite loop
  2. 127
  3. 126, 127
  4. 127 , -127

Answer :

Ans : 4 ; Condition is true for two iterations

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

  1. 11, 8, 5, 2
  2. 11, 10,……2, 1
  3. infinite loop
  4. Compile time error

Answer :

Ans : 1 ;[/learn_more]


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

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

Answer :

Ans : 2 ; 

What will be the output of following code?
#include<stdio.h>
main()
{
int c=0;
for(; (c=printf(“hai”))==3;c++)
printf(“bye”);
}

  1. infinite times it print Hi and Bye
  2. One time Hi and infinite time Bye
  3. One time Hi and Bye
  4. Compile time error

Answer :

Ans : 1 ;

Control Statements – Objective – Part 1

Share this