Working with Array

POST TEST


Question 1:
Determine output;
public clsss Test{
public static void main(string[] args){
int[] x ={1,2,3,4};
int[] y =x;
x=new int[2];
for(int i=0;i"<"x.lenght;i++)
system.out.print(y[i] + " ");
}
}
1,2,3,4
0,0,0,0
1,2
0,0


Question 2:
When you pass an array to a method,the method recives_________
A copy of the array
A copy of the first element.
The reference of the array.
The length of the array.


Question 3
What would be the result of attempting to compile and run the following code?
public clsss HelloWorld{
public static void main(string[] args){
double[] x =new double[](1,2,3);
system.out.println("vaue is "+ x[1]);
}
}
The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.
The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};
The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};
The program compiles and runs fine and the output


Question 4:
Which will legallydelare,construct and initialize an array?
int [] myList = {};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};


Question 5:
What is the value of a[1] after the following code is excuted?
int[] a={0,2,4,1,3};
for(int i=1; i"<"a.lenght; i++)
a[i] = a[(a[i]+3)%a.lenght];
0
1
2
3


Question 6:
Predict the output?
public 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]);
}
}
}
10,20,30,40,50
compile time error
10,10,10,10,10
run time error


Question 7:
Assuming int is of 4bytes, what is the size of int arr[15];?
15
30
45
60


Question 8:


What is the output of the following piece of code?
public class array{ public static void main(String args[]){ int []arr = {1,2,3,4,5}; System.out.println(arr[2]); System.out.println(arr[4]); } }
3 and 5
2 and 4
2 and 5
none of the above

Number of score out of 8 = Score in percentage =