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.