Java程序设计基础课程历年试题
⼀、单项选择题(从下列各题四个备选答案中选出⼀个正确答案,并将其代号写在答题纸相应位置处。答案错选或未选者,该题不得分。每⼩题1分,共20分。)
1.________包含Java程序开发的预定义类和接⼝。
A) Java language specification B) Java JDK
C) Java API D) Java IDE
2.输⼊1 2 3,运⾏下⾯程序,会输出什么?
import java.util.Scanner;
public class Test1 {
public static void main(String[ ] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter three numbers: ");
double number1 = Double();
double number2 = Double();
double number3 = Double();
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display result
System.out.println(average);
}
}
A) 2.0 B) 4.0 C) 3.0 D) 1.0
3.________⽅法返回a的b次⽅。
A) Math.pow(a, b) B) ponent(a, b)
C) Math.pow(b, a) D) Math.power(a, b)
4.假设x=10,y=10,经过表达式(y > 10) && (x++ > 10)计算后x为________
A) 11 B) 10 C) 9 D)8
5.下⾯代码会输出多少遍"Welcome to Java"?
int count = 0;
while (count++ < 10) {
System.out.println("Welcome to Java");
}
A) 11 B) 10 C) 9 D)8
6.参数通常是在⽅法的_________中。
A)双引号B)单引号C)圆括号D)花括号
7.函数il(3.6)运⾏的结果是________。
A) 3 B) 3.0 C) 4.0 D) 5.0
8.分析下⾯代码,回答问题。
public class Test {
public static void main(String[ ] args) {
int[ ] a = new int[4];
a[1] = 1;
a = new int[2];
System.out.println("a[1] is " + a[1]);
}
}
A) 程序输出:a[1] is 1.
B) 程序发⽣⼀个运⾏错误,因为a[1]没有初始化
C) 程序输出:a[1] is 0..
D) 程序发⽣⼀个编译错误,因为new int[2]赋值给了a.
9.假设⽅法p()⽅法头为public static int[ ][ ] p(),下⾯哪个是p()⽅法的返回语句。
A) return new int[ ]{1, 2, 3};
java程序设计基础视频
B) return new int[ ][ ]{{1, 2, 3}, {2, 4, 5}};
C) return int[ ]{1, 2, 3};
D) return {1, 2, 3};
10.分析下⾯代码,回答问题。
public class Test {
public static void main(String[ ] args) {
boolean[ ][ ] x = new boolean[3][ ];
x[0] = new boolean[1]; x[1] = new boolean[2];
x[2] = new boolean[3];
System.out.println("x[2][2] is " + x[2][2]);
}
}
A) 程序会发⽣⼀个运⾏错误,因为x[2][2] is null.
B) 程序会正运⾏,并输出x[2][2] is false.
C) 程序会发⽣⼀个编译错误,因为new boolean[3][ ] is wrong.
D) 程序会正运⾏,并输出x[2][2] is true.
11.可以被⼀个类共享的实例是________。
A)实例变量B)私有变量
C)静态变量D) 公有变量
12.申明个变量Circle x = new Circle(),下⾯哪条语句描述的正确?
A) 可以把⼀个int型值赋给x。B)x包含了⼀个指向园对象的引⽤.
C) x包含了⼀个园的对象。D) x包含了⼀个int型值。
13.________返回StringBuilder类型变量strBuf的最后⾯字符。
A) StringBuilder.charAt(strBuf.length() - 1)
B) strBuf.charAt(strBuf.capacity() - 1)
C) strBuf.charAt(strBuf.length() - 1)
D) StringBuilder.charAt(strBuf.capacity() - 1
14.哪个个修饰符可以使类在同⼀个包中互相访问,⽽在不同的包中不能互相访问?
A) protected B) public
C) private D)默认修饰符.
15.多态表⽰____________。
A)⼀个类可以继承另外⼀个类。
B) ⼀个⽗类变量可以指向⼀个⼦类对象。
C)⼀个类可以包含另外⼀个类。
D)数据域可以申明为私有的。
16.下⾯哪⼀个类是重量级组件。
A) JFrame B) JButton C) JPanel D) JTextField
17.分析下⾯代码,回答问题。
class Test {
public static void main(String[ ] args) {
try {
String s = "5.6";
Integer.parseInt(s); // Cause a NumberFormatException
int i = 0;
int y = 2 / i;
}
catch (Exception ex) {
System.out.println("NumberFormatException");
}
catch (RuntimeException ex) {
System.out.println("RuntimeException");
}
}
}
A) 程序会发⽣个编译错误。
B) 程序会报NumberFormatException异常。
C) 程序会报RuntimeException 异常。
D) 程序会正常执⾏,不报任何异常。
18.分析下⾯代码,回答问题。
Number numberRef = new Integer(0);
Double doubleRef = (Double)numberRef;
A)因为int型可以转化为double类型,所以Intege实例可以转化为Double实例。
B)程序可以正常运⾏,引⽂Integer是Double的⼦类。
C)没有Integer这样的类,应该使⽤Int类。
D)程序会发⽣类型转化异常,因为numberRef不是Double类的实例。
19.使⽤Graphics 类型对象g画⼀条左上⾓到右下⾓的直线,应该是_______
A) g.drawLine(0, 0, getHeight(), getHeight()) B) g.drawLine(0, 0, getWidth(), getHeight())
C) g.drawLine(0, 0, 100, 100) D) g.drawLine(0, 0, getWidth(), getWidth())
20.假设A是Test类中的⼀个匿名内部类,A编译后的⽂件名为______
A) Test&1.class B) Test$A.class
C) A$Test.class D) Test$1.class
⼆、多项选择题(本⼤题共10⼩题,每题2分,共20分)
1._______是⾯向对象程序设计语⾔。
A) Ada B) C C) C++ D) Java
2.下⾯哪些是printf语句中的合法分类符________?
A) %6d B) %4c C) %10.2e D) %10b
3.下⾯哪些表达式是错误的________?
A) int[ ] a = new int[2];
B) int a = new int[2];
C) int[ ] a = new int(2);
D) int a() = new int[2];
4._________返回的是字符串。
A) String.valueOf(12.53) B) String.valueOf(false)
C) String.valueOf(new char[ ]{'a', 'b', 'c'}) D) String.valueOf(123)
5.分析下⾯代码回答问题。
class Test {
private double i;
public Test(double i) {
this.t();
this.i = i;
}
public Test() {
System.out.println("Default constructor");
this(1);
}
public void t() {
System.out.println("Invoking t");
}
}
A) this(1) ⼀定会被this(1.0) 替代
B) this.i 可能会被i 替代
C) this(1) 必须System.out.println("Default constructor")之间本调⽤
D) this.t()可能会被t()替代
6.分析下⾯代码回答问题。
public class Test {
public static void main(String[ ] args) {
Object a1 = new A();
Object a2 = new Object();
System.out.println(a1);
System.out.println(a2);
}
}
class A {
int x;
public String toString() {
return "A's x is " + x;
}
}
A) 当执⾏System.out.println(a1)时,类A中的toString()被调⽤。
B) 当执⾏System.out.println(a1)时,类Object中的toString()被调⽤。
C) 程序不能编译,因为System.out.println(a1)是错误的,应该写为System.out.String());
D) 当执⾏System.out.println(a2)时,类Object中的toString()被调⽤。