Java Basic Interview Question 02

1. What are the four access modifiers in Java? What do they mean?

The four access modifiers in Java are: public, protected, default, and private. They mean the following respectively:

  • public: can be accessed by any class;
  • protected: can be accessed by classes in the same package and subclasses in different packages;
  • default (no modifier): can be accessed by classes in the same package;
  • private: can only be accessed by methods in the same class.

2. What is polymorphism in Java? What are its implementations?

Polymorphism means that the same method has different manifestations on different objects. There are two ways to implement polymorphism in Java:

  • Inheritance: Subclass inherits parent class and rewrites parent class' method...

3. What is the purpose of final keyword in Java?

The final keyword has three uses:

  • Modified class, indicating that the class cannot be inherited;
  • Modified method, indicating that the method cannot be overridden;
  • Modified variable, indicating that the variable can only be assigned once.

4. What is autoboxing and unboxing in Java?

Autoboxing and unboxing refers to the automatic conversion between primitive data types and corresponding wrapper types in Java. For example, a variable of type int can be automatically converted to a variable of type Integer. This process is called automatic boxing; and a variable of type Integer can also be automatically converted to a variable of type int. This process is called automatic unboxing.

5. What is the difference between interface and abstract class in Java?

Both interfaces and abstract classes are mechanisms for achieving polymorphism, but there are several differences between them:

  • Interfaces can only define constants and abstract methods, while abstract classes can define constants, abstract methods and concrete methods;
  • A class can implement multiple interfaces, but can only inherit one abstract class;
  • All methods in an interface are public by default, while methods in an abstract class can have different access modifiers.

6. How to implement threads in Java? What are the ways?

There are two ways to implement threads in Java:

  • Inherit the Thread class and override the run method;
  • Implement the Runnable interface and override the run method.

7. How to achieve synchronization between multiple threads in Java?

There are several ways to achieve synchronization between multiple threads in Java:

  • synchronized keyword;
  • ReentrantLock class;
  • Semaphore class;
  • CountDownLatch class;
  • The CyclicBarrier class.

8. How to handle exceptions in Java? What keywords are there?

There are two ways to handle exceptions in Java:

  • try-catch-finally block;
  • throws keyword.

Common keywords in Java are as follows:

  • try: used to contain code blocks that may throw exceptions;
  • catch: used to catch the exception thrown in the try block and handle it;
  • finally: used to contain some code that needs to be executed regardless of whether an exception occurs;
  • throws: Used to declare which exceptions the method may throw.

9. How to read and write files in Java?

There are several key classes for file read and write operations in Java:

  • FileInputStream and FileOutputStream: used to read and write binary files;
  • FileReader and FileWriter: used to read and write text files;
  • BufferedReader and BufferedWriter: used to cache read and write text files.

10. How to deal with date and time in Java?

There are two key classes for dealing with dates and times in Java:

  • Date class: used to represent date and time;
  • SimpleDateFormat class: Used to format dates and times as strings or parse strings as dates and times.

11. How to do network programming in Java?

Network programming in Java requires the use of the following two key classes:

  • Socket class: used for communication between client and server;
  • ServerSocket class: used to create server-side programs, listen to client requests and provide services.

12. How to perform reflection operation in Java? What are the key classes?

Reflection operations in Java require the use of the following three key classes:

  • Class class: used to represent a class or interface;
  • Constructor class: used to represent a constructor;
  • Method class: used to represent a method.

13. How to implement serialization and deserialization in Java?

Serialization and deserialization in Java require the use of the following two key interfaces:

  • Serializable interface: used to mark a class can be serialized;
  • Externalizable interface: used to control the details of the object serialization process.

14. What is Generics in Java? what's the effect?

Generics mean that you do not specify a specific type when writing code, but specify the type when you use it. The role of generics in Java mainly has the following two aspects:

  • Improve code readability and security, avoid type conversion errors;
  • It can avoid code duplication and improve code reusability.

15. How to implement collection operations in Java?

Implementing collection operations in Java requires the use of the following key classes:

  • List interface: used to represent a list, such as ArrayList, LinkedList, etc.;
  • Set interface: used to represent a collection, such as HashSet, TreeSet, etc.;
  • Map interface: used to represent a mapping table, such as HashMap, TreeMap, etc.

16. How to perform annotation operation in Java? What are the predefined annotations?

Annotation operations in Java require the use of the following two key interfaces:

  • Annotation interface: All annotations must implement this interface;
  • RetentionPolicy enumeration type: Indicates the scope of annotation retention, such as SOURCE, CLASS and RUNTIME.

Common predefined annotations in Java are as follows:

  • @Override: Indicates that the method is to override the method in the parent class or interface;
  • @Deprecated: Indicates that the method is outdated and is not recommended for use;
  • @SuppressWarnings: Indicates that warning messages of the specified type are ignored.

17. How to perform string manipulation in Java?

String operations in Java require the use of the following key classes:

  • String class: used to represent a string and provides a series of string manipulation methods;
  • StringBuilder and StringBuffer classes: used to dynamically create strings and provide a series of string manipulation methods.

18. How to perform regular expression operations in Java? What are the key classes?

Regular expression operations in Java require the use of the following two key classes:

  • Pattern class: used to represent a regular expression pattern;
  • Matcher class: used to apply regular expression patterns to strings and perform matching operations.

19. How to perform enumeration operation in Java?

Enumeration operations in Java require the use of the Enum keyword, for example:

public enum Color {
    
    
    RED, GREEN, BLUE;
}

20. How to perform annotation operation in Java? What are the annotation methods?

There are three ways to perform annotation operations in Java:

  • Single-line comments: start with //, indicating that the content after the line is all comments;
  • Multi-line comments: start with /* and end with */, indicating that this content is a comment;
  • Documentation comments: start with /** and end with */, and the documentation can be generated by the javadoc tool.

Guess you like

Origin blog.csdn.net/qq_43545600/article/details/131355308