0% found this document useful (0 votes)
18 views

Array

Uploaded by

faisalgaming150
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Array

Uploaded by

faisalgaming150
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q1.What is an Array in Java?

a) A collection of elements with different types


b) A collection of elements with the same type
c) A resizable data structure
d) A container for storing key-value pairs

Answer:b)A collection of elements with the same type

Q2.What is the index range for the elements of an array in Java?


a) 0 to length - 1
b) 1 to length
c) -1 to length - 1
d) 0 to length
Answer:a)0 to length - 1

Q3.How do you access an element in an array in Java?


a) By using the element's value
b) By using the element's index
c) By using the element's key
d) By using the element's label
Answer:b)By using the element's index

Q4.What happens if you try to access an array element with an index that is out of
bounds?
a) A runtime exception is thrown
b) The program terminates abruptly
c) The element value is set to null
d) The element value is set to 0
Answer:a)A runtime exception is thrown

Q5.Which of the following is used to declare,construct, and initlaize an array?


A. int arr [] [] = {1, 2, 3, 4};
B. int [] arr = (1, 2, 3);
C. int [] arr = {};
D. int arr [] = {1, 2, 3};
Answer:D)int arr [] = {1, 2, 3};

Q6.Index in array start with ______.


A. -1
B. 0
C. 1
D. infinite
Answer:B)0

Q7.We can calculate the length of an array using ________.


A. sizeof(array)
B. array.len
C. array.length
D. array.sizeof()
Answer:C)array.length

Q8.Which of the following is advantage of java array?


A. Code Optimization
B. Random access
C. Size No-Limit
D. Both A and B
Answer:D)Both A and B

Q9.In java, array elements are stored in ________ memory locations.


A. Random
B. Sequential
C. Sequential & Random
D. Binary search
Answer:B)Sequential

Q10.What will be output for the following code?


class Main
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System.out.print(n);
}}
A. 3
B. 0
C. 6
D. 1
Answer:A)3

Q11.What is the type of variable "b" and "d" in the below snippet?
int a[], b;
int []c, d;
A. "b" and "d" are int
B. "b" and "d" are arrays of type int
C. "d" is int variable; and "b" is int array
D. "b" is int variable; and "d" is int array
Answer: D
Explanation: If [] is declared after variable it is applicable only to one
variable.
If [] is declared before variable it is applicable to all the variables.

Q12.Which is not a true statement about an array?


a) An array expands automatically when it is full.
b) An array is allowed to contain duplicate values.
c) An array understands the concept of ordered elements.
d) An array uses a zero index to reference the first element.
Answer:a) An array expands automatically when it is full.

Q13.What will be output for the following code?


class Test
{
public static void main (String[] args)
{
int arr1[] = {1, 2, 3};
int arr2[] = {1, 2, 3};
if (arr1.equals(arr2))
System.out.println("Same");
else
System.out.println("Not same");
}
}
a)Same
b)Not same
Answer:b)Not same(arr1.equals(arr2) is same as (arr1 == arr2))

Q14.Arrays in java are-


a)object references
b)objects
c) primitive data type
d)None
Answer:b)objects

Q15.Cell numbers of a dimensional array are also known as:


a)packets
b)blocks
c)subscripts
d)compartments
Answer:c)subscripts

You might also like