Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C programming Cast Type Aptitude Questions and Answers
This section contains Aptitude Questions and Answers on Cast Typing, Basically in C programming language there are two types of cast type - Implicit and Explicit.
List of Cast Type Aptitude Questions and Answers
1)
What will be the output of following program ?
#include <stdio.h>
int main()
{
int x=65;
const unsigned char c=(int)x;
printf("%c\n",c);
return 0;
}
- Error
- 65
- A
- Null
Correct Answer: 3
A
Since variable c is character type and statement const unsigned char c=(int)x; will assign value 65 in c, so output will be A (which is the character value of 65).
2)
What will be the output of following program ?
#include <stdio.h>
int main()
{
int x=2.3;
const char c1=(float)x;
const char c2=(int)x;
printf("%d,%d\n",c1,c2);
return 0;
}
- Error
- 2.3,2
- 2.300000,2
- 2,2
Correct Answer: 4
2,2
const char can't be assigned to float casted value, thus assigning rounding of integer value.
3)
What will be the output of following program ?
#include <stdio.h>
int main()
{
char *text="Hi Babs.";
char x=(char)(text[3]);
printf("%c\n",x);
return 0;
}
- Garbage
- B
- Error
- Null
Correct Answer: 2
B
No issue with casting. Simply prints text[3] - which is 'B'.
4)
What will be the output of following program (on 32 bit compiler)?
#include <stdio.h>
int main()
{
int x=65;
const unsigned char c=(int)x;
printf("%c\n",c);
return 0;
}
- 65.00
- 65
- A
- Error
Correct Answer: 3
A
In the statement const unsigned char c=(int)x; The value of x is being assigned to the unsigned char variable c and the printf statement is printing its value using "%c" format specifier – so the output would be 'A' – which is the character value of ASCII code 65.