Você está na página 1de 4

Exercises

Creating And Using Arrays


1) Consider the following code.

char let[] = { 'A', 'J', 'F', 'K' };


int[] num = { 4, 2, 1, 3, 0 };
System.out.println(let[num[num[num[3] num[2]]]]);

What does it print?


a) 1
b) 2
c) F
d) -1
e) 70

2) What kind of variables we declare below?

int[] peso, id;

a) It does not compile


b) 2 arrays of int
c) 1 array of int and 1 int
d) 2 variables of type int
e) none of above

3) What kind of variables we declare below?

int peso[], id;

a) It doesn't compile
b) 2 arrays of int
c) 1 array of int and 1 int
d) 2 variables of type int
e) none of above
Exercises
Creating And Using Arrays
4) Consider the following code.

class References {
String nomes;
String[] sobrenomes = new String[3];
public static void main(String... args) {
References temp = new References();
}
}

Where do the references temp.nomes, temp.sobrenomes and temp.sobrenomes[0]


point to, respectively?

a) objects of class String


b) null
c) null, array of objects of class String and null
d) null, objects of class String and objects of class String
e) null, references to String and references to String

5) Choose the option that does not compile.

a) []int y;
b) int y[];
c) int[]y;
d) int[ ]y;
e) int[ ] y;

6) In which line happens a compile error?

class Teste {
public static void main(String... a) {
int A[] = new int[23]; //B
int B[] = new int[3] { 0, 1, 2}; //C
int C[] = new int[] { 4, 5}; //A
int D[] = { 3, 4, 5 }; } } //D
Exercises
Creating And Using Arrays
a) D
b) A
c) C
d) B
e) The code compiles

7) What does the following code prints?

class A {
public static void main(String[] args) {
int x[] = {2,3,4};
System.out.println(x[x.length] + " " + x[x.length 1]);
}
}

a) 43
b) 34
c) 33
d) 44
e) It doesn't compile

8) Which of these statements are legal?

a) int arr[][] = new int[5][5];


b) int []arr[] = new int[5][5];
c) int[][] arr = new int[5][];
d) int[] arr = new int[5][];
e) int[] arr = new int[5][5];
f) int arr[] = new int[][5];
g) int []arr[] = new int[][5];
h) int arr[4] = new int[];
i) int arr[4] = new int[4];
j) int arr[] = new int[0];
Exercises
Creating And Using Arrays
9) Consider the following code and mark the correct option.

String x[] = new String[25];

a) x[24] is 0
b) x[24] is undefined
c) x[25] is '\0'
d) x[0] is "\0"
e) x[24] is null

10) What does the code below print?

int arr[] = new int[2];


System.out.println(arr[0]);

a) The code does not compile


b) The code throws an exception
c) It prints 0
d) It prints 1
e) Unpredictable result

Você também pode gostar