This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
Can the sizeof operator be used to tell the size of an array passed to a function? |
|
Answer» Passing an ARRAY as a parameter in C or C++ does not PASS information about how many elements there are in the array. Although sizeof() can tell you the size of the pointer and the size of the type it points to, it cannot tell you how many bytes are OCCUPIED by the ENTIRE array. |
|
| 2. |
What is a Jagged Array in Java? |
|
Answer» Jagged arrays are multidimensional arrays in which the member arrays are of different sizes. As an example, we can make a 2D ARRAY where the first array contains three elements, and the SECOND array consists of four elements. Below is an example demonstrating the concept of jagged arrays. public class InterviewBit { public static void MAIN(String[] args){ int[][] 2dArray = new int[2][]; 2dArray[0] = new int[3]; 2dArray[1] = new int[4]; int counter = 0; for(int row=0; row <2dArray.length; row++){ for(int COL=0; col < 2dArray[row].length; col++){ 2dArray[row][col] = counter++; } } for(int row=0; row < 2dArray.length; row++){ System.out.println(); for(int col=0; col < 2dArray[row].length; col++){ System.out.print(2dnArray[row][col] + " "); } } }}Output: 0 1 23 4 5 6 |
|
| 3. |
What is the difference between length and length () in Java? |
|
Answer» In Java, the length() is a method of String class whereas length is an instance VARIABLE of an array.
Output: Length of String using length() method is: 23 |
|
| 4. |
We know that Arrays are objects so why cannot we write strArray.length()? |
|
Answer» We cannot WRITE strArray.length() because length is not a method, it's a data item of an ARRAY. We can USE the methods of OBJECT like toString() and hashCode() against Array. |
|
| 5. |
When will we get ArrayIndexOutOfBounds Exception? |
|
Answer» ArrayIndexOutOfBoundsis a RUNTIME exception that occurs when the PROGRAM tries to ACCESS the INVALID index of an array such as an Index higher than the SIZE of the array or a negative index. |
|
| 6. |
When will we get ArrayStoreException? |
| Answer» | |
| 7. |
Can a Negative number be passed in Array size? |
|
Answer» No, a negative NUMBER cannot be PASSED as ARRAY size. If you pass a negative number in Array size then you will get the NegativeArraySizeException at RUN time. |
|
| 8. |
Find the Target Element in an array. |
|
Answer» First of all, you will get a number n, which indicates the size of the ARRAY. After that, you will get n more inputs corresponding to each index of the array. Then you will be given a target, for which you have to FIND, at which index of array target is present. Print -1 if target is not present in the array. Approach: We will RUN a for loop on the input array and check if the value equivalent to target is present in the array or not. If the target is found then we will print the index of this target value and return else we will return -1. import java.io.*;import java.util.*;public class InterviewBit { public static void main(String[] args) throws Exception { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scn.nextInt(); } int target = scn.nextInt(); for (int i = 0; i < arr.length; i++) { if (target == arr[i]) { System.out.println(i); return; } } System.out.println(-1); }} |
|
| 9. |
Where is an Array stored in JVM memory? |
|
Answer» An Array is an object in java. So, Array is stored in HEAP memory in Java VIRTUAL Machine. |
|
| 10. |
Difference between Array and Object. |
Answer»
|
|
| 11. |
Can you declare an array without assigning the size of an array? |
|
Answer» No, we cannot declare an ARRAY without ASSIGNING size. If we declare an array without size, it will throw COMPILE TIME error. |
|
| 12. |
What is the time complexity for performing basic operations in an array? |
||||||||||||||||||
|
Answer» The Time COMPLEXITY of different operations in an array is: For analyzing the real-time complexity you also have to CONSIDER the time in bringing the block of memory from an EXTERNAL device to RAM which takes O(√N) time.
|
|||||||||||||||||||
| 13. |
What is the default value of Array in Java? |
|
Answer» If we don't SPECIFY the values by ourselves, then Java ASSIGNS default values in them which are 0 for BYTE, short, int, and long, 0.0 for FLOAT and double, false for boolean, and null for OBJECTS respectively. |
|
| 14. |
What will happen if you do not initialize an Array? |
|
Answer» The ARRAY will TAKE DEFAULT values DEPENDING upon the data TYPE. |
|
| 15. |
Difference between Array and ArrayList in Java. |
|||||||||||||||||||||||||||
Answer»
|
||||||||||||||||||||||||||||
| 16. |
Mention some advantages and disadvantages of Arrays. |
|
Answer» Advantages:
Disadvantages: |
|