最新JAVA試卷含答案_第1頁
最新JAVA試卷含答案_第2頁
最新JAVA試卷含答案_第3頁
最新JAVA試卷含答案_第4頁
最新JAVA試卷含答案_第5頁
已閱讀5頁,還剩5頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、姓名: 班級: 學號 一、單項選擇題(本題共24分,每小題3分) 注意:請把答案填入下面框中,答在其它處無效。1. 下面有兩個類的定義,其中有四條語句用標號標出,請判斷 是正確的 package c05; public class AccessControl public void f1() private void f2() protected void f3() void f4() package c05.other2; public class Other2 public static void main(String args) c05. AccessControl test = ne

2、w c05.AccessControl(); test.f1(); test.f2(); test.f3(); test.f4();A) 語句 正確,語句 錯誤B) 語句 錯誤,語句 正確C) 語句 正確,語句 錯誤D) 語句 都正確2. 假設(shè)將local目錄加入到類路徑中,然后在local目錄下編寫類定義: class ClassA public ClassA () System.out.println("Creating a packaged class"); 在local的子目錄中創(chuàng)建下列類Foreign:教研室主任簽字 : 第 1 頁(共 10 頁)第 2 頁(共

3、10 頁) import local.*; public class Foreign public static void main (String args) ClassA pc = new ClassA (); 以下說法哪個是正確的: A) 編譯不正確,ClassA不是公有類,在Foreign類不能訪問 B) 編譯正確,雖然類 ClassA不是公有類,在foreign是其子包 C) 編譯不正確,因為第行前應(yīng)有包申明語句package local; D) 編譯不正確,第行改為ClassA pc = new local.ClassA (); 3. 以下程序用標號標出, 請問 1) abstra

4、ct class NoMethods 2) class Extended1 extends NoMethods 3) public void f() System.out.println("Extended1.f"); 4) public class AbstractBase 5) public static void test1(NoMethods nm) 6) 7) 8) public static void main(String args) 9) NoMethods nm = new Extended1(); 10) test1(nm); 11) 12) 要使第9行

5、的語句正確運行,第6行的語句可能會是下面的哪條語句 。 4.下面程序的輸出結(jié)果是_。 import java.util.*; public class ss public static void main(String args) ArrayList<String> v = new ArrayList<String>(); String a = "hit" String b = "HIT" v.add(a); System.out.println(v.get(v.size()-1); v.add(b); System.out.pr

6、intln(v.get(v.size()-1);A) HIThit B) HITHIT;C) hithit D) hitHIT5. 下面程序的輸出結(jié)果是_。import java.util.*;import java.awt.Point;public class ss public static void main(String args) Point myPoint = new Point( 0, 0 );myPoint.setLocation( 1.0, 0.0 );System.out.println( myPoint );String myString = new String( &q

7、uot;old String" );myString.replaceAll( "old", "new" );System.out.println(myString.replaceAll( "old", "new" ); System.out.println( myString );A) java.awt.Pointx=1,y=0 new Stringold StringB) java.awt.Pointx=0,y=0 new Stringold StringC) java.awt.Pointx=1,y=0

8、 new Stringnew StringD) java.awt.Pointx=1,y=0 old Stringold String6. 給出下面代碼,那個語句是正確的?public class Teststatic int arr = new int10;public static void main(String a) System.out.println(arr1);A 編譯時將產(chǎn)生錯誤; B 編譯時正確,運行時將產(chǎn)生錯誤;C 輸出零; /數(shù)組沒有初始化 D 輸出空。7. 對于下列代碼:import java.io.*;public class Parent public int add

9、Value( int a, int b) int s; s = a+b; return s; class Child extends Parent class MyException extends Exception 下述哪些方法可以加入類Child?第 3 頁(共 10 頁)B public void addValue (int a, int b ). C public int addValue( int a ). D int addValue( int a, int b ).8. 下列哪些語句關(guān)于包的說法是錯誤的?A同一個包中的兩個類必須在同一目錄 B文件名體現(xiàn)體現(xiàn)了類名,不體現(xiàn)包名 C

10、父包和子包在使用上沒有任何關(guān)系D用import語句可以簡化多次使用包名.類名的工作E父類使用子包中的類,或子包使用父包中的類,也必須寫上完整的名字二、程序填空題(共21分,每小題1分)字符串中所有指定的字符刪除后輸出。import java.applet.*; import; import java.awt.event.*;public class ch6 extends Applet implements ActionListener String originalString, modifiedString; int delChar;Label orgStringLbl = new Lab

11、el("原始字符串:"); ("需去除字符:"); TextField orgStringTfd = new TextField(20); ;Button modifyBtn = ("刪除字符");public void init() 第 4 頁(共 10 頁)第 5 頁(共 10 頁) add(orgStringLbl); add(orgStringTfd); add(delCharLbl); add(delCharTfd); add(modifyBtn); orgStringTfd.setText(""); d

12、elCharTfd.setText(""); originalString = "" modifiedString = "" modifyBtn.addActionListener(); public void paint(Graphics g) g.drawString(, 10, 150); public void actionPerformed(ae) if(ae.getSource() = modifyBtn) originalString = orgStringTfd. modifiedString = "&quo

