MCQ

Java MCQ – Data structures (Arrays)

This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Data structures (Arrays)”.
 

1. What is the type of variables “b” and “d” in the following code?
int a[], b;
int []c, d;

A ‘b’ and ‘d’ are of type int

B ‘b’ and ‘d’ are arrays of type int

C ‘b’ is a variable of type int; ‘d’ is an array of type int

D ‘d’ is a variable of type int; ‘b’ is an array of type int

C
If square brackets [] is declared after the variable, it only applies to a single variable. If square brackets [] is declared before the variable, it applies to all variables.

 

 

2. Which of the following statements is incorrect?

A

int arr[];
arr = new int[9];

B

int arr[] = int [9] new;

C

int [] arr = new int[9];

D

int arr[] = new int[9] ;
B
The “new” operator must be replaced by the type of array and the size of the array. The order is important. Example: A, C, et D are corrects.

 

 

3. What does this code display?
int arr[] = new int [9];
System.out.print(arr);

A 00000

B 0

C value stored in arr[0]

D Garbage value

D
“arr” points to an array of integers. System.out.print(arr); will display the garbage value. This is not the same as displaying arr[0].

Garbage value designates the unused values available in memory when it is declared.

 

 

4. What does this code display?
Object[] cars = new String[3];
cars[0] = new Integer(0);

A The code runs successfully

B Compilation error

C ArrayStoreException

D ArrayIndexOutOfBoundsException

C
“ArrayIndexOutOfBoundsException” comes when code tries to access an invalid index for a given array. “ArrayStoreException” comes when you have stored an element of type other than the array type.

 

 

5. Generic type in java does not work with _______?

A Array

B List

C Tree

D Set

A
Generic type in java offers the flexibility to strongly caster collections. Generic types apply to “Set, List and Tree” and they do not apply to “Array”.

 

 

6. How to sort an array?

A System.sort()

B Collection.sort()

C Arrays.sort()

D Array.sort()

C
The Arrays class contains various methods of manipulating arrays (such as sorting, searching, etc.). Array is not a valid class.

 

 

7. How to copy the contents of an array?

A Arrays.copy()

B Array.copy()

C Collection.copy()

D System.arrayCopy()

D
The System class contains various manipulation methods (such as display, hash …). Array is not a valid class.

 

 

8. Can you make an array volatile?

A True

B False

A
You can only make a variable pointing to the volatile array. If the array is modified by replacing the individual elements, the guarantee provided by the volatile variable will not be retained.

 

 

9. The elements of an array is always stored in ________ memory?

A Sequential

B Sequential and random

C Random

D Binary search

A
The elements of an array are stored in contiguous memory. Linked list is stored in a random memory.

 

 

10. Array are stored in which memory space?

A heap space

B stack space

C heap space and stack space

D first generation memory

A
Array is stored in heap space. Whenever an object is created, it is always stored in heap space and the stack keeps the reference.

 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *