Java SE 8の面接質問と回答(パート2)

以前の投稿では、重要なJava SE 8の面接の質問と回答について話しました。この投稿では、さらにJava SE 8の面接の質問と回答について議論します。この投稿を読む前に、以下のURLから私の以前の投稿をご覧ください:「Java SE 8面接の質問(パート1)」。

Java SE 8のインタビュー質問

    1. Java SE 8におけるInternal Iterationとは何ですか?

 

    1. External IterationとInternal Iterationの違いは何ですか?

 

    1. External Iterationの主な欠点は何ですか?

 

    1. Internal IterationがExternal Iterationに比べて持つ主な利点は何ですか?

 

    1. Internal IterationがExternal Iterationに比べて持つ主な欠点は何ですか?

 

    1. External IterationがInternal Iterationに比べて持つ主な利点は何ですか?

 

    1. いつInternal Iterationを使用する必要がありますか?いつExternal Iterationを使用する必要がありますか?

 

    1. Java 8のStream APIのIntermediate OperationsとTerminal Operationsの違いは何ですか?

 

    1. Javaのインターフェースにメソッドの実装を提供することは可能ですか?可能な場合、どのように提供しますか?

 

    1. デフォルトメソッドとは何ですか?なぜJava 8のインターフェースでデフォルトメソッドが必要なのですか?

 

    1. スタティックメソッドとは何ですか?なぜJava 8のインターフェースでスタティックメソッドが必要なのですか?

 

    1. 関数型プログラミングとオブジェクト指向プログラミングの違いは何ですか?

 

    1. 古いJava Date APIの問題を説明してください。Java 8のDate and Time APIとJoda Time APIに比べてJava 8のDate and Time APIの利点は何ですか?

 

    1. Java SE 8でなぜ新しいDate and Time APIが必要なのですか?Java SE 8のDate and Time APIは古いJava Date APIの問題をどのように解決しますか?

 

    1. Javaの古いJava Date APIとJava 8のDate and Time APIの違いは何ですか?

 

    1. 多重継承とは何ですか?Java 8はどのようにして多重継承をサポートしていますか?

 

    デフォルトメソッドによるインターフェースのダイヤモンド問題とは何ですか?Java 8はこの問題をどのように解決しますか?

Java SE 8 の インタビューの 質問と回答

Java SE 8における内部反復処理とは何ですか?

Java 8以前は、私たちは内部イテレーションの概念を持っていませんでした。Java 8では、「内部イテレーション」という新機能が導入されました。Java 8以前、Java言語では、コレクションや配列などの集約オブジェクトの要素を反復するために外部イテレーションのみが存在しました。内部イテレーションとは、「Java APIによって内部的に集約オブジェクトの要素を一つずつ反復すること」を意味します。Javaアプリケーションが外部的に反復するのではなく、Java APIにこのジョブを内部的に行ってもらうのです。

外部イテレーションと内部イテレーションの違いは何ですか? (Gaihatsu iterēshon to naihatsu iterēshon no chigai wa nan desu ka?)

S.No. External Iteration Internal Iteration
1. Available before Java 8 too. It is introduced in Java SE 8
2. Iterating an Aggregated Object elements externally. Iterating an Aggregated Object elements internally (background).
3. Iterate elements by using for-each loop and Iterators like Enumeration, Iterator, ListIterator. Iterate elements by using Java API like “forEach” method.
4. Iterating elements in Sequential and In-Order only. Not required to iterate elements in Sequential order.
5. It follows OOP approach that is Imperative Style. It follows Functional Programming approach that is Declarative Style.
6. It does NOT separate responsibilities properly that is, it defines both “What is to be done” and “How it is to be done”. It defines only “What is to be done”. No need to worry about “How it is to be done”. Java API takes care about “How to do”.
7. Less Readable Code. More Readable code.

外部イテレーションの主な欠点は何ですか?

外部イテレーションには以下の欠点があります:

  • We need to write code in Imperative Style.
  • There is no clear separation of Responsibilities. Tightly-Coupling between “What is to be done” and “How it is to be done” code.
  • Less Readable Code.
  • More Verbose and Boilerplate code.
  • We have to iterate elements in Sequential order only.
  • It does not support Concurrency and Parallelism properly.

内部イテレーションの主な利点は何ですか?

外部イテレーションに比べると、内部イテレーションには以下の利点があります:

  • As it follows Functional Programming style, we can write Declarative Code.
  • More Readable and concise code.
  • Avoids writing Verbose and Boilerplate code
  • No need to iterate elements in Sequential order.
  • It supports Concurrency and Parallelism properly.
  • We can write Parallel code to improve application performance.
  • Clear separation of Responsibilities. Loosely-Coupling between “What is to be done” and “How it is to be done” code.
  • We need to write code only about “What is to be done” and Java API takes care about “How it is to be done” code.

