1. What is Datatype and its function?
It is a type of data which tells the compiler to store and use the value. A string, for example, is a data type that is used to classify text and an integer is a data type used to classify whole numbers. The data type defines which operations can safely be performed to create, transform and use the variable in another computation.
2. What are the different types of JAVA datatype?
3. What are the default value and size of JAVA datatypes?
Data Type
|
Default Value
|
Default size
|
boolean
|
false
|
1 bit
|
char
|
'\u0000'
|
2 byte
|
byte
|
0
|
1 byte
|
short
|
0
|
2 byte
|
int
|
0
|
4 byte
|
long
|
0L
|
8 byte
|
float
|
0.0f
|
4 byte
|
double
|
0.0d
|
8 byte
|
4. Why char uses 2 bytes in JAVA?
In JAVA, char datatype uses Unicode system not ASCII code system. Characters in Java are indices into the Unicode character set. They are 16-bit values that can be converted into integers and manipulated with the integer operators, such as the addition and subtraction operators. The default char value in Java is \u0000.
5. What will be the output of following JAVA code?
class Widen{
public static void main(String[] args){
int a=10;
float f=a;
System.out.println(a);
System.out.println(f);
}
}
Output:
C:\Users\Durga>javac Tutorial_4.java
C:\Users\Durga>java Widen
10
10.0
Explanation:
Here, float variable is assigned with integer value implicitly. This is called data widening.
6. What will be the output of following JAVA code?
class Narrow{
public static void main(String[] args){
float f=10.5f;
//int a=f;//Compile time error
int a=(int)f;
System.out.println(f);
System.out.println(a);
}
}
Output:
C:\Users\Durga>javac Narrow.java
C:\Users\Durga>java Narrow
10.5
10
Explanation:
If integer variable is assigned with the float value implicitly, then it will shows an compilation error due to down casting of data type. Down-casting is not directly possible in Java, so type conversion done by explicitly.
7. What will be the output of following JAVA code?
class Learn{
public static void main(String[] args){
int a=131;
byte b=(byte)a;
System.out.println(a);
System.out.println(b);
}
}
Output:
8. Automatic type conversion in Java takes place when
A. Two type are compatible and size of destination type is shorter than source type.
B. Two type are compatible and size of destination type is equal of source type.
C. Two type are compatible and size of destination type is larger than source type.
D. All of the above
Answer: Option C
public static void main(String[] args){
int a=131;
byte b=(byte)a;
System.out.println(a);
System.out.println(b);
}
}
Output:
C:\Users\Durga>javac Test.java
C:\Users\Durga>java Learn
131
-125
Explanation:
In Java, byte value ranges from -128 to +127. Here, overflow range of byte value again starts assign value from -128 instead of 128. So the byte value of 131 becomes -125.
C:\Users\Durga>java Learn
131
-125
Explanation:
In Java, byte value ranges from -128 to +127. Here, overflow range of byte value again starts assign value from -128 instead of 128. So the byte value of 131 becomes -125.
8. Automatic type conversion in Java takes place when
A. Two type are compatible and size of destination type is shorter than source type.
B. Two type are compatible and size of destination type is equal of source type.
C. Two type are compatible and size of destination type is larger than source type.
D. All of the above
Answer: Option C
9.What will be the output of following JAVA code?
class Learn{
public static void main(String[] args){
byte a=10;
byte b=10;
byte c=a+b;
System.out.println(c);
}
}
Output:
Compilation error
Explanation:
Compile Time Error: due to incompatible types, possible lossy conversion from int to byte
because a+b=20 will be int even a and b are byte values. In order to avoid this, we need to do explicit conversion as follow: //byte c=(byte)(a+b);
🔙PREVIOUS - JAVA Programming - 1.2 Introduction to basic interview questions
🔜NEXT - JAVA Programming - 2.2 Data types - String
No comments:
Post a Comment