@font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } @font-face{ font-family:"Calibri"; } @font-face{ font-family:"Courier New"; } mso-level-number-format:lower-roman; mso-level-suffix:tab; mso-level-text:"%9."; mso-level-tab-stop:none; mso-level-number-position:right; margin-left:189.0000pt;text-indent:-21.0000pt;font-family:'Times New Roman';} p.MsoNormal{ mso-style-name:正文; mso-style-parent:""; margin:0pt; margin-bottom:.0001pt; mso-pagination:none; text-align:justify; text-justify:inter-ideograph; font-family:'Times New Roman'; mso-fareast-font-family:宋体; font-size:10.5000pt; mso-font-kerning:1.0000pt; } mso-style-name:章后习题_2; margin:0pt; margin-bottom:.0001pt; mso-pagination:none; text-align:justify; text-justify:inter-ideograph; font-family:'Times New Roman'; mso-fareast-font-family:宋体; font-size:10.5000pt; mso-font-kerning:1.0000pt; } p.p{ mso-style-name:"普通\(网站\)"; margin-top:5.0000pt; margin-bottom:5.0000pt; mso-margin-top-alt:auto; mso-margin-bottom-alt:auto; mso-pagination:widow-orphan; text-align:left; font-family:宋体; mso-bidi-font-family:'Times New Roman'; font-size:12.0000pt; } span.msoIns{ mso-style-type:export-only; mso-style-name:""; text-decoration:underline; text-underline:single; color:blue; } span.msoDel{ mso-style-type:export-only; mso-style-name:""; text-decoration:line-through; color:red; } margin-top:72.0000pt; margin-bottom:72.0000pt; margin-left:90.0000pt; margin-right:90.0000pt; size:595.3000pt 841.9000pt; layout-grid:15.6000pt; } div.Section0{page:Section0;}
一、 单选题
1.一个可以独立运行的Java应用程序( D)。
A、可以有一个或多个main方法 B、最多只能有两个main方法
C、可以有一个或零个main方法 D、只能有一个main方法
2.下列标识符合法的是(D)。
A、%abcd B、2eft C、package D、_my_name
3. 有一段java 应用程序,它的主类名是A1,那么保存它的源文件名可以是(A)
A、A1.java B、 A1.class C、 A1 D、都对
4.在Java中,不属于整数类型声明的是( C)。
A、int B、byte C、double D、long
5. 下面哪个修饰符修饰的方法只能被本类中的其他方法使用( C )
A、protected B、static C、private D、public
6. 在Java中,“456”属于(B)类的对象。
A、int B、String C、Integer D、char
7. 在子类的构造方法中,可以使用( B )关键字调用父类的构造方法。
A、this B、super C、implements D、extends
8.下面是有关子类继承父类构造方法的描述,其中正确的是( C )。
A、创建子类的对象时, 先调用子类自己的构造方法,然后调用父类的构造方法。
B、子类可以继承父类带参数的构造方法。
C、在子类的方法中可以通过super关键字调用父类的构造方法。
D、子类无法继承父类的构造方法。
9. 子类中能与父类public void methodA(int a){ }方法形成重写的有( D ) 。
A、public int methodA(int a){return 1;}
B、protected void methodA(int a) { }
C、private void methodA(int a){ }
D、public void methodA(int b){ }
10. 下面的方法在被调用时,如实际参数的值为2,方法的返回值是多少?( D)
public int getValue(int i) {
int result = 0;
switch (i) {
case 1: result = result + i;
case 2: result = result + i * 2;
case 3: result = result + i * 3;
}
return result;
}
A、0 B、2 C、4 D、10
11.已知:String s1=”I am astudent!”;则执行String s2=s1.substring(6,13)后,s2的值为( B)。
A、student! B、student C、astudent D、astuden
12.定义一个类名为MyClass.java的类,并且该类可被一个项目中的所有类访问,那么该类的正确声明应为:( B)。
A、private class MyClass extends Object B、public class MyClass
C、class MyClass extends Object D、private class MyClass extends Object
13.以下( C)类不能有子类。
A、class A{} B、abstract class A{}
C、final class A{} D、public class A{}
14.method为Desk类的一个无形式参数无返回值的方法,若要使用类名Desk作为前缀即可以调用它,该方法声明的形式为( C)。
A、public void method() B、final void method()
C、static void method() D、abstract void method()
15.类Test1定义如下:
public class Test1{
public float aMethod(float a,float b){ }
}
将以下哪种方法插入横线位置处是不合法的。(D)
A、public float aMethod(float a,float b,float c){ }
B、public int aMethod(int a,int b){ }
C、public float aMethod(int a,int b,int c){ }
D、public float aMethod(float c,float d){ }
16.为删除ArrayList中的一个特定索引所对应的元素,应使用(A)方法。
A、remove B、removeAt C、delete D、deleteAt
17.设有下面两个类的定义:
class Person{ class Student extends Person{
long id; int score;
String name; int getScore(){
} return score;
}
}
问:类Person和类Student的关系是( B)。
A、包含关系 B、继承关系 C、封装 D、无关系,上述类定义有语法错误
18.给定如下Java代码,编译运行的结果是( C)。
import java.util.*;
public class Test{
public static void main(String args[]){
Map map=new HashMap();
String s=”key”;
map.put(s,”Hello”);
map.put(s,”World”);
System.out.println(map.size());
}
}
A、编译时发生错误 B、运行时引发错误
C、正确运行,输出:1 D、正确运行,输出:2
19.对于catch子句的排列,有关异常类,下列哪种是正确的( B)。
A、父类在先,子类在后 B、子类在先,父类在后
C、有继承关系的异常不能在同一个try程序段内 D、先有子类,其他如何排列都无关
20.当需要在文件中写入字符而不是字节时,在下面的类中最好选用( B )类。
A、java.io.RandomAccessFile B、java.io. Writer
C、java.io.Reader D、java.io. OutputStream
二、程序阅读题
1.请写出以下程序的输出结果:
public class Test {
public static void main(String args[]) {
AB s = new AB("Hello!","I love Java.");
System.out.println( s.toString() );
}
}
class AB {
String sl;
String s2;
AB( String str1, String str2 ) {
sl = str1; s2 = str2;
}
public String toString() {
return sl + s2;
}
}
Hello!I love Java.
2.请写出下列程序的输出结果:
abstract class class1{
abstract void method1();
}
class class2 extends class1{
public void method1(){ System.out.println("子类");}
}
public class mainclass {
public static void main(String args[]){
class1 ac=new class2();
ac.method1();
}
}
子类
3.写出程序的运行结果
public interface Animal{
void voice();
}
class Dog implements Animal{
public void voice(){System.out.println(“WWW!”);}
}
class Cat implements Animal{
public void voice(){ System.out.println(“MMM”);}
}
class Store{
public static Animal get(String choice){ //返回类型为Animal
if(choice.equalsIgnoreCase(“dog”)) //比较时忽略字母大小写
return new Dog();
else
return new Cat();
}
}
public class AnimalTest{
public static void main(String[] args){
Animal an=Store.get(“dog”);
an.voice();
}
}
WWW!
4.阅读以下程序:
public class Test{
public static void fun(int i){
try{
if(i==1) throw new Exception();
System.out.print(“1”);
}catch(Exception e){
System.out.print(“2”);
}finally {
System.out.print(“3”);
}
System.out.print(“4”);
}
public static void main(String args[]){
fun(1);
}
}
运行上面的程序,写出输出结果。
234
5. 写出以下程序的功能。
import java.io.*;
class FileApp{
public static void main(String[ ] args) throws IOException{
int b;
FileInputStream fileIn=new FileInputStream(“newfile.java”);
while((b=fileIn.read())!=-1){
System.out.print((char)b);
}
}
}
在控制台上显示文件”newfile.java”中的内容
三、机试题:
1.按以下要求编写程序
(1) 编写Animal接口,接口中声明run() 方法
(2) 定义Bird类和Fish类实现Animal接口
(3) 编写测试类AnimalTest,分别生成Bird类和Fish类的对象,并调用其中的run()方法。运行结果如下:(14分)
interface Animal{
void run();
}
class Bird implements Animal{
public void run(){ System.out.println("鸟儿在飞……"); }
}
class Fish implements Animal{
public void run(){ System.out.println("鱼儿在游……"); }
}
public class AnimalTest {
public static void main(String[] args) {
Animal animal1=new Bird();
Animal animal2=new Fish();
animal1.run();
animal2.run();
}
}
2. 编写一Demo类,在一ArrayList列表中存储“杭州”、“上海”、“北京”、“广州”四个城市名,从键盘输入某城市名,遍历ArrayList列表,输出该城市名在ArrayList列表中是否存在的信息。(10分)
import java.util.*;
public class Demo {
public static void main(String[] args) {
List list=new ArrayList();
list.add("杭州");
list.add("上海");
list.add("北京");
list.add("广州");
System.out.print("请输入要查找的城市名:");
Scanner input=new Scanner(System.in);
String str=input.next();
boolean flag=false;
for(String s:list){
if(str.equals(s)){
System.out.println(str+"在列表中存在!");flag=true;
}
}
if(!flag) System.out.println(str+"在列表中不存在!");
}
}
3.编写一个父类People,成员变量有姓名(name)、ID、性别(sex)、年龄(age);成员方法有print(),功能是在屏幕上输出上述信息。
再编写一个子类Student,成员变量除了继承的四个属性外,还有就读的学校school,所学的科目数学(math)、语文(chinese)和英语(english),子类中覆盖print()方法,功能是在屏幕上输出Student的所有信息。
编写一个测试类StudentTest,实现在控制台上输出一个学生的相应信息。(16分)
package testb;
class People{
String name;
String id;
String sex;
int age;
public People(String name,String id,String sex,int age){
this.name=name;
this.id=id;
this.sex=sex;
this.age=age;
}
void print(){
System.out.print("姓名:"+name+"\t"+"\t");
System.out.print("ID:"+id+"\t");
System.out.print("性别:"+sex+"\t");
System.out.println("年龄:"+age);
}
}
class Student extends People{
String school;
int math,chinese,english;
public Student(String name,String id,String sex,int age,String school,int math,int chinese,int english){
super(name,id,sex,age);
this.school=school;
this.math=math;
this.chinese=chinese;
this.english=english;
}
void print(){
super.print();
System.out.print("学校:"+school+"\t");
System.out.print("数学:"+math+"\t");
System.out.print("语文:"+chinese+"\t");
System.out.println("英语:"+english);
}
}
public class StudentTest {
public static void main(String[] args) {
Student stu1=new Student("张三","001","男",20,"浙江水利水电学院",90,75,85);
stu1.print();
}
}