java面试编程面试题_Java编程面试的前50个问题

本文提供了一份包含50个Java编程面试问题的清单,旨在测试求职者的编码技能。问题涵盖字符串操作、数字处理、数据结构、算法、异常处理等多个方面,从基础到高级,适合不同经验级别的开发者。文章强调了了解最新Java特性的重要性,如Java 14中的新功能。
摘要由CSDN通过智能技术生成

java面试编程面试题

Java Programming Interview Questions are always the deciding factor in any Java interview. Whether you are a beginner in Java or an expert programmer, you will be tested for your coding skills in the interview. So, it’s a good idea to brush up your coding skills before you face the interview.

Java编程面试问题始终是任何Java面试的决定因素。 无论您是Java的初学者还是专家程序员,都将在面试中测试您的编码技能。 因此,在面试之前提高自己的编码技能是一个好主意。

Java编程面试问题 (Java Programming Interview Questions)

I am providing 50+ Java programming questions to test your coding skills. It’s good for beginners as well as experienced Java developers. The best part is that some of the questions are from the latest releases (Java 14). If you can write Java programs using the latest features, it means you keep yourself up to date, which will work in your favor.

我提供了50多个Java编程问题来测试您的编码技能。 对于初学者和有经验的Java开发人员来说都非常好。 最好的部分是一些问题来自最新版本(Java 14)。 如果您可以使用最新功能编写Java程序,则意味着您可以保持最新状态,这将对您有利。

1.如何在Java中反转字符串? (1. How to reverse a String in Java?)

It might be surprising, but there is no reverse() utility method in the String class. But, it’s a very simple task. We can create a character array from the string and then iterate it from the end to start. We can append the characters to a string builder and finally return the reversed string.

可能令人惊讶,但是String类中没有反向()实用程序方法。 但是,这是一个非常简单的任务。 我们可以从字符串创建一个字符数组,然后从头开始迭代它。 我们可以将字符附加到字符串生成器,最后返回反转的字符串。

public class StringPrograms {

	public static void main(String[] args) {

		String str = "123";
		System.out.println(reverse(str));
	}

	public static String reverse(String in) {
		if (in == null)
			throw new IllegalArgumentException("Null is not valid input");

		StringBuilder out = new StringBuilder();

		char[] chars = in.toCharArray();

		for (int i = chars.length - 1; i > 0; i--)
			out.append(chars[i]);

		return out.toString();
	}
}

Bonus Points: Adding null check in the method and using StringBuilder for appending the characters.

优点 :在方法中添加null检查,并使用StringBuilder附加字符。

Easy to Miss: The indexing in Java starts from 0. So, we have to start at “chars.length – 1” in the for loop.

容易错过 :Java中的索引从0开始。因此,我们必须在for循环中从“ chars.length – 1”开始。

2.如何在不使用第三个变量的情况下交换两个数字? (2. How to swap two numbers without using a third variable?)

It’s a slightly tricky question. It’s a three steps process. It’s better visualized in code.

这是一个有点棘手的问题。 这是一个三步过程。 用代码更好地可视化。

int a = 10;
int b = 20;

b = b + a; // now b is sum of both the numbers
a = b - a; // b - a = (b + a) - a = b (a is swapped)
b = b - a; // (b + a) - b = a (b is swapped)

We can’t return multiple variables in Java. Since Java is Pass-by-Value and these are primitive data types, their values won’t change. For example, below swap function will not change the input integer values.

我们无法在Java中返回多个变量。 由于Java是“按值传递”并且它们是原始数据类型,因此它们的值不会更改。 例如,下面的swap函数将不会更改输入整数值。

public static void swapNumbers(int a, int b) {
    b = b + a;
	a = b - a;
	b = b - a;
}

public static void main(String[] args) {
	int a = 10;
	int b = 20;
	swapNumbers(a, b);
	System.out.printf("a is %d, b is %d", a, b); // a is 10, b is 20
}

3. Java程序检查字符串中是否存在元音? (3. Java Program to check if a vowel is present in the string?)

We can use regular expression to check if the string contains vowels or not.

我们可以使用正则表达式来检查字符串是否包含元音。

public class StringContainsVowels {

	public static void main(String[] args) {

		System.out.println(stringContainsVowels("Hello")); // true
		System.out.println(stringContainsVowels("TV")); // false

	}

	public static boolean stringContainsVowels(String input) {

		return input.toLowerCase().matches(".*[aeiou].*");

	}

}

4. Java程序检查给定数字是否为Prime? (4. Java program to check if the given number is Prime?)

We can write a simple program to divide the given number “n” from 2 to n/2 and check the remainder. If the remainder is 0, then it’s not a prime number.

