
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialProgramming
4.7 Rating 56 Questions 30 mins read5 Readers

This is a frequently asked question in Array interview questions.
An array is a type of data structure that we use to store data in contiguous memory locations. But this data should be of the same data type as the one used for array declaration.
The advantage of having a contiguous memory is that we can easily find the location of any element in the array by just finding its index value. For example, if we want to access an element present at the 3rd index(4th element) in an array arr, then we can write arr[3]. This is generally because when it comes to arrays, we mostly follow 0-based indexing and not 1-based indexing.
While this is good and all, an array cannot be resized once declared. This may sometimes lead to memory leaks which are not good for any programmer.
Expect to come across this popular question in Programming questions on Arrays.
The main difference between them is that array is static, meaning we have to specify the size of the array along with its data type to initialize an array which can sometimes become the reason for memory leaks. While in the case of ArrayList, no such memory leaks happen as it is dynamic, meaning it can adjust its size according to the input given.
This is because, in the case of an ArrayList, as soon as an element is added, JVM checks whether it has enough space or not by calling the ensureCapacity() method. If the space exists, the element gets added to the ArrayList, else the ArrayList gets resized.
In the resizing process, an array of a larger size is created, the old array is copied to the new array using the Arrays.copyOf method, and the new array is then assigned to the existing array.
Let us understand some more differences in a tabular format:
Array | ArrayList | |
|---|---|---|
Definition | An 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. |
Performance | It is fast as compared to ArrayList. | Its resizing ability slows it down. |
Type of Data Structure stored | It can store both objects and primitive data type like int, long, float, etc. | It cannot store primitive data type and automatically converts primitive data types into objects. |
Type - Safety | It is not type safe as we cannot save generics along with the array thus making the array convertible in nature. | ArrayList, on the other hand, can store generics. Hence is not convertible and thus is type-safe. |
In Java, when we initialize an array with the new keyword, the size argument is of the type int and a 32-bit Java int can go up to 2,147,483,647 elements which can be considered as the theoretical max limit of an array when it is initialized with an int data type.
However, in reality, the virtual machines of different operating systems may not allocate every bit of it to the array elements. Thus, the maximum number of elements a Java array can hold is typically a little less than the upper limit of a Java int.

In the above example, we tried to test out this theoretical max limit of an array but what we received was an OutOfMemory exception. This is because this is not the actual max limit of an array. Moreover, it totally depends upon the type of OS we are using.
Commonly we use the terms ‘size’ when we want to know how many elements an array can hold, on the contrary in java we do not have a size() method nor do we have a length() method in java. What we do have is the length property which we use to find out the length of the array or the number of elements it holds.
To confuse the matters even more, Java Collection class that implements a list interface does have a size() method. Collection classes like Vector, ArrayList, and Stacks have a size method that is used to calculate the number of elements present in them.
A must-know for anyone heading into an Array interview, this question is frequently asked in Array interview questions.
Advantages
Disadvantages
There are two ways of initializing an array:
But if for some reason we don't initialize the array in both scenarios, the array will be initialized with some random values, and at each memory location as in the array, elements are stored in contiguous order. These random values can again be categorized into two categories - Default values and Garbage values.
In the case of Java Programming Language, if the elements of the array which has already been declared are not set to their specific values, they will be initialized as the default values of the Data Type that was used for Array Declaration.


