Explore topic-wise InterviewSolutions in Current Affairs.

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.

  • length in Java
    • The length variable returns the length of an array i.e. a number of elements present in an array.
    • After initializing, the length of an array cannot be changed, so the length variable can directly be used to get the length of an array.
    • It is used only for an array.
    • Example:
public class InterviewBit { public static VOID MAIN(String args[]) { int array[] = {1, 2, 3, 4, 5}; System.out.println("Length of an array is: " + array.length); }}

OUTPUT

Length of an array is: 5
  • length() in Java
    • It is a static method of String class.
    • The length() returns the number of characters stored in a string object.
    • The string class uses this method as the length of a string can be modified using the various operations performed on a string object.
    • The String class uses a char[] array internally.
    • Example:
public class InterviewBit { public static void main(String args[]) { String STR = "Welcome to InterviewBit"; System.out.println("Length of String using length() method is: " + str.length()); }}

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»
  • ArrayStoreException is a runtime EXCEPTION
  • For example, you will GET this exception at RUN time if you declare a String ARRAY and then try to insert integer elements in the array. 
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»
  • An object REPRESENTS a thing with characteristics (called a PROPERTY), whereas an array CREATES a list of data and stores it in a single variable. USING brackets and dots, we can access, alter, and delete items from objects, while a variety of built-in methods and zero-based indexing allow us to access and modify items in arrays. We can iterate over object properties and array items using various different loops (e.g. for, for…in, for…of, forEach()).
  • All Java objects are DYNAMICALLY allocated on the heap. Unlike C++, where objects can be allocated in memory either on Heap or on Stack. When we use the new() method in C++, the object is allocated on the heap, otherwise on Stack if not global or static.
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.

ARRAY OPERATIONREAL TIME COMPLEXITYASSUMED TIME COMPLEXITY
Accessing the i-th element.O(√N)O(1)
Traversing all elements.O(N + √N)O(N)
OVERRIDE element at i-th index.O(√N)O(1)
Insert an element.O(N + √N)O(N)
Delete an element.O(N + √N)O(N)
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»
DefinitionAn Array is a collection of similar data types STORED in contiguous memory locations.An ArrayList is a class of Java Collections framework which contains popular classes like Vector, HashMap, etc.
Static/ Dynamicstatic in SIZE.dynamic in size.
ResizableAn array is not resizable as it is a fixed-length data structure.An ArrayList is a variable-length data structure that can be resized. As an element is being added to an ArrayList, JVM checks to see if it has enough space by calling the ensureCapacity method. If a space exists, it adds the element to the ArrayList, otherwise, it resizes the ArrayList.In the resizing process, an array of a larger size is created, the old array is COPIED to the new array using Arrays.copyOf, and the new array is then assigned to the existing array.
InitializationSize of an array should be declared directly or indirectly while initializing. An instance of ArrayList can be created without specifying its size. Java creates an ArrayList of default size.
PerformanceIt is fast in comparison to ArrayList as it has a fixed size.Slow as the resize operation in ArrayList slows down the performance.
Primitive/ Generic typeCan store both objects and primitives type.Arraylist automatically converts primitive type to object.
Iterating ValuesA for loop or for each loop is used to iterate over an array.An iterator is used to iterate over ArrayList.
LengthIt provides a length variable that DENOTES the length of an array.size() method can be used to DETERMINE the size of ArrayList.
Single/ Multi-DimensionalCan be multi-dimensional.Single-dimensional.
16.

Mention some advantages and disadvantages of Arrays.

Answer»

Advantages:

  • Multiple ELEMENTS of Array can be sorted at the same time.
  • Using the index, we can access any element in O(1) time.

Disadvantages:

  • You need to specify how many elements you're going to store in your array ahead of time and We can not INCREASE or decrease the size of the Array after creation.
  • You have to shift the other elements to FILL in or close GAPS, which takes worst-case O(n) time.
Previous Next