我们可以编写一个简单的程序将给定的数字“ n”从2除以n / 2,然后检查余数。 如果余数为0,则它​​不是质数。

public class PrimeNumberCheck {

	public static void main(String[] args) {

		System.out.println(isPrime(19)); // true
		System.out.println(isPrime(49)); // false
	}

	public static boolean isPrime(int n) {
		if (n == 0 || n == 1) {
			return false;
		}
		if (n == 2) {
			return true;
		}
		for (int i = 2; i <= n / 2; i++) {
			if (n % i == 0) {
				return false;
			}
		}

		return true;
	}
}

But, this is not very memory and time-efficient. For a given number N, if there is a prime number M between 2 to √N (square root of N) that evenly divides it, then N is not a prime number.

但是,这不是很节省内存和时间。 对于给定的数N,如果在2到√N(N的平方根)之间有一个质数M对其进行均分,则N不是质数。

You can read more about it here.

您可以在此处了解更多信息。

5.使用递归的斐波那契数列 (5. Fibonacci Series using recursion)

We can use a for loop to print fibonacci series.

我们可以使用for循环来打印斐波那契数列。

public static void printFibonacciSeries(int count) {
	int a = 0;
	int b = 1;
	int c = 1;
	for (int i = 1; i <= count; i++) {
		System.out.print(a + ", ");
		a = b;
		b = c;
		c = a + b;
	}
}

The fibonacci number is generated by adding the previous two numbers – F(N) = F(N-1) + F(N-2). We can use recursion to print fibonacci series.

斐波那契数是通过将前两个数相加而得出的-F(N)= F(N-1)+ F(N-2)。 我们可以使用递归来打印斐波那契数列。

public class FibonacciNumbers {
	public static int fibonacci(int n) {
		if (n <= 1)
			return n;
		return fibonacci(n - 1) + fibonacci(n - 2);
	}

	public static void main(String args[]) {
		int n = 10;
		System.out.println(fibonacci(n));
	}
}

6.检查整数列表是否仅包含奇数? (6. Check if a List of integers contains only odd numbers?)

We can use for loop and check each element one by one if they are odd or not.

我们可以使用for循环并逐个检查每个元素是否为奇数。

public static boolean onlyOddNumbers(List<Integer> list) {
	for (int i : list) {
		if (i % 2 == 0)
			return false;
	}
	return true;
}

If the list is huge, we can use parallel stream for faster processing.

如果列表很大,我们可以使用并行流进行更快的处理。

public static boolean onlyOddNumbers(List<Integer> list) {
	return list
			.parallelStream() // parallel stream for faster processing
			.anyMatch(x -> x % 2 != 0); // return as soon as any elements match the condition
}

7.回文检查 (7. Palindrome Check)

A palindrome string is one whose reverse is also the same string. So we can reverse the input string and check if both strings are equal or not. We can also use the String charAt(int index) method to check for palindrome string.

回文字符串是反向字符串也相同的字符串。 因此,我们可以反转输入字符串,并检查两个字符串是否相等。 我们还可以使用String charAt(int index)方法检查回文字符串。

boolean checkPalindromeString(String input) {
	boolean result = true;
	int length = input.length();
	for(int i=0; i < length/2; i++) {
		if(input.charAt(i) != input.charAt(length-i-1)) {
			result = false;
			break;
		}
	}
	return result;
}

8.如何从字符串中删除空格 (8. How to remove Whitespaces from String)

We can use Character.isWhitespace() method to remove whitespaces from the string.

我们可以使用Character.isWhitespace()方法从字符串中删除空格。

String removeWhiteSpaces(String input){
	StringBuilder output = new StringBuilder();
	
	char[] charArray = input.toCharArray();
	
	for(char c : charArray) {
		if (!Character.isWhitespace(c))
			output.append(c);
	}
	
	return output.toString();
}

9.如何从字符串中删除开头和结尾的空格? (9. How to remove leading and trailing whitespaces from a string?)

Java String class contains two methods to remove leading and trailing whitespaces – trim(), and strip(). The strip() method was added to the String class in Java 11. However, the strip() method uses Character.isWhitespace() method to check if the character is a whitespace. This method uses Unicode code points whereas the trim() method identifies any character having codepoint value less than or equal to ‘U+0020’ as a whitespace character.

Java String类包含两种删除开头和结尾空格的方法– trim()和strip()。 strip()方法已添加到Java 11中的String类中。但是,strip()方法使用Character.isWhitespace()方法来检查字符是否为空格。 此方法使用Unicode代码点,而trim()方法将代码点值小于或等于'U + 0020'的任何字符标识为空白字符。

The strip() method is the recommended way to remove whitespaces because it uses the Unicode standard.

推荐使用strip()方法删除空格,因为它使用Unicode标准。

Str
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值