4、【单选题】7.4 有程序代码如下,下面描述运行结果错误的是( ) import java.util.scanner; public class demomain { public static void main(string []s){ scanner in = new scanner(system.in); int a=in.nextint(); string b=in.nextline(); system.out.print(a b); } } a、若输入4 t ,则显示4 t b、若输入4回车,则显示4 c、若输入4回车,再输入t,则显示4t d、若输入t回车,则显示程序错误
5、【单选题】7.5 有程序代码,下面描述运行结果错误的是( ) import java.util.scanner; public class demomain { public static void main(string []s){ scanner in = new scanner(system.in); string a=in.nextline(); int b=in.nextint(); system.out.print(a b); } } a、若输入a 5 ,则显示a 5 b、若输入a 5 回车,再输入7,则显示a 57 c、若输入a回车,再输入5,则显示a5 d、若输入a回车,再输入b,则显示程序错误
4、【填空题】9.1读程序写出运行结果 public class t{ public static void main(string []args){ int week=3; while (week>0){ switch (week){ case 1: system.out.println("星期一"); case 2: system.out.println("星期二"); break; default: system.out.println("其他"); } week--; } }}
5、【填空题】9.2读程序写出运行结果 public class demo{ public static void main(string []args){ for(int month=3;month<=12;month=month 3){ switch (month/3){ case 1: {system.out.println("第一季度"); break;} case 2: {system.out.println("第二季度"); break;} case 3: {system.out.println("第三季度"); break;} default: system.out.println("第四季度"); } } }}
6、【填空题】9.4读程序写出运行结果 public class classdemo1 { public static void main(string []args){ int []a = {1,2,3,4,1},i; for (i=0;i 7、【简答题】9.3读程序写出运行结果
4、【单选题】4.4 阅读下面的程序,写出运行结果 class samp {samp(int i){system.out.println(i);}} public class test{ public static void main(string []args ){ samp []p=new samp[10]; for(int i=0;i<5;i ){p[i]=new samp();} 上述程序,执行的结果是( ) a、正常执行,显示10行samp b、正常执行,显示5行samp c、编译错误 d、正常执行,显示20行samp
5、【单选题】4.5 假定一个类的定义为: class a { public a(int aa,int bb) {a=aa--;b=a*bb--;} int a,b; } 则执行a x=new a(5,4);语句后,x.a和x.b的值分别为( ) a、4和5 b、5和20 c、4和20 d、20和5
6、【单选题】4.6 若有以下程序代码: class person{ string name,department; int age; public person(string n){name=n;} public person(string n, int a){name=n; age=a;} public person(string n, string d, int a ){ //执行此代码 } 下面那个选项可以添加到“//执行此代码”前面( ) a、person(n,a) b、this(person(n,a)) c、this(n, a) d、this(name.age)
2.5this与对象数组
1、【单选题】5.1 若有下面的程序 class samp {samp(){system.out.print(”samp”);}} public class test{ public static void main(string []args ){ samp [] p=new samp[10]; } 程序运行结果是( ) a、显示10个samp b、显示1个samp c、无任何显示 d、编译错误
2、【单选题】5.2 若有下面的程序 class samp {samp(){system.out.println(”samp”);}} public class test{ public static void main(string []args ){ samp []p=new samp[10]; for(int i=0;i<5;i ){p[i]=new samp();} 程序运行结果是( ) a、显示10行samp b、显示5行samp c、无任何显示 d、编译错误
4、【单选题】7.3 若有程序如下: class student{ string stuname; static double moneybox; public student(string stuname,double m){ this.stuname=stuname; moneybox = m; } public void showmoney(){ system.out.println(stuname " " moneybox); } } public class studentdemo { public static void main(string[] args) { student stu1=new student("张同学",10); student stu2=new student("李同学",20); stu2.showmoney(); } } 则程序的运行结果为( ) a、张同学 10 b、李同学 30 c、李同学 20 d、编译错误,因moneybox无初始值
5、【单选题】7.5 若有程序如下: public class studentdemo { int j; public static void main(string[] args) { studentdemo.show(2); } public static void show(int i){ system.out.print(i " " j); } } a、2 0 b、编译错误,因j无初始值 c、编译错误,因j没有定义 d、编译错误,因j为非静态变量
6、【单选题】7.6若有下面的程序代码 class test{ private int m; public static void fun(){ //some code } } 方法fun()如何来访问变量m( ) a、将private int m 改成protected int m b、将private int m 改成public int m c、将private int m 改成static int m d、将private int m 改成int m
7、【单选题】7.7若有下面的程序代码 public class test{ long a[]=new long[10]; public static void main(string args[]){ system.out.println(a[6]); } } 哪一个选项是正确的( )。 a、不输出任何内容 b、输出0 c、当编译时有错误出现 d、当运行时有错误出现
2.8第二部分 章节测验
1、【填空题】8.1 在横线上填上适当的语句,使程序能正常运行。 class test{ 【1】 float fl; 【2】 {num=1;f1=2.0f;} public int getint(){return num;} public float getfloat(){return fl;} } class demo{ public static void main(string []args){ { 【3】 t=new test(); system.out.print(t.getint() ” “ t.getfloat());} 运行结果为: 1 2.0
2、【填空题】8.2 在横线上填上适当的语句,使程序能正常运行。 【1】{ 【2】(int a,int b){ return a-b; } } class studentdemo{ public void show(student s,int a, int b){ system.out.println(s.sum(a,b)); } } public class demo{ public static void main(string []args){ int x=20,y=10; student s=【3】; 【4】=new studentdemo(); st.show(s,x,y); } }
3、【填空题】8.3 在横线上填上适当的语句,使程序能正常运行。 【1】{ 【2】 public void setage(int a){age=a;} 【3】{【4】} } public class demo{ public static void main(string []args){ student s=【5】 s.age=20; system.out.println(s.getage()); //显示age的值 } }
4、【填空题】8.4 阅读程序,写出运行结果 class student{ private string name; public static int age=20; public student(string name){ this.name=name; age ; } public void show(){ system.out.println("name=" name ",age=" age);} } public class t{ public static void main(string []args){ student s1=new student("张同学"); s1.show(); student s2=new student("赵同学"); s2.show(); s1.show(); } }
5、【填空题】8.5 阅读程序,写出运行结果 class point{ int x=1; static int y=2; public void show(){system.out.println(x "," y);}} public class tempdemo{ public static void main(string []args){ point p1=new point();p1.show(); p1.x=2;p1.y=3; point p2=new point();p2.show(); p1.show(); p1.y=5; p2.show(); }}
6、【填空题】8.6 阅读程序,写出运行结果 class demo{ int a=0; static int b=0; public demo(){ b; system.out.println("a,b:" a b); } public demo(int a){ this(); this.a=a; system.out.println("a,b:" a b); }} public class classdemo1 { public static void main(string []args){ demo d1=new demo(); d1.a = 3; system.out.println("a,b:" d1.a d1.b); demo d2=new demo(2); demo d3=new demo(); }}
7、【填空题】8.7 阅读程序,写出运行结果 class student{ string stuname="学生"; static int no=0; public student() {no ; system.out.println(stuname no);} public student(int a) {no=a; system.out.println(stuname a);} public void show(){ system.out.println(stuname no); } } class demo { public static void main(string[] args) { student st1=new student(); st1.show(); student st2=new student(2); st2.show(); st1.show(); }}
9、【简答题】8.9按照以下要求设计一个学生类student,并进行测试。 l student类中包含学号(strno)、成绩(score)两个属性。两个成员变量的访问权限设为私有。 l 分别为每个属性定义两个方法,一个方法用于设置值,另一个方法用于获取值。 l 定义一个无参构造方法,将学号设置为空字符串,成绩设置为0。 l 在测试类中创建一个student对象,通过对象调用相应方法,为对象的两个属性赋值(“李敏”,80),并显示对象的属性值。显示内容及格式为:学号:李敏 成绩:80。
2、【单选题】若有如下类的定义 class father{int a; public father(){}} class son extends father{int b;} 则下面描述正确的选项是( ) a、son类中仅包含成员变量b b、son类中仅包含成员变量a和b c、son类中仅包含成员变量a和b 、以及成员方法father d、father类中包含成员变量a和b}
3、【单选题】已知类关系如下: class employee{} class manager extends employee{} class director extends employee{} 则下列语句正确的是( ) a、employee e=new manager(); b、director d=new manager(); c、director d =new employee (); d、manager m=new director ();
3.2继承中访问权限、方法重载及覆盖
1、【单选题】若有如下程序: class employee{private int a=1;} class manager extends employee{ int b=2; public void show(){ system.out.print(a); }} class maindemo{ public static void main(string []args){ manager m = new manager(); m.show(); system.out.print(m.b); }} 则程序运行结果是( ) a、3 b、12 c、编译错误 d、结果不确定
2、【单选题】若有以下程序: class employee{ public void show(int i){} //① public void show(int i,int j){} //②} class manager extends employee{ public void show(){} //③ public void show(int i){} //④} 则下面描述错误的是( ) a、语句 ①与语句 ③是方法重载 b、语句 ①与语句④是方法覆盖 c、语句②与语句③是方法重载 d、语句②与语句④是方法覆盖
3、【单选题】若有以下程序: class employee{ public void show(){ system.out.print(“1”);} } class manager extends employee{ public void show(){ system.out.print(“2”);} } class maindemo{ public static void main(string []args){ manager m = new manager(); m.show(); }} 则程序运行结果为( ) a、1 b、2 c、12 d、无法确定
2、【单选题】若有以下程序代码 class father{public void show(){}} class son extends father{ public void show(){} public void print(){}} class main{ public static void main(string []s){ father f; // ① f=new father(); //② f.show(); // ③ f = new son(); //④ f.show(); //⑤ f.print(); //⑥ } 则能实现多态的语句组合是( ) a、①②③ b、①② ④⑥ c、①② ⑤⑥ d、① ④⑤
3.5抽象类的概念及实现
1、【单选题】5.1 下列哪个类声明是正确的( ) a、public abstract class car{…} b、abstract private move(){…} c、protected private number; d、abstract final class h1{}
3、【单选题】若有以下程序 interface father{ // ① double f =3.5; public void show(){} //②} class son implements father{ public void show(){ f = 5.0; //③ } } class main{ public static void main(string []s){ father f = new father(); //④ }} 则下面描述错误的是( ) a、①句无语法错误 b、②句有语法错误 c、③句无语法错误 d、④句有语法错误
3、【单选题】若有以下程序 //程序1 package p1; // ① class father{ int i; public static void show(){}} //程序2 package p2; import p1.class; //② class son { public void show(){ father f = new father(); f.i = 5; //③ f.show(); //④ }} 则①~④中有语法错误的语句是( ) a、① b、② c、③ d、④
1、【填空题】读下面程序,写出程序运行结果。 class father { int num1=0; public father(){ system.out.println("num1:" num1);} public father(int num1){ this.num1=num1; system.out.println("num1:" num1); } } class son extends father{ int num2=10; public son(){system.out.println("num2:" num2);} public son(int i,int j){ super(i); num2=j; system.out.println( "num1:" num1 ",num2:" num2); } } public class demo{ public static void main(string []args){ father father=new father(); son son=new son(); son son1=new son(20,30); } } 运行结果为:
2、【填空题】在(1)~(3)处填上适当的语句,使程序能正常运行 class person { public void show(){}} (1) { public void show(){ system.out.println("student");} } class teacher extends person{ (2) { system.out.println("teacher");}} public class demo{ public static void main(string []args){ (3) p.show(); p=new student(); p.show(); p=new teacher(); p.show(); }}
3、【填空题】读下面程序,写出程序运行结果。 class point{ int x; public point(){x=0;} public point(int x){this.x=x;} public void show(){system.out.println(x);}} class spoint extends point{ int y; public spoint(){y=0;} public spoint(int x,int y){super(x); this.y=y;} public void show(){system.out.println(x "," y);}} public class tempdemo{ public static void main(string []args){ point p=new point(); p.show(); point p1=new point(2); p1.show(); spoint sp=new spoint(); sp.show(); spoint sp1=new spoint(2,3); sp1.show(); }} 运行结果为:
4、【填空题】读下面程序,写出程序运行结果。 class point{ int x=1; public void show(){system.out.println(x);}} class spoint extends point{ int y=2; public void show(){system.out.println(x "," y);}} class ppoint extends point{ int z=3; public void show(){system.out.println(x "," z);}} public class tempdemo{ public static void main(string []args){ point p; p=new point(); p.show(); p=new spoint(); p.show(); p.x=2; p.show(); p=new ppoint(); p.show(); }} 运行结果为:
5、【填空题】在(1)~(3)处填上适当的语句,使程序能正常运行。. (1) class employee { public abstract void show(); } class salemp extends employee{ public void show(){system.out.println("salemp");} } class telemp extends employee { (2) { system.out.println("telemp ");} } public class demo{ public static void main(string []args){ salemp p=new salemp(); p.show(); (3) p1=new telemp (); p1.show(); }}
6、【填空题】读下面程序,写出程序运行结果。 class person{ int a,b; public person(int a,int b){ system.out.println("a,b:" a b); }} class student extends person{ int c; public student(int a,int b,int c){ super(a,b); this.c= c; system.out.println("c:" c); } public void show(){ system.out.println("a,b,c:" a b c); }} public class classdemo1{ public static void main(string []args){ person p1=new person(1,2); student p2=new student(2,3,4); p2.show(); }} 运行结果为:
7、【填空题】读下面程序,写出程序运行结果。 abstract class graph{ int a; public graph(){a=1;} public graph(int a){this.a=a;} public abstract void showarea(); } class rect extends graph{ int b; public rect(int a,int b){super(a); this.b= b;} public void showarea(){ system.out.println("area:" (a*b));} } class circle extends graph{ public circle(){}; public circle(int a){super(a);} public void showarea(){ system.out.println("area:" (3.14*a*a)); } } public class classdemo1{ public static void main(string []args){ graph g1; g1=new rect(2,3); g1.showarea(); g1=new circle(2); g1.showarea(); g1= new circle(); g1.showarea(); } } 运行结果为:
8、【填空题】在(1)~(3)处填上适当的语句,使程序能正常运行。. abstract class person { public (1) show(); } class student extends person{ public (2) { system.out.println("student");}} (3) { public void show(){ system.out.println("teacher");}} public class demo{ public static void main(string []args){ person p; p=new student(); p.show(); p=new teacher(); p.show(); }}
1、【填空题】预测程序运行结果 class student{ private int age; public void setage(int a){ age=a;} public int getage(){ return age; } } public class studentdemo{ public static void main(string []args){ student s=new student(); s.setage(20); system.out.println(s.getage());}}
2、【填空题】预测程序运行结果 class person{ string name; public void setname(string name) { this.name=name;} public string getname() {return name;} } class student extends person{ } class teacher extends person{ } public class studentdemo{ public static void main(string []args){ student s=new student(); s.setname("tom"); system.out.println(s.getname()); teacher t=new teacher(); t.setname("john"); system.out.println(t.getname()); }}
3、【填空题】预测程序运行结果 class father { int num1=0; public father(){ system.out.println("num1:" num1);} } class son extends father{ int num2=10; public son(){ system.out.println("num2:" num2);} } public class studentdemo{ public static void main(string []args){ son son=new son(); }}
4、【填空题】预测程序运行结果 class person { int num1=0; public void show() { system.out.println("num1:" num1);}} class student extends person{ int num2=10; public void show(){ system.out.println("num2:" num2);}} class teacher extends person{ int num2=20; public void show(){ system.out.println("num2:" num2);}} public class studentdemo{ public static void main(string []args){ person p=new person(); p.show(); p=new student(); p.show(); p=new teachr(); p.show(); }}
5、【简答题】预测程序运行结果 class student{ private int age; private string name; public student(string name, int age){ this.name=name; this.age=age;} public void show(){ system.out.println(“name=” name ” age=” age);} } public class studentdemo{ public static void main(string []args){ student s=new student(); s.show();}}
6、【简答题】预测程序运行结果 class student{ private string name; public static string classno; public student(){} public student(string name, string classno){ this.name=name; this.classno=classno;} public static void show(){ system.out.println(“name=” name ”classno=” classno);}} public class studentdemo{ public static void main(string []args){ student s1=new student(“张”,”计1”); student s2=new student(“李”,”计2”); s1.show();
3、【单选题】若有下面程序, class demo{ public static void main(string []s){ int a = 5, b = 0; a = a/b; }} 则程序运行时会抛出哪种异常( ) a、classnotfoundexception b、indexoutofboundsexception c、nullpointerexception d、airthmeticexception