第五章題庫答案_第1頁
第五章題庫答案_第2頁
第五章題庫答案_第3頁
第五章題庫答案_第4頁
第五章題庫答案_第5頁
已閱讀5頁,還剩31頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、一、選擇題1 定義類頭時,不可能用到的關鍵字是( b)。 a) class b)private c)extends d)public2.下列類定義中,不正確的是(c)。a) class x . b) class x extends y . c) static class x implements y1,y2 . d) public class x extends applet . 3.下列類頭定義中,錯誤的是( a)。 a)public x extends y . b) public class x extends y . c)class x extends y implements y1 .

2、 d)class x .4.設 a為已定義的類名,下列聲明a類的對象a的語句中正確的是( d)。 a) float a a; b) public a a=a( ); c) a a=new int( ); d) static a a=new a( );5.設 a為已定義的類名,下列聲明a類的對象a的語句中正確的是(a)。 a) public a a=new a( ); b) public a a=a( ); c) a a=new class( ); d) a a;6.設 x 、y 均為已定義的類名,下列聲明類x的對象x1的語句中正確的是(c)。 a) public x x1= new y( );

3、 b) x x1= x ( ); c) x x1=new x( ); d) int x x1;7. 設x 、y為已定義的類名,下列聲明x類的對象x1的語句中正確的是(a)。 a) static x x1; b) public x x1=new x(int 123); c) y x1; d) x x1= x( );8.通過類myclass中的不含參數的構造方法,生成該類的一個對象obj,可通過以下語句實現: _。 (myclass obj=new myclass( );)9. 設i , j為類x中定義的int型變量名,下列x類的構造方法中不正確的是( a)。 a) void x(int k )

4、i=k; b) x(int k ) i=k; c) x(int m, int n ) i=m; j=n; d) x( )i=0;j=0; 10. 有一個類a,以下為其構造方法的聲明,其中正確的是( a )。 a)public a(int x). b)static a(int x). c)public a(int x). d)void a(int x).11. 有一個類a,以下為其構造方法的聲明,其中正確的是( b )。 a)void a(int x). b)a(int x). c)a(int x). d)void a(int x).12. 設i、j、k為類x中定義的int型變量名,下列類x的構

5、造方法中不正確的是( b )。 a) x( int m) . b) void x( int m) . c) x( int m, int n) . d) x( int h,int m,int n) . 13. 設i , j為類x中定義的double型變量名,下列x類的構造方法中不正確的是(a )。 a) double x(double k ) i=k; return i; c) x( )i=6;j=8; b) x(double m, double n ) i=m; j=n; d) x(double k ) i=k; 14. 設a , b為類myclass中定義的int型變量名,下列myclass

6、類的構造方法中不正確的是(a)。a) double myclass(double k ) a=k; return a; b) myclass( )a=6;b=8; c) myclass (double m, double n ) a=m; b=n; myclass (double k ) a=k; 15 定義類頭時,不可能用到的關鍵字是(a)。 a)protected b) class c)extends d)public16.下列類頭定義中,錯誤的是(a)。 a)public testclass extends y . b) public class testclass extends y

7、. c)class testclass extends y implements y1 . d) class testclass .17.設 b為已定義的類名,下列聲明b類的對象b的語句中正確的是(d)。 a) float b b; b) public b b=b( ); c) b b=new int( ); d) static b b=new b( );18.設 classa為已定義的類名,下列聲明classa類的對象ca的語句中正確的是(a)。 a)public classa ca=new classa( ); b) public classa ca=classa( ); c) class

8、a ca=new class( ); d) ca classa;19.設 a 、b 均為已定義的類名,下列聲明類a的對象a1的語句中正確的是(c)。 a) public a a1= new b( ); b) a a1= a ( ); c) a a1=new a( ); d) int a a1;20. 設a 、b為已定義的類名,下列聲明a類的對象a1的語句中正確的是(a)。 a) static a a1; b) public a a1=new a(int 123); c) b a1; d) a a1= a( );21.通過類a中的不含參數的構造方法,生成該類的一個對象a,可通過以下語句實現: _

