一、选择题
编译Java源程序文件将产生相应的字节码文件,这些字节码文件的扩展句为( )。
参考答案:
D
点击评论后加载...
登录 后可参与评论
设 x = 1 , y = 2 , z = 3,则表达式 y+=z--/++x 的值是( )。
参考答案:
B (解析: ++x=2, z--先取3, 3/2=1, y=2+1=3)
点击评论后加载...
登录 后可参与评论
为AB类的一个无形式参数无返回值的方法method书写方法头,使得使用类名AB作为前缀就可以调用它,该方法头的形式为( )。
参考答案:
A (解析: 类名直接调用需static)
点击评论后加载...
登录 后可参与评论
以下代码片段的输出是什么?
int i = 10;
System.out.println(i++);
System.out.println(++i);
参考答案:
B
点击评论后加载...
登录 后可参与评论
下面程序段执行后b的值是?
Integer integ = new Integer(9);
boolean b = integ instanceof Object;
参考答案:
B
点击评论后加载...
登录 后可参与评论
以下代码体现了面向对象的什么特性?
Animal myAnimal = new Dog();
myAnimal.sound();\\输出“woof!”
参考答案:
C
点击评论后加载...
登录 后可参与评论
以下代码片段执行后count的值为( )。
class Counter {
static int count = 0;
Counter() { count++; }
}
public class Main{
public static void main(String[] args){
new Counter();
new Counter();
new Counter();
}
参考答案:
C
点击评论后加载...
登录 后可参与评论
以下代码的输出结果是?( )
class Test{
static int x = 10; int y = 20;
}
public class Main{
public static void main(String[]args){
Test t1 = new Test();
Test t2 = new Test();
t1.x=100;
t1.y=200;
System.out.println(t2.x + “,” + t2.y);
}}
参考答案:
B
点击评论后加载...
登录 后可参与评论
二、填空题
想要在Java图形界面实现按钮点击触发时间,需要为______组件添加监听器,添加监听器的方法为______。
参考答案:
JButton 和 addActionListener
点击评论后加载...
登录 后可参与评论
三、判断题
四、程序题
程序中声明了学生和大学生两个类,大学生类继承了学生类并多出了“生活费”属性,请将代码补充完整,
public class Student{
private String name;
public Student(String name) {
this.name = name;
}
}
public class Undergraduate ______ ______ {
private int livingExpensive;
public Undergraduate(String name, int livingExpensive) {
______(______);
this.livingExpensive = livingExpensive;
}
}
参考答案:
extends Student, super(name)
点击评论后加载...
登录 后可参与评论
请将横线处代码补充完整。
public class JdbcTest{
public static void main(String[] args) {
String url = "jdbc:mysql://127.0.0.1:3306/myDatabase";
String username = "root";
String password = "123456";
Connection conn;
try {
conn = ______ . ______(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.______("select * from user");
while(rs.next()) {
System.out.println(rs.getString(1) + "\t" + rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
} catch(Exception e) {
e.printStackTrace();
}
}
}
参考答案:
DriverManager, getConnection, executeQuery
点击评论后加载...
登录 后可参与评论
请将横线处代码补充完整,使售票功能可以多线程运行(需要在售票环节添加同步)。
class Ticket implements ______ {
private int tickCount = 100;
@Override
public void run() {
______(this) {
while(true) {
if(tickCount > 0) {
try {
Thread.______(10); //线程休眠10毫秒
} catch(Exception e){
e.printStackTrace();
}
tickCount--;
} else { break; }
}
}
}
}
参考答案:
Runnable, synchronized, sleep
点击评论后加载...
登录 后可参与评论
请将下列代码补充完整,使之能够在构造方法中完成参数初始化,并将圆形面积的计算结果输出:
public class Circle {
private static final double PI = 3.14;
private double radius;
public Circle(double radius) {
______.radius = radius;
}
public double getArea() {
return ______;
}
public static void main(String[] args) {
Circle circle = new Circle(2.0);
double area = circle. getArea() ;
System.out.println("圆形的面积是" + area);
}
}
参考答案:
this, PI * radius * radius
点击评论后加载...
登录 后可参与评论
请将以下代码补充完整,使程序可以建立数据库连接并查询数据。
public static void main(String[] args){
String url = "jdbc:mysql://127.0.0.1:3306/database1";
String username = "test1";
String password = "123456";
try {
Class.______("com.mysql.cj.jdbc.Driver");
Connection conn = ______.getConnection(url, username, password);
Statement stmt = conn.createStatement();
______ rs = stmt.executeQuery("select * from employee");
while( ______) {
// 内容输出
}
} catch(Exception e) {
e.printStackTrace();
} finally {
rs.close();
stmt.close();
conn.close();
}
}
参考答案:
forName, DriverManager, ResultSet, rs.next()
点击评论后加载...
登录 后可参与评论
请将横线处代码补充完整,使代码能够输出100~1000之间的水仙花数。水仙花数是指其本身等于它的个位、十位和百位的立方和(原数=个位3+十位3+百位3)。
public static void main(String[] args) {
for (int i = 100; i < 1000; i++) {
int ge = i % 10;
int shi = ______;
int bai = i / 100;
if (i == ( ______)) {
System.out.println("水仙花数:" + ______ );
}
}
}
参考答案:
i / 10 % 10, ge*ge*ge + shi*shi*shi + bai*bai*bai, i
点击评论后加载...
登录 后可参与评论
五、编程题
模拟一个火车票售票系统。假设有10张票,由三个窗口(三个线程)同时出售。
要求:
1. 使用实现Runnable接口的方式创建线程。
2. 保证售票过程的线程安全(使用synchronized关键字),避免出现超卖(同一张票被多个窗口卖出)的问题。
3. 输出每个窗口售出的票号。
参考答案:
class TicketWindow implements Runnable {
private int tickets = 10;
@Override
public void run() {
while (true) {
synchronized (this) {
if (tickets > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " 售出票号: " + tickets--);
} else {
break;
}
}
}
}
}
public class Main {
public static void main(String[] args) {
TicketWindow tw = new TicketWindow();
new Thread(tw, "窗口1").start();
new Thread(tw, "窗口2").start();
new Thread(tw, "窗口3").start();
}
}
点击评论后加载...
登录 后可参与评论
设计一个雇员(Employee)类和管理者(Adminer)类。
1. Employee包含name, salary属性和show()方法。
2. Adminer继承Employee,添加fee(补贴)属性,重写show()方法以显示补贴。
3. 编写测试类,声明两个对象分别指向Employee和Adminer,并输出信息。
参考答案:
class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void show() {
System.out.println("姓名:" + name + ", 工资:" + salary);
}
}
class Adminer extends Employee {
double fee;
public Adminer(String name, double salary, double fee) {
super(name, salary);
this.fee = fee;
}
@Override
public void show() {
super.show();
System.out.println("补贴:" + fee);
}
}
public class Test {
public static void main(String[] args) {
Employee emp = new Employee("张三", 5000);
Employee admin = new Adminer("李四", 8000, 2000);
emp.show();
admin.show();
}
}
点击评论后加载...
登录 后可参与评论
假设有一个数组a,其中的值为{12,6,35,4,10},请在程序中使用冒泡排序进行排序,并声明一个FileOutputStream类的对象,向名为myText.txt文件中写入排序的结果。
参考答案:
import java.io.FileOutputStream;
public class SortToFile {
public static void main(String[] args) {
int[] a = {12, 6, 35, 4, 10};
// 冒泡排序
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - 1 - i; j++) {
if (a[j] > a[j + 1]) {
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
// 写入文件
try (FileOutputStream fos = new FileOutputStream("myText.txt")) {
StringBuilder sb = new StringBuilder();
for (int num : a) {
sb.append(num).append(" ");
}
fos.write(sb.toString().getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
}
点击评论后加载...
登录 后可参与评论