Java 文字列の面接の質問

WBOY
リリース: 2024-08-30 16:29:16
オリジナル
856 人が閲覧しました

コア Java では、String は、基本的な Java プログラミングの開始を理解するための重要なクラスの 1 つです。文字列クラスは明確に定義されており、java.lang パッケージに保持されています。これは不変クラスであるため、開発者は毎回インスタンスを作成せずに直接使用できます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

Java String に関連する仕事を探している場合は、2023 年の Java String 面接の質問に備える必要があります。それぞれの面接は、職種の違いに応じて実際に異なります。ここでは、面接での成功に役立つ重要な Java String 面接の質問と回答を用意しました。

この記事では、Java String の面接で最も重要でよく聞かれる 10 の質問を紹介します。これらの質問は次の 2 つの部分に分かれています:

パート 1 – Java 文字列面接の質問 (基本)

この最初のパートでは、基本的な Java String インタビューの質問と回答について説明します

Q1. Java プログラミング言語の String クラスについて、開発者がそれをどのように使用できるかを詳しく説明します。 Java プログラミング言語の String をデータ型として考慮する必要がありますか? 「はい」の場合、詳しく説明してください?

答え:

文字列は Java の重要なクラスの 1 つであり、基本的に java.lang パッケージに保持されます。 Java の他の int 変数や Long 変数のようなプリミティブ データ型としてはまったく定義されていません。主に、単一の表現に多数の文字列を保持します。 String オブジェクトは、最初から最後までほぼすべてのコーディングを使用するため、Java プログラミングで非常に人気があり、Java コア パッケージで定義される不変の最終クラスであり、JVM は通常、作成されたすべての String オブジェクトを格納するために 1 つの String プールを作成します。 🎜>

Q2. Java プログラミング言語では、String オブジェクトを作成するためにさまざまな方法が利用できます。同じものを作成するために利用できるアプローチを詳細に説明し、コード スニペットを使用して関連する例を示しますか?

答え:

開発者が Java プログラミング言語で String オブジェクトを作成できる方法はいくつかあります。アプローチは以下のとおりです:

  • New 演算子の使用: 他のオブジェクト作成と同様に、単純な new 演算子を使用して 1 つの文字列オブジェクトを作成できますが、JVM の String プールには保存されません。
  • 演算子を使用: 文字列オブジェクトは、演算子を使用するだけで作成でき、その中に対応する値を提供します。同じユーティリティでは、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. 「Palindrome」と呼ばれる Java 言語でよく使われるキーワードが 1 つあります。 Java プログラミングにおける 1 つの文字列オブジェクト参照が「Palindrome」として呼び出せる場合について説明してください。それをチェックするための Java メソッドを記述することは可能ですか? 「はい」の場合は、コード スニペットを含む例をいくつか挙げてください。

答え:

1 つの String オブジェクトは、その特定の String クラスの値が反転されたときに同じになる可能性がある場合、「回文」として呼び出すことができます。例として、「cbc」のように言うことができます。ここでは、この文字列値を回文とみなすことができます。この値を逆にすると常に同じ結果が得られます。

通常、インタビュアーの期待に基づいて、文字列内で同じものを検証するために利用できる 2 つの一般的なアプローチを利用できます。

    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 を返します;
}
}

true を返します;

Q4. Java プログラミング言語で定義された String オブジェクトの 1 つから、いくつかの ASCI 文字を削除するか、指定された文字を削除するとします。 Java プログラミングの 1 つのメソッドを使用してこれを行う方法について説明します。コードスニペットで説明してください?

答え:

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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!