×

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.


Related Topics

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Java Throw and Throws Keyword

Exceptions in Java enable us to construct high-quality programs where faults are checked at compile time rather than run time and where we may define unique exceptions that make code...

6 minutes read.

Java Math tanh() Method

The tanh() method of Java Math class returns the hyperbolic tangent of the specified double value. Syntax: public static double tanh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic tangent is to...

2 minutes read.

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Java Math ulp() Method

The ulp() method of Java Math class returns the size of an ulp of the argument. Syntax: public static double ulp(double x)public static float ulp(float x) Parameters: The parameter ‘x’ represents the floating-point number...

2 minutes read.

Method Overloading In Java

If the same class consists of different methods with the same name and the methods can vary by different number of parameters then it is known as Method Overloading. Method...

2 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.