You are on page 1of 7

How many loops are there in C

(A) 2
(B) 3
(C) 4
(D) 1
-----------------------------------------------------------------------------

Ans : B

int main()
{
for(; ;)
for(; ;)
printf("Hello..");

return 0;
}

(A) Compilation Error


(B) Runtime Error
(C) Hello is printed one time
(D) Hello is printed infinite times
--------------------------------------------------------------------------------------------

Always remember, a for loop with only two ; is infinite loop. And it is because there is no
initialization, condition & increment/decrements

Ans : D
int main()
{
for(; ;);
for(; ;);
printf("Hello");
return 0;
}
(A) Compilation Error
(B) Runtime Error
(C) Nothing is printed
(D) Hello is printed infinite times
--------------------------------------------------------------------------------

blank for loop with ; ; is always infinite loop. printf() will never executed in this program.

Ans : C
int main()
{ int i;
for(i = 0,i<5,i++)
{ printf("Hello"); }
return 0;
}
(A) Hello is printed 5 times
(B) Compilation Error
(C) Runtime Error
(D) Hello is printed 4 times
---------------------------------------------------------------------------
for loop should have semicolumn ; not comma , in its syntax. Using , instead of ; produces
compilation error.

Ans : B
int main()
{
int i;
for(i=0; i<5; ++i++)
{
printf("Hello");
}
return 0;
}

(A) Hello is printed 5 times


(B) Compilation Error
(C) Hello is printed 2 times
(D) Hello is printed 3 times

----------------------------------------------------------------
C doesn't allow using preincrement & postincrement on a variable on same time like ++i++

Ans : B
int main()
{
int i,j;
for(i = 0,j=0;i<5;i++)
{
printf("%d%d--",i,j);
}
return 0;
}

(A) 0--01--12--23--34--
(B) 00--10--20--30--40--
(C) Compilation Error
(D) 00--01--02--03--04--
-----------------------------------------------------------
In this code only i is incrementing starting from 0 to 4 but j is 0 only.

Ans : D
int main()
{
int i;
for(i=0; i<5; i++);
{
printf("Karandikar");
}

return 0;
}

(A) Karandikar is printed 5 times


(B) Compilation Error
(C) Karandikar is printed 1 times
(D) Nothing is printed
----------------------------------------------------------------------------------------
You might be thinking that it should print 5 times but it is printing 1 time only because for loop is
terminated by ; which means for loop will run 5 times but printf() is not part of that loop.

Ans : C

int main()
{
while(1)
{
printf("Karandikar");
}

return 0;
}
(A) 1 time
(B) Compilation Error
(C) Infinite times
(D) Runtime Error

---------------------------------------------------------------------------------

Ans : C
int main()
{
int a = 0;
while(a==0)
{
printf("YCAP");
}

return 0;
}
(A) 0 times
(B) Infinite times
(C) 1 time
(D) Nothing is printed

----------------------------------------------------------------------

Ans : B

int main()
{
int a = 0;
while(a++ < 5)
printf("CppBuzz.com");
return 0;
}
(A) 4 times
(B) 5 times
(C) 0 time
(D) Infinite times

--------------------------------------------------------
Here while loop is execuated for a = 0 to a = 4 which is 5 times execuation hence CppBuzz.com is
printed 5 times.

Ans : B
int main()
{
int a = 0;

while(a++ < 5-++a)


printf("Manish");

return 0;
}
(A) 5 times
(B) 4 times
(C) 2 times
(D) 1 time
------------------------------------------------------------------------
Do this complex calculation and see the result in 2nd iteration

Ans : B

int main()
{
int a = 0;
if(a = printf("How old are you?"))
{
printf("%d - too young", a);
}

return 0;
}
(A) 0 - too young
(B) Compilation Error
(C) Nothing is printed
(D) How old are you? 16 - too young

----------------------------------------------------------------------------------------------------

Check no of charactors in printf

Ans : D
#include "stdio.h" // Compile on 32 bit system
#include "string.h"

int main()
{
char arr[] = "CRT_Training"
printf("%d", ++arr[0]);
return 0;
}

(A) C
(B) 68
(C) D
(D) 67
-------------------------------------------------------------------------------------------
ASCII value of 0th character + 1

Ans : B

#include "stdio.h"
int main()
{
while(!printf("hello")) { }
return 0;
}

(A) compilation error


(B) hello
(C) no output
(D) runtime error
----------------------------------------------------------------------------

it will print hello one time, While is expecting false case but printf is returning true. So loop not
executed

Ans : B

You might also like