Java程序设计英语考试题目及答案(计算机16-1数字媒体)

    xiaoxiao2025-04-14  108

    文章目录

    Q1. Write a program to print out all odd numbers in [0, 100].Q2.Write a Java program that takes a year of integer and print whether that year is a leap year or not.Q3.Write a method to calculate n!.Q4.Write a Java program to count the number of 1 in an integer.Q5.Write a Java program to rotate an integer, e.g. 1254 -> 4521.Q6.Write a method to check whether a number is prime.Q7.Write a Java program to reverse an array of integer, e.g. [1,3,8,6] -> [6,8,3,1].Q8.Write a program to find the elements that appears only once in an array of integers.Q9.Write a program to get the last index of a character within a string.Q10.Write a program to replace all the 'd' characters with 'f' characters of a string.Q11.Write a program to convert all the characters in a string to lowercase.Q12.Write a program to convert all the characters in a string to upppercase.Q13.Write a Java method to display the middle character of a string. Note: If the length of the string is even there will be two middle characters.Q14.Write a Java program to find the length of the longest consecutive elements sequence from a given unsorted array of integers. Example: Given [49, 1, 3, 200, 2, 4, 70, 5], the longest consecutive elements sequence is [1, 2, 3, 4, 5]. Therefore the program will return its length 5.Q15.(25 points) Write a Java program to calculate the median (中间值) of an given unsorted array of integers.Q16.Write a method to check whether a given 3-digit number is Armstrong number or not. An Armstrong number of 3-digit is a number for which the sum of cube of its digits is equal to number. For example, 371 is an Armstrong number because 3*3*3 + 7*7*7 + 1*1*1 = 371.Q17.Design a java class Circle for represent a circle on a two-dimensional plane with X and Y coordinates.The class Circle provides the following methods:Q18.Write a method to collect the common elements in two arrays of integers.Q19.Given two sorted integer arrays in ascending order. Write a method to merge two arrays of integers also in ascending order.Q20.Write a Java method to print the number of vowels (元音) and the number of spaces in a string.

    Q1. Write a program to print out all odd numbers in [0, 100].

    Signature: void showOddNumbers() Solution:

    void showOddNumbers(){ for(int i = 0; i <= 100; i++) if(i % 2 == 1) System.out.println(i); }

    Q2.Write a Java program that takes a year of integer and print whether that year is a leap year or not.

    Boolean isLeapYear(int year) Solution

    Boolean isLeapYear(int year){ return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }

    Q3.Write a method to calculate n!.

    Signature: int factorial(int n) Solution

    int factorial(int n){ return n > 1 ? n * factorial(n - 1) : 1; }

    Q4.Write a Java program to count the number of 1 in an integer.

    Signature: int countOnes(int n) Solution:

    int countOnes(int n){ int count = 0; while(n > 0){ if(n % 10 == 1) count ++; n /= 10; } return count; }

    Q5.Write a Java program to rotate an integer, e.g. 1254 -> 4521.

    Signature: int roteateInteger(int n)

    int countOnes(int n){ int output = 0; while(n > 0){ output = output * 10 + n % 10; n /= 10; } return output ; }

    Q6.Write a method to check whether a number is prime.

    Signature: Boolean isPrime(int n)

    Boolean isPrime(int n){ for(int i = 2; i < n - 1; i++) if( n % i == 0) return false; return true; }

    Q7.Write a Java program to reverse an array of integer, e.g. [1,3,8,6] -> [6,8,3,1].

    Signature: void reverseArray(int[] arr)

    void reverseArray(int[] arr){ for(int i = 0; i < arr.length / 2; i++){ int tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } }

    Q8.Write a program to find the elements that appears only once in an array of integers.

    Signature: int[] find(int[] arr)

    int[] find(int[] arr){ // 统计所有只出现1次的值 int total = 0; int[] marks = new int[arr.length]; for(int i = 0; i < arr.length; i++) { int occ = 0; for(int j = 0; j < arr.length; j++) if(arr[j] == arr[i]) occ++; if(occ == 1) { total++; marks[i] = 1; } } // 整理输出数组 int[] outarr = new int[total]; int p = 0; for (int i = 0; i < arr.length; i++) if (marks[i] == 1) outarr[p++] = arr[i]; return outarr; }

    Q9.Write a program to get the last index of a character within a string.

    Signature: int locate(String s, char c)

    int locate(String s, char c){ for(int i = s.length() - 1; i >=0; i--) if(s.charAt(i) == c) return i; return -1; }

    Q10.Write a program to replace all the ‘d’ characters with ‘f’ characters of a string.

    Signature: String replace(String s)

    String replace(String s){ char[] chs = new char[s.length()]; for(int i = 0; i < chs.length; i++) chs[i] = s.charAt(i) == 'd' ? 'f' : s.charAt(i); return new String(chs); }

    Q11.Write a program to convert all the characters in a string to lowercase.

    Signature: String toLower(String s)

    String toLower(String s){ char[] chs = new char[s.length()]; for(int i = 0; i < chs.length; i++){ char c = s.charAt(i); chs[i] = c >= 65 && c <= 97 ? (char) (c + 32) : c; } return new String(chs); }

    Q12.Write a program to convert all the characters in a string to upppercase.

    Signature: String toUpper(String s)

    String toUpper(String s){ char[] chs = new char[s.length()]; for(int i = 0; i < chs.length; i++){ char c = s.charAt(i); chs[i] = c >= 97 && c <= 122 ? (char) (c - 32) : c; } return new String(chs); }

    Q13.Write a Java method to display the middle character of a string. Note: If the length of the string is even there will be two middle characters.

    Signature: char[] getMiddleCharacters(String s)

    char[] getMiddleCharacters(String s){ char[] chs = new char[s.length() % 2 == 1 ? 1 : 2]; if(chs.length==1){ chs[0] = s.charAt(s.length() / 2); }else{ chs[0] = s.charAt(s.length() / 2); chs[1] = s.charAt(s.length() / 2 + 1); } return chs; }

    Q14.Write a Java program to find the length of the longest consecutive elements sequence from a given unsorted array of integers. Example: Given [49, 1, 3, 200, 2, 4, 70, 5], the longest consecutive elements sequence is [1, 2, 3, 4, 5]. Therefore the program will return its length 5.

    Signature: int getLongestConsecutives(int[] arr)

    int getLongestConsecutives(int[] arr){ Arrays.sort(arr); int max = 0; for(int i = 0; i < arr.length; i++) max = arr[i+1] == arr[i] + 1 ? max + 1 : 1; return max; }

    Q15.(25 points) Write a Java program to calculate the median (中间值) of an given unsorted array of integers.

    Signature: int getMedian(int[] arr)

    int getMedian(int[] arr){ int temp = 0; int size = numbers.length; for(int i = 0 ; i < size - 1; i ++){ for(int j = 0 ;j < size - i - 1 ; j++){ if(numbers[j] > numbers[j+1]) { temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } } return arr[arr.length / 2]; }

    Q16.Write a method to check whether a given 3-digit number is Armstrong number or not. An Armstrong number of 3-digit is a number for which the sum of cube of its digits is equal to number. For example, 371 is an Armstrong number because 333 + 777 + 111 = 371.

    Signature: Boolean isArmstrongNumber(int n)

    Boolean isArmstrongNumber(int n){ int h = n / 100; int t = n / 10 % 10; int d = n % 10; return h * h * h + t * t * t + d * d * d == n; }

    Q17.Design a java class Circle for represent a circle on a two-dimensional plane with X and Y coordinates.The class Circle provides the following methods:

    double getLength(), returns the length of the circle;double getArea(), returns the area of the circle;boolean isContained(Cirlce c), returns a boolean indicating whether Circle c is contained in the circle;boolean isCollidedWith(Cirlce c), returns a boolean indicating whether Circle c is collided with the circle.

    class Circle{ double x, y, r; public double getLength(){ return 2 * Math.PI * r;} public double getArea(){ return Math.PI * r * r}; public Boolean isContained(Circle c){ return Math.sqrt((c.x - x)*(c.x - x)+(c.x - x)*(c.x - x)) < r; } public Boolean isCollidedWith(Circle c){ double d = Math.sqrt((c.x - x)*(c.x - x)+(c.x - x)*(c.x - x)); return d < r + c.r && d > Math.Max(r, c.r); } }

    Q18.Write a method to collect the common elements in two arrays of integers.

    Signature: int[] getCommonElements(int[] arr1, int[] arr2)

    int[] getCommonElements(int[] arr1, int[] arr2){ Arrays.sort(arr1); Arrays.sort(arr2); List<Integer> list = new ArrayList<Integer>(); int i = 0, j = 0; while(i < arr1.length && j < arr2.length) { if(arr1[i] > arr2[j]) j++; else if(arr1[i] < arr2[j]) i++; else{ list.add(arr1[i]); i++; j++; } } return list.toArray(); }

    Q19.Given two sorted integer arrays in ascending order. Write a method to merge two arrays of integers also in ascending order.

    Signature: int[] merge(int[] arr1, int[] arr2)

    int[] merge(int[] arr1, int[] arr2){ int[] c = new int[arr1.length + arr2.length]; int i = 0, j = 0, k = 0; while(i < arr1.length && j < arr2.length) if(arr1[i] < b[j]) c[k++] = arr1[i++]; else c[k++] = arr2[j++]; while(i < arr1.length) c[k++] = arr1[i++]; while(j<b.length) c[k++] = arr2[j++]; return c; }

    Q20.Write a Java method to print the number of vowels (元音) and the number of spaces in a string.

    Signature: void printNumbes(String s)

    void printNumbes(String s){ char[] chs = {'a', 'A', 'e', 'E', 'o', 'O', 'i', 'I', 'u', 'U'}; int num_vowels = 0; int num_spaces = 0; for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); boolean existed = false; for(int j = 0; j < chs.length(); j ++){ if(c == chs[j]){ existed = true; break; } } if(!existed) num_vowels++; else if(c == ' ') num_spaces++; } System.out.printf("Number of vowels: %d\nNumber of spaces: %d\n", num_vowels, num_spaces); }
    最新回复(0)