Data type questions in c



Data types interview questions and answers with explanation


Note:  As you know size of data types is compiler dependent in c. Answer of all question is based upon that compilers whose word size is two byte. Just to know you, size of data type in 2 bytes compilers is as follow:

char :   1 byte
int :    2 byte
float :  4 byte
double : 8 byte

Please adjust the answers according to size of data types in you compiler.


Frequently asked technical objective types multiple choice data types questions of placement in  c programming language

1.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    printf("%d\t",sizeof(6.5));
    printf("%d\t",sizeof(90000));
    printf("%d",sizeof('A'));
    return 0;
}

Choose all that apply:
(A)4 2 1
(B)8 2 1 
(C)4 4 1
(D)8 4 1
(E)8 4 2


Explanation:

Turbo C++ 3.0: 8 4 2
Turbo C ++4.5: 8 4 2
Linux GCC:  8 4 4
Visual C++: 8 4 4

By default data type of numeric constants is:
6.5 :  double
90000: long int
‘A’: char
In C size of data type varies from compiler to compiler.
In TURBO C 3.0 (16 bit compilers) size of:
double is 8 byte
Long int is 4 byte
Character constant is 2 byte   (size of char data type is one byte)
In TURBO C 4.5 or Linux GCC compilers (32 bit compilers) size of:
double is 8 byte
long int is 8 byte
Character constant is 2 byte   

2.

Consider on following declaring of enum.
(i)        enum  cricket {Gambhir,Smith,Sehwag}c;
(ii)      enum  cricket {Gambhir,Smith,Sehwag};
(iii)    enum   {Gambhir,Smith=-5,Sehwag}c;
(iv)      enum  c {Gambhir,Smith,Sehwag};
Choose correct one:
(A)
Only (i) is correct declaration
(B)Only (i) and (ii) is correct declaration
(C)Only (i) and (iii) are correct declaration
(D)Only (i),(ii) and are correct declaration
(E)All four are correct declaration


Explanation:

Syntax of enum data type is:
enum  [<tag_name>]{
    <enum_constanat_name> [=<integer_ value>],
    …
} [<var_name>,…]

Note:
[] : Represents optional .
<>: Represents any valid c identifier

3.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    signed x;
    unsigned y;
    x = 10 +- 10u + 10u +- 10;
    y = x;
    if(x==y)
         printf("%d %d",x,y);
    else if(x!=y)
         printf("%u  %u",x,y);
    return 0;
}

Choose all that apply:
(A)0 0
(B)65536 -10 
(C)0 65536 
(D)65536 0
(E)Compilation error


Explanation:

Turbo C++ 3.0: 0 0
Turbo C ++4.5: 0 0
Linux GCC: 0 0
Visual C++: 0 0

Consider on the expression:
x = 10 +- 10u + 10u +- 10;
10: It is signed integer constant.
10u: It is unsigned integer constant.
X: It is signed integer variable.
In any binary operation of dissimilar data type for example: a + b
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
As we know operators enjoy higher precedence than binary operators. So our expression is:
x = 10 + (-10u) + 10u + (-10);
  = 10 + -10 + 10 + (-10);
  = 0
Note: Signed is higher data type than unsigned int.
So, Corresponding signed value of unsigned 10u is +10

4.
Which of the following is not modifier of data type in c?

(A)extern
(B)interrupt
(C)huge
(D)register
(E)All of these are modifiers of data type


Explanation:

To know more about these go to following link:

5.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    double num=5.2;
    int  var=5;
    printf("%d\t",sizeof(!num));
    printf("%d\t",sizeof(var=15/2));
    printf("%d",var);
    return 0;
}

Choose all that apply:
(A)4 2 7
(B)4 4 5
(C)2 2 5
(D)2 4 7
(E)8 2 7


Explanation:

Turbo C++ 3.0: 2 2 5
Turbo C ++4.5: 2 2 5
Linux GCC: 4 4 5
Visual C++: 4 4 5