9、。 (a a=new a( );)22. 設m , n為類a中定義的int型變量名,下列a類的構造方法中不正確的是( a)。 a) void a(int k ) m=k; b) a(int k ) m=k; c) a(int m, int n )m=i; n=j; d) a( )m=0;n=0; 23. 有一個類person,以下為其構造方法的聲明,其中正確的是( a )。 a)public person (int x). b)static person (int x). c)public a(int x). d)void person (int x).24. 有一個類student,以下為其

10、構造方法的聲明,其中正確的是( b )。 a)void student (int x). b) student (int x). c)s(int x). d)void s(int x).25. 設i、j、k為類school中定義的int型變量名,下列類school的構造方法中不正確的是( b )。 a) school ( int m) . b) void school ( int m) . c) school ( int m, int n) . d) school ( int h,int m,int n) . 二、填空題1.下面是一個類的定義,請完成程序填空。(myclass int j)pu

11、blic class _ int x, y; myclass ( int i, _) / 構造方法 x=i; y=j; 2. 下面是一個類的定義,請將其補充完整。(student string)class _ string name;int age;student( _ s, int i) name=s; age=i; 3.下面是一個類的定義,請將其補充完整。(myclass static)class _ / 定義名為myclass的類 _ int var=666; static int getvar() return var; 4.下面程序的功能是通過調用方法max()求給定的三個數的最大值

12、,請將其補充完整。(max(i1,i2,i3) static)public class class1 public static void main( string args ) int i1=1234,i2=456,i3=-987; int maxvalue; maxvalue=_; system.out.println("三個數的最大值:"+maxvalue); public _ int max(int x,int y,int z) int temp1,max_value; temp1=x>y?x:y; max_value=temp1>z?temp1:z;

13、return max_value; 5.下面是一個類的定義,請將其補充完整。(class static)_ a string s; _ int a=666; a(string s1) s=s1; static int geta( ) return a; 6.下面是一個類的定義,請完成程序填空。(room int n)public class _ int a, b; room ( int m, _) / 構造方法 a=m; b=n; 7. 下面是一個類的定義,請將其補充完整。(class string)_ teacher string name;int age;teacher ( _ s, in

14、t i) name=s; age=i; 8.下面是一個類的定義,請將其補充完整。(desk static)class _ / 定義名為desk的類 _ int var=666; static int getvar() return var; 9.下面程序的功能是通過調用方法max()求給定的二個整數的最大值,請將其補充完整。(max(a1,a2) int)public class class1 public static void main( string args ) int a1=1234,a2=456; int maxvalue; maxvalue=_; system.out.print

15、ln("二個數的最大值:"+maxvalue); public static _ max(int x,int y) int max_value; max_value =x>y?x:y; return max_value; 10.下面是一個類的定義,請將其補充完整。(class static)_ bank string s; _ int a=666; bank(string s1) s=s1; static int geta( ) return a; 三、程序閱讀題1.下面是一個類的定義,根據題目要求回答以下問題.class bprivate int x; privat

16、e char y;public b(int i,char j)x=i; y=j;public void show()system.out.println("x="+x+" y="+y);public void methodc(int x)this.x=this.x+x; y+;show();(1)定義類b的一個對象b,將類中的變量x初始化為10、變量y初始化為a,請寫出相應的語句。(b b=new b(10,a);)(2)若在(1)問基礎上有方法調用語句:b.show();則輸出如何?(x=10; y=a)(3)若在(1)問基礎上增加語句: b.meth

17、odc(1); 則輸出為何?(x=11; y=b)(x=11; y=b)2.閱讀程序,回答問題。 public class test52 string static str1="hello, java world! t" string static str2="hello, students! " public static void main(string args) system.out.print(str1); system.out.println(str2); 問題:1)這是哪種形式的 java 程序 ?(java應用程序) 2)程序的輸出是什么

18、? (hello, java world!hello, students! )3. 寫出下列程序的輸出結果public class testpublic static void main(string args)count mycount = new count();int times = 0;for(int i=0;i<100;i+)increment(mycount , times);system.out.println(“count is” + mycount.count);system.out.println(“time is”+ times);public static voi

19、d increment(count c , int times)c.count+;times+;class countpublic int count;count(int c)count =c;count()count =1;答案:count 101times 04.寫出下列程序的輸出結果:public class testpublic static void main(string args)cirecle circle1 = new circle(1);cirecle circle2 = new circle(2);/ attempt to swap circle 1 with circl

