Top 50 câu hỏi phỏng vấn Java có đáp án (Eng & Viet)

Content

Phần 1: JAVA CORE (15 câu)

Xem thêm: Top các quyển sách lập trình Java nhất định phải biết

1. What is object oriented programming (OOP)? Lập trình hướng đối tượng là gì?

Tiếng Anh

Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints, which are used to create individual instances of objects.

Tiếng Việt

Lập trình hướng đối tượng (OOP) là một mô hình lập trình dựa trên khái niệm lớp và đối tượng. Nó được sử dụng để tạo ra cấu trúc một chương trình phần mềm có tính chất đơn giản, có thể tái sử dụng và mở rộng.

2. What is polymorphism? Tính Đa hình là gì?

Tiếng Anh

Polymorphism is an object-oriented programming concept that refers to the ability of a variablefunction, or object to take on multiple forms. In a programming language exhibiting polymorphism, class objects belonging to the same hierarchical tree (inherited from a common parent class) may have functions with the same name, but with different behaviors.

Tiếng Việt

Đa hình là một khái niệm lập trình hướng đối tượng đề cập đến khả năng của một biến, hàm hoặc đối tượng có nhiều dạng. Trong một ngôn ngữ lập trình thể hiện tính đa hình, các đối tượng lớp thuộc cùng một cây phân cấp (được kế thừa từ một lớp cha chung) có thể có các hàm có cùng tên nhưng có các hành vi khác nhau.

3. What is inheritance? Kế thừa là gì?

Tiếng Anh

Inheritance is an Object Oriented Programming (OOP) feature that allows the properties of an object to be used by different objects.

Tiếng Việt

Kế thừa là một khái niệm trong Lập trình hướng đối tượng (OOP) cho phép các thuộc tính của một đối tượng được sử dụng bởi các đối tượng khác nhau.

4. What is encapsulation? Tính đóng gói là gì?

Tiếng Anh

Encapsulation is one of the fundamentals of OOP (object-oriented programming). It refers to the bundling of data with the methods that operate on that data. Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties’ direct access to them.

Tiếng Việt

Đóng gói là một trong những nguyên tắc cơ bản của OOP (lập trình hướng đối tượng). Nó đề cập đến việc gói dữ liệu với các phương thức hoạt động trên dữ liệu đó. Đóng gói được sử dụng để ẩn các giá trị hoặc trạng thái của một đối tượng dữ liệu có cấu trúc bên trong một lớp, ngăn chặn các bên trái phép truy cập trực tiếp vào chúng.

5. What is abstract in Java? Trong Java tính trừu tượng là gì?

Tiếng Anh

Abstraction is the concept of object-oriented programming that “shows” only essential attributes and “hides” unnecessary information. The main purpose of abstraction is hiding the unnecessary details from the users. Abstraction is selecting data from a larger pool to show only relevant details of the object to the user. It helps in reducing programming complexity and efforts. It is one of the most important concepts of OOPs.

Tiếng Việt

Tính trừu tượng là khái niệm về lập trình hướng đối tượng chỉ “hiển thị” các thuộc tính cần thiết và “ẩn” các thông tin không cần thiết. Mục đích chính của trừu tượng hóa là ẩn các chi tiết không cần thiết khỏi người dùng. Trừu tượng hóa đang chọn dữ liệu từ một nhóm lớn hơn để chỉ hiển thị các chi tiết có liên quan của đối tượng cho người dùng. Nó giúp giảm bớt sự phức tạp và nỗ lực lập trình. Đây là một trong những khái niệm quan trọng nhất của OOP.

6. What is difference between class and object? Sự khác nhau giữa lớp và đối tượng là gì?

Tiếng Anh

ClassObject
Class is used as a template for declaring and 
creating the objects.
An object is an instance of a class.
When a class is created, no memory is allocated.Objects are allocated memory space whenever they are created.
The class has to be declared first and only once.An object is created many times as per requirement.
A class can not be manipulated as they are not
available in the memory.
Objects can be manipulated.
A class is used to bind data as well as methods together as a single unit.Objects are like a variable of the class.

Tiếng Việt

LớpĐối tượng
Lớp được dùng để khai báo và tạo đối tượngMột đối tượng là một instance của một lớp
Khi một lớp được tạo, không có bộ nhớ nào được cấp phát.Các đối tượng được cấp phát dung lượng bộ nhớ bất cứ khi nào chúng được tạo ra.
Lớp phải được khai báo đầu tiên và chỉ một lần.Một đối tượng được tạo nhiều lần theo yêu cầu.
Một lớp không thể bị can thiệp vì chúng không có sẵn trong bộ nhớ.Đối tượng có thể bị can thiệp
Một lớp được sử dụng để liên kết dữ liệu cũng như các phương thức với nhau thành một single unit (đơn vị.)Các đối tượng giống như một biến của lớp.
Educative

7. What is difference between abstract and interface? Sự khác nhau giữa abstract và interface là gì?

Tiếng Anh

AbstractInterface
An abstract class can contain both abstract and non-abstract methods.Interface contains only abstract methods.
An abstract class can have all four; static, non-static and final, non-final variables.Only final and static variables are used.

To declare abstract class abstract keywords are used.
The interface can be declared with the interface keyword.
It does not support multiple inheritance..It supports multiple inheritance.

It has class members like private and protected, etc.
It has class members public by default.

Tiếng Việt

AbstractInterface
Một lớp trừu tượng có thể chứa cả phương thức trừu tượng và không trừu tượng.Interface chỉ chứa các phương thức trừu tượng
Một lớp trừu tượng có thể có cả 4 biến:static, non-static and final, non-final variables.Interface chỉ có 2 biến: final and static variables
Để khai báo lớp trừu tượng, sử dụng từ khóa abstractInterface có thể được khai báo với từ khóa interface.
Không hỗ trợ đa kế thừaHỗ trợ đa kế thừa
Có các class member như private và protectedMặc định có các class member public.

8. How many modifiers in Java? Có bao nhiêu modifiers trong Java?

Tiếng Anh

There are two types of modifiers in Java: access modifiers and non-access modifiers.

Access modifiers has 4 types:

  1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
  2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.
  3. Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
  4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

Non-access modifiers: there’re many non-access modifiers such as static, abstract, synchronized, native, volatile, transient, etc…

Tiếng Việt

Có hai loại modifiers trong Java:  access modifiers and non-access modifiers

Access modifiers có 4 loại:

Private: Cấp độ truy cập chỉ ở trong class. Nó không thể được truy cập từ bên ngoài class.

Default: Cấp độ truy cập mặc định chỉ nằm trong package. Nó không thể được truy cập từ bên ngoài package. Nếu bạn không chỉ định bất kỳ cấp độ truy cập nào thì đây sẽ là cấp độ mặc định.

Protected: truy cập được bảo vệ nằm trong package và bên ngoài package thông qua lớp con. Nếu bạn không tạo class con thì không thể được truy cập từ bên ngoài package.

Public: truy cập công khai ở mọi nơi. Nó có thể được truy cập từ bên trong class, bên ngoài class, bên trong package và bên ngoài package.

Non-access modifiers: có nhiều loại như static, abstract, synchronized, native, volatile, transient v.v …

9. What is difference between Hashmap, Hashset and Hashtable? Sự khác nhau giữa Hashmap, Hashset và Hashtable là gì?

Tiếng Anh

HashmapHashsetHashtable
It allows one null for key and multiple null for valuesIt can have a single null value.It does not allow null for key as well as for value.
It does not maintain any order among its objects.It does not maintain any order among its objects.It does not maintain any order among its objects.
It uses put method to insert a new element.It uses add method to insert a new element.It uses put method to insert a new element.
It is not Thread-Safe because it is not Synchronized but it gives better performance.Like HashMap, it is not Thread-Safe because it is not Synchronized.It is Thread-Safe because it is Synchronized.

Tiếng Việt

HashmapHashsetHashtable
Cho phép một null cho key và nhiều null cho valueCó thể có một null value duy nhấtHashtable không cho phép null đối với key cũng như value.
Không duy trì bất kỳ trật tự nào giữa các đối tượng của nóKhông duy trì bất kỳ trật tự nào giữa các đối tượng của nóKhông duy trì bất kỳ trật tự nào giữa các đối tượng của nó
Sử dụng phương thức put để insert một phần tử mới. Sử dụng phương thức add để insert một phần tử mới. Sử dụng phương thức put để insert một phần tử mới.
Hashmap không phải Thread – Safe vì nó không được Synchronized (đồng bộ hóa) nhưng nó lại mang lại hiệu suất tốt hơn. Giống như HashMap, Hashset không phải Thread – Safe vì nó không được Synchronized (đồng bộ hóa.) Hashtable là Thread – Safe vì nó được Synchronized (đồng bộ hóa.)

10. What is difference between Hashmap and Treemap? Sự khác nhau giữa Hashmap và Treemap là gì?

Tiếng Anh

BasicHashmapTreemap
DefinitionJava HashMap is a hashtable based implementation of Map interface.
Java TreeMap is a Tree structure-based implementation of Map interface.
Interface ImplementsHashMap implements Map, Cloneable, and Serializable interface.TreeMap implements NavigableMap, Cloneable, and Serializable interface.
Null Keys/ ValuesHashMap allows a single null key and multiple null values.TreeMap does not allow null keys but can have multiple null values.
Homogeneous/ HeterogeneousHashMap allows heterogeneous elements because it does not perform sorting on keys.TreeMap allows homogeneous values as a key because of sorting.
PerformanceHashMap is faster than TreeMap because it provides constant-time performance that is O(1) for the basic operations like get() and put().TreeMap is slow in comparison to HashMap because it provides the performance of O(log(n)) for most operations like add(), remove() and contains().
Data Structure
HashMap class uses the hash table.TreeMap internally uses a Red-Black tree, which is a self-balancing Binary Search Tree.
Comparison MethodIt uses equals() method of the Object class to compare keys. The equals() method of Map class overrides it.It uses the compareTo() method to compare keys.
FunctionalityHashMap class contains only basic functions like get(), put(), KeySet(), etc. .TreeMap class is rich in functionality, because it contains functions like: tailMap(), firstKey(), lastKey(), pollFirstEntry(), pollLastEntry().
Order of elementsHashMap does not maintain any order.The elements are sorted in natural order (ascending).
UsesHashMap should be used when we do not require key-value pair in sorted order.TreeMap should be used when we require key-value pair in sorted (ascending) order.

Tiếng Việt