内部イテレーションの主な欠点は何ですか? (Naibu iterēshon no hon’na ketten wa nanidesu ka?)

外部イテレーションに比べて、内部イテレーションには主な欠点が一つあります。

  • In Internal Iteration, as Java API takes care about Iterating elements internally, we do NOT have control over Iteration.

外部イテレーションの主な利点は何ですか?

内部イテレーションに比べて、外部イテレーションは1つの主要な利点があります。

  • In External Iteration, as Java API does NOT take care about Iterating elements, we have much control over Iteration.

いつ内部イテレーションを使用する必要がありますか?いつ外部イテレーションを使用する必要がありますか? (Itsu naibu iterēshon o shiyo suru hitsuyō ga arimasu ka? Itsu gaibu iterēshon o shiyo suru hitsuyō ga arimasu ka?)

内部イテレーションまたは外部イテレーションを使用する状況を理解する必要があります。

  • When we need more control over Iteration, we can use External Iteration.
  • When we do NOT need more control over Iteration, we can use Internal Iteration.
  • When we need to develop Highly Concurrency and Parallel applications and we , we should use Internal Iteration.

Java 8のStream APIの中間操作と終端操作の違いは何ですか?

S.No. Stream Intermediate Operations Stream Terminal Operations
1. Stream Intermediate operations are not evaluated until we chain it with Stream Terminal Operation. Stream Terminal Operations are evaluated on it’s own. No need other operations help.
2. The output of Intermediate Operations is another Stream. The output of Intermediate Operations is Not a Stream. Something else other than a Stream.
3. Intermediate Operations are evaluated Lazily. Terminal Operations are evaluated Eagerly.
4. We can chain any number of Stream Intermediate Operations. We can NOT chain Stream Terminal Operations.
5. We can use any number of Stream Intermediate Operations per Statement. We can use only one Stream Terminal Operation per Statement.

Javaのインターフェースでメソッドの実装を提供することは可能ですか?可能な場合、どのように提供しますか?

以下は、Java 7以前ではインターフェースのメソッドの実装を提供することができませんでした。しかし、Java 8以降では可能になりました。Java SE 8では、次の2つの新しい概念を使用して、インターフェース内でメソッドの実装を提供することができます。

  • Default Methods
  • Static Methods

デフォルトメソッドとは何ですか?なぜJava 8インターフェースにデフォルトメソッドが必要なのですか?

デフォルトメソッドは、「default」キーワードを使用してインターフェースに実装されるメソッドです。これはJava SE 8で導入された新機能です。デフォルトメソッドが必要な理由は以下の通りです。

  • It allow us to provide method’s implementation in Interfaces.
  • To add new Functionality to Interface without breaking the Classes which implement that Interface.
  • To provide elegant Backwards Compatibility Feature.
  • To ease of extend the existing Functionality.
  • To ease of Maintain the existing Functionality.

「Static Method」とは何ですか?なぜJava 8のインターフェースではStaticメソッドが必要ですか?

静的メソッドは、クラス(またはインターフェース)に関連付けられたユーティリティメソッドまたはヘルパーメソッドです。オブジェクトに関連付けられているわけではありません。次の理由から、静的メソッドが必要です。

  • We can keep Helper or Utility methods specific to an interface in the same interface rather than in a separate Utility class.
  • We do not need separate Utility Classes like Collections, Arrays etc to keep Utility methods.
  • Clear separation of Responsibilities. That is we do not need one Utility class to keep all Utility methods of Collection API like Collections etc.
  • Easy to extend the API.
  • Easy to Maintain the API.

関数型プログラミングとオブジェクト指向プログラミングの違いは何ですか?

Functional Programming OOP
Does not exist State Exists State
Uses Immutable data Uses Mutable data
It follows Declarative Programming Model It follows Imperative Programming Model
Stateless Programming Model Stateful Programming Model
Main Fcous on: “What you are doing” Main focus on “How you are doing”
Good for Parallel (Concurrency) Programming Poor for Parallel (Concurrency) Programming
Good for BigData processing and analysis NOT Good for BigData processing and analysis
Supports pure Encapsulation It breaks Encapsulation concept
Functions with No-Side Effects Methods with Side Effects
Functions are first-class citizens Objects are first-class citizens
Primary Manipulation Unit is “Function” Primary Manipulation Unit is Objects(Instances of Classes)
Flow Controls: Function calls, Function Calls with Recursion Flow Controls: Loops, Conditional Statements
It uses “Recursion” concept to iterate Collection Data. It uses “Loop” concept to iterate Collection Data. For example:-For-each loop in Java
Order of execution is less importance. Order of execution is must and very important.
Supports both “Abstraction over Data” and “Abstraction over Behavior”. Supports only “Abstraction over Data”.
We use FP when we have few Things with more operations. We use OOP when we have few Operations with more Things. For example: Things are classes and Operations are Methods in Java.

