[go: up one dir, main page]

0% found this document useful (0 votes)
159 views10 pages

ARRAY

The document contains questions about arrays in Java. It asks about array declaration, initialization, accessing elements, and using arrays in for loops. Key points: - Arrays are declared with the data type followed by empty square brackets, like int[] or String[]. - The new keyword is used to initialize an array, specifying the size in square brackets, like new int[5]. - Array indexes start at 0, so a 5 element array ranges from 0 to 4. - Individual elements are accessed using square brackets and the index, like arrayName[0]. - The length property returns the number of elements in the array. - For loops are commonly used to iterate through

Uploaded by

tine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views10 pages

ARRAY

The document contains questions about arrays in Java. It asks about array declaration, initialization, accessing elements, and using arrays in for loops. Key points: - Arrays are declared with the data type followed by empty square brackets, like int[] or String[]. - The new keyword is used to initialize an array, specifying the size in square brackets, like new int[5]. - Array indexes start at 0, so a 5 element array ranges from 0 to 4. - Individual elements are accessed using square brackets and the index, like arrayName[0]. - The length property returns the number of elements in the array. - For loops are commonly used to iterate through

Uploaded by

tine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ARRAY

Questions 1-5 refer to the following piece of Java code:


String[] word = new String[5];
1. What is the array name?
2. What is the base type?
3. What is the length of the array?
4. What is the range of values an index accessing this array can have?
5. What is one of the indexed variables (or elements) of this array?
Questions 6-8 refer to the following piece of Java code:
double[] score = new double[10];
6. What is the value of score.length ?
7. What is the fi rst index of score ?
8. What is the last index of score ?
9. What is the output of the following code?
char[] letter = {'a', 'b', 'c'};
for (int index = 0; index < letter.length; index++)
System.out.print(letter[index] + ", ");
10. What is the output of the following code?
double[] a = {1.1, 2.2, 3.3};
System.out.println(a[0] + " " + a[1] + " " + a[2]);
a[1] = a[2];
System.out.println(a[0] + " " + a[1] + " " + a[2]);
11. Consider the following class defi nition:
public class SomeClass
{
public static void doSomething(int n)
{
<Some code goes in here.>
<The rest of the definition is irrelevant to this question.>
Which of the following are acceptable method calls?
int[] a = {4, 5, 6};
int number = 2;

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

int buses[] = new int[5];

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] It copies what is in cell 0 into all other cells.


[b] It changes every element to the value of x.
[c] It copies 0 into every element.
[d] It puts the array into numerical ascending order.
18. What is the output of the following code fragment? Answer: D
int [ ] evens = {2, 4, 6, 8, 10};
evens[0] = 44;
evens[4] = evens[2];
System.out.println(evens[0] + " " + evens[4]);

[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] 6.5 9.5


[b] 12.5
[c] 15.0
[d] 9.5 12.5
22. What is the output for the following code fragment? Answer: D
int [ ] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int j = 0; j < numbers.length; j++)
System.out.print(numbers[ j ] + " ");

[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] 2004 2006 0


[b] 2004 2006
[c] 4010
[d] an error will occur
25. What is the output for the following code fragment? Answer: B
int [ ] array = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
for (int i = 0; i < 4; i++)
System.out.print(array[i] + " ");

[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] Joe Sue Tom Jill Patty


[b] Patty Jill Tom Sue Joe
[c] Joe Tom Patty
[d] Sue Jill
28. What is the output of the following code fragment? Answer: D
int [ ] array = { 2, 4, 6, 8, 1, 3, 5, 7};
int george = 0;
for (int i = 0; i < array.length; i++)
george += array[i];
System.out.print(george);

[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)
{...
}

[a] int value = SomeMethod(money, grades);


[b] SomeMethod(money, grades);
[c] double value = SomeMethod(money, grades);
[d] int value = SomeMethod(money);
31. Which of the following is a valid statement? Answer: B
A. char[] c = new char(); C. char[] c = new char(4);
B. char[] c = new char[5]; D. char[] c = new char[];
32. Which of these is an incorrect array declaration? Answer: D

A. int arr[] = new int[5]

B. int [] arr = new int[5]

C. int arr[]

arr = new int[5]

D. int arr[] = int [5] new

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}; 

37. Array data access using Answer: C


A. Operator
B. Variable
C. index
D. Pointer
38. Index in array start with Answer: B
A. -1
B. 0
C. 1
D. infinite
39. What will be the output of the program? Answer: A

class Main

public static void main(String args[]) {

int arr[] = {10, 20, 30, 40, 50};

for(int i=0; i < arr.length; i++)

System.out.print(" " + arr[i]);

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

int[] marks = {35,65,95};


System.out.print(marks.length + "," + marks[1]);

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

You might also like