20、e2system.out.println(“before swap:circle1 = ”+circle1.radius+”circle2 = ”+circle2.radius);swap(circle1,circle2);system.out.println(“after swap: circle1 = ”+circle1.radius+”circle2 = ”+circle2.radius);public static void swap(circle x , circle y)system.out.println(“before swap: x = ”+x.radius+”y = ”+y

21、.radius);circle temp = x;x = y;y = temp;system.out.println(“before swap: x = ”+x.radius+”y = ”+y.radius);答案:5.閱讀下面程序,回答問題:public class fooint i;static string s;void imethod()static void smethod()設f是foo的一個實例,下列語句正確嗎?system.out.println(f.i);system.out.println(f.s);f.imethod();f.smethod();system.out.pr

22、intln(foo.i);system.out.println(foo.s);foo.imethod();foo.smethod();答案: system.out.println(f.i);答案: 正確system.out.println(f.s);答案: 正確f.imethod();答案: 正確f.smethod();答案: 正確system.out.println(foo.i);答案: 錯誤system.out.println(foo.s);答案: 正確foo.imethod();答案: 錯誤foo.smethod();答案: 正確6.下列程序的輸出結果是什么?public class f

23、oostatic int i=0;static int j=0;public static void main(string args)int i=2;int k=3;int j=3;system.out.println(“i + j is ”+ i + j);k = i +j;system.out.println(“k is ”+k);system.out.println(“j is ”+ j);答案:i + j is 23k is 2j is 07. 根據下面的程序,指出下面每個元素的作用域(類作用域或塊作用域)a) 變量xb) 變量yc) 方法cuded) 變量 ie) 變量 yposp

24、ublic class cubetest int x;public void print()int ypos = 10;for(x=1;x<=10;x+) system.out.println(cude(x); for(int i=1;i<=ypos;i+) system.out.println(" "); public int cude (int y) return y*y*y; 答案:a) 變量x : 類作用域b) 變量y : 塊作用域c) 方法cude : 類作用域d) 變量 i : 塊作用域e) 變量 ypos : 塊作用域四、簡答題1. 面向對象的軟件

25、開發方法用什么把數據和基于數據的操作封裝在一起?(類)2. 在一個類定義中,用什么描述對象的狀態? 用什么描述對象的行為?(屬性;方法)3. 什么方法是一個特殊的方法,用于對對象進行初始化? (構造方法)4. 一個源程序文件中,能有多于一個的 public 類嗎?(不能)5. 構造方法的方法名可由編程人員任意命名嗎? (不能,必須與類名同名)6. 類的構造方法名必須和類名相同嗎?(必須)7. 構造函數有返回值嗎?(沒有)8. 構造函數可以重載嗎? (可以)9. 如果一個類定義中沒有定義構造方法,該類有構造函數嗎? (有,編譯器會自動生成一個缺省的不帶參數的構造函數)10. 如果一個類定義中已經

26、定義了構造方法,java還會給它定義缺省的構造方法嗎?(不會)11. 類的訪問控制權有哪兩種?(public 和 缺省的(即沒有訪問控制修飾符))12. 用什么修飾符修飾的方法稱為靜態方法? 用什么修飾符修飾的屬性稱為靜態屬性?(static)13. 靜態屬性和靜態方法僅屬于類的一個具體對象嗎?它屬于誰? (不是;它是類的所有對象公有的)14. static 變量能是 private 變量嗎?(可以)15. 使用靜態成員都能以什么做前綴?(可以用類名或對象名做前綴)16. static方法中能有this引用嗎?(不能)17. 非static方法中可以有this引用嗎?(可以)18static方