13、t; int i=0, j=0; while( (j=originalString(delChar,i) != -1 ) modifiedString=modifiedString+ i = j+1; modifiedString=modifiedString+ riginalString.substring(i,); ; else showStatus("事件" + ae + "未定義處理操作。"); orgStringTfd.setText(""); delCharTfd.setText(""); origin

14、alString = "" modifiedString = "" 2. 下列Applet程序的作用是滾動顯示一個字符串,當字符串從屏幕的右邊消失后,重新從左邊出現(xiàn)病繼續(xù)向右移動,請把程序填寫完整但不要修改程序。 import java.awt.*; import java.applet.*; public class ch8 extends Applet implements final String rollingMessage = " I AM SAILING." Thread m_Draw = null;public void

15、 init() m_Draw = new Thread(this);public void paint(Graphics g) g.drawString(rollingMessage, beginX, 40);public void start() m_Draw. ;try .sleep(50);catch(InterruptedException e) public void run() try while( ;Thread.sleep(100);e) 三、請寫出下列程序運行的輸出結(jié)果。 用”表示空格(本題18分,每空31. 如果運行時輸入數(shù)據(jù)4,下列程序段的輸出結(jié)果是 第 6 頁(共 10

16、 頁)public static void p(String s, boolean b) System.out.println(s + ": " + b);public static void compare(String lval, String rval) p("lval = rval", lval = rval);p("lval != rval", lval != rval);p("lval.equals(rval)", lval.equals(rval);public static void main(St

17、ring args) compare("Hello", "Hello");2. 運行以下程序,執(zhí)行的結(jié)果是_。 class Level1Exception extends Exception class Level2Exception extends Level1Exception class Level3Exception extends Level2Exception class A public void f() throws Level1Exception throw new Level1Exception();class B extends A

18、 public void f() throws Level2Exception throw new Level2Exception();class C extends B public void f() throws Level3Exception throw new Level3Exception();第 7 頁(共 10 頁)public class ThreeLevelExceptions public static void main(String args) A a = new C();try a.f(); catch(Level1Exception e) System.out.pr

19、intln("Caught " + e);3. 運行PassArrayElement,執(zhí)行的結(jié)果是_。 class PassArrayElementint x ;public static void main(String args) int x = new int1; x0= 5; change(x0);System.out.println(x0);public static void change(int y) y =3; 4. 運行PassArray,執(zhí)行的結(jié)果是_。 class PassArraypublic static void main(String args

20、) int x = new int1;x0= 5;change(x);System.out.println(x0);public static void change (int x)class PassArrayRefint x ;public static void main(String args)PassRef obj = new PassRef();obj.x = 5;第 8 頁(共 10 頁) x0 = 3; 5. 運行PassArrayRef,執(zhí)行的結(jié)果是_。 change(obj); System.out.println(obj.x); public static void ch

21、ange(PassRef obj) obj = new PassArray(); obj.x=3; 6. 運行下列程序,執(zhí)行的結(jié)果是_。 class Aclass Dogprivate String name; private int age; public int step; Dog(String s,int a)name=s; age=a; step=0; public void run(Dog fast) fast.step+; public static void main (String args) A a=new A(); Dog d=a.new Dog("Tom&quo

22、t;,3); d.step=25; d.run(d);System.out.println(d.step);不得更改程序結(jié)構(gòu)。1. 請指出下列程序的錯誤,并予以更正(7分)public class MyClasspublic static void main(String args) try (1分)myMethod();catch(Exception e) (1分)System.out.println(e.toString(); (1分) public static void myMethod() throws MyException (2分) throw (new MyException(

23、); (1分)class MyException extends Exception (1分)第 9 頁(共 10 頁)public String toString() return ("用戶自定義的異常");2. 改寫以下多線程程序,使之能夠?qū)崿F(xiàn)在四個售票處,發(fā)售某日某次列車的共100張票。(本題7分)public class ThreadDemopublic static void main(String args) new ThreadTest().start(); new ThreadTest().start(); new ThreadTest().start();

24、 new ThreadTest().start(); class ThreadTest extends Thread private int tickets=100;public void run() while(true) if(tickets>0)System.out.println(Thread.currentThread().getName()+" is saling ticket " + tickets-);1. public class ThreadDemo (1分) (2分) new Thread(t).start(); new Thread(t).st

25、art(); (2分) new Thread(t).start(); new Thread(t).start(); (1分)private int tickets=100;public void run() (1分)if(tickets>0)System.out.println(Thread.currentThread().getName() +第 10 頁(共 10 頁) 1、編寫一個學生類Student,要求包括以下私有屬性:“學號”、“班級”、“姓名”、“性別”、“年齡”;以下方法:“獲取學號”、“獲取班級”、“獲取姓名”、“獲取性別”、“獲取年齡”、“修改年齡”,要求如下:(1)

26、 學生的年齡必須在10-60之間,否則就是非法年齡。(2) 為學生類Student增加一個toString()方法,能把學生的所有的屬性組合成一個字符串。(3) 寫一個測試程序,打印出學生張明的所有信息(學號008、班級603、姓名張明、性別男、年齡19),創(chuàng)建學生請使用帶參數(shù)的構(gòu)造函數(shù)。(本小題13分)class Studentprivate String studentId;private int classId;private String studentName;private char studentGender;private int studentAge; (正確定義屬性1分)S

27、tring getStudentId()return studentId;int getClassId()return classId;char getStudentGender()return studentGender;String getStudentName()return studentName;int getStudentAge()return studentAge; (正確定義獲取方法1分) boolean setStudentAge(int newAge) (正確定義修改方法2分) if( newAge>10 && newAge<60)student

28、Age = newAge;return true;else第 11 頁(共 10 頁)return false;/(正確定義構(gòu)造函數(shù)3分)Student(String id, int clsId, char gender, String name, int age) studentId = id;classId = clsId;studentName = new String(name);studentAge = age;studentGender = gender;/(正確定義tostring函數(shù)3分)public String toString() (1分) return "學生信息:n"+ "學號 " + this.getStudentId()+ "t班號 " + this.getClassId()+ "t姓名 " + this.getStudentName() + "t性別 "+ (this.getStudentGe

溫馨提示

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

評論

0/150

提交評論