BasicHashmapTreemap
Định nghĩaHashMap là một hashtable dựa trên implement của Map interface.
Java TreeMap là một Map interface dựa trên cấu trúc Tree structure
Interface ImplementsHashMap có thể sao chép và có thể tuần tự hóa.TreeMap implements NavigableMap, Cloneable, and Serializable interface.
Null Keys/ ValuesHashMap cho phép 1 null key and nhiều null valuesTreeMap không cho phép null key nhưng có thể có nhiều null values.
Tính đồng nhất/ không đồng nhấtHashMap không thực hiện sắp xếp theo key nên các giá trị không đồng nhấtTreeMap sắp xếp theo key nên các giá trị có tính đồng nhất
PerformanceHashMap nhanh hơn TreeMap vì nó cung cấp hiệu suất thời gian không đổi là O(1) cho các hoạt động cơ bản như get() và put()TreeMap chậm so với HashMap vì nó cung cấp hiệu suất của O(log(n)) cho hầu hết các thao tác như add(), delete() và contain()
Data Structure
HashMap class sử dụng bảng băm hash tableTreeMap sử dụng Red-Black tree, là Cây tìm kiếm nhị phân tự cân bằng Binary Search Tree.
Comparison MethodSử dụng phương thức equals() của Object class để so sánh các key. Phương thức equals() của Map class ghi đè lên Hashmap.Sử dụng phương thức compareTo() để so sánh các key
FunctionalityHashMap class chỉ chứa các hàm cơ bản như get(), put(), KeySet(), v.v.TreeMap class rất phong phú về chức năng, bởi vì nó chứa các chức năng như: tailMap(), firstKey(), lastKey(), pollFirstEntry(), pollLastEntry()
Sắp xếp thứ tự phần tử HashMap không có sắp xếpCác phần tử được sắp xếp theo thứ tự tự nhiên (tăng dần).
Cách sử dụngHashmap nên được sử dụng khi không yêu cầu cặp key-value được sắp xếp theo thứ tựTreeMap nên được sử dụng khi được yêu cầu cặp key-value được sắp xếp theo thứ tự (tăng dần).

11. What is difference between Array and ArrayList? Sự khác nhau giữa Array và ArrayList là gì?

Tiếng Anh

BasicArrayArrayList
DefinitionAn array is a dynamically-created object. It serves as a container that holds the constant number of values of the same type. It has a contiguous memory location.
The ArrayList is a class of Java Collections framework. It contains popular classes like Vector, HashTable, and HashMap.
Static/ DynamicArray is static in size.ArrayList is dynamic in size.
ResizableAn array is a fixed-length data structure.ArrayList is a variable-length data structure. It can be resized itself when needed.
InitializationIt is mandatory to provide the size of an array while initializing it directly or indirectly.We can create an instance of ArrayList without specifying its size. Java creates ArrayList of default size.
PerformanceIt performs fast in comparison to ArrayList because of fixed size.ArrayList is internally backed by the array in Java. The resize operation in ArrayList slows down the performance.
Primitive/ Generic type
An array can store both objects and primitives type.We cannot store primitive type in ArrayList. It automatically converts primitive type to object.
Iterating ValuesWe use for loop or for each loop to iterate over an array.We use an iterator to iterate over ArrayList.
LengthArray provides a length variable which denotes the length of an array..ArrayList provides the size() method to determine the size of ArrayList.
Adding ElementsWe can add elements in an array by using the assignment operator.Java provides the add() method to add elements in the ArrayList.
Single/ Multi-DimensionalArray can be multi-dimensional.ArrayList is always single-dimensional.

Tiếng Việt

BasicArrayArrayList
DefinitionMột Array là một đối tượng được tạo động. Nó đóng vai trò chứa các giá trị cố định cùng một kiểu dữ liệu. Nó có một vị trí bộ nhớ liền kề
ArrayList là một class của Java Collections framework. Nó chứa các class phổ biến như Vector, Hash Table và HashMap.
Static/ DynamicArray có size static (cố định)ArrayList có size dynamic (động)
ResizableMột Array là một cấu trúc dữ liệu có độ dài cố địnhArrayList là một cấu trúc dữ liệu có độ dài thay đổi. Nó có thể tự thay đổi kích thước khi cần thiết.
InitializationBắt buộc phải cung cấp kích thước của một mảng trong khi khởi tạo nó trực tiếp hoặc gián tiếpJava tạo ArrayList có kích thước mặc định.
Chúng ta có thể tạo một ArrayList mà không cần chỉ định kích thước của nó
PerformanceArray hoạt động nhanh so với ArrayList vì kích thước cố địnhArrayList được hỗ trợ nội bộ bởi mảng trong Java. Thao tác thay đổi kích thước trong ArrayList làm chậm hiệu suất
Primitive/ Generic type
Một mảng có thể lưu trữ cả đối tượng (object) và kiểu nguyên thủy (primitives type)Không thể lưu trữ kiểu nguyên thủy trong ArrayList. Nó tự động chuyển đổi kiểu nguyên thủy (primitives type) thành đối tượng (object)
Iterating ValuesSử dụng vòng lặp for hoặc for each để lặp qua một ArraySử dụng một trình iterator để lặp lại ArrayList
LengthArray cung cấp length variable biểu thị độ dài của array đóArrayList cung cấp phương thức size() để xác định kích thước của ArrayList.
Adding ElementsThêm các phần tử vào một array bằng cách sử dụng toán tử gán phần tử.Thêm phần tử vào ArrayList bằng add() method
Single/ Multi-DimensionalArray có thể là mảng đa chiềuArrayList là mảng một chiều

12. What is difference between List and Set? Sự khác nhau giữa List và Set là gì?

Tiếng Anh