sizeof(Expr)  operator always returns the an integer value which represents the size of the final value of the expression expr.
Consider on the following expression:
!num
=!5.2
=0
0 is int type integer constant and it size is 2 by in TURBO C 3.0 compiler and  4 in the TURBO C 4.5 and Linux GCC compilers.
Consider on the following expression:
var = 15/2
=> var = 7
=> 7
7 is int type integer constant.
Any expression which is evaluated inside the sizeof operator its scope always will be within the sizeof operator. So value of variable var will remain 5 in the printf statement.

6.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    const int *p;
    int a=10;
    p=&a;
    printf("%d",*p);
    return 0;
}

Choose all that apply:
(A)0
(B)10
(C)Garbage value
(D)Any memory address
(E)Error: Cannot modify const object


Explanation:

Turbo C++ 3.0: 10
Turbo C ++4.5: 10
Linux GCC: 10
Visual C++: 10

In the following declaration
const int *p;
p can keep address of constant integer.

7.
Consider on following declaration:
(i)        short i=10;
(ii)      static i=10;
(iii)    unsigned i=10;
(iv)      const i=10;

Choose correct one:
(A)
Only (iv) is incorrect
(B)Only (ii) and (iv) are incorrect
(C)Only (ii),(iii) and (iv) are correct
(D)Only (iii) is correct
(E)All are correct declaration 


Explanation:

Default data type of above all declaration is int.

8.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    int a= sizeof(signed) +sizeof(unsigned);
    int b=sizeof(const)+sizeof(volatile);
    printf("%d",a+++b);
    return 0;
}

Choose all that apply:
(A)10
(B)9
(C)8
(D)Error: Cannot find size of modifiers
(E)Error: Undefined operator +++


Explanation:

Turbo C++ 3.0: 8
Turbo C ++4.5: 8
Linux GCC: 16
Visual C++: 16

Default data type of signed, unsigned, const and volatile is int. In turbo c 3.0 size of int is two byte.
So, a = 4 and b =4
Now, a+++b
= a++ + b
= 4 + 4  //due to post increment operator.
=8
Note: In turbo c 4.5 and Linux gcc compiler size of int is 4 byte so your out will be 16

9.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    signed x,a;
    unsigned y,b;
    a=(signed)10u;
    b=(unsigned)-10;
    y = (signed)10u + (unsigned)-10;
    x = y;
    printf("%d  %u\t",a,b);
    if(x==y)
         printf("%d %d",x,y);
    else if(x!=y)
         printf("%u  %u",x,y);
    return 0;
}

Choose all that apply:
(A)10 -10   0 0
(B)10 -10   65516 -10 
(C)10 -10   10 -10
(D)10 65526      0 0
(E)Compilation error


Explanation:

Turbo C++ 3.0: 10 65526 0 0
Turbo C ++4.5: 10 65526 0 0
Linux GCC: 10 4294967286 0 0 
Visual C++: 10 4294967286 0 0

a=(signed)10u;
signed value of 10u is +10
so, a=10
 b=(unsigned)-10;
unsigned value of -10 is :
MAX_VALUE_OF_UNSIGNED_INT – 10 + 1
In turbo c 3.0 complier max value of unsigned int is 65535
So, b = 65526
y = (signed)10u + (unsigned)-10;
  = 10 + 65526 = 65536 = 0 (Since 65536 is beyond the range of unsigned int. zero is its corresponding cyclic vlaue)
X = y = 0

10.
Which of the following is integral data type?

(A)void
(B)char
(C)float
(D)double
(E)None of these


Explanation:

In c char is integral data type. It stores the ASCII value of any character constant.

11.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    volatile int a=11;
    printf("%d",a);
    return 0;
}

Choose all that apply:
(A)11
(B)Garbage
(C)-2
(D)We cannot predict
(E)Compilation error


Explanation:

Turbo C++ 3.0: We cannot predict
Turbo C ++4.5: We cannot predict
Linux GCC: We cannot predict
Visual C++: We cannot predict

We cannot predict the value of volatile variable because its value can be changed by any microprocessor interrupt.

