Java Data Types Questions and Answers

1. What are the primitive data types in Java ?
Answer: There are eight primitive data types.
byte.
short.
int.
long.
float.
double.
boolean.
char.

2. Is Java primitive data type stored on stack or heap?
Answer: Primitive types declared locally will be on the stack while primitive types that are defined as part of an object instance are stored on the heap. Local variables are stored on stack while instance and static variables are stored on the heap.

3. What is the output?
Answer: 
System.out.println(1.0/0);
There will be no exception instead it prints Infinity.
1.0 is a double literal and double datatype supports infinity.

4. What are the Wrapper classes available for primitive types ?
Answer:  following are the wrapper classes available:
boolean  - java.lang.Boolean
byte - java.lang.Byte
char - java.lang.Character
double - java.lang.Double
float - java.lang.Float
int - java.lang.Integer
long - java.lang.Long
short - java.lang.Short
void - java.lang.Void

5. What are wrapper classes ?
Answer:  They are wrappers to primitive data types. They allow us to access primitives as objects.

6. What is casting?
Answer:  There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

No comments:

Post a Comment