ARRAY
ARRAY
SomeClass.doSomething(number);
SomeClass.doSomething(a[2]);
SomeClass.doSomething(a[3]);
SomeClass.doSomething(a[number]);
SomeClass.doSomething(a);
12. How many of the following are legal declarations? Answer: C
[]double lion;
double[] tiger;
double bear[];
A. None
B. One
C. Two
D. Three
13. How do you determine the number of elements in an array? Answer: A
A. buses.length
B. buses.length()
C. buses.size
D. buses.size()
14. Which of the following declares an array of integers named number? Answer: B
[a] int number ;
[b] int [ ] number ;
[c] int new number [ ] ;
[d] int number = int [ ] ;
15. What is the output of the following code fragments? Answer: C
int [ ] fun = new int [5];
fun[0] = 1;
fun[1] = 2;
fun[2] = 3;
fun[3] = 4;
fun[4] = 5;
int j = 3;
System.out.println(fun[ j-1]) ;
[a] 1
[b] 2
[c] 3
[d] 4
16. What is the output of the following code fragment? Answer: C
int [ ] odd = {1, 3, 5, 7, 9, 11 };
System.out.println( odd[0] + " " + odd[3] ) ;
[a] 1 5
[b] 6
[c] 1 7
[d] 8
17. What does the following method do? Answer: A
public static void numbers (int [ ] num)
{
for(int x = 1; x < num.length; x++)
num[x] = num[0];
}
[a] 44 10
[b] 2 10
[c] 54
[d] 44 6
19. What are the subscripts for this array? int [ ] k = { 11, 12, 13, 14, 15}; Answer: A
[a] 0, 1, 2, 3, 4
[b] 1, 2, 3, 4, 5
[c] 11, 12, 13, 14, 15
[d] 10, 11, 12, 13, 14
20. What is the output of the following code fragment? Answer: B
int [ ] items = {2, 7, 3, 5, 8, 9};
int funny = items[0];
for (int i = 0; i < items.length; i++)
{
if (items[ i ] > funny)
funny = items[ i ];
}
System.out.print(funny);
[a] 2
[b] 9
[c] 2 3 5 7 8 9
[d] 2 7 3 5 8 9
21. What is the output of the following code fragment? Answer: B
double [ ] x = new double [ 4];
x[0] = 8.5;
x[1] = 6.5;
x[2] = 9.5;
x[3] = 12.5;
System.out.println(x[1 + 2]);
[a] 1 2 3 4 5
[b] 6 7 8 9 10
[c] 2 3 4 5 6 7 8 9 10
[d] 1 2 3 4 5 6 7 8 9 10
23. What is the output for the following code fragment? Answer: A
int [ ] a = new int [5];
a[0] = 5;
a[1] = 4;
a[2] = 3;
System.out.print(a[0] + a[1] + a[4]);
[a] 9
[b] 5 3 1
[c] 5 3 0
[d] 8
24. What is the output for the following code fragment? Answer: D
int [ ] car = new int [ 7];
car[0] = 2004;
car[1] = 2006;
System.out.println(car[0] + " " + car[1] + " " car[7]);
[a] 2 4 6 8 10
[b] 2 4 6 8
[c] 2 4 6
[d] 2 4 6 8 10 12 14 16 18 20
26. What is the output for the following code fragment? Answer: A
int [ ] dedo = { 1, 3, 6, 9, 2, 5, 7};
int que = dedo[0];
for (int k = 0; k < dedo.length; k++)
{
if (dedo[k] < que)
que = dedo[k];
}
System.out.print(que);
[a] 1
[b] 2
[c] 1 3 6 9 2 5 7
[d] 7
27. What is the output from the following code fragment? Answer: C
String [ ] name = { "Joe", "Sue", "Tom", "Jill", "Patty"};
for (int i = 0; i < name.length; i = i + 2)
System.out.print(name[i] + " ");
[a] 2 4 6 8 1 3 5 7
[b] 2 4 6 8
[c] 7
[d] 36
29. What is the length of this array? Answer: C
double[ ] stuff = {1.5, 2.5, 3.5, 4.5, 5.5, 6.5};
[a] 1
[b] 5
[c] 6
[d] 7
30. Which code line could possibly "call" this method? Answer: A
public static int SomeMethod(double[ ] array, int[ ] number)
{...
}
C. int arr[]
33. What is the result of the following program after compiling and running it? Answer: A
public class Test {
public static void main(String[] args)
{
int [] a = new int[0];
System.out.print(a.length);
}
}
A 0
B Compilation error, arrays cannot be initialized to zero size.
C Compilation error, it is a.length() not a.length
D None of the above
34. What is the result of the following program after compiling and running it? Answer: B
public class Test {
public static void main(String[] args)
{
int [] arr = {120,200,016};
for (int index = 0; index <arr.length; index++)
System.out.print(arr[index] + " ");
}
}
A 120 200 16
B 120 200 14
C 120 200 016
D 016 is a compile error. It should be written as 16.
35. What is the result of the following program after compiling and running it? Answer: D
public class Test {
public static void main(String[] args)
{
int [] arr = {0,2,4,1,3};
for (int index = 0; index <arr.length; index++){
arr[index] = arr[(arr[index] + 3) % arr.length];
System.out.print(arr[index] + " ");
}
}
}
A 0 1 2 3 4
B 1 2 3 0 4
C 2 3 0 1 0
D 1 1 4 3 1
36. What will legally declare, construct, and initialize an array? Answer: D
A int [] myList = {};
B int [] myList = (5, 8, 2);
C int myList [] [] = {4,9,7,0};
D int myList [] = {4, 3, 7};
class Main
A. 10 20 30 40 50
B. Compiler Error
C. 10 20 30 40
D. None of the above
40. Which are the special symbols used to declare an Answer: C
array in Java?
A) Braces { }
B) Parentheses ()
C) Square Brackets [ ]
D) Angled Brackets < >
41. Which are the special symbols used to initialize an array at the time of the declaration itself? Answer: A
A) Braces { }
B) Parentheses ()
C) Square Brackets [ ]
D) Angled Brackets < >
42. What is the output of the below Java program? Answer: C
A) 2,65
B) 3,95
C) 3,65
D) Compiler error
43. Which is the correct way of knowing Array Size in Java? Answer: B
A)
//int[] ary;
ary.length()
B)
//int[] ary;
ary.length
C)
//int[] ary;
ary->length()
D)
//int[] ary;
ary->length
44. What is the output of the below Java program with arrays? Answer: C
public class Polo {
public static void main(String args[])
{
String[] computer = {"RAM","HDD","MOUSE"};
String[] parts = {computer[0],computer[2]};
System.out.print(parts[1]);
}
}
A) RAM
B) HDD
C) MOUSE
D) Compiler error
45. If an index of an element is N, what is its actual position in the array? Answer: C
A) N-1
B) N
C) N+1
D) N+2