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.



Post a Comment