C programming: What are Data types in C ?, With Examples and Interview Questions

 Data types in C, With Examples and Interview Questions

Data Types In C

  • Data types are the keywords used for declaring variables or functions of different types.
  • The type of the variable defines how much space it takes in the memory and the type of the function defines the return type of the function that means which type of value the function is going to return.


syntax for defining datatype with variable name:
datatype variable_name;


syntax for defining datatype with variable name:
datatype function_name();                                                                



Data types in C



Primitive Data types: Primary Data types are also called as Fundamental Data Types.


Let's see the basic data types memory size and their range:

memory size and their range in  ;Data types in C



Void data type:

  • void type represents no value.
  • void type does not have any value.
  • void type does not return values for functions.  



Let's see an example to understand the use of basic data types more precisely:

Data types in C



Derived Data types: As the name itself denotes, derived data types are derived from primary data types. They includes Arrays, Pointers, and Functions. These concepts will be discussed further.

User-defined Data types: User-defined data type is used when user wants to define an identifier and that identifier can be later used for variables as sometimes, the basic set of data types defined in the C language such as int, float etc. may be insufficient for your application. In circumstances such as these, you can create your own data types which are based on the standard ones. These includes Structures, Unions, and Enumerations.


   Interview Questions on Data Types:                


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

a) Function
b) Pointer
c) Enumeration
d) Array

Answer : c


2. Integral data type is ____?

a) Void
b) Char
c) Float
d) Double

Answer : b


3. The Format Identifier %u is used for?

a) Integer
b) Float
c) Char
d) Decimal
e) Unsigned decimal

Answer : e


4. Size of an int data type is ____ ?

a) 4 Bytes
b) 8 Bytes
c) Depends on Compiler/System
d) Cannot be determined

Answer : c


5. Sort int in C language is?

a) Basic Datatype of C
b) Qualifier
c) All of the mentioned
d) short is the qualifier and int is the basic datatype

Answer : d


6. Data type qualifiers can be classified into?

a) 4
b) 5
c) 2
d) 8

Answer : c


7. The C language defines ____ fundamental data types

a) 3
b) 4
c) 5
d) 6

Answer : b


8. What is Enum datatype syntax?

a) Enum[data type]{const1, const2, const3....}
b) Enum{const1, const2,....}
c) Enum[int datatype]
d) None

View Answer : a

Explanation : Syntax of enum data type is:
enum []{
[=],

} [,…]

Note:
[] : Represents optional .


9. Character literals in C syntax are?

a) A
b) ‘A’
c) “4”
d) None

Answer : b


10. Range of unsigned int is?

a) 0 to 65,535 or 0 to 4,294,967,295
b) 0 to 65,535
c) 0 to 4,294,967,296
d) None

Answer : a


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

#include
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 2
(E) 8 4 4

Answer : e


12. 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


Answer : e


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

#include
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

Answer : a


Explanation : 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.




14. 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

Answer : e


15. 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


Answer : e


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

#include
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


Answer : d

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


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

#include
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

Answer : a

Explanation : Default data type of signed, unsigned, const and volatile is int.


18. 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

Answer : e



Previous                                                                         Next



For any query related to the content mail to [email protected]

2 comments:

  1. There are a lot of issues with this article. Because many things are highly processor/compiler/OS specific....

    ReplyDelete
  2. Actually, there are dependably pre-aggregated adaptations accessible for your OS of decision. learning app Garden Grove

    ReplyDelete

Powered by Blogger.