Array and ArrayList in Java | Selenium Interview Question

Recently one of my friend has gone for interview in one of the leading travel domain company where he was asked about Array and ArrayList and interviewer was keen about performance of value insertion in Array and ArrayList.

So lets begin with small introduction of Array and ArrayList.

Array is a data structure defined in core java which stores multiple data of same type (Can store Object as well) and it is of fixed size.But if you want to learn more about Array and its implementation then I would suggest you, Read Arrays in Java and its implementation in WebDriver.

Similarly ArrayList is part of Collection Framework which store similar kind of Objects and in the core of it, it use array. So if you want to learn more about ArrayList then I would suggest you to read Java Doc For ArrayList .

So up to now we understand that both stores the data of same type but one have primitive data type along with object and other only takes object. But this is not the only thing that we need to remember. There are some parameters over whichArray and ArrayList we need to analyse.

Primitive: Array can store primitive data type like int, char, double etc and objects While ArrayList can only store objects in it.

But if you want to add any primitive in ArrayList in that case it converts primitive data type to equivalent Object define in Java using Auto-boxing.

So here 1 which is int, internally get converted to Integer Object.

Size: 1- In Layman term storage capability of Array is fixed while ArrayList has dynamic storage capability.
2- In Array we define size of array at time of its initialisation while In ArrayList we never define the size at the time of its initialisation. But internally ArrayList use concepts of Array and when ever we create any ArrayList, it creates one Array of some specific size and once this size is attained, It dynamically creates array of double of initial size and copy initial data in new array that is dynamically created.

 

Support to Generics: 1- ArrayList is type-safe while Array is not.

2- Type-safety in Java is handled using Generics and ArrayList use generics while this can’t be used in array. If any time we try to use generics in that case it throws exception ArrayStoreException because instance of Array would not be able to convert it in to the type defined for Array.

Dimensions: Array can be defined multi-dimensional while ArrayList can be one dimensional.

Iteration: If user wants to iterate through each element of ArrayList then he/she needs to use Iterator from Java or For each loop to look into or to access each element, While it can be done through for loop and For Each loop for array.

Performance: this is one factor that takes constant time in both (Up to initial size of ArrayList) if we are calling add() and get() in ArrayList.But automatic resizing in ArrayList might cause slowness in insertion of data (Adding Data in ArrayList).

Length: Array uses length variable to find actual size or length of array but in case of ArrayList, we need to call size() function to find the length of ArrayList.

So this was the basic difference between Array and its dynamic brother ArrayList.

Hope this post will be helpful.

Happy Learning…

If you want to learn more about Java then please read Java Tutorial.

 

 

5 Commonly Asked Java String Question in Selenium Interview

Nowadays interview’s conducted for Automation Engineers are having lots of Java Question. Sometimes the same question with small changes is asked which have multiple approaches to implement. One of the most favourite parts in java for Automation Engineers is String.

Find Duplicate String Using Java

So here I am listing top 5 String questions asked in Interview and I am pretty sure you would find it helpful and will answer these question easily if asked by an interviewer.

But if you want to learn about String in Java, then I would suggest you, read Exploring String class in Java.

For more java tutorial I would suggest reading Java Tutorial.

Question 1: Write a program to reverse a string.

Input String:  “This is the string to reverse”

Final Output: “esrever ot gnirts eht si sihT”

Answer: 

Above question can be answered in many ways but here I am going to post two solutions. In which one is the simplest without giving much stress to mind just by using basic java concept and the second one is the most efficient that I have found(This solution can be used in reversing array as well)

Solution 1: 

  • First, we will receive the string (Either in form of variable or Scanner method)
  • We will find the length of the string.
  • We will use StringBuilder and will append the last character as the first character

Note:  In all my program I am using TestNG (@Test).

Here is the output on the console:

 

Solution 2:

  1. First, We will take String(In this example we will take the string in one variable ) and will convert String into StringBuilder.
  2. After conversion from String to StringBuffer, We will find the length of StringBuilder.
  3. Finally, we will swap characters from both ends until we will reach to mid of string.

Here is the code :

Here is the output on the console:

A task for you:

Create a Function which takes String and gives output as reversed String.

 

Question 2: In the above question 1, we have been asked to reverse complete string character by character but at the same time interviewer might ask to reverse words in place of each character like this.

Input String: “This is the string to reverse”

Output String: “reverse to string the is This”

Answer:

This question again going to have many approaches but I am going to post the most optimal way where we will use the split function of string.

Solution:

  1. Take the String (In this example we are going to use a variable in place of Scanner class) and will break the string in an array using the split function of String using regular expression space “\\s”.
  2. We will find the length of the array and will iterate the array in reverse.

Here is the output on the console:

 

Question 3: Find duplicate characters in the provided String and also find the count of each duplicate character.

Input String: “Bharat”

Output: a

 

Answer:
If the above question was asked to find only the duplicate character then it was just a simple question that can be solved using the loop and some of the concept of an array. But as soon as the count is added with this question it becomes a very good question to solve.

 

Solution: For this first, we need to find duplicate value and its count where the character could be unique but its count might change with each iteration. So here we need to use hashmap where the key will remain constant and could not be duplicate but its value could be updated using its key.

  1. We will take the String.
  2. We will convert it in character array.
  3. We will define one hashmap where each character would act as a key and its count as value. Here we will check the presence of the key in the map and if the key exists we will increase the count of character or if it doesn’t present as the key in the map we will add it.
  4. In the end, we will print the key and value pair where the key would be duplicate character and value would be its count.

Note:  Here I am assuming that lower case and upper case are being counted as same

But if you want to count both separately then replace this line of code

to this

So here is the code

Here is the output on console(Pasting extract of the console)

 

Question 4:

In Java write one program which finds duplicate words in the string and also find the count of the word.

 

Answer:

This question can be solved in the same ways as it was done for the duplicate character. The only thing that we need to incorporate is to split the string using space.

Solution:

  1. We will take the provided string and will split it using space in the string array.
  2. After this, we will compare each word from the place of word index+1 to the length-1.
  3. If we would find any value at both indexes in the array then we will increase the count.
  4. On the basis of the count, we will print the duplicate word.

Here is the code:

Output in the console.

But if the interviewer asks for the count of each word present in the sentence then in place of this line of code

with this line

then out of the above code would be like this

So we can see that the count of each word is given in output.

 

Question 5: 

How you will find that these two Strings are equal.

String firstString =  “Hello”

String secondString = new String(“Hello”)

 

Answer:

This is one of the trickiest question interviewers asks to check your knowledge of String.

To find the answer to this question read this article Learn String operations in Java using examples

Hope you like it if you like it then share it with your friends.

Reference Used:
Stack Overflow

1 2 3 8