首頁 > Java > java教程 > 主體

Java 字串面試題

WBOY
發布: 2024-08-30 16:29:16
原創
909 人瀏覽過

在核心Java中,String是理解基礎java程式入門的關鍵類別之一。 string 類別定義良好並保存在 java.lang 套件中。它是一個不可變的類,因此開發者可以直接使用它,而無需每次都建立實例。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

如果您正在尋找與 Java String 相關的工作,您需要準備 2023 年 Java String 面試題。根據不同的工作概況,每次面試確實有所不同。在這裡,我們準備了重要的 Java String 面試問題和答案,這將幫助您在面試中取得成功。

在本文中,我們將介紹 10 個最重要且最常見的 Java String 面試問題。這些問題分為以下兩部分:

第 1 部分 – Java 字串面試問題(基礎)

第一部分涵蓋基本的 Java String 面試問題和答案

Q1。詳細解釋 Java 程式語言中的 String 類,開發人員如何使用該類別。我們應該將 Java 程式語言中的 String 視為一種資料類型嗎?如果有請詳細說明一下?

答案:

字串是Java中的關鍵類別之一,它基本上保存在java.lang套件中。它根本不像Java int 或long 等其他變數那樣被定義為原始資料型別。它主要以單一表示形式保存大量字串。 String物件在java程式設計中非常流行,幾乎使用了從頭到尾的所有編碼,它是Java核心包中定義的一個不可變的最終類,JVM通常會建立一個String池來儲存所有建立的String物件。

Q2。在 Java 程式語言中,有多種方法可用於建立 String 物件;詳細解釋可用於建立相同內容的方法,並給出一些具有程式碼片段的相關範例?

答案:

開發人員可以透過多種方式在 Java 程式語言中建立 String 物件。方法如下:

  • 使用New運算子:我們可以像建立其他物件一樣,使用簡單的new運算子建立字串對象,但它不會儲存在JVM的字串池中。
  • 使用「運算子:僅使用「運算子即可建立字串對象,並在其中提供對應的值。同樣的用途,JVM 會將該值儲存在字串池中並相應地傳回。
  • 使用其他:可以從位元組數組、字元數組、StringBuilder 或 StringBuffer 建立字串物件。
String str = new String("abcd"); à not store in JVM String pool
String str1 = "abcd"; à store in JVM String pool
登入後複製

讓我們進入下一個 Java String 面試題

Q3。 Java 語言中常使用 one 關鍵字,稱為「Palindrome」。請解釋一下,當Java程式設計中的一個字串物件引用可以作為「回文」呼叫時,是否可以編寫一個java方法來檢查它?如果是,請給一些帶有程式碼片段的範例。

答案:

當特定 String 類別的值在反轉時可以相同時,一個 String 物件可以稱為「回文」。舉個例子,我們可以說“cbc”,這裡這個字串值可以被認為是回文,因為這個值的反轉總是會提供相同的結果。

通常有兩種流行的方法可用於在 String 中驗證相同的方法,根據面試官的期望,我們可以依賴相同的方法:

  • StringBuilder strBuilder = new StringBuilder(“cbc”);

strBuilder.reverse();

  • 字串 str1 = “abc”;

Int lenStr1 = str1.length();
For(int j=0; j If(str1.charAt(j) != str1.charAt(lenStr1 – j-1)){
返回 false;
}
}

回傳真;

第四季。假設您想從 Java 程式語言中定義的 String 物件之一中刪除一些 asci 字元或提供或給定字元;如何在 Java 程式設計中使用單一方法來完成此任務。請用程式碼片段解釋一下?

答案:

These are the basic Java String Interview Questions asked in an interview. One of the key methods we normally used to replace the entire string is the replace All method. It will help the developer for replacing the entire content of the String with another String content as per the requirement of the project. But one of the key characteristics of a replacement. All method is it always accepting String as it’s one of the specific arguments, so it will very much require for using one of the Character classes for creating the string and use the same properly for replacing it with another expected String or an empty string. Please find below the code snippet with an example.

public String replaceString(String str1, char c1){
Return str1.replaceAll(Character.toString(c1), "……");
}
登入後複製

Q5. One of the very common requirements in a programming language is to make some property in upper case or lower case. Is there any specific methodology defined for String object in a Java programming language to present that property value in upper case or lower case? Please explain with a code snippet?

Answer:

It will be one of the very common functionalities required for the developer in any case for the String object variable value. A developer can easily be used to UpperCase and LowerCase methods on the specific String class to getfor getting the entire string value in total upper case or total lower case. This method has one more optional argument, which is a locale object. So someone can able to use some specific locale rules for making the string value in upper case or lower case.

String s = "abc";
String s1 = "ABC"
System.out.println("Upper case of S: "+s.toUpperCase()+", lower case of S1: "+s1.toLowerCase());
登入後複製

Part 2 – Java String Interview Questions (Advanced)

Let us now have a look at the advanced Java String Interview Questions.

Q6. Is there any method in String call subsequence? If yes, please explain the same in details with a proper definition?

Answer:

CharSequence interface is a very much useful interface in java introduce to Java 1.4. The string class normally implements that specific interface. So the implementation of the subsequence method inside the String class can be possible due to the above reason. Actually, it invokes one of the frequent methods of String called the substring method internally.

Q7. One another very common requirement for the developer in Java programming is to compare two objects as per development requirements. Is it possible to compare two String object in the Java programming language? If yes, please explain how it can be done?

Answer:

Comparing between two String values can be done by below approaches:

  • Using a comparable interface: Java String normally implements a comparable interface, so compare To method can be utilized easily to compare two string values.
  • By using equals or equalsIgnoreCase method: String value can be compared easily by using direct method equal or eualsIgnoreCase.

Let us move to the next Java String Interview Questions

Q8. Is it possible to convert one of the String objects into a char data type or one char data type covert to a String object? If yes, please explain how it can be achievable?

Answer:

charAt method can be used to get an individual character by providing an index, and the CharAraay method converts one string value to a character array.

String str = "abc";
Char c = str.charAt(0);
String str1 = new String("This is a test String");
char[] carray= str1.toCharArray();
for(char c1: carray){
System.out.print(c1);
}
登入後複製

Q9. One of the very popular methodologies in the Java programming language called the switch case. Is it possible to use switch case methodology in case of using String object for java programming? If yes, please explain how it can be possible?

Answers:

This is the most asked Java String Interview Question in an interview. Switch case is one of the common functionality for avoiding writing a lot of if-else logic in the entire code. It always helps in maintaining code quality properly. Earlier switch case reference only is able to do for the integer or long values only. But from the Java 7 version onwards, java allows using switch case on String object as well. So as of now, the String value can be used for switch case utility.

Q10: Suppose planning to perform every kind of permutation of String class in java programming. Please give some details programming of how we can able to do this by using String object properly?

Answer:
We can use this by using the below code:

Set<String> perm = new HashSet<String>();
char initial = str.charAt(0);
String rem = str.substring(1);
Set<String> words = permutationFinder(rem);
for (String strNew : words) {
for (int i = 0;i<=strNew.length();i++){
perm.add(charInsert(strNew, initial, i));
}
}
return perm;
登入後複製

以上是Java 字串面試題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板