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


How will you assign an Octal and Hexadecimal literal to an


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

How will you assign an Octal and Hexadecimal literal to an
“int” variable?
Answer:
Octal and Hexadecimal literals can be assigned to an int
variable as follows:
int iValue1 = 0100;
int iValue2 = 0xf1dead;


033.
034.
An Octal literal need to be prefixed with ‘0’. Octal values can only
use the digits 0–7
A Hexadecimal literal need to be prefixed with ‘0x’. Hexadecimal
values can use the digits 0–9 and the characters A, B, C, D, E,
and F.
In the code snippet below, which lines will compile and
which will cause an error?
char cValue1 = ‘@’; //Line 1
char cValue2 = ‘\u004E’; //Line 2
char cValue3 = 80000; //Line 3
char cValue4 = (char)–100; //Line 4
char cValue5 = 128; //Line 5
Answer:
Except Line 3, all lines will compile. Line 3 causes a compilation
error because the range of character is from 0 to 65,536. In order
for the line 3 to compile we have to modify the line as below:
char cValue3 = (char) 80000;
What happens when the following code is compiled and
executed?
int [] iArray = new int[5];
int iInt =–5;
iArray[iInt] = 5;
Answer:
The code above compiles fine but on execution it will throw an
ArrayIndexOutofBoundsException. This is because iInt
is a negative number which cannot be used as an array index.


035. Consider the following code snippet and explain its
output.
public class ArrayDec {
public static void main (String argv[]){
ArrayDec ad = new ArrayDec ();
ad.arrayMethod();
}
public void arrayMethod () {
int intArr1[]= {1,2,3};
int[] intArr2 = {1,2,3};
int intArr3[] = new int[] {1,2,3};
System.out.print(intArr3.length);
}
}
Answer:
The code above compiles fine and produces the output 3. Since
all the array declarations are correct, the code compiles fine. It
prints the length of the intArr3 which is 3.




Download 1.52 Mb.

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




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