Professional Documents
Culture Documents
Arrays 1.2
Arrays 1.2
Topic/Course
Sub-Topic (Example: name of college)
Array
• Java array is an object which contains fixed set of elements of a similar
data type.
• Array can contains primitives data types as well as objects of a class
depending on the definition of array.
• Index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
• Can get the length of the array using the length member.
Types
//instantiation
var = new datatype[size];
//instantiation
var = new datatype[rowsize][columnsize];
Example:
class Test {
public static void main(String args[]) {
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
class Test {
public static void main(String args[]) {
int arr[]= new int[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
A) 0 B) 1
C) 2 D) 3
Question 6
Predict the output
public static void main(String args[]){
int[] x = {5,6,7,8,9};
int[] y = x;
y[2] = 10;
System.out.println(x[2]);
System.out.println(y[2]);
}
A) 10 10 B) 7 10
class Sample{
public static void main(String args[]){
int[] x = {5,6,7,8,9};
int y = x;
System.out.println(y);
}
}
C) Address of x D) None
Question 8
Predict the output
A) 1 B) 6
A) 2 B) 3
C) 4 D) 9
Question 10
Predict the output
class Test2 {
public static void main(String[] args)
{
String str[] = { “Java", “Programming", “Done" };
System.out.println(str.length);
System.out.println(str[0].length);
}
}
A) Error B) 3 5
C) 3 13 D) None
THANK YOU