12.
What is the range of signed int data type in that compiler in which size of int is two byte?
(A)-255 to 255
(B)-32767 to 32767
(C)-32768 to 32768
(D)-32767 to 32768
(E)-32768 to 32767


Explanation:

Note: Size of int is always equal to word length of micro preprocessor in which your compiler has based.

13.
What will be output when you will execute following c code?

#include<stdio.h>
const enum Alpha{
      X,
      Y=5,
      Z
}p=10;
int main(){
    enum Alpha a,b;
    a= X;
    b= Z;
    printf("%d",a+b-p); 
    return 0; 
}

Choose all that apply:
(A)-4
(B)-5 
(C)10
(D)11
(E)Error: Cannot modify constant object


Explanation:

Turbo C++ 3.0: -4
Turbo C ++4.5: -4
Linux GCC: -4
Visual C++: -4

Default value of enum constant X is zero and
Z = Y + 1 = 5 + 1 = 6
So, a + b – p
=0 + 6 -10 = -4

14.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    char a=250;
    int expr;
    expr= a+ !a + ~a + ++a;
    printf("%d",expr);
    return 0;
}

Choose all that apply:
(A)249
(B)250
(C)0
(D)-6
(E)Compilation error


Explanation:

Turbo C++ 3.0: -6
Turbo C ++4.5: -6
Linux GCC: -6
Visual C++: -6

char a = 250;
250 is beyond the range of signed char. Its corresponding cyclic value is: -6
So, a = -6
Consider on the expression:
expr= a+ !a + ~a + ++a;
Operator! , ~ and ++ have equal precedence. And it associative is right to left.
So, First ++ operator will perform the operation. So value a will -5
Now,
Expr = -5 + !-5 + ~-5 + -5
= -5 + !-5 + 4 - 5
= -5 + 0 + 4 -5
= -6

15.
Consider on order of modifiers in following declaration:

(i)char volatile register unsigned c;
(ii)volatile register unsigned char c;
(iii)register volatile unsigned char c;
(iv)unsigned char volatile register c;










(A)Only (ii) is correct declaration
(B)Only (i) is correction declaration
(C)All are incorrect
(D)All are correct but they are different
(E)All are correct and same


Explanation:

Order of modifier of variable in c has not any significant.

Choose correct one:

16.
What will be output when you will execute following c code?

#include<stdio.h>
int main(){
    int a=-5;
    unsigned int b=-5u;
    if(a==b)
         printf("Avatar");
    else
         printf("Alien");
    return 0;
}

Choose all that apply:
(A)Avatar
(B)Alien
(C)Run time error
(D)Error: Illegal assignment
(E)

Error: Don’t compare signed no. with unsigned no.



Explanation:

Turbo C++ 3.0: Avatar
Turbo C ++4.5: Avatar
Linux GCC: Avatar
Visual C++: Avatar

int a=-5;
Here variable a is by default signed int.
unsigned int b=-5u;
Constant -5u will convert into unsigned int. Its corresponding unsigned int value will be :
65536 – 5 + 1= 65532
So, b = 65532

In any binary operation of dissimilar data type for example: a == b
Lower data type operand always automatically type casted into the operand of higher data type before performing the operation and result will be higher data type.
In c signed int is higher data type than unsigned int. So variable b will automatically type casted into signed int.
So corresponding signed value of 65532 is -5
Hence, a==b

17.
What will be output when you will execute following c code?

#include<stdio.h>
extern enum cricket x;
int main(){
    printf("%d",x); 
    return 0;
}
const enum cricket{
    Taylor,
    Kallis=17,
    Chanderpaul
}x=Taylor|Kallis&Chanderpaul;

Choose all that apply:
(A)0
(B)15
(C)16
(D)17
(E)Compilation error


Explanation:

Turbo C++ 3.0: 16
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: 16

x=Taylor|Kallis&Chanderpaul
= 0 | 17 & 18
= 0 |(17 & 18)
//& operator enjoy higher precedence than |
=0 |16
=16

