Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C while loop Aptitude Questions and Answers
This section contains Aptitude Questions and Answers on C language while Loops (with multiple answers) with explanation.
Submitted by Ashish Varshney, on February 21, 2018
List of C programming for while Aptitude Questions and Answers
1) What will be the output in given input?
When, input: 'a' and 1
int main()
{
int n,i=0;
while(scanf("%d",&n)==1)
{
printf("END\n");
}
return 0;
}
- Runtime_error and Compile time error
- Compile time error and Runtime_error
- Syntax error and Syntax error
- No output and END
Correct answer: 4
No output and END
scanf function return integer value i.e. 1 if successful read data otherwise 0 in unsuccessfully read data.
1_ For, input ‘a’, scanf return 0 and while will not execute.
2_ For, input 1, scanf return 1 and while will be execute.
2) How many times above loop will execute?
int main()
{
int n=10,i=0;
while(1)
{
printf("END\n");
n++;
}
return 0;
}
- 10
- 5
- 4
- Infinite
Correct answer: 4
Infinite
While loop is always true as we set 1 as termination condition. This type of loop is helpful in continuous running processes.
3) What is the value of 'n' after while loop executed?
int main()
{
int n=0,a=5,b=10;
while(n<=(a^b))
{
n++;
}
printf("%d",n);
return 0;
}
- 15
- 16
- 10
- None of this.
Correct answer: 2
16
While loop termination condition, it consist XOR operation in variable a and b to set the limit.
Decimal Binary
A 5 00000101
B 10 00001010
Therefore,
A^B= 00000101
00001010
00001111 (In binary) 15(decimal
4) What is the value of 'n' after while loop executed?
int main()
{
int n=0,a=5,b=10;
while(n<=(b<<a))
{
n++;
}
printf("%d",n);
}
- 64
- 256
- 321
- 320
Correct answer: 3
321
While loop termination condition is set by performing left shift operation on b upto a.
Decimal Binary
A 5 00000101
B 10 00001010
Therefore,
B <<A
10<<5 = 0000101000000 =320(decimal)
5) What will be the output?
int main()
{
int a=16,n=0;
while(n<=~(~a))
{
n++;
}
a=n;
printf("%d",~a);
return 0;
}
- -18
- -17
- 18
- 17
Correct answer: 1
-18
While loop termination condition decides by unary operation performs on variable ‘a’. To find out Bitwise compliment
N`= -(n+1)
i.e. n`= -(-(16+1)+1) = -(-17+1) = -(-16) = 16.