以下の内容は、FP、IP、およびOOPの比較に関する詳細な情報については、以前の投稿「FP、OOP(IP)の比較」をご覧ください。

古いJava Date APIの問題を説明してください。また、Java 8のDate and Time APIが古いDate APIとJoda Time APIに比べてどのような利点がありますか?

Javaの古い日付APIは、Java SE 8より前に利用可能なDate、Calendar、SimpleDateFormatなどの日付APIのことを指します。Javaの古い日付APIは、Java 8の日付および時刻APIやJoda Time APIと比較して、以下の問題や欠点があります。

  • Most of the API is deprecated.
  • Less Readability.
  • java.util.Date is Mutable and not Thread-Safe.
  • java.text.SimpleDateFormat is not Thread-Safe.
  • Less Performance.

Java SE 8の日付と時刻のAPIは、Javaの旧日付APIと比べて以下の利点があります。

  • Very simple to use.
  • Human Readable Syntax that is More Readability.
  • All API is Thread-Safe.
  • Better Performance.

なぜ私たちはJava SE 8で新しい日付と時刻のAPIが必要なのでしょうか?Java SE 8の日付と時刻のAPIは、旧Javaの日付APIの問題をどのように解決しているのでしょうか?

高いパフォーマンス、スレッドセーフ、スケーラビリティの高いJavaアプリケーションを開発するためには、Java 8の日付と時刻APIが必要です。Java 8の日付と時刻APIは、すべてのJavaの古い日付APIの問題を、イミュータビリティとスレッドセーフの原則に従って解決しています。

Javaの古いJava Date APIとJava 8の日付と時刻APIの違いは何ですか?

Javaの古いJava Date APIとJava 8のDate and Time APIの違い:

S.No. Java’s OLD Java Date API Java 8’s Date and Time API
1. Available before Java 8 too. It is introduced in Java SE 8
2. Not Thread Safe. Thread Safe.
3. Mutable API. Immutable API.
4. Less Performance. Better Performance.
5. Less Readability. More Readability.
6. It’s not recommended to use as its deprecated. It’s always recommended to use.
7. Not Extendable. Easy to Extend.
8. It defines months values from 0 to 11, that is January = 0. It defines months values from 1 to 12, that is January = 1.
9. It’s an old API. It’s a new API.

Multiple Inheritanceとは何ですか?Java 8はどのようにMultiple Inheritanceをサポートしていますか?

複数の継承とは、クラスが複数の親クラスから特性や機能を受け継ぐことができることを意味します。しかし、Java 7以前では、複数の継承は不可能でした。なぜなら、Javaは「クラスはただ一つのクラスまたは抽象クラスを継承すべき」というルールを従っているからです。しかし、Javaではインターフェースを使用して複数のインターフェースからの実装継承を行うことは可能です。ただし、Java 8では「インターフェースでのメソッドの実装」をサポートするために、新たな機能であるデフォルトメソッドが導入されました。この機能により、Java 8ではいくつかの制限付きで複数の継承をサポートしています。

デフォルトメソッドによるインタフェースにおけるダイヤモンド問題とは何ですか?Java 8はこの問題をどのように解決していますか?

diamond problem interface default methods
interface A {
	default void display() {
		System.out.println("A");
	}
}

interface B extends A {
	default void display() {
		System.out.println("B");
	}
}

interface C extends A {
	default void display() {
		System.out.println("C");
	}
}

class D implements B, C {
}

上記のコードスニペットでは、クラスDは「デフォルトメソッドのdisplayがクラスCとBから継承されており、パラメーター()と()を持つ重複するメソッド名です」というコンパイル時エラーが発生します。これは、JavaコンパイラがクラスDでどのdisplay()を使用するか混乱するためです。クラスDはインターフェースBとCの両方からdisplay()メソッドを継承しています。この問題を解決するために、Java 8では以下の解決策が提供されています。

class D implements B, C {

	@Override
	public void display() {
		B.super.display();
	}
	
}

このB.super.display();は、このダイヤモンドの問題を解決します。Cのインターフェースデフォルトメソッドを使用したい場合は、C.super.display();を使用してください。これがJava 8のインタビューの問題のすべてです。今後の投稿でさらにJava SE 8のインタビューの問題を取り上げます。私の投稿が気に入ったり、問題や提案がある場合は、コメントを投稿してください。

コメントを残す 0

Your email address will not be published. Required fields are marked *