Sunday, July 28, 2024

Arrays MCQ

  What is an array in Java?

a) A data structure that stores elements of different data types

b) A collection of elements of the same data type, stored in contiguous memory locations

c) A special variable that can store multiple values of different types

d) A structure that allows dynamic resizing of elements


How is the length of an array determined in Java?

a) By using the length() method

b) By using the size() method

c) By using the length property

d) By using the size property


Which of the following declares an array in Java?

a) int[] array = new int[];

b) int array() = new int();

c) int array[];

d) array int[];


What is the index of the first element in an array in Java?

a) 0

b) 1

c) -1

d) The size of the array


Which of the following statements initializes an array of integers with a size of 5 in Java?

a) int[] numbers = new int[5];

b) int[5] numbers;

c) int numbers[5];

d) int numbers = new int[5];


What happens if you try to access an index outside the bounds of an array in Java?

a) It results in a compilation error.

b) It throws an ArrayIndexOutOfBoundsException.

c) It automatically resizes the array.

d) It returns null.


How is the size of an array determined after its initialization in Java?

a) It cannot be changed after initialization.

b) By using the resize() method

c) By using the length property

d) By using the size() method


Which of the following correctly copies elements from one array to another in Java?

a) array2 = array1;

b) array2 = array1.clone();

c) array2 = array1.copy();

d) array2 = array1.copyArray();


In Java, can an array store elements of different data types?

a) Yes, with specific conversions

b) No, arrays can only store elements of the same data type

c) Yes, by default without any restrictions

d) Yes, using the mixedDataType property


What is the syntax to access the third element of an array named arr in Java?

a) arr[3]

b) arr(3)

c) arr{3}

d) arr.at(3)


Answers:


b) A collection of elements of the same data type, stored in contiguous memory locations

c) By using the length property

c) int array[];

a) 0

a) int[] numbers = new int[5];

b) It throws an ArrayIndexOutOfBoundsException.

a) It cannot be changed after initialization.

b) array2 = array1.clone();

b) No, arrays can only store elements of the same data type

a) arr[3]


What is the declaration syntax for a single-dimensional integer array named numbers in Java?

a) int numbers();

b) int[] numbers;

c) numbers int[];

d) int numbers[]();


In Java, what is the index range for accessing elements in an array of size n?

a) 0 to n-1

b) 1 to n

c) -n to n-1

d) 0 to n


How are arrays initialized in Java?

a) numbers[] = new int[];

b) int numbers() = new int();

c) int[] numbers = new int[5];

d) int numbers[]();


What happens if you try to access an index beyond the array size in Java?

a) It results in a compilation error.

b) It throws an ArrayIndexOutOfBoundsException.

c) It returns the default value of the array's data type.

d) It returns null.


Which method is used to obtain the length of a single-dimensional array in Java?

a) length()

b) size()

c) length

d) size


What is the default initial value of an integer array in Java if no values are assigned explicitly?

a) 0

b) 1

c) -1

d) null


How can you assign values to an array in Java after its declaration?

a) numbers[] = {1, 2, 3};

b) numbers = {1, 2, 3};

c) numbers = new int[] {1, 2, 3};

d) All of the above


What is the correct way to access the third element of an array named arr in Java?

a) arr(3)

b) arr[3]

c) arr{3}

d) arr.at(3)


How do you find the length of an array named data in Java?

a) data.length()

b) data.size()

c) data.length

d) data.size


What is the role of an index in a single-dimensional array in Java?

a) It represents the size of the array.

b) It helps to locate and access elements within the array.

c) It defines the data type of the array.

d) It represents the array's capacity.


Answers:


b) int[] numbers;

a) 0 to n-1

c) int[] numbers = new int[5];

b) It throws an ArrayIndexOutOfBoundsException.

c) length

a) 0

c) numbers = new int[] {1, 2, 3};

b) arr[3]

c) data.length

b) It helps to locate and access elements within the array.


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...