JAVA大学期末考试真题(带答案)
1.Which is the basic unit of a java program?(B )
- Java方法 B、Java类
- Java变量 D、以上都是
2.Let null be the value of a variable a,please select the possible type of (B )
- Integer B、String
- System D、都有可能
3.Which of the following class is correct?( D)
- public class Bean{}
- public class Circle{
double r;
r==0.0;
}
- public class Point{
public point(){}
}
- 都正确
4.Given Word w=new Word(“Java”).Please select the proper constructor( C)
- public word(){} B、public word(String w){}
- public Word(String w){} D、public void Word(String W){}
5.Which of the following privilege has the
- 私有访问权限 private B、默认访问权限default
- 受保护的访问权限 protected D、公共访问权限public
6.Let int[] data={3,8,12,6},the value of data[2] is (C )
A、3 B、8 C、12 D、6
7.Given Person p=new Student().We can conclude that( D)
- Person类的父亲是Object类
- Student类的父亲是Object类
- Student类是Person类的直接子类或者间接子类
- 都正确
8.Which of the following option is correct?(B )
- int[] data=new int[0];
- String[] lines=new String[5];
- String[] person={};
- 都正确
9.Please select a proper class with belongs to the package java.lang( D)
- System B、Object C、Integer D、都属于
10.Let Paper be a class extending from Document and d be a variable of type Document,when running Paper paper=(Paper)d,the possible exception may be thrown is(B )B
- NullPointerException B、ClassCastException
- IOException D、都有可能
- How many new objects are constructed when running String[] persons=new String[3]?( A)
A、1 B、2 C、3 D、4
12.The keyword declaring a subclass is (C)C
- Object B、public
C. extends D、implements
13. Given the following program. The correct statement is ( D)
Public class Address{
Private String city;
Private String zipcode;
Public Address(String zip){}
}
A、Address addr = new this(); B、Address addr = new Address();
C、Address addr = new this(“210000”); D、Address addr = new Address(“210000”);
14.Consider the following program, which is correct?( D)
Public class Test{
Public static void main(String[] args){
String name;
String hello = “Hello,” + name;
System.out.println(hello);
}
}
A、程序运行输出结果为Hello B、程序运行结果为Hello, null
C、程序运行产生空指针异常 D、程序存在语法错误无法运行
15.Given Document doc = new News( ).We can conclude the option( ) must be wrong(A)
A、 New d = doc; B、New d = (News)doc;
C、Document d = doc; D、Document d = (News)doc;
16.在Paper类中覆盖equals方法的正确形式是( C)
A、public boolean equals() B、public Boolean
C、public Boolean equals(paper obj) D、都正确
17.The parent class of FileNotFoundExceptions is (B )
- Exception B、IOException
C、RuntimeException D、ClassNotFoundException
18.Let Person be a java class and variables a and b be of type Person. Which option is syntactically(语法上) invalid in any case?(D )
A、Boolean ok = a==b B、boolean ok = a.equals(b);
C、boolean ok = a instance of Person; D、boolean ok = a>b;
19.Which of the following statement is true?(A )
A、static variable are shared by all the instances of the class.
B、static methods are tied to a specific object.
C、static constants are final variables and tied to a specific object
D、The static modifier can only be used to declare static variables.
- 运行下面程序最可能的输出的结果是( D)
Import java.util;
Public class Test{
Public static void main(String[] args){
ArrayList list = new ArrayList();
list.add(“John”);
list.add(3);
list.add(null);
list.add(null);
System.out.println(list);
}
}
A、0x38dd B、[John,3]
C、[John,3,null] D、[John,3,null,null]
21.Whcih is the basic environment for developing java program?(C )
A、Eclipse B、NetBeans C、JDK D、JRE
22.boolean is a wrapper class declared in the package( B)
A、java.io B、java.lang
C、java.net D、java.util
23.Which class is used to read a text file?( D)
A、Reader B、FileReader C、InputStream D、FileInputStream
24.Let variable a be a member of class Point, the possible type( D)
A、int B、double C、Point D、Any of the above
25.Which the following method is correct?(C )
A、 public void next(){return null;}
B、public int next(){return null;}
C、public int[] next(){return null;}
D、public int[] next(){return {2,3};}
26.Given int a=b.length .Please select a proper type for b.( C)
A、String B、int C、int[] D、Integer
27.Given Person as follows.Please add a constructor from the following option( D)
Public class Person{
Private int age;
Private String name;
Public Person(int age, String name){}
}
A、Public Person(){this();}
B、Public Person(int age){this(age);}
C、Public Person(String name){this(name);}
D、Public Person(Person copy){this(copy.age,,copy.name);}
28.Considering the following code, which is correct?( C)
Public class Person{
Static int[] arr = new int[5];
Public static void main(String[] args){System.out.println(arr[0]);}
}
A、Cannot be compiled B、Can be compiled but cannot run
C、Output 0 D、Output null
29.Given the program as follows.How many properties have a Student object?(D )
Class Person{
Private int age;
Private String name;
}
Class Student extends Person{
Private String school;
}
A、0 B、1 C、2 D、3
30.FileInputStream is a class declare in java.io.Please select a proper constructor for it.(C )
A、Public FileInputStream(String file)
B、Public FileInputStream(String file)throws Exception
C、Public FileInputStream(String file)throws IOException
D、Public FileInputStream(String file)throws FileNotFoundException
31.Given Person[] persons = this.friends(). We can conclude the proper form of friends must be(D )
A、void friends() B、String friends()
C、Person friends() D、Person[] friends()
- Which of the following statement is wrong in any case?( B)
- return this B、return this();
- return this.age; D、return this.toString();
33.Given Student extends Person{}. The person class of Student is( B)
A、Object B、Person C、Student D、Nothing
34.(Continue with 33)By declare Student a = b; the type of variable b must be( C)
A、Object B、Person C、Student D.、Any of the above
35.Given Report as follows. Please select a valid return statement for makeInfo( A)
Public class Report{
Private String title = “Unknown”
Private static String makeInfo(){ ;}
}静态方法不能对非静态变量进行引用
A、return new Report().title B、return title C、return this.title D、都正确
36.Given String[] persons = {“Hello”, “World”}. Which statement is correct?(AB)
A、persons = new String[10]; B、persons[0] = “John”;
C、persons[2] = “Marry”; D、都正确
37.Which is wrong about Java class?(C )
A、each Java class contains toString() methond
B、each Java class is a direct or indirect subclass of Object
C、each Java class could act as a superclass to derive subclass
D、each Java class is a date type
38.Given a statement n = new News(), where News is a subclass of Document.We can conclude that the type of n is( D)
A、Object B、Document C、News D、都可能
39. Given a statement Document doc = new Paper().where Document is an abstract class. Wecan conclude that( A)
A、Paper must be a concrete class
B、Paper must be the direct subclass of Document
C、The statement Paper obj = new Paper() must be invalid
D、The statement Object obj = new Paper() must be invalid
40.Please select a correct option(D )
A、Exception e = new Exception();
B、Object obj = new NullPointerException();
C、RuntimeException e = new RuntimeException();
D、都正确
辨析题
YES1.Java程序只有8种简单的数据类型,java.lang包含为每种简单数据类型申明了一个对应的引用数据类型,我们把这些引用数据类型称为封装类。
NO2.根据语句 Point p = new Point);可知Point类至少显式申明了一个构造方法。
YES3.使用BuferedReader可以用一行字符串一行字符串的方式读取输入内容。
YES4. URL类是java.net包内中明的预定义类,我们可以用它来读取网络中HTML页面文件
的内容。
NO5.根据语句Record[] data = new Record[10];可以断定Record一定是具体类。在java.lang里面的抽象类
YES6.任何一个Java类都至少有一个构造方法。
NO7.由Exception维承的任何自定义类都属于告运行时异常。
YES8.开发Java程序的 般过程是先编写源程序,保存在后级名为java的文件中,然后编译源程序,检查程序语法上是否正确,并翻译成后缀名为class的字节码文件,最后加载、
解释执行字节码文件。
NO9.根据中明public abstract class Circle extend Shape{}可知Shape类一定是抽象类。
YES10.己知a, b均为Point类型的变量并且a.equals(b)的值为true.那么我们可以判断Point类一定覆盖了equals方法。
二、请改正下面程序中的语法错误。(共2小题,每题3分,共计6分)
1、 public class NamedBean{
int id;
String name;
public NamedBean(String name){
this(1,name);
}
public NameBean(int id,String name){
this.id = id;
this.name = name;}
}
2、 class MoneyException extends Exception{}
class Money {
public static int parse(String txt)throws MoneyException {return 0;}
}
class Test{
public static void main(String[] args) throws MoneyException{
String txt=”三万八千元”
System.out.println(Money.parse(txt));
}
}
3、 abstract class Human{
String name = “Unknown”;
public Human(String name)
{
this.name = name;
}
}
class Boy extends Human{
public Boy(String name){
super(name);
}
}
4、 import java.io.*;
class Test{
public static void main(String[] args) throws IOException {
FileReader reader = new FileReader(“sample.txt”);
int c = reader.read();
while(c != -1){
System.out,println((char)c);
C = reader.read();
}
Reader.close();
}
}
程序分析题(共8小题,每题5分,共计40分)
Reading Program One and then answer questions 1-3
- What is the out put when running this program?
Product:iPhone Price:1000.0元
- Please present the members declared in class Product , including constructors.
price,name,Product(String n),getinfo()
- Please illustrate the detailed process of constructing the Product object. 请说明构建Product对象的详细过程。
检测类是否被加载(常量池)
在堆上为对象分配内存
为分配的内存空间初始化零值:
对对象设置hashcode、所属的类
执行 init 方法
Reading Program Two and then answer questions 4-6
- What is the output when running the program?
(10,10)
- Please point out the application of method override. 请指出方法重写的应用。
当子类需要父类的功能,而功能的主体子类还有自己特有的内容时,可以重写父类中的方法,这样,即沿袭了父类的功能,又定义了子类自己特有的内容。
- Polymorphism is one of the important characters of object-oriented programming . Please point out how the program applies the character. 多态性是面向对象编程的重要特征之一。请指出程序如何应用该特征。
向下转型,将父类对象的引用强转成子类类型,使用子类特有的方法。大大提高程序的可拓展性。
Reading Program Three and then answer questions 7-8
- What is the output when running the program?
南京
扬州
镇江的周边城市有:南京扬州
- 当main方法执行完第5条语句后,请画出此时内存中程序运行的栈堆结构状态并做简要说明.
Program one
public class Product{
private double price = 1000;
private String name = “UNKNOWN”;
public Product (String n){ name = n;}
public String getinfo(){ return = “Product:” + name + “\tPrice:” + price + “元”;}
}
class Test{
Product p = new Product(“iPhone”);
System.out.println(p.getinfo());
}
Program two
class Point{
protected int x,y;
public Point(int x, int y){ this.x = x;this.y = y;}
public String getinfo(){ return “(“ + x + “,” + y + “)”;}
}
class Point3D extends Point{
private int z;
public Point3D(int x,int y,int z){
super(x,y); this.z = z;
}
public String getinfo(){return “(“ + x + “,” + y + “,” + z + “)”;}
}
class Test{
public static void main(String[] args){
Point p1 = new Point(10,10);
System.out.println(p1.getinfo());
}
Program Three
class City{
private String name;
private City[] neighbours = {};
public City(String name){ this.name = name;}
public void addNeighbour(City city){
int len = neighbours.length;
City[] temp = new City[len + 1];
for(int i = 0;i < len;i++) temp[i] = neighbours[i];
temp[len] = city;
neighbours = temp;
}
public String toString(){
if(neighbours.length == 0) return name;
String info = name + “的周边城市有:”;
for(){
info += neighbours[i].name;
}
return info;
}
}
public class Test{
public static void main(String[] args){
City c1 = new City(“南京”);
City c2 = new City(“扬州”);
City c3 = new City(“镇江”);
c3.addNeighbour(c1);
c3.addNeighbour(c2);
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
Reading Program One and then answer questions 1-3
- What is the output when running the program?
- Please present the members declared in class Node,including constructors.
- Please illustrate the detailed process of constructing the Node object.
Reading Program Two and then answer questions 4-6
- What is the output when running the program?
2012-4-22
2012-4-10
2012-4-10
- Please in terms of the program explain the main usage of the keyword this.
This()指向本类相应的构造方法,”this.”为指向本对象中的成员变量
- 请使用方法调用栈详细说明date2.setDay(10)语句的执行过程
- 把形参10压入栈中
- 调用方法setDay,把this.day压入栈中,引用堆中对象date2中的day变量
- 通过this.day把对象中的day重新赋值为10
- 依次释放栈中的this.day和setDay
Reading Program Three and then answer questions 7-8
- What is the output when running the program?
id:100
Bean:Java Programming id:200
Bean:Java Programming id:300
Bean:Java Programming id:300
- Polymorphism is one of the important characters of object-oriented programming point out how the program applies the character.
向下转型,将父类对象的引用强转成子类类型,使用子类特有的方法。大大提高程序的可拓展性。
Program one
public class Node{
private int id;
private String label = “UNKNOWN”;
public Node(String label){this.label = label;}
public String getinfo(){ return “Node:” + id + “\tLabel:” + label;}
}
Program two
class MyDate{
private int day;
private int month;
private int year;
public MyDate() {this(1,1,1960);}
public MyDate(int d,int m,int y){day = d;month = m;year = y;}
public void setDay(int day){this.day = day}
public String getinfo(){return year + “-“ + month + “-“ day;}
}
public class Test{
public static void main(String[] args){
MyDate date1 = new MyDate(22,4,2012);
System.out.println(date1.getinfo());
MyDate date2 = date1;
date2.setDay(10);
System.out,println(date1.getinfo());
System.out,println(date2.getinfo());
}
}
Program three
class Bean{
protected int id;
public Bean(int id){
this.id = id;
}
public String toString(){
return “id:” + id;
}
}
class NamesBean extends Bean{
private String name;
public NamedBean(int id,String name){
this.id =id;
this.name = name;
}
public String toString(){
return “Bean:” + name + “\t” + super.toString();
}
}
public class Test{
public static void main(String[] args){
Bean bean_1 = new Bean(100);
System.out.println(bean_1);
Bean bean_2 = new NamedBean(200,”Java Programming”);
System.out,println(bean_2);
bean_1 = bean_2;
bean_1.id = 300;
System.out.println(bean_1);
System.out,println(bean_2);
}
}
程序设计题(共3小题,每题8分,共计24分)
- Given class Test as follows.Please complete the program such that it can correctly and output the result as follows.
John’s friend is Mary
Mary’s friend is John
class Person{
String name;
Person friend=null;
public Person(String name) {
this.name = name;
}
void setFriend(Person person) {
this.friend = person;
if(person.friend==null) person.friend=this;
}
String getinfo() {
return name + "'s friend is " + friend.name;
}
}
public class Test{
public static void main(String[] args){
Person john = new Person(“John”);
Person mary = new Person(“Mary”);
john.setFriend(mary);
System.out.println(john.getinfo());
System.out.printlb(mary.grtinfo());
}
}
- Given an abstract class Sltape and a test class Test as follows.Please complete the subclass of Shape such that it can run correctly and output the result as follows.
r的面积是:100.0
c的面积是:314.1592653589793
class RectangleShape extends Shape{
double length,width;
public double getArea() {
return length*width;
}
}
class CircleShape extends Shape{
double radius;
public double getArea() {
return radius*radius*Math.PI;
}
}
abstract class Shape{
public abstract double getArea();
}
public class Test{
public static void main(String[] args){
RectangleShape r = new RectangleShap();
r.length = 10;r.width = 10;
System.out,println(“r的面积是:” + r.getArea());
CircleShape c = new CircleShape();
c.radius = 10;
System.out.println(“c的面积是:” + c.getArea());
System.out,println(“Point.counter=” + Point.counter);
}
}
- Given class Test follows.Please complete the program such that it can run correctly and output the result as follows.
[Trump’s:1,eldest:1,son:1,has:1,tested:1,positive:1,for:1,the:1,coronavirus:1]
class ArrayList{
Weword[] list = new Weword[9];
int i=0;
int size() {
return i;
}
Weword get(int i){
return list[i];
}
void add(Weword w) {
list[i++]=w;
}
public String toString() {
String ans="[";
for(int j=0;j<i;j++) {
ans+=list[j].word + ":" + list[j].num;
if(j!=i-1) ans+=",";
}
ans+="]";
return ans;
}
}
class Weword{
int num=0;
String word;
Weword(String word){
this.word=word;
}
}
import java.util.*;
public class Test{
private static Weword addTo(Weword w,ArrayList list){
for(int i =0;i < list.size();i==) if(w.equals(list.get(i))) return list.get(i);
list.add(w);
return w;
}
public static void main(String[] args){
String line = “Trump’s eldest son has tested positive for the coronavirus”;
String[] words = line.split(“ “);
ArrayList list = new ArrayList();
for(int i = 0;i < words.length;i++){
Weword w = addTo(new Weword(words[i],list);
w.num++;
}
System.println(list);
}
}
- Given class Test as follows.Please complete the program such that it can run correctly and output the result as follows.
(10,10)
圆心:(10,10) 半径:18.8
class Point{
int x,y;
Point(int x,int y){
this.x=x;
this.y=y;
}
String getInfo() {
return "(" + x + "," + y + ")";
}
}
class Circle{
Point center;
double r;
Circle(Point center,double r){
this.center=center;
this.r=r;
}
String getInfo() {
return “圆心:(“ + center.x + center.y + "半径:" + r;
}
}
public class Test{
public static void main(Stringp[] args){
Point p = new Point(10,10);
Circle c = new Circle(p,18.8);
System.out.println(c.center.getInfo());
System.out.println(c.getInfo());
}
}
- Given class Test as follows.Please complete the program such that it can run correctly and output the result as follows.
我是经理张三
我是员工李四
class User{
String name;
String say() {
return null;
}
}
class Manager extends User{
Manager(String name){
this.name = name;
}
String say() {
return "我是经理" + name;
}
}
class Employee extends User{
Employee(String name){
this.name = name;
}
String say() {
return "我是员工" + name;
}
}
public class Test{
public static void main(String[] args){
User[] users = {new Manager(“张三”),new Employee(“李四”)};
for(int i = 0;i < users.length;i++){
System.out.println(users[i].say());
}
}
}
- Given class Test as follows.Please complete the program such that it can run correctly and output the result as follows.
[Java:12次,NUFE:1次]
class ArrayList{
Word[] words = new Word[100];
int i=0;
boolean contains(Word w) {
for(int j=0;j<i;j++) if(words[j].word.equals(w.word)) return true;
return false;
}
void add(Word w) {
words[i++]=w;
}
public String toString() {
String ans = "[";
for(int j=0;j<i;j++) {
ans+=(words[j].word + ":" + words[j].num + "次");
if(j!=i-1) ans+=",";
}
return ans + "]";
}
}
class Word{
String word;
int num=1;
Word(String word){
this.word=word;
}
}
import java.util.*;
public class Test{
public static void miain(String[] args){
ArrayList list = new ArrayList();
Word[] words = {
new Word(“Java”),
new Word(“NUFE”),
new Word(“Java”)
};
for(int i = 0;i < words.length;i++){
if(list.contains(words[i])) continue;
List.add(words[i]);
}
words[0].num = 122;
System.out,println(list);
}
}