ListSet
The list implementation allows us to add the same or duplicate elements.The set implementation doesn’t allow us to add the same or duplicate elements.
The insertion order is maintained by the List.It doesn’t maintain the insertion order of elements.
List allows us to add any number of null values.Set allows us to add at least one null value in it.
The List implementation classes are LinkedList and ArrayList. The Set implementation classes are TreeSet, HashSet and LinkedHashSet.
We can get the element of a specified index from the list using the get() method.We cannot find the element from the Set based on the index because it doesn’t provide any get method().
It is used when we want to frequently access the elements by using the index.It is used when we want to design a collection of distinct elements.
The method of List interface listiterator() is used to iterate the List elements.The iterator is used when we need to iterate the Set elements.

Tiếng Việt

ListSet
Cho phép thêm các phần tử giống nhau hoặc trùng lặp.Không cho phép thêm các phần tử giống nhau hoặc trùng lặp.
List giữ thứ tự khi insertSet không giữ thứ tự khi insert
List cho phép thêm số lượng null values bất kỳSet cho phép thêm ít nhất một null value vào trong đó.
Sử dụng LinkedList and ArrayList. Sử dụng TreeSet, HashSet and LinkedHashSet.
Get element bằng phương thức get().Không cung cấp bất kỳ phương thức get() nào.
List được sử dụng trong trường hợp truy cập thường xuyên các phần tử bằng cách sử dụng indexSet được sử dụng khi thiết kế một tập hợp các phần tử riêng biệt.
Phương thức listiterator() của List Interface được sử dụng để lặp lại các phần tử List.Vòng lặp được sử dụng khi chúng ta cần lặp lại các phần tử Set.

13. What is synchronized? What is it used for? Synchronized là gì? Nó được dùng để làm gì?

Tiếng Anh

Synchronization in Java is the capability to control the access of multiple threads to any shared resource.

The synchronization is mainly used to

  1. To prevent thread interference.
  2. To prevent consistency problem.

There are two types of synchronization

  1. Process Synchronization
  2. Thread Synchronization

Tiếng Việt

Synchronized trong Java là khả năng kiểm soát quyền truy cập của nhiều thread vào bất kỳ tài nguyên nào được chia sẻ.

Synchronized được sử dụng để:

  1. Để ngăn chặn thread interference.

2. Để ngăn chặn vấn đề consistency

Có hai loại đồng bộ hóa

  1. Process Synchronization
  2. Thread Synchronization

14. How many ways to create a thread? Có bao nhiêu cách để tạo một thread

Tiếng Anh

There are two ways to create a thread:

  1. By extending Thread class
  2. By implementing Runnable interface.

Thread class:

Thread class provide constructors and methods to create and perform operations on a thread. Thread class extends Object class and implements Runnable interface.

Runnable interface:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().

Tiếng Việt

Có 2 cách để tạo 1 Thread:

  1. Bằng cách extend Thread class
  2. Bằng cách implement Runnable interface.

Thread class:

Thread class cung cấp các constructors và methods để tạo và thực hiện các thao tác trên một Thread. Thread class extend Object class và implement Runnable interface.

Runnable interface:

Runnable interface nên được implement bởi bất kỳ class nào được thực thi bởi một Thread. Runnable interface chỉ có một method là run().

15. What is Reflection. What is it used for? Reflection là gì? Nó được dùng để làm gì?

Tiếng Anh

Java Reflection is a process of examining or modifying the run time behavior of a class at run time.

The java.lang.Class class provides many methods that can be used to get metadata, examine and change the run time behavior of a class.

The java.lang and java.lang.reflect packages provide classes for java reflection.

Where it is used

The Reflection API is mainly used in:

  • IDE (Integrated Development Environment) e.g., Eclipse, MyEclipse, NetBeans etc.
  • Debugger
  • Test Tools etc.

Tiếng Việt

Java reflection là một quá trình kiểm tra hoặc sửa đổi run time behavior của một Class trong lúc run time.

Lớp java.lang.Class cung cấp nhiều phương thức có thể được sử dụng để lấy metadata, kiểm tra và thay đổi run time behavior của một class.

Các java.lang và java.lang.reflect package cung cấp các Class cho Java Reflection

Java Reflection được sử dụng ở đâu

Reflection API chủ yếu được sử dụng trong:

  • IDE (Integrated Development Environment), ví dụ: Eclipse, MyEclipse, NetBeans, v.v.
  • Debugger
  • Test tools, v.v.

Phần 2: DESIGN PATTERNS (10 câu)

1. Explain the advantages of Java design pattern? Giải thích lợi ích của Java design pattern?

Tiếng Anh

  • The Design Patterns are reusable in multiple projects.
  • The Design Patterns provide a solution that helps to define the system architecture.
  • The Design Patterns capture software engineering experiences.
  • The Design Patterns provide transparency to the design of an application.
  • They are testified and well-proved since they have been built upon the knowledge and experience of expert software developers.

Tiếng Việt

  • Design Patterns có thể tái sử dụng trong nhiều dự án.
  • Design Patterns cung cấp các giải pháp khi xây dựng kiến ​​trúc hệ thống.
  • Design Patterns thể hiện được kinh nghiệm của software engineer
  • Design Patterns cung cấp tính minh bạch cho thiết kế của một ứng dụng.
  • Lợi ích của việc sử dụng Design Patterns rất rõ ràng vì chúng được xây dựng dựa trên kiến ​​thức và kinh nghiệm của các developer chuyên nghiệp.

2. How many Categories Java Design patterns? Có bao nhiêu loại Java Design Patterns?

Tiếng Anh

Based on problem analysis. There’re 4 categories Java design patterns:

  1. Creational patterns
  • Factory method / Abstract Factory / Builder / Prototype /Singleton

2. Structural patterns

  • Adapter / Bridge / Filter / Composite / Decorator / Facade / Flyweight / Proxy

