Core java interview Questions You'll Most Likely Be Asked (Second Edition)


following is the correct way to initialize the array?


Download 1.52 Mb.
Pdf ko'rish
bet16/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   12   13   14   15   16   17   18   19   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

following is the correct way to initialize the array?
int ttt = new int[3][3];
int[]ttt = new int[3][3];
int[][]ttt = new int[3][3];
int ttt [3][3] = new int[][];


029.
030.
Answer:
(c) Answer c is correct. The correct syntax for declaring a multi–
dimensional array is
int[][]ttt =new int[3][3];
Explain the primitive data types in Java.
Answer:
There are eight primitive data types in Java. These are as
follows:
Name
Width
(Bits)
Range
byte
8
–128 to 127
short
16
–32,768 to 32,767
int
32
–2,147,483,648 to 2,147,483,647
long
64
–9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float
32
1.4e–045 to 3.4e+038
double
64
4.9e–324 to 1.8e+308
char
16
0 to 65536
boolean
1
true/false
What are the possible ways to assign a decimal number to
a float variable?
Answer:
The following are the ways in which a decimal number can be
assigned to a float variable:


031.
032.
float fValue1 = 100.5f;
float fValue2 = 100.5F;
float fValue3 = (float) 100.5;
A decimal number is treated as a double by default. So, in order
to assign it to a float variable, it needs to be either followed with
‘f’ or ‘F’ or it should be explicitly be cast as a float type.
Identify the error in the following code snippet and explain
how it can be fixed.
byte bValue = 10; //line 1
bValue = bValue + 10; //line 2
Answer:
The above code will cause a compilation error at line 2. Line 2
assigns an int value to a byte variable and produces an int
result. So, when the int value is assigned to the byte variable
bValue, it causes a compilation error. This code can be fixed by
specifying an explicit cast as follows:
bValue = (byte) (bValue + 10);

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   12   13   14   15   16   17   18   19   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling