资源描述
2025年高职第二学年(邮政快递智能技术)JAVA编程设计阶段测试试题及答案
(考试时间:90分钟 满分100分)
班级______ 姓名______
第 I 卷(选择题 共30分)
每题给出的四个选项中,只有一项是符合题目要求的。(总共6题,每题5分,每题只有一个正确答案,请将正确答案填写在题后的括号内)
w1. 以下关于Java变量命名规则的描述,错误的是( )
A. 变量名可以包含字母、数字、下划线和美元符号
B. 变量名不能以数字开头
C. 变量名不能是Java中的关键字
D. 变量名可以和类名相同
w2. 已知有定义int a = 5; 那么执行语句a += a -= a a; 后,a的值为( )
A. 0
B. -40
C. -120
D. 120
w3. 以下哪个是Java中的合法注释( )
A. / This is a comment /
B. // This is a comment / and this is also part of it /
C. / This is a comment / // and this is another comment
D. / This is a comment / nested comment / /
w4. 若要将一个字符串转换为整数,应使用以下哪个类的方法( )
A. Integer
B. String
C. Math
D. Double
w5. 以下关于Java数组的说法,正确的是( )
A. 数组的长度可以在运行时动态改变
B. 数组元素的数据类型必须一致
C. 可以通过数组名直接访问数组中的元素
D. 数组可以存放不同类型的对象
w6. 以下代码段执行后,输出结果是( )
```java
int[] arr = {1, 2, 3, 4, 5};
for (int i = 0; i < arr.length; i++) {
if (i % 2 == 0) {
arr[i] = arr[i] 2;
}
}
for (int num : arr) {
System.out.print(num + " ");
}
```
A. 2 2 6 4 10
B. 1 4 3 8 5
C. 2 4 6 8 10
D. 1 2 3 4 5
第 II 卷(非选择题 共70分)
w7. (10分)简述Java中方法重载的概念,并举例说明。
w8. (15分)编写一个Java程序,实现计算两个整数的最大公约数。
w9. (15分)阅读以下代码,指出其中的错误并改正。
```java
class MyClass {
int x;
public void MyMethod() {
System.out.println("This is a method");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.x = 10;
obj.MyMethod();
System.out.println(obj.x);
}
}
```
w10. (20分)材料:快递包裹信息管理系统中,需要对包裹的重量、体积、收件人地址等信息进行处理。假设现有一个包裹类Parcel,包含重量weight(double类型)、体积volume(double类型)、收件人地址address(String类型)等属性,以及计算包裹运费的方法calculateShippingFee()。已知运费计算规则为:重量在1kg及以内,每kg运费10元;超过1kg的部分,每kg运费15元;体积超过10立方米,额外加收50元。
问题:请完成Parcel类的设计,包括属性定义和方法实现。
w11. (20分)材料:在快递业务中,经常需要对快递单号进行管理。快递单号是一个字符串,其格式为:12位数字 + 1位校验位。校验位的计算方法是:将前12位数字依次乘以1、2、3、...、12,然后将这些乘积相加,再对11取模,得到的结果用11减去,如果结果为10,则校验位为X。例如,快递单号123456789012X,前12位数字依次乘以1到12的结果相加为:1×1 + 2×2 + 3×3 + 4×4 + 5×5 + 6×6 + 7×7 + 8×8 + 9×9 + 10×10 + 11×11 + 12×12 = 650,650对11取模为9,11 - 9 = 2,而不是X,所以该快递单号格式错误。
问题:编写一个Java程序,判断给定的快递单号格式是否正确。
答案:
w1. D
w2. C
w3. A
w4. A
w5. B
w6. A
w7. 方法重载是指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可。参数列表不同包括参数的个数不同、参数的类型不同或者参数的顺序不同。例如:
```java
class MathUtils {
public int add(int a, int b) {
return a + b;
}
public double add(double a, double b) {
return a + b;
}
}
```
w8.
```java
public class GCDCalculator {
public static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void main(String[] args) {
int num1 = 48;
int num2 = 60;
int result = gcd(num1, num2);
System.out.println("The greatest common divisor of " + num1 + " and " + num2 + " is " + result);
}
}
```
w9. 错误1:MyClass类中的方法MyMethod()定义错误,方法名不能以大写字母开头。应改为public void myMethod()。
改正后的代码如下:
```java
class MyClass {
int x;
public void myMethod() {
System.out.println("This is a method");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.x = 10;
obj.myMethod();
System.out.println(obj.x);
}
}
```
w10.
```java
class Parcel {
double weight;
double volume;
String address;
public Parcel(double weight, double volume, String address) {
this.weight = weight;
this.volume = volume;
this.address = address;
}
public double calculateShippingFee() {
double baseFee = weight <= 1? weight 10 : 10 + (weight - 1) 15;
if (volume > 10) {
baseFee += 50;
}
return baseFee;
}
}
```
w11.
```java
public class ExpressNumberValidator {
public static boolean isValidExpressNumber(String number) {
if (number.length() != 13) {
return false;
}
int sum = 0;
for (int i = 0; i < 12; i++) {
sum += (number.charAt(i) - '0') (i + 1);
}
int mod = sum % 11;
int checkDigit = 11 - mod;
if (checkDigit == 10) {
checkDigit = 'X' - '0';
}
return number.charAt(12) - '0' == checkDigit;
}
public static void main(String[] args) {
String expressNumber = "123456789012X";
if (isValidExpressNumber(expressNumber)) {
System.out.println(expressNumber + " is a valid express number.");
} else {
System.out.println(expressNumber + " is an invalid express number.");
}
}
}
```
展开阅读全文