Array and String based questions in Java
1. What is an Array in Java?
A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of both object references and basic types (int, float, and double). It is static since it has a set length. Instead of stack memory, heap memory is used to create arrays. If you try to access an incorrect index in an array, an exception will be raised.
2. How to declare array in Java?
In Java, you may declare an array in the following way:
arrayVariableName = new dataType[arraySize];
dataType[] arrayVariableName = new dataType[arraySize];
You may declare an int array as follows for the int data type:
temp = new int[256] int[]
3. Can we change the size of an Array in java after creation?
We can't adjust the size of an array after it's been created. Other data structures, on the other hand, can alter in size after they've been created.
4. Can we declare an Array without the size of an Array?
No, we can’t declare an Array without the size of the Array. We will face a compile-time error.
5. Can we pass the negative number in the size of an Array?
No, a negative value cannot be used as an Array size. If you enter a negative value for the Array size, there won't be a compiler error. Instead, a runtime NegativeArraySizeException exception will be raised.
6. In JVM memory, where is the Array stored?
In Java, an array is an object. As a result, the array is saved in heap memory in the JVM.
7. Write the advantages of Array in Java?
- Arrays employ a single term to represent a group of similar data components.
- Arrays assign memory in contiguous memory regions for all of their components and allow random access to any of the elements at any time. There is therefore no possibility of allocating more RAM in the case of arrays. As a result, memory overflows and array shortages are prevented.
- Linked lists, stacks, queues, trees, and graphs are just a few of the other data structures that may be built using arrays.
- Two-dimensional arrays are used to represent matrices.
8. Write the disadvantages of Arrays in Java?
- The number of elements in an array should be known ahead of time.
- A static structure is an array (which means the array is of fixed size). The array's size can’t be changed once it has been declared. It does not have the ability to increase or reduce the amount of memory assigned to it.
- Insertion and deletion in an array are tough due to the items being stored in successive memory areas and the shifting procedure is expensive.
- You waste memory space when you allocate more memory than you require, and you also have difficulty when you allocate less memory.
9. What is String in Java?
A String is a Class defined in the java language package in Java. It is not a fundamental data type like an int or a long. The String class represents character strings. Almost all Java programs use strings, and there are a few interesting aspects about them that we should be aware of. As all String objects are stored in a String Pool by the JVM, String in Java is immutable and final.
Using double quotes to build a String object and overloading the "+" operator for concatenation are two further intriguing characteristics of String.
10. Write a method to extract a specific character from a particular String?
A replacement to replace every instance of a String with a different String, use the All method. It's important to keep in mind that it accepts a String as an argument, so we'll create a String using the Character class and use it to replace all of the characters with an empty String.
11. Write a specific method to determine if an input String is a Palindrome?
A String is considered to be a palindrome if its value remains the same after it is turned around. An illustration of a palindrome string is "aba."
The StringBuffer and StringBuilder classes contain a reverse function, while the String class does not. To determine whether the String is a palindrome or not, we can use the reverse approach.
private static boolean isPalindrome(String str) {
if (str == null)
return false;
StringBuilder strBuilder = new StringBuilder(str);
strBuilder.reverse();
return strBuilder.toString().equals(str);
}
12. Define the String subsequence method?
The introduction of the CharSequence interface in Java 1.4, which String implements, is the sole explanation for the subSequence function's inclusion in the String class. Internally, it makes use of the String substring method.
13. Define a way to convert a String to a byte array and vice versa?
The String getBytes() method is used to convert a String to a byte array. The String function Object() new String is used to convert a byte array to a String (byte[] arr).
14. How can we split String in java?
A String can be divided into a String array using the Split(String regex) function.
15. What do you mean by String Pool?
As the name suggests, String Pool is a Java stack memory pool for Strings. We are aware that the String class is a particular Java class and that we may create String objects with double-quoted data using the new operator.