Java Questions & Answers – Data Structures-Arrays

This set of Tricky Java Questions and Answers focuses on “Data Structures-Arrays”.

1. What is the type of variable ‘b’ and ‘d’ in the following Java snippet?

int a[], b;
int []c, d;

a) ‘b’ and ‘d’ are int
b) ‘b’ and ‘d’ are arrays of type int
c) ‘b’ is int variable; ‘d’ is int array
d) ‘d’ is int variable; ‘b’ is int array
View Answer

Answer: c
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.
advertisement
advertisement

2. Which of these is an incorrect array declaration?
a) int arr[] = new int[5] ;
b) int [] arr = new int[5] ;
c)

int arr[];
arr = new int[5];

d) int arr[] = int [5] new;
View Answer

Answer: d
Explanation: Operator new must be succeeded by array type and array size. The order is important and determines the type of variable.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. What will be the output of the following Java code?

  1. int sf_arr[] = new int [5];
  2. System.out.print(sf_arr);

a) 00000
b) Value at sf_arr[0] c) Error
d) Class name@ hashcode in hexadecimal form
View Answer

Answer: d
Explanation: In this Java code, we are trying to print the array sf_arr, but sf_arr is an array of integers, and when we directly print it using System.out.print, we will get a reference to the array object rather than the elements of the array, its format will be class name followed by an “@” sign and the object’s hash code in hexadecimal form.

4. What will be the output of the following Java code snippet?

advertisement
advertisement
  1. Object[] names = new String[3];
  2. names[0] = new Integer(0);

a) ArrayIndexOutOfBoundsException
b) ArrayStoreException
c) Compilation Error
d) Code runs successfully
View Answer

Answer: b
Explanation: 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 type of array.

5. Generics does not work with?
a) Set
b) List
c) Tree
d) Array
View Answer

Answer: d
Explanation: Generics gives the flexibility to strongly typecast collections. Generics is applicable to Set, List and Tree. It is not applicable to Array.

6. How to sort an array?
a) Array.sort()
b) Arrays.sort()
c) Collection.sort()
d) System.sort()
View Answer

Answer: b
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a valid class.

7. How to copy contents of array?
a) System.arrayCopy()
b) Array.copy()
c) Arrays.copy()
d) Collection.copy()
View Answer

Answer: a
Explanation: Arrays class contains various methods for manipulating arrays (such as sorting and searching). Array is not a valid class.

8. Can you make an array volatile?
a) True
b) False
View Answer

Answer: a
Explanation: You can only make variable pointing to array volatile. If an array is changed by replacing individual elements then guarantee provided by volatile variable will not be held.

9. Where is an array stored in memory?
a) heap space
b) stack space
c) heap space and stack space
d) first generation memory
View Answer

Answer: a
Explanation: Array is stored in heap space. Whenever an object is created, it’s always stored in the Heap space and stack memory contains the reference to it.

10. An array elements are always stored in ________ memory locations.
a) Sequential
b) Random
c) Sequential and Random
d) Binary search
View Answer

Answer: a
Explanation: Array elements are stored in contiguous memory. Linked List is stored in random memory locations.

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice tricky questions and answers on all areas of Java, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.