3. Behavioral patterns

  • Interpreter / Template method / Chain of responsibility / Command pattern / Iterator pattern / Strategy pattern / Visitor pattern

4. J2EE patterns

  • MVC Pattern / Data Access Object pattern / Front controller pattern / Intercepting filter pattern / Transfer object pattern

Tiếng Việt

Dựa trên problem analysis. Có 4 loại Java Design Patterns:

  1. Creational patterns
  • Factory method / Abstract Factory / Builder / Prototype /Singleton

2. Structural patterns

  • Adapter / Bridge / Filter / Composite / Decorator / Facade / Flyweight / Proxy

3. Behavioral patterns

  • Interpreter / Template method / Chain of responsibility / Command pattern / Iterator pattern / Strategy pattern / Visitor pattern

4. J2EE patterns

  • MVC Pattern / Data Access Object pattern / Front controller pattern / Intercepting filter pattern / Transfer object pattern

3. Explain the Singleton pattern? Giải thích về Singleton pattern

Tiếng Anh

Singleton pattern in Java is a pattern which allows a single instance within an application. One good example of the singleton pattern is java.lang.Runtime.

Singleton Pattern states that define a class that has only one instance and provides a global point of access to it.

In other words, it is the responsibility of the class that only a single instance should be created, and all other classes can use a single object.

Tiếng Việt

Singleton pattern trong Java chỉ cho phép một instance duy nhất.

Một ví dụ điển hình về singleton pattern là java.lang.Runtime.

Các trạng thái Singleton Pattern xác định một class chỉ có một instance và cung cấp một global point để truy cập. Nói cách khác, nhiệm vụ của class là chỉ tạo một instance và tất cả các class khác có thể sử dụng một single object.

4. How many ways can you create a singleton pattern? Có bao nhiêu cách để tạo Singleton pattern

Tiếng Anh

There are two ways of creating a Singleton pattern.

1. Early Instantiation: It is responsible for the creation of instance at load time.

2. Lazy Instantiation: It is responsible for the creation of instance when required.

Tiếng Việt

Có hai cách để tạo một Singleton pattern.

1. Early Instantiation: chịu trách nhiệm tạo instance tại load time.

2. Lazy Instantiation: chịu trách nhiệm tạo instance khi được yêu cầu.

5. Describe the uses of the composite pattern? Mô tả cách sử dụng Composite pattern

Tiếng Anh

It is used in the following cases:

  • When we want to represent a partial or full hierarchy of objects.
  • In case we need to add the responsibilities dynamically to the individual object without affecting other objects.

Tiếng Việt

Composite pattern được sử dụng trong các trường hợp sau:

  • Khi chúng ta muốn miêu tả một phần hoặc toàn bộ phân cấp của các object.
  • Trong trường hợp chúng ta cần thêm các trách nhiệm một cách linh hoạt cho từng object mà không ảnh hưởng đến các object khác.

6. What are Some Design Patterns which are used in the JDK library? – Kể tên một số design patterns được sử dụng trong JDK library

Tiếng Anh

Some of the design patterns which are used in the JDK library:

  • The decorator pattern is used by Wrapper classes.
  • Singleton pattern is used by Calendar classes (Runtime).
  • The Wrapper classes use factory pattern like Integer.valueOf.
  • Event handling frameworks use observer pattern like swing, awt.

Tiếng Việt

Một số design patterns được sử dụng trong JDK library:

  • Decorator pattern được sử dụng bởi Wrapper classes.
  • Singleton pattern được sử dụng bởi Calendar classes (Runtime).
  • Wrapper classes sử dụng factory pattern như Integer.valueOf.
  • Event handling frameworks sử dụng observer pattern như swing, awt.

7. What are the Creational Patterns?

Tiếng Anh

Creational design patterns are related to the way of creating objects. Creational design patterns are used when a decision is made at the time of instantiation of a class.

Tiếng Việt

Creational design patterns có liên quan đến cách tạo object. Nó được sử dụng khi muốn tạo instance của một class.

8. What Is Factory Pattern? Factory Pattern là gì?

Tiếng Anh

  • It is the most used design pattern in Java.
  • These design patterns belong to the Creational Pattern as this pattern provides one of the best ways to create an object.
  • In the Factory pattern, we don’t expose the creation logic to the client and refer the created object using a standard interface.
  • Factory Pattern allows the sub-classes to choose the type of objects to create.
  • The Factory Pattern is also known as Virtual Constructor.

Tiếng Việt

  • Đây là design pattern được sử dụng nhiều nhất trong Java.
  • Các design pattern này thuộc Creational Pattern vì nó cung cấp một trong những cách tốt nhất để tạo một object.
  • Trong Factory pattern, chúng ta ẩn logic tạo object và việc tạo object sẽ được tạo thông qua việc sử dụng Interface.
  • Factory Pattern cho phép các sub-class chọn loại object để tạo.
  • Factory Pattern còn được biết đến là Virtual Constructor.

9. Difference between Strategy and State design Pattern in Java?

Tiếng Anh

  • The state design pattern is used to manage and define the state of an object.
  • The Strategy pattern is used to describe a set of action of object

Tiếng Việt

  • State design Pattern được sử dụng để quản lý và xác định state (trạng thái) của một object
  • Strategy pattern được sử dụng để mô tả một bộ action (hành động) của một object

10. What are the Adapter patterns?

Tiếng Anh

Adapter pattern converts the interface of a class into another interface based on the requirement.

It is also known as Wrapper.