27、法能處理非static成員嗎?(不能)19非static方法能處理static成員嗎?(能)20. 類的私有屬性和私有方法能不能被其子類直接訪問? (不能)五、編程題1、基本類定義:編寫一個類,描述學生的學號、姓名、成績。學號用整型,成績用浮點型,姓名用string類型。編寫一個測試類,輸入學生的學號和成績,并顯示該學號的學生姓名,以及成績。class studentint snum;double score;string name;public class t1_studentpublic static void main(string args)student s=new student(

28、);s.snum=101;s.score=97;="張三"system.out.println(+"的學號是"+s.snum+"成績是"+s.score);2、基本類定義:將上述類進行改寫,學生擁有判斷自己的成績是否及格的功能。并能夠打印輸出自己的姓名和是否及格的信息(方法名:printinfo()編寫一個測試類,對學生類進行測試。class studentint snum;double score;string name;void printinfo()if(score>=60)system.out.p

29、rintln("我的姓名是"+name+"我的成績為及格");elsesystem.out.println("我的姓名是"+name+"我的成績為不及格");public class t1_studentpublic static void main(string args)student s=new student();s.snum=101;s.score=97;="張三"system.out.println(+"的學號是"+s.snum+"

30、;成績是"+s.score);s.printinfo();3、構造方法:編寫一個類,描述桌子,包括以下幾種屬性:長、寬、高、顏色。并且使該類具有這樣的功能:在定制桌子(即創建桌子對象時),就可以同時指定桌子的長寬高來訂制。也可以同時指定長、寬、高、顏色來訂制,也可單獨指定桌子顏色來訂制。并編寫一個測試類測試這幾種定制方法。public class t2_deskpublic static void main(string args)desk d1=new desk(2,1.5,0.6);system.out.println("第一個桌子的長寬高分別為:"+d1.l

31、ength+" "+d1.width+" "+d1.height);desk d2=new desk(1,0.6,0.3,"red");system.out.println("第二個桌子的長寬高和顏色分別為:"+d2.length+" "+d2.width+" "+d2.height+" "+d2.color);desk d3=new desk("green");system.out.println("第三個桌子的顏色是&qu

32、ot;+d3.color+"色");class deskdouble length;double width;double height;string color;desk(double l,double w,double h)length=l;width=w;height=h;desk(double l,double w,double h,string c)length=l;width=w;height=h;color=c;desk(string c)color=c;4、訪問器方法:編寫一個類,描述銀行賬戶,包括收入、支出和賬戶余額三種屬性,同時包括對這三種屬性的讀、寫的

33、訪問器方法,這三種屬性都定義為私有的。該類定義的銀行賬戶還能夠通過自己的收入和支出自動計算賬戶余額。對于賬戶余額只能讀取,自動計算,但不能夠直接賦值,也就是不能夠寫。編寫一個測試類,輸入收入和支出項,打印賬戶余額。public class t4_bankpublic static void main(string args)bank b=new bank();b.setincome(30);b.setoutcome(10);system.out.println(b.getall();class bankprivate double income;private double outcome;p

34、rivate double all;public void setincome(double i)income=i;public double getincome()return income;public void setoutcome(double o)outcome=o;public double getoutcome()return outcome;public double compute()all=income-outcome;return all;public double getall()return compute();5、給方法傳遞參數:編寫一個類,描述汽車,其中用字符型描

35、述車的牌號,用浮點型描述車的價格。編寫一個測試類,其中有一個修改價格方法,對汽車對象進行操作,根據折扣數修改汽車的價格,最后在main方法中輸出修改過后的汽車信息。public class t5_changecarpublic static void main(string args)car c=new car();c.carnum="遼b1111"c.price=10;system.out.println("before change ,the car's num and price is:"+c.carnum+" "+c.

36、price);changeprice(c);system.out.println("after change ,the car's price is:"+c.price);public static void changeprice(car c)c.price=c.price*0.8;class carstring carnum;double price;6、static關鍵字:編寫一個類teacher,描述教師的課時數量和計算課時的系數,均為double類型。teacher類還有一個方法,coursecompute(),可計算教師的當量課時,用課時量乘系數,返回值

37、類型為double,要求系數設置成static類型。編寫一個測試類進行測試。創建兩個教師對象,計算的系數為1.2,輸出計算后的兩位老師的當量課時。將系數修改后,輸出修改后的當量課時。public class t6_teacherpublic static void main(string args)teacher t1=new teacher(96);teacher t2=new teacher(64);teacher.classxishu=1.2;double realcoursenum=t1.coursecompute();system.out.println("第一位老師的實際

38、課時數為:"+realcoursenum);realcoursenum=t2.coursecompute();system.out.println("第一位老師的實際課時數為:"+realcoursenum);teacher.classxishu=1.5;realcoursenum=t1.coursecompute();system.out.println("更改系數后,第一位老師的實際課時數為:"+realcoursenum);realcoursenum=t2.coursecompute();system.out.println("

39、更改系數后,第一位老師的實際課時數為:"+realcoursenum);class teacherpublic teacher(int coursenum)this.coursenum=coursenum;int coursenum;static double classxishu;double coursecompute()return coursenum*classxishu;7、設計一個描述二維平面上點的類position,該類需要描述點的橫坐標和縱坐標,并提供訪問器方法和計算兩點間距離的方法。寫一個程序測試這個類。public class position private i

40、nt x; private int y; public position(int x,int y) this.x=x; this.y=y;public int getx() return x; public int gety() return y; public double distance(position a) double d=math.sqrt(a.getx()-x)*(a.getx()-x)+(a.gety()-y)*(a.gety()-y); return d; public static void main(string args) int x1,x2,y1,y2; syste

41、m.out.println(“請輸入第一個點的橫坐標:”); x1=myinput.readint(); system.out.println(“請輸入第一個點的縱坐標:”); y1=myinput.readint(); system.out.println(“請輸入第二個點的橫坐標:”); x2=myinput.readint(); system.out.println(“請輸入第二個點的縱坐標:”); y2=myinput.readint(); position a=new position(x1,y1); position b=new position(x2,y2); system.ou

42、t.println(“兩座標點之間的距離是:”+a.distance(b);8、設計一個birthday類,其成員變量有:year,month,day;提供構造方法、輸出birthday對象值的方法和計算年齡的方法。編寫程序測試這個類。import java.util.date;public class birthday private int year; private int month; private int day; public birthday(int year,int month,int day) this.year=year; this.month=month; this.d

43、ay=day;public void print() system.out.println(“您的生日是:”+year+” 年”+month+” 月”+day+” 日”);public void age() int age= int age=calendar.getinstance().get(calendar.year)-year; system.out.println(“您的年齡是:”+age);public static void main(string args) birthday b=new birthday(1980,12,6); b.print(); b.age();9、設計一個

44、學生類,其中成員變量應包括:學號,姓名,性別,班級,并提供方法打印學生信息,和計算已經創建學生對象的數目。編寫一個程序測試這個類。public class student private long id; private string name; private char sex; private string banji; private static int count=0; public student(long id,string name,char sex,string banji) this.id=id; =name; this.sex=sex; this.ban

45、ji=banji; count+;public void print()system.out.println(“該生信息如下:”);system.out.println(“學號:”+id+” 姓名:”+name+” 性別:”+sex+” 班級:”+banji);public static void count() system.out.println(“已經創建的學生個數為:”+count); public static void main(string args) student a=new student(01,“zhangsan”,男,”計專01班”); a.print(); stude

46、nt.count(); student b=new student(03,”劉倩”,女,”計專03班”); b.print(); student.count();10(testclass.java)設計一個類,用于表示一個學生的學號、姓名及所學的數學、英語及程序設計3門課程;設計一個類表示一個班,這些學生都學習上述3門課程;設計一個類,用于輸出學生的姓名及3門課程的總成績。class studentpublic long no;public string name;public float math;public float english;public float program;stude

47、nt(long no,string name,float math,float english,float program)this.no=no;=name;this.english=english;gram=program;class classmatestudent st;classmate()class printstudentprintstudent()void print(student s)system.out.println(+"t"+(s.math+s.english+gram);public clas

48、s testclasspublic static void main(string args)final int n=2;classmate m=new classmate();m.st=new studentn;m.st0=new student(2002001,"tom",76,85,92);m.st1=new student(2002002,"jack",90,86,78);printstudent prt=new printstudent();system.out.println("name"+"ttotalscore");for(int i=0;i<n;i+)prt.print(m.sti); 11.(testemployee.java)定義并測試一個代表員工的employee類,它的屬性包括“員工姓名”、“員工號碼”、“員工基本薪水”、“員工薪水增長額”;它的方法包括“構造方法”、“獲取員工姓名”、“獲取員工號碼”、“獲取員工基本薪水”、“計算薪水增長額”及“計算增長后的工資總額”。class employeeprivate string name;private long i

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論