行业知识
Java 期末复习 习题集
2026-12-04 16:33:34
来源:互联网转载
文章目录
- 血泪教训
- ❌ 代码补全题
- ❌ 阅读程序写结果
- 阅读程序
- ✅ 数据类型
- ✅ equals
- ✅ 类
- ✅ 静态变量和成员变量
- ✅ 多态
- ✅ 继承
- ✅ 数组
- ✅ 字符串
- ✅ HashTable
- ✅ 文件读写
- ✅ IO流
- 程序补全
- ✅ 对象操作
- ✅ 文件读写
- ✅ 内部类 ⭐
- ✅ 匿名内部类 ⭐
- 博士练习题
- 单选题
- 填空题(期末不考此题型)
- 判断题
- 程序阅读题
- 1. 读代码写结果
- 2. 读代码写结果
- 3. 读代码写结果
- 4. 读代码写结果
- 5. 读代码写结果
- 试卷一
- 一、选择题
- 二、判断题
- 试卷二
- 一、选择题
- 二、判断题
- 试卷三
- 一、选择题
- 二、判断题
- 试卷四
- 一、选择题
- 二、判断题
- 试卷五
- 一、选择题
- 不进
- 二、面向对象编程
- 面向对象特征
- 静态变量和成员变量
- 访问权限修饰符
- 三、继承
- 子类继承父类方法重写注意事项
- final 关键字
- 四、多态
- 多态成员访问特点
- 重写和重载
- 抽象类的特点
- 抽象类的成员特点
- 五、接口和内部类
- 接口特点
- 接口成员特点
- 抽象类和接口的区别
- 内部类
- 匿名内部类
- 六、设计模式
- 单例设计模式 ⭐
- ✨ 枚举实现
- ✨ 饿汉式单例
- ✨ 懒汉式单例
- 简单工厂模式
- 工厂方法模式
- 抽象工厂模式
- 适配器模式
- 七、GUI编程
- 布局管理器
- 流布局管理器
- 事件响应的三大要素
- 事件响应的步骤
- AWT概述
- Swing概述
- 文本控件
- 八、集合
- 泛型通配符
- Map 接口概述
- 九、异常处理
- 异常体系
- 编译型异常 和 运行时异常
- throws 和 throw
- finally
- 十、文件与IO流
- IO 流基本类
- 十一、多线程
- 线程的生命周期
- 线程状态转换图
- 十二、网络编程
- 网络模型
- 网络通信三要素
- UDP 和 TCP
- Socket 套接字
- UDP 传输
- TCP 传输
- 十三、类加载器与反射
- 类加载流程
- 类加载器
- 反射
- 反射获取构造方法
- 反射获取成员变量
- 反射获取成员方法
- 动态代理
- 十四、数据库编程
- SQL 语句
- JDBC
- 获取数据库连接
- PrepareStatement
血泪教训
真题如下
❌ 代码补全题
几乎都是实验原题
- 内部类
- 模拟用户登录
- 三个售票窗口抢100张票
- LinkedList模拟栈
❌ 阅读程序写结果
- 父子类调用输出顺序(简单)
- 字符串
public class Test{public static void main(String[] args){String a = "helloworld";String b = "hello";String c = "world";System.out.println(a == b + c);System.out.println(a.equals(b + c));System.out.println(a == "hello" + "world");System.out.println(a.equals("hello" + "world"));System.out.println(a == "helloworld");System.out.println(a.equals("helloworld"));}}阅读程序
✅ 数据类型
public class Test{public static void main(String[] args){int test = 0;System.out.println(test);// 0System.out.println('a');// aSystem.out.println('a' + 1);// 98System.out.println("hello" + 'a' + 1);// helloa1System.out.println('a' + 1 + "hello");// 98helloSystem.out.println("5+5=" + 5 + 5);// 5+5=55System.out.println(5 + 5 + "=5+5");// 10=5+5final double PI = 3.14;byte b = 024;// 0 开头表示八进制,2*8 + 4 = 20short c = 0x17af;// 0x 开头表示十六进制int d = 10000;long e = -145L;float f = -8.29F;double g = 1.6E-4;char h = 'h';boolean i = false;System.out.println("b=" + b);// b=20System.out.println("c=" + c);// c=6063System.out.println("d=" + d);// d=10000System.out.println("e=" + e);// e=-145System.out.println("f=" + f);// f=-8.29System.out.println("g=" + g);// g=1.6E-4System.out.println("h=" + h);// h=hSystem.out.println("i=" + i);// i=false}}byte b = (byte) 130;// byte 的范围为 -128 ~ 127System.out.println(b);// -126
✅ equals
阅读下面的程序代码,并回答问题
String s1 = new String("abcde");String s2 = new String("abcde"); boolean b1= s1.equals(s2);boolean b2 = s1== s2; System.out.print(b1+" "+b2);- 程序段执行后,在命令行的输出结果如何?
true false
- 解释输出问题1 的结果的原因?
- equals方法比较两个字符串的内容是否相等
(String 重写了equals() 和 hashCode()方法,正常情况 equals() 和 “==” 一个效果)
- 运算符“==”判断两个对象是否指向同一个引用,即是否为同一个对象。
- equals方法比较两个字符串的内容是否相等
✅ 类
写出下面的程序编译运行后的结果
public class Test{public static void main(String args[]){new Student("Tom", 'm', 90, 88);new Student("Jack", 'm', 66, 89);new Student("Mary", 'f', 76, 86);System.out.println("name\tsex\tchinese\tenglish");Student.print();}}class Student{protected String name;protected char sex;protected int chinese;protected int english;protected Student next;static Student list;Student(String name, char sex, int chinese, int english){this.name = name;this.sex = sex;this.chinese = chinese;this.english = english;this.next = list;list = this;}static void print(){Student friend = list;if (friend == null)System.out.println("The list is empty.");else{do{System.out.println(friend.toString());friend = friend.next;} while (friend != null);}}public String toString(){return new String(name + "\t" + sex + "\t" + chinese + "\t" + english);}}参考答案
✅ 静态变量和成员变量
class Person{// 姓名String name;// 年龄int age;// 国籍(注意国籍是静态的)// String country;static String country;public Person(){}public Person(String name, int age){this.name = name;this.age = age;}public Person(String name, int age, String country){this.name = name;this.age = age;this.country = country;}public void show(){System.out.println("姓名:" + name + ",年龄:" + age + ",国籍:" + country);}}public class PersonDemo{public static void main(String[] args){// 创建对象1Person p1 = new Person("邓丽君", 16, "中国");p1.show();// 创建对象2Person p2 = new Person("杨幂", 22);p2.show();// 创建对象3Person p3 = new Person("凤姐", 20);p3.show();p3.country = "美国";p3.show();p1.show();p2.show();}}运行结果
姓名:邓丽君,年龄:16,国籍:中国姓名:杨幂,年龄:22,国籍:中国姓名:凤姐,年龄:20,国籍:中国姓名:凤姐,年龄:20,国籍:美国姓名:邓丽君,年龄:16,国籍:美国姓名:杨幂,年龄:22,国籍:美国
✅ 多态
读程序,写出正确的运行结果。
// AbstractClassDemo.java源代码如下:abstract class Shape { //定义抽象类Shape和抽象方法display abstract void display();}class Circle extends Shape { void display() { //实现抽象类的方法 System.out.println("Circle"); }}class Rectangle extends Shape { void display() { //实现抽象类的方法 System.out.println("Rectangle"); }}class Triangle extends Shape { void display() { //实现抽象类的方法 System.out.println("Triangle"); }}public class AbstractClassDemo { public static void main(String args[]) { (new Circle()).display(); //定义无名对象来调用对应的display方法 (new Rectangle()).display(); (new Triangle()).display(); }}参考答案
Circle;Rectangle; Triangle。
读程序,写出正确的运行结果。
public class Test{public static void main(String[] args){A a = new B();a.show();B b = new C();b.show();}}class A{public void show(){show2();}public void show2(){System.out.println("我");}}class B extends A{/*public void show() {show2();}*/public void show2(){System.out.println("爱");}}class C extends B{public void show(){super.show();}public void show2(){System.out.println("你");}}参考答案
爱你
✅ 继承
读程序,写出正确的运行结果。
public class Father { int a = 100; public void miner() { a--;// 只对当前类的成员变量 a 生效,并不影响子类的 a } public static void main(String[] args) { Father x = new Father(); Son y = new Son(); System.out.println(y.a); System.out.println(y.getA()); y.miner();//调用的是父类的方法 System.out.println(y.a); System.out.println(y.getA()); }}class Son extends Father { int a = 0; public void plus() { a++; } public int getA() { return super.a; }}参考答案
0100099
✅ 数组
应用程序的main方法中有以下语句,则输出的结果是什么?
int b[][]={{1, 1, 1}, {2,2}, {3}};int sum=0;for(int i=0; i for(int j=0; j sum+=b[i][j]; } }System.out.println("sum="+sum); public static void main(String args[]) { try { String s = "ABCDE"; byte b[] = s.getBytes(); FileOutputStream file = new FileOutputStream("test.txt", true); file.write(b); file.close(); } catch (IOException e) { System.out.println(e.toString()); } }} public static void main(String[] argv) { try { BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); String inputLine; While((inputLine = is.readLine()) != null) { System.out.println(inputLine); } is.close(); } catch (IOException e) { System.out.println("IOException: " + e); } }} private int i = 0; //此属性值用于检验 Leaf increment() { //定义方法increment(),返回值是Leaf类的对象 i++; return (1); //将当前对象的地址作为返回值返回 } void print() { System.out.println(" i = " + i); } public static void main(String args[]) { Leaf x = (2); //创建Leaf类的对象x x.increment().increment().increment().print(); } //多次调用方法increment(),返回的都是x的地址,i 值表示调用次数} public static void main(String args[]) { try { File inFile = new File("file1.txt"); //指定源文件 File outFile = new File("file2.txt"); //指定目标文件 FileInputStream fis = (1); FileOutputStream fos = new FileOutputStream(outFile);// 类名可参考此代码 int c; //逐字节从源文件中输入,再输出到fos流 while((c = fis.read()) != -1)( 2); fis.close(); fos.close(); } catch(Exception e) { System.out.println("FileStreamsTest: " + e); } }}public int num = 10;class Inner {public int num = 20;public void show() {int num = 30;System.out.println(num);// 局部变量System.out.println(this.num);// 内部类的成员变量System.out.println(Outer.this.num);// 外部类的成员变量}}}class OuterInnerDemo{public static void main(String[] args){Outer.Inner oi = new Outer().new Inner();oi.show();}}void show();}static class Outer{ // 补齐代码public static Inner method(){return new Inner(){public void show(){System.out.println("HelloWorld");}};}}public static void main(String[] args){Outer.method().show();}int m = 5;void zengA(int x){m = m + x;}int jianA(int y){return m - y;}}class B extends A{int m = 3;int jianA(int z){return super.jianA(z) + m;}}public class Test{public static void main(String[] args){A a1 = new A();A a2 = new A();B b = new B();A t = (A) b;A a3, a4;a3 = b;a4 = a1;a1.zengA(5);a3.zengA(6);b.m = 4;System.out.println(a1.m); // 【代码 1】System.out.println(a2.m); // 【代码 2】System.out.println(a3.m); // 【代码 3】System.out.println(a4.m); // 【代码 4】System.out.println(a3.jianA(1)); // 【代码 5】11-1+4 = 14}}public abstract double f(double x, double y);}class B extends A{double m;public double f(double a, double b){return a * b + m;}}public class E{public static void main(String args[]){B b = new B();A a = b;b.m = 2.0;System.out.println(a.f(2.0, 3.0)); // 【代码 1】 2 * 3 + 2 = 8.0}}static int m = 5;void zengA(int x){m = m + x;}}public class E{public static void main(String args[]){A a1 = new A();A a2 = new A();a2.zengA(5);System.out.println(a1.m); // 【代码 1】System.out.println(a2.m); // 【代码 2】}}int x = 100;double y = 200.3;A(int x){x = x;}A(double y){this.y = y;}double B(){return x + y;}}public class Test{public static void main(String[] args){A a1 = new A(-80);// 构造方法:细节 x=x,相当于啥也没干System.out.println(a1.B()); // 【代码 1】100+200.3 = 300.3A a2 = new A(60.5);System.out.println(a2.B()); // 【代码 2】}}public double f(double x, double y);}class B implements A{double m;public double f(double a, double b){return a * b + m;}}public class E{public static void main(String args[]){B b = new B();A a = b;b.m = 2.0;System.out.println(a.f(2.0, 3.0));// 2 * 3 + 2 = 8}}String name;String id;void display(){System.out.println(“Name:”+name+” ID :”+id);}public static void main(String args[]){Student student=new student;student.name="Alisha";student.id=”C001”;student.display();}} 2) public static void main(String args[]){ 3) float f=0.0; 4) f+=1.0; 5) } 6)} 2) int m,n; 3) public Test(){} 4) public Test(int a){m=a;} 5) public static void main(String args[]){ 6) Test t1,t2; 7) int j,k; 8) j=0;k=0; 9) t1=new Test();10) t2=new Test(j,k); 11) } 12) } String x = "1"; int y; public static void main(String args[]) { int z = 2; // 非 staic 的 x,y 不能在static的main方法里直接使用 System.out.println(x + y + z); }}125,21,5,168,98};int min = x[0];for (int i=1; iif(x[i] - 程序段执行后,在命令行的输出结果如何?