Home > Java > javaTutorial > Selection of high-frequency Java code interview questions

Selection of high-frequency Java code interview questions

王林
Release: 2023-06-15 16:44:48
Original
791 people have browsed it

As Java programmers, we will definitely be asked some questions about Java code when participating in interviews. These questions can sometimes be very basic and are often asked very frequently. In this article, I hope to share some common and valuable high-frequency Java coding interview questions that I think.

  1. Finding the maximum and minimum values ​​in an array

This problem is relatively basic. Usually the interviewer may ask you to handwrite a simple algorithm to solve it.

The following is a simple method:

public static void findMinMax(int[] arr) {
    if (arr == null || arr.length == 0) {
        return;
    }
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] < min) {
            min = arr[i];
        }
        if (arr[i] > max) {
            max = arr[i];
        }
    }

    System.out.println("Minimum value: " + min);
    System.out.println("Maximum value: " + max);
}
Copy after login
  1. How to reverse a string

This is a classic interview question that usually tests you Is it possible to implement simple string operations using some API in Java.

The following is an implementation method:

public static String reverseString(String str) {
    if (str == null || str.length() == 0) {
        return "";
    }

    char[] charArray = str.toCharArray();
    int i = 0;
    int j = str.length() - 1;

    while (i < j) {
        char temp = charArray[i];
        charArray[i] = charArray[j];
        charArray[j] = temp;
        i++;
        j--;
    }

    return new String(charArray);
}
Copy after login
  1. Determine whether a string is a palindrome string

This is another common string Interview questions. Usually the interviewer will ask you to handwrite an algorithm to determine whether a string is a palindrome string.

The following is an implementation method:

public static boolean isPalindromeString(String str) {
    if (str == null || str.length() == 0) {
        return true;
    }

    int i = 0;
    int j = str.length() - 1;

    while (i < j) {
        if (str.charAt(i) != str.charAt(j)) {
            return false;
        }
        i++;
        j--;
    }

    return true;
}
Copy after login
  1. Implementing a singleton pattern

This is a very common Java interview question, which usually requires you Hand-write an implementation of the singleton pattern to ensure that only one instance can be created.

The following is a general implementation method:

public class Singleton {
    private static Singleton instance;

    private Singleton() {
        // 私有构造方法
    }

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }

        return instance;
    }
}
Copy after login

This is a simple implementation method, but it is not thread-safe. In a multi-threaded situation, it may happen that multiple instances are created. It should be noted that in order to ensure thread safety, we need to set the getInstance method as a synchronized method.

  1. How to implement a blocking queue

This is a relatively advanced Java interview question. You are usually required to hand-write a simple blocking queue implementation to ensure that when the queue is full or empty, the threads performing insertion and deletion operations on the queue can be blocked waiting for the state to be reduced.

The following is a simple blocking queue implementation:

public class BlockingQueue<T> {
    private Queue<T> queue = new LinkedList<T>();
    private int capacity;

    public BlockingQueue(int capacity) {
        this.capacity = capacity;
    }

    public synchronized void put(T item) throws InterruptedException {
        while (queue.size() == capacity) {
            wait();
        }
        queue.add(item);
        notifyAll();
    }

    public synchronized T take() throws InterruptedException {
        while (queue.isEmpty()) {
            wait();
        }
        T item = queue.remove();
        notifyAll();
        return item;
    }
}
Copy after login

The above implementation uses the wait() and notifyAll() methods in Java to implement thread blocking and awakening. When the queue is full or empty, the thread that performs insertion or deletion operations on the queue will be blocked waiting for the queue status to change.

In this article, I share some common Java interview questions, which are usually asked very frequently and are knowledge points worth mastering in depth. If you are preparing for a Java interview, I hope these questions can help you!

The above is the detailed content of Selection of high-frequency Java code interview questions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template