java模擬試卷3及答案_第1頁
java模擬試卷3及答案_第2頁
java模擬試卷3及答案_第3頁
java模擬試卷3及答案_第4頁
java模擬試卷3及答案_第5頁
免費預覽已結束,剩余19頁可下載查看

下載本文檔

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

文檔簡介

1、復習題3一、選擇題1. JDKI供的編譯器是(B)0(A) java.exe(B) javac.exe(。javap.exe(D) javaw.exe2 .以下作為Javari序入口的main方法聲明正確的(C)。(A) public void main(String args口)(B) public int main(String args)(CC public static void main(String args)(D) public static int main(String args口)3 .以下標識符錯誤的是(C)o(A) Public (B)張三(。class(D) main

2、A)。4 .java中定義字符串String s='pzhu”,下面操作可以取得字符串長度的是(A) s.length()(B) s.length (Q s.size()(D) length(s)5 .如下定義數組,操作正確的是( D )0int a=1,2,3;(A) a3=100(B) a0.length (Q a+(D) a.length6 .如下定義二維數組操作錯誤的是(int a=1,2,3;(A) a01=200(B) a0.length (。a11=100(D) a.length7 .以下數據類型存儲空間最大的是( B )0(A) byte (B) long(。float

3、(D) char8 .面向對象的三大特性,不包括如下(A )。(A)異常 (B)封裝 (Q繼承(D)多態9、關于類的定義以下說法錯誤( B )0(A)類定義使用clas業鍵字(B)每個類中必須有一個 main方法(C) 一個包可以包含多個類(D) java中所有類都是Object類的子類10.關于構造方法以下說法錯誤的是(D )。(A)構造方法名必須與類名一致(B)構造方法可以重載(C)構造方法是通過new來調用 (D)每個類都必須編寫構造方法代碼11關于繼承如下說法錯誤的是(C )。(A) Javal!單繼承的(B)通過extend冰定義繼承(C)所有父類方法都可以被 override的 (

4、D)繼承呈現的是is a的關系12 .以下代碼執行的結果是(C )。頁腳內容3System.out.println律枝花學院 pzhu".length();(A)編譯錯誤(B)運行錯誤(C) 9(D) 1413 .用來存儲鍵值對的容器是()(A) ArrayList(B) LinkedList (。HashSet(D) HashMap 14 java中用來拋出異常的關鍵字是(C )(A) try(B) catch(C) throw(D) throws15關于巾nally塊中的代碼,以下說法不正確的是( A )。(A) try塊中的return語句會中斷巾nally塊中語句的執行(B)

5、無論匯nally塊前的語句運行是否產生異常,其中的語句都會執行(C) finally塊中的語句通常中用作資源的清理(D) try塊中的System.exit(體句會中斷finally塊中語句的執行16關于Java字符串說法錯誤的是(B )。(A) Java中的字符串是常量(B) Java中的字符串不是對象(C) Java中的字符串存儲在常量池中(D) 一個字符串定義后的長度不可變17關于JDBCM乍數據庫,以下說法不正確的()。(A) JDBCR能操作MySQL據庫(B) JDBC定義的 Connection,Statement,ResultSet1接口(C ) JDBCM乍數據庫必須要有相應

6、的實現了JDB駿口的驅動(D) JDBCT以通過將客戶端的SQ胺遞給數據庫服務器來實現數據庫的操作18以下程序代碼錯誤的是( B )。abstract class P。class A extends P。abstract class B extends P。(A) P p=new A();(B) P p=new B();(C) A a=new A();(D) P p=new P()void foo()。;19以下C ollection c創建有誤的是( D )。(A) Collection c=new ArrayList()( B ) Collection c=new LinkedList()

7、;(C) Collection c=new HashSet();D) Collection c=new HashMap();20.以下程序代碼錯誤的是( C )0interface IAvoid f();(A) abstract class A implements IA B ) class A implements IAvoid f()(C) class A implements IAvoid f(String s) D) IA a=new IA()void f()二、程序閱讀System.out.println("D");21閱讀程序,并寫出程序運行結果 public

8、class T21 static int init()System.out.println("A");return 0;static boolean test(int i)System.out.println("B");return i<1;static int add(int i)System.out.println("C");return +i;public static void main(String口 args) 22.閱讀程序,并寫出程序運行結果class TObjectTObject()System.out.pri

9、ntln("A");void m(String s)System.out.println("B");void m(int i)System.out.println("C");void m()for(int t=init();test(t);t=add(t)頁腳內容5System.out.println("D");P()頁腳內容#System.out.println("P");abstract void goo();class A extends PA()super();void goo() Sy

10、stem.out.println("A");void foo()System.out.println("F");class B extends Ppublic String toString()return "E"public class T22 public static void main(String口 args) TObject obj=new TObject();System.out.println(obj);obj.m();obj.m(1);obj.m("1");答:輸出結果為:D C B23閱讀程序,

11、并寫出程序運行結果abstract class Pvoid goo() public static void main(String口 args) System.out.println("B");void koo()System.out.println("K");public class T23 答:P A F P K24閱讀程序,并寫出程序運行結果interface ITvoid t1();void t2();abstract class TA implements ITpublic void t1() A a=new A();a.goo();a.fo

12、o();B b=new B();b.koo();System.out.println("A")public void t3() System.out.println("B")class TB extends TA頁腳內容7IT obj=new TB();obj.t1();obj.t2();TA aObj=(TA)obj;aObj.t1();aObj.t3();TB bObj=(TB)obj;bObj.t2(100);答:A E D C A Bpublic void t1() public static void main(String口 args) Sy

13、stem.out.println("C");public void t2() System.out.println("D");答:B C D C B Epublic void t2(int i) System.out.println("E");public class T24 三、程序填空程序一:如下程序測試 Math.random生成隨機數的奇偶比率,仔細閱讀程序和運行結果, 補全空白處的代碼。/*測試Math.random成隨機數的奇偶比率*/public class T25 /* 生成給定數量的0到1000隨機整數,并把生成的隨

14、機存入到一個int數組中* param int count要生成的隨機數量* return int口生成的隨機數存儲數組* /int口 createArray(int count)int number=new intcount; / 創建長度為 count的 int 數組for(int i=0;i<count;i+)int n=(int)(Math.random()*1000); numberi= n ;/在number數組中寫入生成的隨機數System.out.println("number"+i+”="+numberi); return number;

15、/返回生成的數組/*計算給定數組的奇數的比率* param int口 number要計算的數組* return double 奇數的比率* /double calculateOddRate(int口 number)int count=number.length ; /讀取數組元素的個數,即要計算平均數 的整數個數double odd=0; / 奇數計數for(int n:number)if( n%2=1)/如果n是奇數,奇數計數加1odd+;return odd/count;public static void main(String口 args) T25 t=new T25();int口 n

16、umber=t.createArray(100);double oddRate=t.calculateOddRate(number);System.out.println("J數為:"+oddRate*100+"%");System.out.println(偶數為:"+(1-oddRate)*100+"%");運行結果:number0=907./此處省略98行number99=598奇數為:52.0%偶數為:48.0%程序二:以下程序是通過JDB限取數據表Student的基本操作,認真閱讀程序和運行結 果,補全程序的空白處。

17、表:StudentsIDNAMEGENDER頁腳內容112name02女4name04女部分程序如下class Studentprivate int id;private String name;private String gender;public Student(int id, String name, String gender) super();this.id = id; = name;this.gender = gender;/此處省略n行public String toString() return "Student id=" + id +

18、", name=" + name + ", gender=" + gender+ ""public class T30 /*取得數據庫連接*/Connection getConnection()/此處省略n行/*查詢數據庫中所有學生的數據,將一條學生信息記錄轉化成一個Studetn對象,*多個記錄生成多個Student將生成的對象放入到List中,一起返回到*/List<Student> queryAllStudent()List<Student> stuList= new ArrayList<Stude

19、nt>();/ 倉U 建可以存儲 Studen的 ListConnection conn=null;Statement st=null;ResultSet rs=null;try conn=getConnection();st=conn.createStatement(); A®過連接創建 statementrs=st.executeQuery("SELECT ID,NAME,GENDER FROM Students");while( rs.next() /結果是否有記錄Student stu=new Student(rs.getInt("ID&q

20、uot;),rs.getString("NAME"),rs.getString("GENDER");stuList.add(stu);/ 把 stu對象加入至U stuList中 catch (SQLException e) e.printStackTrace();finallytry rs.close();st.close();conn.close();頁腳內容13 catch (SQLException e) return stuList;/*顯示List中的學生*/void showStudent(List<Student> stuLi

21、st)for( §tuden s:stuList) / 指明 s的類型System.out.println(s);public static void main(String口 args) T30 demo=new T30();List<Student> stuList=demo.queryAllStudent();demo.showStudent(stuList);運行結果Student id=2, name=Name02, gende疾Student id=4, name=Name04, gende疾四、基本代碼編寫35、(5分)編寫一個main方法,計算如下數組元素

22、的平均值double source=2,5,9,10,3;36、( 7分)文件名解析器,仔細閱讀如下代碼和運行結果,完成 WindowsFileNameParse 類的代碼,執行后得到給定的運行結果。interface FileNameParsevoid showSourceFileName();String getDiskName();String getFullFileName();String getFileName();String getExtendName();String getDir();class WindowsFileNameParse implements FileNam

23、eParseprivate String fileName;WindowsFileNameParse(String fileName)this.fileName=fileName;public void showSourceFileName()System.out.println("單析文件名:"+this.fileName);/請完成此類的中其他方法的代碼 /public class T36 public static void main(String口 args) FileNameParse fp=new WindowsFileNameParse("d:/My

24、Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java");fp.showSourceFileName();System.out.println(B符:"+fp.getDiskName();System.out.println儀件全名(帶擴展名):"+fp.getFullFileName();System.out.println儀件名(不帶擴展名):"+fp.getFileName();System.out.println儀件擴展名:"+fp.getExtendName();System.out

25、.println("&徑(不帶盤符):"+fp.getDir();運行結果解析文件名:d:/My Documents/MyJob/Pages/2012-2013-2/PageA/src/T37.java盤符:d文件全名(帶擴展名):T37.java文件名(不帶擴展名):T37文件擴展名:java路徑(不帶盤符):/My Documents/MyJob/Pages/2012-2013-2/PageA/src附String類部分的api docpublic int indexOfString str)Returns the index within this strin

26、g of the first occurrence of the specified substring.Examples: "abca”.indexOf("a") return 0Parameters:str - the substring to search for.頁腳內容17Returns: the index of the first occurrenceof the specified substring, or -1 if there is no such occurrence.public int lastIndexOString str)Retu

27、rns the index within this string of the last occurrence of the specified substring. The la occurrence of the empty string "" is considered to occur at the index value this.length().Examples: "abca".lastIndexOf("a") return 3Parameters:str - the substring to search for.Re

28、turns:the index of the last occurrenceof the specified substring, or -1 if there is no such occurrence.public String substring(int beginIndex)Returns a new string that is a substring of this string. The substring begins with the character a the specified index and extends to the end of this string.E

29、xamples:"Harbison”.substring(3) returns "bison""emptiness".substring(9) returns "" (an empty string)Parameters:beginIndex - the beginning index, inclusive.Returns:the specified substring.public String substring(int beginIndex, int endIndex)Returnsa new string that

30、is a substringof this string. The substring beginsat the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring endIndex-beginIndex.Examples:"hamburger”.substring(4, 8) returns "urge""smiles”.substring(1,5) returns "mile&quo

31、t;Parameters:beginIndex - the beginning index, inclusive.endIndex - the ending index, exclusive.Returns:the specified substring.五、設計并編程頁腳內容1937、仔細閱讀給定的代碼和程序運行結果,完方法 size() del()代碼編寫MyLis斐是可以存儲字符串對象的、基于鏈表的List的簡單實現class MyListNode String element;MyListNode nextNode = null;MyListNode(String element) t

32、his.element = element;class MyList private MyListNode firstNode = null;public void add(String element) / 加入字符串到 MyListMyListNode node = new MyListNode(element);if (firstNode = null) firstNode = node; else MyListNode lastNode = firstNode;while (lastNode.nextNode != null) lastNode = lastNode.nextNode;lastNode.nextNode = node;public int size() /返回 MyLis仲

溫馨提示

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

評論

0/150

提交評論