Tiếng Việt

Adapter pattern sử dụng để converts interface của một class thành interface khác dựa trên yêu cầu.

Nó còn được biết đến là Wrapper.

Phần 3: SPRING / SPRING BOOT (10 câu)

1. Difference between Spring Vs Spring Boot?

Tiếng Anh

  • Spring is a web application framework based on Java. It provides tools and libraries to create a complete cutomized web application.
  • Wheras Spring Boot is a spring module which is used to config spring application faster

Tiếng Việt

  • Spring là một web application framework dựa trên Java. Nó cung cấp các công cụ và thư viện để tạo một ứng dụng web hoàn chỉnh.
  • Spring Boot là một spring module được sử dụng để config spring application nhanh hơn.

2. What are the different bean scopes in spring?

Tiếng Anh

There are 5 bean scopes in spring framework.

  1. Singleton: The bean instance will be only once and same instance will be returned by the IOC container. It is the default scope.
  2. Prototype: The bean instance will be created each time when requested.
  3. Request: The bean instance will be created per HTTP request.
  4. Session: The bean instance will be created per HTTP session.
  5. Globalsession: The bean instance will be created per HTTP global session.

Tiếng Việt

Có 5 bean scopes trong spring framework.

  1. Singleton: trong lifecycle của application chỉ có 1 instance của object được tạo ra. Instance giống nhau sẽ được trả về bởi IOC container. Nó là default scope.
  2. Prototype: bean instance sẽ được tạo mỗi lần khi được yêu cầu.
  3. Request: bean instance sẽ được tạo theo yêu cầu HTTP.
  4. Session: bean instance sẽ được tạo cho mỗi phiên HTTP.
  5. Globalsession: bean instance sẽ được tạo cho mỗi HTTP global session.

3. What is the difference between constructor injection and setter injection?

Tiếng Anh

There are many key differences between constructor injection and setter injection.

  1. Partial dependency: can be injected using setter injection but it is not possible by constructor.
  2. Overriding: Setter injection overrides the constructor injection.
  3. Changes: We can easily change the value by setter injection. It doesn’t create a new bean instance always like constructor. So setter injection is flexible than constructor injection.

Tiếng Việt

Có nhiều sự khác biệt chính giữa constructor injection và setter injection.

Phụ thuộc một phần: có thể được inject bằng cách sử dụng setter injection. Constructor thì không thể thực hiện được điều đó.

Overriding: Setter injection ghi đè constructor injection.

Changes: Chúng ta có thể dễ dàng thay đổi giá trị bằng setter injection. Nó không tạo bean instance như constructor. Vì vậy, setter injection linh hoạt hơn constructor injection.

4. What is AOP? The advantages of spring AOP?

Tiếng Anh

AOP is an acronym for Aspect Oriented Programming. It is a methodology that divides the program logic into pieces or parts or concerns.

It increases the modularity and the key unit is Aspect.

The advantages of spring AOP?

AOP enables you to dynamically add or remove concern before or after the business logic. It is pluggable and easy to maintain.

Tiếng Việt

AOP là từ viết tắt của Aspect Oriented Programming. Đó là một phương pháp phân chia logic chương trình thành nhiều thành phần và mối quan tâm (trình tự, log,…).

Nó làm tăng tính module hoá chương trình và key unit là Aspect.

Những lợi thế của spring AOP?

AOP cho phép tự động thêm hoặc xóa mối quan tâm trước hoặc sau logic nghiệp vụ. Nó dễ kết hợp và dễ maintain.

5. What is IOC and DI?

Tiếng Anh

IOC (Inversion of Control) and DI (Dependency Injection) is a design pattern to provide loose coupling. It removes the dependency from the program.

Tiếng Việt


IOC (Inversion of Control) và DI (Dependency Injection) là một design pattern. Nó giúp giảm sự phụ thuộc giữa các thành phần phụ thuộc lẫn nhau.

6. In which scenario, you will use singleton and prototype scope?

Tiếng Anh

  • Singleton scope should be used with EJB stateless session bean
  • Prototype scope with EJB stateful session bean

Tiếng Việt

  • Singleton scope nên được sử dụng với EJB stateless session bean
  • Prototype scope nên được sử dụng với EJB stateful session bean

7. What is thymeleaf?

Tiếng Anh

It is a server side Java template engine for web application. It’s main goal is to bring elegant natural templates to your web application.

It can be integrate with Spring Framework and ideal for HTML5 Java web applications.

Tiếng Việt

Thymeleaf là một Java template engine cho web application phía server side. Nó cung cấp các template cho web application.

Nó có thể được integrate với Spring Framework và phù hợp với HTML5 Java web applications.

8. How to use thymeleaf?

Tiếng Anh

In order to use Thymeleaf we must add it into our pom.xml file like:

<dependency>    

<groupId>org.springframework.boot</groupId>    

<artifactId>spring-boot-starter-thymeleaf</artifactId>    

</dependency>    

Tiếng Việt

Để sử dụng Thymeleaf, chúng ta phải thêm nó vào file pom.xml

9.  What is @RestController annotation in Spring Boot?

Tiếng Anh

The @RestController is a stereotype annotation. It adds @Controller and @ResponseBody annotations to the class. We need to import org.springframework.web.bind.annotation package in our file, in order to implement it.

Tiếng Việt

@RestController là một annotation. Nó thêm @Controller and @ResponseBody annotations vào class. Chúng ta cần phải import org.springframework.web.bind.annotation package

10. What is @RequestMapping annotation in Spring Boot?

Tiếng Anh

