Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C Basic Input/Output - Aptitude Questions & Answers
C programming Basic Aptitude Questions and Answers: In this section, you will find C Aptitude Questions and Answers on Basic Input, Output, Data Type, printf, scanf etc.
List of C programming basic input, output, declaration, data types Aptitude Questions and Answers
1) What will be the output of following program ?
#include <stdio.h>
void main()
{
printf("includehelp.com\rOK\n");
printf("includehelp.com\b\b\bOk\n");
}
- OK
includehelp.ok
- OK
includehelp.okm
- includehelp.com
includehelp.okm
- OKcludehelp.com
includehelp.okm
Correct Answer - 4
OKcludehelp.com
includehelp.Okm
/r is an escape sequence which means carriage return. Carriage return takes back the cursor to the leftmost side in a line.
Thus in the statement printf("includehelp.com\rOK\n");
First "includehelp" is printed ( not still displayed) then cursor moves to leftmost position ("i" here) and starts printing "OK" which results in overwriting of first two characters of "includehelp". Thus the final output is "OKcludehelp.com" and then cursor moves to next line due to character feed \n.
In the second statement /b escape character is used which is equivalent to backspacing the cursor. Overwrite also took place here due to three backspaces.
N.B: The output is platform dependent.
2) What will be the output of following program ?
#include <stdio.h>
void main(){
unsigned char c=290;
printf("%d",c);
}
- 34
- 290
- Garbage
- ERROR
Correct Answer - 1
34
290 is beyond the range of unsigned char. Its corresponding value printed is: (290 % (UINT_MAX +1) where UINT_MAX represents highest (maximum) value of UNIT type of variable.
Here it's character type and thus UINT_MAX=255
Thus it prints 290 % (UINT_MAX +1)=34
3) What will be the output of following program ?
#include <stdio.h>
void main(){
int a=0;
a=5||2|1;
printf("%d",a);
}
- 1
- 7
- 0
- 8
Correct Answer - 1
1
Bitwise OR operator has precedence over logical OR operator
Thus the expression 5 || 2 | 1 is actually 5 || (2 |1)
Now
2= 0000 0010
1= 0000 0001
2|1= 0000 0011=3 (refer to bitwise operator)
5 || 3 returns true as both are nonzero
Thus a=1
4) What will be the output of following program (on 32 bit compiler)?
#include <stdio.h>
int main(){
float a=125.50;
int b=125.50;
char c='A';
printf("%d,%d,%d\n",sizeof(a),sizeof(b),sizeof(125.50));
printf("%d,%d\n",sizeof(c),sizeof(65));
return 0;
}
- 4,4,4
1,4
- 4,4,8
1,1
- 4,4,4
1,1
- 4,4,8
1,4
Correct Answer - 4
4,4,8
1,4
sizeof(a)= 4 bytes (float), sizeof(b)=4 bytes(int...in Visual studion, 2 in turboc), sizeof(125.50)=8 bytes (125.50 will consider as double constant), sizeof(c)= 1 byte (character), sizeof(65) = 4 bytes ( 65 is an integer ).
5) What will be the output of following program ?
#include <stdio.h>
int main(){
static char a;
static long b;
int c;
printf("%d,%d,%d",a,b,c);
return 0;
}
- 0,0,0
- Garbage,Garbage,Garbage
- Garbage,Garbage,0
- 0,0,Gargabe
Correct Answer - 4
0,0,Garbage Value
Static variable always initialize with 0, and other one by garbage value.
N.B: output is compiler dependent
6) What will be the output of following program ?
#include <stdio.h>
int main()
{
int ok=-100;
-100;
printf("%d",ok);
return 0;
}
- 100
- ERROR
- -100
- 0
Correct Answer - 3
-100
-100 is evaluated and this does not effect the value of ok.
7) What will be the output of following program ?
#include <stdio.h>
enum numbers
{
zero, one, two, three , four=3,five,six,seven=0,eight
};
void main()
{
printf("%d,%d,%d,%d,%d,%d,%d,%d,%d",zero,one,two,three,four,five,six,seven,eight);
}
- 0,1,2,3,3,4,5,0,1
- 0,1,2,4,5,6,7,8,9
- 0,1,2,3,3,1,2,3,4
- 0,1,2,3,3,4,5,0,9
Correct Answer - 1
0,1,2,3,3,4,5,0,1
We have discussed that enum values starts with 0 (if we do not provide any value) and increase one by one.
We are assigning 3 to four, then four will be 3, five will be 4 ... again we are assigning 0 to seven then seven will be 0 and eight will be 1.
8) What will be the output of following program ?
#include <stdio.h>
int main(){
int a,b,c;
a=0x10; b=010;
c=a+b;
printf("\nAddition is= %d",c);
return 0;
}
- Addition is = 20
- Addition is = 24
- Addition is = Garbage
- ERROR
Correct Answer - 2
Addition is = 24
0x10 is hex value it's decimal value is 16 and 010 is an octal value it's decimal value is 8, hence answer will be 24.
9) What will be the output of following program ?
#include <stdio.h>
int main()
{
int var=250;
printf("value of var = %d\n",var);
200+50;
"includehelp.com";
printf("%s\n","includehelp");
return 0;
}
- value of var = 250
includehelp.com
- value of var = 250
includehelp
- ERROR
- value of var = 250
Garbage
Correct Answer - 2
value of var = 250
includehelp
200+50; and "includehelp.com"; are executable statements, will not give any error because there is no action is performed by these statements.
10) What will be the output of following program ?
#include <stdio.h>
int main()
{
int a=100;
printf("%d\n"+1,a);
printf("Value is = %d"+3,a);
return 0;
}
- ERROR
- 101,
Value is = 103
- d
ue is = 100
- 100
100
Correct Answer - 3
d
ue is = 100
The "+" moves the string pointer passed to printf().
"%d\n"+1 ... Prints "d"
Value is = %d"+3 ... Prints "ue is = 100"
11) What will be the output of following program ?
#include <stdio.h>
int main()
{
extern int ok;
printf("value of ok = %d",ok);
return 0;
}
extern int ok=1000;
- Declaration error
- value of ok = 1000.
- Linking error
- value of ok = 0.
Correct Answer - 2
value of ok = 1000.
extern keyword is used to extend the visibility of variables thus ok is declared and defined memory though the statement extern int ok=1000; is outside main function.
12) What will be the output of following program ?
#include <stdio.h>
int main()
{
int a=23;
;
;printf("%d",a);
;
return 0;
}
- 23
- Error
- ;23;
- ;23
Correct Answer - 1
23
; (statement terminator) and no expression/statement is available here, so this is a null statement has no side effect, hence no error is occurred.
13) Find the output of this program,(program name is : static_ex.c)?
#include <stdio.h>
int main()
{
int intVar=24;
static int x=intVar;
printf("%d,%d",intVar,x);
return 0;
}
- 24,24
- 24,0
- ERROR: Illegal Initialization
- Run time error.
Correct Answer - 3
ERROR: Illegal Initialization.
You can not assign a variable into static variable at the time of declaration.
14) Find the output of this program ?
#include <stdio.h>
int main()
{
int a=15;
float b=1.234;
printf("%*f",a,b);
return 0;
}
- 1.234
- 1.234000
- 1.234000
- ERROR
Correct Answer - 3
1.234000
You can define width formatting at run time using %*, This is known as Indirect width precision .
printf("%*f",a,b); is considered as "%15f", hence value of b is printed with left padding by 15.
15) Find the output of this program ?
#include <stdio.h>
int main()
{
float a,b;
a=3.0f;
b=4.0f;
printf("%.0f,%.1f,%.2f",a/b,a/b,a/b);
return 0;
}
- 1,0.8,0.75
- 0,0.7,0.75
- 0,0.8,0.75
- ERROR: Invalid format specifier
Correct Answer - 1
1,0.8,0.75 (Using rounding off technique)
16) Which is the valid identifier (variable name) to store student’s age?
- int student-age;
- int student_age;
- int -age;
- int _age;
18) Which statement does not require semicolon?
- goto xyz
- int x=20
- #define MAX 1000
- do{ ... }while(count<=100)
Correct Answer - 3
#define MAX 1000 - does not require semicolon.