18.
Which of the following is not derived data type in c?

(A)Function
(B)Pointer
(C)Enumeration
(D)Array
(E)All are derived data type


Explanation:

Enum is primitive data type.

19.
What will be output when you will execute following c code?

#include<stdio.h>
enum A{
    x,y=5,
    enum B{
         p=10,q
    }varp;
}varx;

int main(){
    printf("%d %d",x,varp.q);
    return 0;
}

Choose all that apply:
(A)0 11
(B)5 10 
(C)4 11
(D)0 10
(E)Compilation error


Explanation:

Turbo C++ 3.0: Compilation error
Turbo C ++4.5: Compilation error
Linux GCC: Compilation error
Visual C++: Compilation error

Nesting of enum constant is not possible in c.

20.
Consider on following declaration in c:

(i)short const register i=10;
(ii)static volatile const int i=10;
(iii)unsigned auto long register i=10;
(iv)signed extern float i=10.0;

Choose correct one:
(A)Only (iv)is correct
(B)Only (ii) and (iv) is correct
(C)Only (i) and (ii) is correct
(D)Only (iii) correct
(E)All are correct declaration 


Explanation:

Option (III) is in correct due to we cannot specify two storage class auto and register in the declaration of any variable.
Option (iv) is in correct due to we cannot use signed or unsigned modifiers with float data type. In c float data type by default signed and it cannot be unsigned.



55 comments:

Anonymous said...

nice

Anonymous said...

thanks itz really good

Anonymous said...

Really thanks 4 providing such a huge range of beyond imagination quests.. Thanks a lot...
@Admin: jz an advice for options of answers use radiobutton group instead.. because multiple answers are getting selected of same quest and they arent getting unchecked also..

sateeshnaidu said...

for question 1 output is 8 4 1.

sateeshnaidu said...

can u provide solution to que:4

Anonymous said...

How i present 0123 as integer variable.

Mohammed Ansari said...

Marvelous...Very useful to me....My grateful thanks to you......................

Anonymous said...

hey where can i find a pdf of all these questions ?

Preeti Kashyap said...

its very useful to me,,,,,,,,,,,,,,,,thanks

Anonymous said...

Plz Clear Question No.14 because what is -6. signed char range is 0 to -128 to 127

Anonymous said...

hey anonymous i googled n got the answer for ur above question.. here it is....

Cyclic nature of data type in C
In C some data types shows one special properties that when we assign a value beyond range of that data type then it will not any compiler error but assign a number according to some cyclic order. This property is known as cyclic nature of data type.
Data type which shows cyclic nature:
(a) char
(b) int
(c) long int
Data type which doesn’t show cyclic nature:
(a) float
(b) double
(c) long double

1. Cyclic nature of unsigned char:
Range of unsigned char is 0 to 255. If we will assign a value greater than 255 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If number is less than 0 then move in anti clockwise direction.


Short cut formula to find cyclic value:
If number is X where X is greater than 255 then
New value = X % 256
If number is Y where Y is less than 0 then
New value = 256 – (Y% 256)


2. Cyclic nature of signed char:

Range of unsigned char is -128 to 127. If we will assign a value greater than 127 then value of variable will be changed to a value if we will move clockwise direction as shown in the figure according to number. If number is less than -128 then move in anti clockwise direction.



Short cut formula to find cyclic value:
If number is X where X is greater than 127 then
p = X % 256
if p <=127
New value = p
else
New value = p – 256
If number is Y where Y is less than -128 then
p = Y % 256
If p <= 127
New value = -p
else
New value = 256 -p

My name Ayesha

ritesh kumar said...

I hope this link will help you.

Cyclic nature of data types

Ganesh Kumar.J said...

very nice
you can test your c skills

Anonymous said...

where did you get int's size is 2, it is 4 byte.

Shivdas Lendale said...

very nice
this test is amesign cncept

sunil mundhara said...

nice exercises!! learned a lot of new things!!

Anonymous said...

nice really.. njoy it...

Anonymous said...

