Home >Java >JavaInterview questions >A complete collection of Java written test handwritten algorithm interview questions with answers
#1. Count the number of words in an English article.
public class WordCounting { public static void main(String[] args) { try(FileReader fr = new FileReader("a.txt")) { int counter = 0; boolean state = false; int currentChar; while((currentChar= fr.read()) != -1) { if(currentChar== ' ' || currentChar == '\n' || currentChar == '\t' || currentChar == '\r') { state = false; } else if(!state) { state = true; counter++; } } System.out.println(counter); } catch(Exception e) { e.printStackTrace(); } } }
Supplement: This program may be written in many ways. The code chosen here is the code given by teachers Dennis M. Ritchie and Brian W. Kernighan in their immortal book "The C Programming Language" , pay tribute to the two teachers. The same goes for the code below.
2. Enter the year, month and day, and calculate the day of the year that the date is.
public class DayCounting { public static void main(String[] args) { int[][] data = { {31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; Scanner sc = new Scanner(System.in); System.out.print("请输入年月日(1980 11 28): "); int year = sc.nextInt(); int month = sc.nextInt(); int date = sc.nextInt(); int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0]; int sum = 0; for(int i = 0; i < month -1; i++) { sum += daysOfMonth[i]; } sum += date; System.out.println(sum); sc.close(); } }
3. Palindrome prime numbers: The so-called palindrome numbers are the same numbers read forward and backward (for example: 11, 121, 1991...), and palindrome prime numbers are both Palindrome numbers are prime numbers (numbers that are only divisible by 1 and itself). Program to find the palindrome prime numbers between 11 and 9999.
public class PalindromicPrimeNumber { public static void main(String[] args) { for(int i = 11; i <= 9999; i++) { if(isPrime(i) && isPalindromic(i)) { System.out.println(i); } } } public static boolean isPrime(int n) { for(int i = 2; i <= Math.sqrt(n); i++) { if(n % i == 0) { return false; } } return true; } public static boolean isPalindromic(int n) { int temp = n; int sum = 0; while(temp > 0) { sum= sum * 10 + temp % 10; temp/= 10; } return sum == n; } }
4. Full permutations: Give all permutations of the five numbers 12345.
public class FullPermutation { public static void perm(int[] list) { perm(list,0); } private static void perm(int[] list, int k) { if (k == list.length) { for (int i = 0; i < list.length; i++) { System.out.print(list[i]); } System.out.println(); }else{ for (int i = k; i < list.length; i++) { swap(list, k, i); perm(list, k + 1); swap(list, k, i); } } } private static void swap(int[] list, int pos1, int pos2) { int temp = list[pos1]; list[pos1] = list[pos2]; list[pos2] = temp; } public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; perm(x); } }
5. For a one-dimensional array with N integer elements, find the sum of its subarrays (arrays composed of elements with consecutive subscripts in the array) the maximum value.
A few examples are given below (the largest subarray is in bold):
Array: { 1, -2, 3, 5, -3, 2 }, the result is: 8
2) Array: { 0, -2, 3, 5, -1, 2 }, the result is: 9
3) Array: { -9, -2,-3, -5, -3 }, the result is: -2
can be solved using dynamic programming:
public class MaxSum { private static int max(int x, int y) { return x > y? x: y; } public static int maxSum(int[] array) { int n = array.length; int[] start = new int[n]; int[] all = new int[n]; all[n - 1] = start[n - 1] = array[n - 1]; for(int i = n - 2; i >= 0;i--) { start[i] = max(array[i], array[i] + start[i + 1]); all[i] = max(start[i], all[i + 1]); } return all[0]; } public static void main(String[] args) { int[] x1 = { 1, -2, 3, 5,-3, 2 }; int[] x2 = { 0, -2, 3, 5,-1, 2 }; int[] x3 = { -9, -2, -3,-5, -3 }; System.out.println(maxSum(x1)); // 8 System.out.println(maxSum(x2)); // 9 System.out.println(maxSum(x3)); //-2 } }
6. Implement string reversal using recursion
public class StringReverse { public static String reverse(String originStr) { if(originStr == null || originStr.length()== 1) { return originStr; } return reverse(originStr.substring(1))+ originStr.charAt(0); } public static void main(String[] args) { System.out.println(reverse("hello")); } }
7. Enter a positive integer and decompose it into the product of prime numbers.
public class DecomposeInteger { private static List<Integer> list = new ArrayList<Integer>(); public static void main(String[] args) { System.out.print("请输入一个数: "); Scanner sc = new Scanner(System.in); int n = sc.nextInt(); decomposeNumber(n); System.out.print(n + " = "); for(int i = 0; i < list.size() - 1; i++) { System.out.print(list.get(i) + " * "); } System.out.println(list.get(list.size() - 1)); } public static void decomposeNumber(int n) { if(isPrime(n)) { list.add(n); list.add(1); } else { doIt(n, (int)Math.sqrt(n)); } } public static void doIt(int n, int div) { if(isPrime(div) && n % div == 0) { list.add(div); decomposeNumber(n / div); } else { doIt(n, div - 1); } } public static boolean isPrime(int n) { for(int i = 2; i <= Math.sqrt(n);i++) { if(n % i == 0) { return false; } } return true; } }
8. There are n steps. You can walk 1, 2 or 3 steps at a time. How many ways can you walk after n steps?
public class GoSteps { public static int countWays(int n) { if(n < 0) { return 0; } else if(n == 0) { return 1; } else { return countWays(n - 1) + countWays(n - 2) + countWays(n -3); } } public static void main(String[] args) { System.out.println(countWays(5)); // 13 } }
9. Write an algorithm to determine whether all letters of an English word are all different (not case sensitive)
public class AllNotTheSame { public static boolean judge(String str) { String temp = str.toLowerCase(); int[] letterCounter = new int[26]; for(int i = 0; i <temp.length(); i++) { int index = temp.charAt(i)- 'a'; letterCounter[index]++; if(letterCounter[index] > 1) { return false; } } return true; } public static void main(String[] args) { System.out.println(judge("hello")); System.out.print(judge("smile")); } }
10. There is a sorted integer array with duplicate elements. Please delete the duplicate elements. For example, A= [1, 1, 2, 2, 3]. The processed array should be A = [1, 2, 3].
public class RemoveDuplication { public static int[] removeDuplicates(int a[]) { if(a.length <= 1) { return a; } int index = 0; for(int i = 1; i < a.length; i++) { if(a[index] != a[i]) { a[++index] = a[i]; } } int[] b = new int[index + 1]; System.arraycopy(a, 0, b, 0, b.length); return b; } public static void main(String[] args) { int[] a = {1, 1, 2, 2, 3}; a = removeDuplicates(a); System.out.println(Arrays.toString(a)); } }
11. Given an array, in which there is a duplicate element accounting for more than half, find this element.
public class FindMost { public static <T> T find(T[] x){ T temp = null; for(int i = 0, nTimes = 0; i< x.length;i++) { if(nTimes == 0) { temp= x[i]; nTimes= 1; } else { if(x[i].equals(temp)) { nTimes++; } else { nTimes--; } } } return temp; } public static void main(String[] args) { String[]strs = {"hello","kiss","hello","hello","maybe"}; System.out.println(find(strs)); } }
12. Write a method to find the byte length of a string?
public int getWordCount(String s){ int length = 0; for(int i = 0; i < s.length(); i++) { int ascii = Character.codePointAt(s, i); if(ascii >= 0 && ascii <=255) length++; else length += 2; } return length; }
The above is the detailed content of A complete collection of Java written test handwritten algorithm interview questions with answers. For more information, please follow other related articles on the PHP Chinese website!