×

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 to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Java Integer highestOneBit()

The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value.  Syntax public static...

1 minute read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Morris Traversal for Inorder in Java

Through Morris’s traversal, a tree is traversed without the aid of recursion or stacks. Based on the threaded binary tree, the Morris traversal is used. We perform internal modification throughout...

4 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Minimum Number of Platforms Required for a Railway Station

The train station problem is one of the most significant problems typically posed in the programming round interview to gauge a candidate's aptitude for logic and problem-solving. Problem of Railway Station The...

6 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

The Diamond Operator in Java 7

The Diamond operator is a comparatively recent Java operator originally developed in JDK 7 to enhance type identification and eliminate boilerplate Java code. The Diamond operator is characterized by a...

2 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Java InetAddress class

InetAddress class The InetAddress class refers to the IP address, both IPv4 and IPv6.An instance of an InetAddress consists of an IP address and possibly its corresponding hostname. It provides a method to get the...

9 minutes read.

How to open the Java control panel

The many methods for launching the Java control panel will be covered in this section. We will also go through how the Java Control Panel can be used. Java Control Panel The...

2 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Why are generics used in Java

Java has a feature called generics that allows you to make a class, interface, and function accepting any (reference) type as a parameter. In other words, it is the idea...

4 minutes read.