The @RequestMapping annotation is used to provide routing information. It tells to the Spring that any HTTP request should map to the corresponding method. We need to import org.springframework.web.annotation package in our file.

Tiếng Việt

The @RequestMapping annotation được sử dụng để cung cấp thông tin routing. Nó gửi thông tin đến Spring rằng bất kì HTTP request phải map với method tương ứng.

Chúng ta cần import org.springframework.web.annotation package vào file.

Phần 4: HIBERNATE (10 câu)

1. What is hibernate?

Tiếng Anh

Hibernate is an open-source and lightweight ORM tool that is used to store, manipulate, and retrieve data from the database.

Tiếng Việt

Hibernate là một công cụ open source ORM đơn giản. Nó được sử dụng để lưu trữ, thao tác và truy xuất dữ liệu từ database.

2. What is ORM?

Tiếng Anh

ORM is an acronym for Object/Relational mapping. It is a programming strategy to map object with the data stored in the database. It simplifies data creation, data manipulation, and data access.

Tiếng Việt

ORM là từ viết tắt của Object/Relational mapping. Nó map object với dữ liệu được lưu trữ trong database. Nó đơn giản hóa việc tạo dữ liệu, thao tác dữ liệu và truy cập dữ liệu.

3. Some of the advantages of using ORM over JDBC.

Tiếng Anh

ORM has the following advantages over JDBC:

  • Application development is fast.
  • Management of transaction.
  • Generates key automatically.
  • Details of SQL queries are hidden.

Tiếng Việt

ORM có những ưu điểm sau so với JDBC:

  • Phát triển ứng dụng nhanh chóng.
  • Quản lý transaction.
  • Generates key tự động.
  • Chi tiết về các truy vấn SQL được ẩn.

4. What is Session?

Tiếng Anh

  • It maintains a connection between the hibernate application and database.
  • It provides methods to store, update, delete or fetch data from the database such as persist(), update(), delete(), load(), get() etc.
  • It provides factory methods to return these instances.

Tiếng Việt

  • Nó duy trì kết nối giữa hibernate application và database..
  • Nó cung cấp các method để lưu trữ, cập nhật, xóa hoặc fetch dữ liệu từ database, chẳng hạn như persist(), update(), delete(), load(), get(), v.v.
  • Nó cung cấp các factory methods để trả về các instance.

5. Is Session a thread-safe object?

Tiếng Anh

No, Session is not a thread-safe object, many threads can access it simultaneously. In other words, you can share it between threads.

Tiếng Việt

Không. Session không phải là thread-safe object. Nhiều thread có thể truy cập đồng thời. Nói cách khác, bạn có thể chia sẻ nó giữa các thread với nhau.

6. What are the states of the object in hibernate?

Tiếng Anh

There are 3 states of the object (instance) in hibernate.

  1. Transient: The object is in a transient state if it is just created but has no primary key (identifier) and not associated with a session.
  2. Persistent: The object is in a persistent state if a session is open, and you just saved the instance in the database or retrieved the instance from the database.
  3. Detached: The object is in a detached state if a session is closed. After detached state, the object comes to persistent state if you call lock() or update() method.

Tiếng Việt

Có 3 trạng thái của object (instance) trong hibernate.

  1. Transient: Object ở trạng thái Transient nếu nó được tạo nhưng không có primary key (identifier) và không được liên kết với session.
  2. Persistent: Object ở trạng thái Persistent nếu một session đang open và instance vừa được save xuống database hoặc retrieved instance từ database.
  3. Detached: Object ở trạng thái Detached nếu session bị close. Sau trạng thái Detached, Object chuyển sang trạng thái Persistent nếu bạn gọi phương thức lock() hoặc update().

7. How many types of association mapping are possible in hibernate?

Tiếng Anh

There can be 4 types of association mapping in hibernate.

  1. One to One
  2. One to Many
  3. Many to One
  4. Many to Many

Tiếng Việt

Có 4 loại liên kết mapping trong hibernate.

  1. One to One
  2. One to Many
  3. Many to One
  4. Many to Many

8. What is lazy loading in hibernate?

Tiếng Anh

Lazy loading in hibernate improves the performance. It loads the child objects on demand.

Since Hibernate 3, lazy loading is enabled by default, and you don’t need to do lazy=”true”. It means not to load the child objects when the parent is loaded.

Tiếng Việt

Lazy loading trong hibernate giúp cải thiện về hiệu suất. Nó load các object con theo yêu cầu.

Kể từ Hibernate 3, lazy loading được bật mặc định và bạn không cần phải thực hiện lazy=”true”. Có nghĩa là không cần load các object con khi object cha đã được load.

9. What is HQL (Hibernate Query Language)?

Tiếng Anh

Hibernate Query Language is known as an object-oriented query language. It is like a structured query language (SQL).

The main advantage of HQL over SQL is:

  1. You don’t need to learn SQL
  2. Database independent
  3. Simple to write a query

Tiếng Việt

Hibernate Query Language là ngôn ngữ truy vấn hướng đối tượng. Nó giống như một ngôn ngữ truy vấn có cấu trúc (SQL).

Ưu điểm chính của HQL so với SQL là:

  1. Không cần học SQL
  2. Cơ sở dữ liệu độc lập
  3. Đơn giản để viết câu truy vấn

10. What are the inheritance mapping strategies?

Tiếng Anh

There are 3 ways of inheritance mapping in hibernate.

  1. Table per hierarchy
  2. Table per concrete class
  3. Table per subclass

Tiếng Việt

