Home »
Aptitude Questions and Answers »
C Aptitude Questions and Answers
C Declaration and Initialization Aptitude Questions and Answers
This section contains aptitude questions and answers on C language Declarations and Initialization.
1) What will be the output of following program ?
int main(){
int m=10;
int x=printf("%d ",m);
printf("%d",x);
return 0;
}
- 10 3
- 103
- 10 2
- 102
Correct answer: 1
10 3
printf() returns total number of printed characters, the statement int x=printf("%d ",m) will print 10 (10 and one space) and return 3. Thus output will be 10 3 [10<space>3].
2) What will be the output of following program ?
int main(){
int x='A';
printf("%02X",x);
return 0;
}
- 65
- 97
- Error
- 41
Correct answer: 4
41
Statement int x='A'; will declare x as integer and assign the ASCII value of 'A' (that is 65) in it. And the statement printf("%02X",x); will print the ASCII value (65) in the 2 bytes Hexadecimal forma (that is 41). Thus output will be 41.
3) What will be the output of following program ?
int main(){
char a=0b1010;
printf("%02X",a);
return 0;
}
- 0A
- A
- 0a
- 10
Correct answer: 1
0A
Statement char a = 0b1010; will declare 'a' as the character and assign the binary value (1010) in it as 0b1010 is assigned. 0b is an integer literal which simply defines the number afterward the prefix 0b to be in binary. Thus 0b1010 is actually binary value 1010 that is equivalent to 10 in decimal. And the statement printf("%02X", a); will print the value of a (10) in 2 bytes hexadecimal format, that is 0A. Thus, the output will be 0A.
4) What will be the output of following program (on 32 bit compiler)?
int main(){
char a=2*2+2;
printf("%d",a);
return 0;
}
- 0
- Garbage
- 6
- 8
Correct answer: 3
6
Statement char a = 2*2+2; will declare a as the character and assign 6 in it (according to operator precedence and associability, operator * (Multiply) is evaluated first, then the expression 2*2+2 will be evaluated as (2*2) +2). So a has an ASCII value of 6. Since the placeholder in the printf("%d", a); statement is %d, thus, it prints the ASCII value of the character type variable.
5) What will be the output of following program ?
int main(){
unsigned char x=-1;
printf("%02X",x);
return 0;
}
- 0xFFFFFFFF
- FF
- 0xFF
- ff
Correct answer: 2
FF
Statement unsigned char x=-1; will declare x as unsigned char and assign value -1 in it, which is of type int. When we assign -1 in an unsigned type of variable, it converts this value from int to unsigned int. The rule for signed-to-unsigned conversion is reduced modulo (UINT_MAX + 1, so -1 will be converted to UINT_MAX, where UINT_MAX represents the highest (maximum) value of the UNIT type of variable.
UNIT_MAX% (UNIT_MAX+1) = -1 || UNIT_MAX
unsigned char can store 2 bytes of value that is equivalent to 255 in decimal and FF in Hexadecimal, thus output will be FF since we are printing up to two hexadecimal places.
6) Which is the correct name of a variable?
(A) -var (B) var-1 (C) _var (D) var_1
- Only (A)
- Only (B)
- Both (A) and (B)
- Both (C) and (D)
7) Which special character can be used to separate two parts (words) of a variable/identifier name?
- - (Hyphen)
- _ (Underscore)
- $ (Dollar)
- # (Hash)
Correct answer: 2
_ (Underscore)
Underscore ( _ ) is the only special character that can be used within the variable/identifiers name, it can be placed as first character, between the words and as the last character.
8) What will be the result of following program?
#include <stdio.h>
int main()
{
char x='AB';
printf("%c",x);
return 0;
}
- Error
- Runs successfully and prints 'A' (without quote)
- Run with warnings and prints 'B' (without quote)
- None of these
Correct answer: 3
Run with warnings and prints 'B' (without quote)
This program will run with a warning, because we are trying to initialize character variable with multi character constant, overflow will be occurred and output will be 'B'.
Warnings
main.c:4:9: warning: multi-character character constant [-Wmultichar]
char x='AB';
^
main.c:4:2: warning: overflow in implicit constant conversion [-Woverflow]
char x='AB';
^
9) Will following string be terminated with NULL?
char str[]={'H','e','l','l','o'};
- YES
- NO
Correct answer: 2
NO
No, such kind of initialization with declaration does not insert NULL at the end of the string.
Following two methods can be used to declare a string variable with NULL termination:
char str[]={'H','e','l','l','o','\0'};
char str[]="Hello";
Consider the following program to understand string initialization better:
#include <stdio.h>
int main()
{
char str1[]={'H','e','l','l','o'};
char str2[]={'H','e','l','l','o','\0'};
char str3[]="Hello";
printf("%s\n",str1);
printf("%s\n",str2);
printf("%s\n",str3);
return 0;
}
Output
Hello
Hello
Hello
Here, str1 is not terminated with NULL and other string variables str2 and str3 are terminated with NULL.
10) Predict the output of following program.
#include <stdio.h>
int main()
{
int (x)=10;
printf("x= %d",x);
return 0;
}
- x= 10
- x= 0
- Error
- None of these
Correct answer: 1
x= 10
Such kind of declaration form int (x)=10; is also acceptable in C/C++ programming language, compiler will not return any error.
11) Predict the output of following program.
#include <stdio.h>
int main()
{
int i=50;
const int x=i++;
printf("x= %d",++x);
return 0;
}
- x= 50
- x= 51
- x= 52
- Error
Correct answer: 4
Error
Here, x is a read only (integer constant) and we cannot change the value of any constant, following error will occur:
error: increment of read-only variable 'x'
printf("x= %d",++x);
^
12) Predict the output of following program.
#include <stdio.h>
int main()
{
int a=(10,20);
printf("a= %d",a);
return 0;
}
- a= 10
- a= 20
- a= 30
- Error
Correct answer: 2
a= 20
In the declaration statement int a=(10,20); value 10, 20 are placed within the brackets ( ), thus (10,20) will be evaluated first. Comma operator has left to right associativity, then result will be (20), thus output of the program will be x= 20.