Question
Question
Ans: B
Hint:
C Language has only three loops - for, while, do while
Question: 2
What is the output of below program?
void main()
{
for(; ;)
for(; ;)
printf("Hello..");
}
(A) Compilation Error (B) Runtime Error
(C) Hello is printed one time (D) Hello is printed infinite times
Ans: D
Hint:
Always remember, a for loop with only two ; is infinite loop. And it is
because there is no initialization, condition & increment/decrements
Question: 3
What is output of below program?
void main()
{
for(; ;);
for(; ;);
printf("Hello");
}
(A) Compilation Error (B) Runtime Error
(C) Nothing is printed (D) Hello is printed infinite times
Ans: C
Hint:
blank for loop with ; ; is always infinite loop. printf() will never
executed in this program.
Question: 4
for(i = 0,i<5,i++)
{
printf("Hello");
}
}
(A) Hello is printed 5 times (B) Compilation Error
(C) Runtime Error (D) Hello is printed 4 times
Ans: B
Hint:
for loop should have semicolumn ; not comma , in its syntax. Using ,
instead of ; produces compilation error.
Question: 5
What is output of below program?
void main()
{
int i;
for(i=0; i<5; ++i++)
{
printf("Hello");
}
}
(A) Hello is printed 5 times (B) Compilation Error
(C) Hello is printed 2 times (D) Hello is printed 3 times
Ans: B
Hint:
C doesn't allow using preincrement & postincrement on a variable on
same time like ++i++
Question: 6
Ans: B
Hint:
In this code only i is incrementing starting from 0 to 4 but j is 0 only.
Question: 7
void main()
{
int i,j,k,count;
count=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
count++;
}
}
printf("%d",count);
}
(A) 5 (B) 10 (C) 25 (D) 50
Ans: C
Hint:
This code has 25 iterations of for loops that is why count++ is executed
25 times hence its value becomes 25.
Question: 8
void main()
{
int i;
for(i=0; i<5; i++);
{
printf("cppbuzz");
}
}
(A) cppbuzz is printed 5 times (B) Compilation Error
(C) cppbuzz is printed 1 times (D) Nothing is printed
Ans: C
Hint:
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.
Question: 9
What is output of below program?
void main()
{
int i,j,count;
count=0;
for(i=0; i<5; i++);
{
for(j=0;j<5;j++);
{
count++;
}
}
printf("%d",count);
}
(A) 55 (B) 54 (C) 1 (D) 0
Ans: C
Hint:
if we put ; after for() then for loop doesn't run anything which is inside
{}
Question: 10
How many times CppBuzz.com is printed?
void main()
{
while(1)
{
printf("CppBuzz.com");
}
}
(A) 1 time (B) Compilation Error
(C) Infinite times (D) Runtime Error
Ans: C
Hint:
while(1) means while will run for forever it also means while(true)
Question: 12
What is printed on console?
void main()
{
int a = 0;
while(a)
{
printf("CppBuzz.com");
}
}
(A) CppBuzz.com is printed Infinite times
(B) CppBuzz.com is printed 1 time only
(C) Nothing is printed
(D) Program gives error
Ans: C
Hint:
Here while loop is evaluated as while(0) which means while loop will
not be executed.
Question: 13
Ans: B
Hint:
while(0==0) is evaluated as while(1) which means while loop is
execuated infinite times
Question: 14
How many times CppBuzz.com is printed on console?
void main()
{
int a = 0;
while(a++)
{
printf("CppBuzz.com");
}
}
(A) Nothing is printed on screen (B) 0 time (C) 1 time
(D) Infinite times (Untill Stack overflow)
Ans: A
Hint:
Here while loop is evaluated as while(0) because it has a post increment
operator and a is incremented later after while loop. While(0) means
loop will not run.
Question: 15
How many times CppBuzz.com is printed?
void main()
{
int a = 0;
while(a++);
{
printf("CppBuzz.com");
}
}
(A) 0 time (B) 1 time
(C) Compilation Error (D) Infinite times
Ans: B
Hint:
Here while loop is evaluated as while(0) because it has post increment
operator. And since while(0) is terminated by ; so printf() will not be part
of it and hence it is printed 1 time.
Question: 16
How may times CppBuzz.com is printed?
void main()
{
int a = 0;
while(++a++);
{
printf("CppBuzz.com");
}
}
(A) 1 time (B) 0 time
(C) Infinite times (D) Error: Lvalue Required
Ans: D
Hint:
We can't use preincrement and postincrement operator same time on
same variable.
Question: 17
How many times CppBuzz.com is printed?
void main()
{
int a = 0;
while(++a)
{
printf("CppBuzz.com");
}
}
(A) 1 time (B) Infinite Times(Untill Stack is overflow)
(C) 2 times (D) Error
Ans: B
Hint:
Here while loop is evaluated as while(1) which means it will run infinite
times.
Question: 18
Ans: C
Hint:
while loop is evaluated as while(0) which means while loop will be
executed 0 times and since printf() is also part of it so nothing is printed
here.
Question: 19
Ans: B
Hint:
Here while loop is evaluated as while(0) which means it will be
executed 0 times and since printf is also part of it so nothing would be
printed. Note:- if there are no { } braces after any loop then only the next
statement is considered as a part of the loop.
Question: 20
How many times CppBuzz.com is printed?
void main()
{
int a = 0;
while(a++ < 5)
printf("CppBuzz.com");
}
(A) 4 times
(B) 5 times
(C) 0 time
(D) Infinite times
Ans: B
Hint:
Here while loop is execuated for a = 0 to a = 4 which is 5 times
execuation hence CppBuzz.com is printed 5 times.
Question: 21
main()
{
int x = 10;
{
int x = 0;
printf("%d",x);
}
}
Ans: C
Hint:
It will print nearest values that is x =10
Question: 22
#include<stdio.h>
main()
{
int a[] = {10,20,30};
printf("%d",*a+1);
}
(A) 10 (B) 20
(C) 11 (D) 21
Ans: C
Hint:
a refers to its base address that is why *a means values at first address
that is 10. Hence 10+1 is 11
Question: 22
What is the output of the program?
#include <stdio.h>
int main()
{
for(i=3;i<15;i++);
printf("%d",i); return 0; }
(A) 3 (B) 15 (C) 16 (D) Compilation error
Ans: D Hint: i variable is not declared
Question: 23
union example{
};
void main(){
Ans: A
Hint:
The size of empty Union is 0 bytes. Also remember that empty Enums &
Structures also consumes 0 byes in C programming.