In the case of JavaScript, this default value is undefined. While in the case of C++, these random values will be the garbage values that the compiler has initialized the array with, and it's not a good thing for a programmer to rely on. While he can do so in the case of Java.
C++
JS
One of the most frequently posed Array coding questions, be ready for it.
The question is that you are given an array of size ‘n’ and a target value. You have to find the index at which this target value is and also tell its time complexity.
Approach
We will run a loop through the input array and check whether the given target value is present or not. If it’s present, we will return its index value, and if it's not, we will return -1.
We can also use Binary Search Algorithm, but we can only use it if the given array is sorted. And we just cannot sort our array as it will increase the time complexity to O(n log n).
Approach
import java.io.*;
import java.util.*;
class Main {
static void binarySearch(int v[], int x)
{
int lo = 0, hi = v.length - 1;
while (hi - lo > 1) {
int mid = (hi + lo) / 2;
if (v[mid] < x) {
lo = mid + 1;
}
else {
hi = mid;
}
}
if (v[lo] == x) {
System.out.println("Found At Index " + lo );
}
else if (v[hi] == x) {
System.out.println("Found At Index " + hi );
}
else {
System.out.println("Not Found" );
}
}
public static void main (String[] args) {
int v[]= {1, 3, 4, 5, 6};
int x = 1;
binarySearch(v, x);
x = 6;
binarySearch(v, x);
x = 10;
binarySearch(v, x);
}
}
Output
In Java, we have something known as JVM - Java Virtual Machine. It is mainly a part of JRE - Java Runtime Environment and acts as a run-time engine to run Java applications. JVM is the one that actually calls the main method present in a java code.
Now to answer the question as to why arrays are stored in the heap part of the JVM and not in any other part, we first have to understand that in Java reference types are stored in the Heap area. As arrays are also referenced types, (they can be created using the “new” keyword) they are also stored in the Heap area.
Let us understand this concept with the help of an example:
int myArray[]; //array declaration
myArray = new int[4];//memory allocation using the new keyword
myArray[0] = 25;//allocating memory
myArray[1] = 32;//allocating memory
myArray[2] = 56;//allocating memory
myArray[3] = 93;//allocating memory
The int myArray[] is just a reference to the array of four integers. While to allocate the memory to an array, we have to use the new keyword. See the diagram below to get a better understanding of the concept.
The above statement is true. This is because in Java language we are free to use as many array-specific syntaxes as we want for both initializing (putting explicit values into an array) and constructing (creating the array object itself) in a single line.
Let us see how the above statement is true. We have initialized a variable y as 56 and it is inside an array of length 5. Now the compiler will replace the value of the y variable with its value as we print the array. See the image below.
But what if we try to initialize the variable after initializing the same array. We will get an error. This is because we have initialized the y variable after using it in the array which becomes the root cause of the error as y is an unknown variable for him.
A staple in Array interview questions, be prepared to answer this one.
There are various reasons to choose a Linkedlist over an Array. Some of them have been listed below:
In the case of a Linked list, we can add any element in the middle at any index without replacing any element from the original list, which we, of course, can’t do in both static and dynamic arrays.
It's no surprise that this one pops up often in Array programming questions in Java.
The missing integer can be found using a very simple math formula - S = [n(n+1)]/2. Adding all the numbers given in the array and subtracting them from S will give us our answer or the missing number.
But this formula will not work if the array contains duplicates or is missing more than one number. If the array contains duplicates, sort the array and find and remove the duplicate element from the array and then use the above formula to find the missing number.
A common question in Array related interview questions, don't miss this one.
First of all, we won’t use the ‘==’ operator here, as it will not give the desired result, and it will compare them as objects.
The best way to check whether the two arrays are the same or not is first to compare their lengths, if they are the same, then we can proceed, but if not, return arrays' arrays are not the same.
Then sort both the arrays in ascending order and, using a loop compare each index value using the == operator - through this we can ensure that only the values are compared and not the whole array.
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args)
{
int arr1[] = {1, 2, 4, 5, 3};
int arr2[] = {3, 4, 2, 1, 5};
int N = arr1.length;
int M = arr2.length;
if (N != M)
System.out.println("Arrays are not equal");
Arrays.sort(arr1);
Arrays.sort(arr2);
for (int i = 0; i < N; i++)
if (arr1[i] != arr2[i])
System.out.println("Arrays are not equal");
System.out.println("Arrays are equal");
}
}
Output
There are other ways of checking whether the two arrays are the same or not - Using the Arrays.equals() method and Arrays.deepEquals() method.
A dynamic array is an array of variable size which will automatically expand its size when we try to insert a new element in it and there is no room for it. At this stage a new array is created with a new address and reference of double the size than the previous one so that the old elements as well the new elements can be appended. See the figure below.

In Java, Array works as a dynamic array while in C/C++ we have vectors showing us the same characteristics as of a dynamic array.
Implementation of Dynamic Array
In dynamic arrays elements are stored contiguously in the same manner from start to end but if after all the elements are appended and some space is left, that space is called reserved space.
We can add until that reserved space is consumed, but once it gets completely full then only a new array of double the size is created, and the same process continues ,of copying and inserting old and new elements in that new array and waiting until the reserved space gets filled. Or another way to add an element is that first, create a function that creates a new array of double size, copies all the elements from the old array, and returns the new array.
The table given below gives us a brief idea as to how much growth we can expect from different programming to show us when it comes to dynamic arrays:
| Programming Language | Growth Rate |
|---|---|
Java | 1.5 |
Python | ~1.125 |
C++ | 1.5 |
Go | 1.5 - 2 |
Yes, we can make both primitive data types and reference types volatile, but we can only make the variable pointing towards the array volatile and the whole array.
To understand this concept, first we have to understand that the concept of making an array volatile is simply taken to protect the elements of the array from multiple threads which can modify the data of the array, and that this is only possible with the help of volatile keywords.
The volatile keyword can be applied to both primitive data types and objects. It never caches the variable's value and always reads it from main memory. Although it cannot be used with classes or methods, it is perfectly fine with static variables. It stops the compiler from reordering code while ensuring visibility and ordering.