Can anyone explain the output of program as under:

#include
int array[]={1,2,3,4,5,6,7,8};
#define SIZE (sizeof(array)/sizeof(int))
main()
{
if(-1<=SIZE) printf("1");
else printf("2");
}

waseem hajwane said...

what is the output of this program....and also explain it..how the answer it comes
main()
{
int a, b, c;
a = 10;
b = 20;
c = printf("%d",a) + ++b;
printf ("%d",c);
}

Anonymous said...

i think the output will be 31..because here we used pre increment operation for b
i.e.,10+21=31.


or else the output will be 1023

Anonymous said...

2

sourabh patel said...

#include
#include
void main()
{
float a=0.7;
if(a<0.7)
printf("stoned");
else
printf("Avenged");
}

sourabh patel said...

plz reply what is the ans

vijay said...

here the reason is real numbers are stored in IEEE 754 format so the bit fields for float fills out to be 32 bit ie..a
where as the bit fields for 0.7 which by default acts as double which is of 64 bits when you try to compare a will be lesser than 0.7 because the a is type casted implicitly which implicitly becomes 8 bytes .for further details look at memory representation of real numbers.

Anonymous said...

2+21=23

because printf("%d",a) results 2 because a= 10 has two digits ..

shivin kapoor said...

Explain how its output is 1023

triggers said...

bro how you created this quizz and can you tell me which site u used to create this quiz :)

Himanshu said...

good knowledge of quiz thanks..

abhishek joshi said...

the solution is 8 4 2

Anonymous said...

please explane me this the output is 1 7
#include

int x = 0;

int main()

{

int i = (f() + g()) | g(); //bitwise or

int j = g() | (f() + g()); //bitwise or
printf("%d",i);
printf("%d",j);
}

int f()

{

if (x == 0)

return x + 1;

else

return x - 1;

}

int g()

{

return x++;

}

Anonymous said...

why is 8 4 2? more precisely why is ..2 instead 1(char : 1 byte)

Vijay Kumar said...

why because char constant consider a int so what it showing 4 bytes for example the capital value of A equivalent int value is 65(int) if u going to print both is same
printf(sizeof('A'));

printf(sizeof(65));

Anonymous said...

very useful for all beginers

sreejith sree said...

in tubo c int's size is 2

sushant kumar said...

11.
What will be output when you will execute following c code?

#include
int main(){
volatile int a=11;
printf("%d",a);
return 0;
}

i have executed this code on GCC compiler and i am getting the output as 11.
but in the explanation of this code it is written that compiler can not predict the volatile variable.. why it is so ... please clear my doubt.

chandan pandey said...

What are the various stage of programming development ?

chandan pandey said...

Explain different tools for program logic development.

chandan pandey said...

Why are flow-charts used? Explain symbols for making a flow chart-charts?

chandan pandey said...

What are main programming characteristics?

chandan pandey said...

What are various methods of compiling a C program?

chandan pandey said...

Write a C program to find the largest of three numbers.

chandan pandey said...

Write a note on operator in C.

chandan pandey said...

what are statement different types in C? Explain all three iterative statement with syntax.

chandan pandey said...

define array. How will you do the following?:
1. declare array
2. Initialize array
3. Enter data in array
4. Access elements of an array
5. Get output from an array.
--- - -- - and - --- - -
Write a program to add two 3*3 matrix using array.

chandan pandey said...

best question of c language:-
Question:- Define function. What are advantage of using functions? how are functions.

Meena tvd22 said...

8 4 2 is the output

Engg Warez said...

main can be written as var name

RAJU SINGH said...

thanks for this question

vinay said...

in qstn no 13 how we get z=y+1 ??

nasir anees said...

write a program that input two number and finds if second number is square of 1st number

nasir anees said...

please solve this program plz

nasir anees said...

write a program that input two number and finds if second number is square of 1st number
please solve this program plz

Sifat Khan said...

awesome

Shwetha Kamath said...

Great post

vrushali pawar said...

Gud post,please add data structure mcq type questions n answer.