Có 3 cách kế thừa mapping trong hibernate

  1. Table per hierarchy
  2. Table per concrete class
  3. Table per subclass

Phần 5: WEBSERVICE (5 câu)

1. What is WDSL?

Tiếng Anh

WSDL stands for Web Service Description Language. It describes the web services and exchanges the information in a decentralized or distributed environment. It is used to create a relationship between the service requester and the service provider. It is an XML based language to describe the functionality of a SOAP-based web service.

Tiếng Việt


WSDL là viết tắt của Web Service Description Language. Nó mô tả các dịch vụ web và trao đổi thông tin trong môi trường phi tập trung hoặc phân tán. Nó được sử dụng để tạo mối quan hệ giữa người yêu cầu dịch vụ và nhà cung cấp dịch vụ. Nó là một ngôn ngữ dựa trên XML để mô tả chức năng của SOAP-based web service.

2. What is SOAP?

Tiếng Anh

SOAP stands for Simple Object Access Protocol. It is a XML-based protocol for accessing web services.

SOAP is a W3C recommendation for communication between two applications.

SOAP is XML based protocol. It is platform independent and language independent. By using SOAP, you will be able to interact with other programming language applications.

Tiếng Việt

SOAP là viết tắt của Simple Object Access Protocol. Nó là một giao thức dựa trên XML để truy cập các dịch vụ web.

SOAP là một đề xuất của W3C để có thể communicate giữa hai application.

SOAP là giao thức dựa trên XML. Đó là nền tảng độc lập và ngôn ngữ độc lập. Bằng cách sử dụng SOAP, bạn có thể tương tác với các ứng dụng ngôn ngữ lập trình khác.

3. Advantages and Disadvantages of Soap Web Services

Tiếng Anh

  • Advantages of Soap Web Services
    • WS Security: SOAP defines its own security known as WS Security.
    • Language and Platform independent: SOAP web services can be written in any programming language and executed in any platform.
  • Disadvantages of Soap Web Services
    • Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing the SOAP applications. So it is slow and consumes more bandwidth and resource.
    • WSDL dependent: SOAP uses WSDL and doesn’t have any other mechanism to discover the service.

Tiếng Việt

  • Ưu điểm của Soap Web Services
    • Bảo mật WS: SOAP có tính chất bảo mật riêng, được gọi là Bảo mật WS.
    • Ngôn ngữ và nền tảng độc lập: Các dịch vụ web SOAP có thể được viết bằng bất kỳ ngôn ngữ lập trình nào và được thực thi trong bất kỳ nền tảng nào.
  • Nhược điểm của Soap Web Services
    • Chậm: SOAP sử dụng định dạng XML phải được phân tích cú pháp để đọc được. Có rất nhiều tiêu chuẩn phải tuân theo khi phát triển các ứng dụng SOAP. Vì vậy, nó chậm và tiêu tốn nhiều băng thông và tài nguyên hơn.
    • Phụ thuộc vào WSDL: SOAP sử dụng WSDL và không có bất kỳ cơ chế nào khác thay thế để mất tính phụ thuộc vào nó.

4 What is RMI & when use RMI?

Tiếng Anh

The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.

The RMI provides remote communication between the applications using two objects stub and skeleton.

Tiếng Việt


RMI (Remote Method Invocation) là một API cung cấp cơ chế tạo ứng dụng phân tán trong java. RMI cho phép một đối tượng gọi các phương thức trên một đối tượng đang chạy trong một JVM khác.

RMI cung cấp giao tiếp từ xa giữa các ứng dụng sử dụng hai đối tượng stub và skeleton.

5. What is the difference between SOAP and REST?

Tiếng Anh

RESTSOAP
Assumes a point-to-point communication model–not usable for distributed computing environment where message may go through one or more intermediariesDesigned to handle distributed computing environments
Better suited for point-to-point or where the intermediary does not play a significant roleWell suited for intermediated services
No constraints on the payloadPayload must comply with the SOAP schema
Only the most well established standards apply e.g. HTTP, SSL. No established standards for other aspects.  DELETE and PUT methods often disabled by firewalls, leads to security complexity.A large number of supporting standards for security, reliability, transactions.
Built-in error handling (faults)No error handling
Tied to the HTTP transport modelBoth SMTP and HTTP are valid application layer protocols used asTransport for SOAP

Tiếng Việt

RESTSOAP
Không sử dụng được cho môi trường điện toán phân tánĐược thiết kế để xử lý các môi trường điện toán phân tán
Phù hợp hơn với point-to-point hoặc nơi các dịch vụ trung gian không đóng vai trò quan trọngPhù hợp cho các dịch vụ trung gian
Không có ràng buộc nào đối với payload Payload phải tuân theo SOAP schema
Chỉ những tiêu chuẩn được thiết lập tốt nhất mới được áp dụng, ví dụ: HTTP, SSL. Không có tiêu chuẩn được thiết lập cho các khía cạnh khác. Các phương thức DELETE và PUT thường bị vô hiệu hóa bởi tường lửa, dẫn đến sự phức tạp về bảo mật. Có nhiều tiêu chuẩn hỗ trợ về bảo mật, độ tin cậy, giao dịch.
Xử lý lỗi tích hợp (lỗi) Không xử lý lỗi
Gắn liền với mô hình HTTP transportCả SMTP và HTTP đều là các giao thức lớp ứng dụng hợp lệ được sử dụng như Transport cho SOAP

*Dịch bởi: gocnhoccuachi.com

* Nguồn tiếng Anh tham khảo: https://www.javatpoint.com/

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments