Java數(shù)組練習題(帶答案)Word版_第1頁
Java數(shù)組練習題(帶答案)Word版_第2頁
Java數(shù)組練習題(帶答案)Word版_第3頁
Java數(shù)組練習題(帶答案)Word版_第4頁
Java數(shù)組練習題(帶答案)Word版_第5頁
已閱讀5頁,還剩7頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、傳播優(yōu)秀Word版文檔 ,希望對您有幫助,可雙擊去除!一 填空題1) 數(shù)組的元素通過 下標 來訪問,數(shù)組Array的長度為 Array.length 。2) 數(shù)組復制時,=將一個數(shù)組的 引用 傳遞給另一個數(shù)組。3) JVM將數(shù)組存儲在 棧 (堆或棧)中。4) 數(shù)組的二分查找法運用的前提條件是數(shù)組已經(jīng) 排序 。5) Java中數(shù)組的下標的數(shù)據(jù)類型是 整型 。6) 數(shù)組最小的下標是 0 。7) arraycopy()的最后一個參數(shù)指明 復制元素的個數(shù) 。8) 向方法傳遞數(shù)組參數(shù)時,傳遞的是數(shù)組的 引用 。9) 數(shù)組初始化包括 數(shù)組的申明,創(chuàng)建和初始化 。10) 數(shù)組下標訪問超出索引范圍時拋出 數(shù)組

2、越界 異常11) 浮點型數(shù)組的默認值是 0.0f 。12) 數(shù)組創(chuàng)建后其大小 不能 改變。二 選擇題1. 下面錯誤的初始化語句是_ABD_A. char str=hello;B. char str100=hello;C. char str=h,e,l,l,o;D. char str=hello;2. 定義了一維int型數(shù)組a10后,下面錯誤的引用是_B_A. a0=1;B. a10=2;C. a0=5*2;D. a1=a2*a0;3. 下面的二維數(shù)組初始化語句中,正確的是_A. float b22=0.1,0.2,0.3,0.4;B. int a=1,2,3,4;C. int a2= 1,2,

3、3,4;D. float a22=0;4. 引用數(shù)組元素時,數(shù)組下標可以是_D_A. 整型常量 B. 整型變量 C. 整型表達式 D. 以上均可5. 定義了int型二維數(shù)組a67后,數(shù)組元素a34前的數(shù)組元素個數(shù)為_A. 24 B. 25 C. 18 D. 176. 下列初始化字符數(shù)組的語句中,正確的是_B_A. char str5=hello;B. char str=h,e,l,l,o,0;C. char str5=hi;D. char str100=;7. 數(shù)組在Java中儲存在 C 中A. 棧 B. 隊列 C. 堆 D. 鏈表8. 下面程序的運行結(jié)果是_main() int a=1,2,

4、3,4,5,6;System.out.printf(%d, a11);A. 3 B. 4 C. 5 D. 69. 下面程序的運行結(jié)果是_C_main() int x=30;int numbers=new intx;x=60;System.out.println(numbers.length);A. 60 B. 20 C. 30 D. 5010. 下面程序的運行結(jié)果是_BDF_main() char s1=ABCDEF.toCharArray();int i=0;while(s1i+!=0)System.out.println(s1i+);A. ABCDEF B. BDF C. ABCDE D.

5、 BCDE11. 下面不是創(chuàng)建數(shù)組的正確語句C A.floatf=newfloat66;B.floatf=newfloat6; C.floatf=newfloat6;D.floatf=newfloat6;12. 下面不是數(shù)組復制方法的是(C) A. 用循環(huán)語句逐個復制數(shù)組 B. 用方法arraycopy C. 用=進行復制 D. 用clone方法13. 數(shù)組a的第三個元素表示為D A. a(3) B. a3 C.a(2) D. a214. 當訪問無效的數(shù)組下標時,會發(fā)生B A. 中止程序 B. 拋出異常 C. 系統(tǒng)崩潰 D. 直接跳過15. 使用arraycopy()方法將數(shù)組a復制到b正確的

6、是A A. arraycopy(a,0,b,0,a.length) B. arraycopy(a,0,b,0,b.length) C. arraycopy(b,0,a,0,a.length) D. arraycopy(a,1,b,1,a.length)16. 關(guān)于數(shù)組默認值,錯誤的是 B A. char-u0000 B. Boolean-true C. float-0.0f D. int- 017. 關(guān)于數(shù)組作為方法的參數(shù)時,向方法傳遞的是 A A. 數(shù)組的引用 B. 數(shù)組的棧地址 C. 數(shù)組自身 D. 數(shù)組的元素18. 關(guān)于數(shù)組復制,下列說法錯誤的是AC A. =可以實現(xiàn)數(shù)組復制 B. 運用

7、循環(huán)語句進行數(shù)組復制必須兩個數(shù)組長度相同 C. arraycopy()方法沒有給目標數(shù)組分配內(nèi)存空間 D. 數(shù)組復制是數(shù)組引用的傳遞19. 下列語句會造成數(shù)組new int10越界是D A. a0 += 9; B. a9=10;C. a9 D. for(int i=0;i=10;i+) ai+;20. 在JDK環(huán)境下編譯JAVA源程序使用的命令是(B ) A.java B.javac C.jvm D.tomcatD. 子類不能使用父類的構(gòu)造方法21. main方法是javaApplication程序執(zhí)行的入口點。關(guān)于main方法放入方法以下合法的是( )A.publicstaticvoidma

8、in(); B.publicstaticvoidmain(Stringargs) C.publicstaticintmain(Stringarg)D.publicvoidmain(Stringarg)22. 執(zhí)行完代碼int x=newint25;后以下( A )說明正確的A. x24為0 B. x24未定義 C. x25為0 D. x0為空23. 關(guān)于數(shù)組排序方法,錯誤的是 C A. 選擇排序 B. 插入排序 C. 二分排序 D. 用arrays.sort( )排序24. 關(guān)于char類型的數(shù)組,說法正確的是 D A. 其數(shù)組的默認值是A B. 可以僅通過數(shù)組名來訪問數(shù)組 C. 數(shù)組不能轉(zhuǎn)換

9、為字符串 D. 可以存儲整型數(shù)值25. 對于數(shù)組a10,下列表示錯誤的是B A. a0 B. a(0) C. a9 D. a126. 下列數(shù)組聲明,下列表示錯誤的是 A. int a B. int aC. int a D. inta三、是非題1.下標用于指出數(shù)組中某個元素位置的數(shù)字。( )2.把數(shù)組中元素按某種順序排列的過程叫做查找。( )3.確定數(shù)組中是否含有某個關(guān)鍵字的過程叫做排序。( )4.一個數(shù)組可以存放許多不同類型的數(shù)值。( )5.數(shù)組的下標通常是float型。( )6.數(shù)組的某個元素被傳遞給一個方法并被該方法修改,當被調(diào)用方法執(zhí)行完畢時,這個元素中含有修改過的數(shù)值。( )7.數(shù)組可

10、以聲明為任何數(shù)據(jù)類型。( )8.數(shù)組由具有一名字和相同類型的一組連續(xù)內(nèi)存單元構(gòu)成。( )9.在數(shù)組聲明中可以用等號及一個逗號分隔的初始值表初始化數(shù)組元素,該數(shù)組大小只能由用戶來決定。( )10.將一個數(shù)組傳遞給一個方法,必須加在數(shù)組名后加方括號。( )11.Java語言中的數(shù)組元素下標總是從0開始,下標可以是整數(shù)或整型表達式。( )12.下面這條語句正確嗎?( ) double myList; myList = 1.9, 2.9, 3.5, 4.6;14. 數(shù)組中有l(wèi)ength()這個方法,如array.length()表示數(shù)組array中元素的個數(shù)( )15.下面這條語句正確嗎?( ) in

11、t t32 = 1,2,3,4,5,6;16.數(shù)組聲明后其大小固定。( )17.設有整型數(shù)組的定義:int A.=new int8; ,則a.length的值為7。( )18. 數(shù)組一旦創(chuàng)建,其大小不能再改變。( )19.用任何方式創(chuàng)建數(shù)組時,都必須指定數(shù)組的長度。( )20.聲明數(shù)組時,要指定數(shù)組長度,以便為數(shù)組分配內(nèi)存。( )四、簡答題1. 如何聲明和創(chuàng)建一個一維數(shù)組? 2. 如何訪問數(shù)組的元素?3.數(shù)組下標的類型是什么?最小的下標是什么?一維數(shù)組a的第三個元素如何表示?4.數(shù)組越界訪問會發(fā)生什么錯誤?怎樣避免該錯誤?5.給方法傳遞數(shù)組參數(shù)與傳遞基本數(shù)據(jù)類型變量的值有何不同?6.復制數(shù)組有

12、哪些方法?8.聲明數(shù)組變量會為數(shù)組分配內(nèi)存空間嗎?為什么?五、程序題1.有一個整數(shù)數(shù)組,其中存放著序列1,3,5,7,9,11,13,15,17,19。請將該序列倒序存放并輸出。1) public class Test 2) public static void main(String args) 3) int a = 1,3,5,7,9,11,13,15,17,19;4) int t;5) System.out.println(數(shù)組的初始狀態(tài)為:);6) for (int i=0; i a.length; i+)7) System.out.print( + ai);8) System.out

13、.println();9) 10) for (int i=0; i a.length/2; i+) 11) t = ai;12) ai = aa.length-i-1;13) aa.length-i-1=t;14) 15) 16) System.out.println(數(shù)組逆序存放后的狀態(tài)為:);17) for (int i=0; i a.length; i+)18) System.out.print( + ai);19) 20) 2.編寫一個程序,提示用戶輸入學生數(shù)量 姓名和他們的成績,并按照成績的降序來打印學生的姓名。1) public class exercise16 2) public

14、 static void main(String args) 3) String numberString = 4) JOptionPane.showInputDialog(Enter the number of students);5) int numberOfStudents = Integer.parseInt(numberString);6) 7) String names = new StringnumberOfStudents;8) double scores = new doublenumberOfStudents;9)10) for (int i = 0; i = 1; i-)

15、 17) double currentMax = scores0;18) int currentMaxIndex = 0;19)20) for (int j = 1; j = i; j+) 21) if (currentMax = 0; i-) 37) System.out.println(namesi + t + scoresi);38) 39) 40) 3.編寫一個程序,使它能夠讀入10個整數(shù),并且存儲其中互不相同的數(shù),最后將這10個數(shù)輸出。1) import javax.swing.JOptionPane;2)3) public class exercise5 4) private st

16、atic int j = 0;5) 6) public static void main(String args) 7) int arrayInt = new int10;8) int i = 0;9)10) do 11) String numberString = JOptionPane12) .showInputDialog(Enter a number: );13)14) int number = Integer.parseInt(numberString);15)16) if (isNotHave(arrayInt, number) 17) arrayInti = number;18)

17、 i+;19) j+;20) 21) else22) 23) do 24) numberString = JOptionPane25) .showInputDialog(This number is exit,enter a another number: );26) number = Integer.parseInt(numberString);27) while (!isNotHave(arrayInt, number);28) 29) arrayInti = number;30) i+;31) j+;32) 33) while (i arrayInt.length);34)35) Str

18、ing output = ;36) for (int k : arrayInt) 37) output += k + ;38) 39)40) JOptionPane.showMessageDialog(null, The elements of arrayInt is 41) + output, output arrayInt, JOptionPane.INFORMATION_MESSAGE);42) 43)44) public static boolean isNotHave(int arrayInt, int n) 45) for (int i = 0; i j; i+) 46) if (

19、arrayInti = n)47) return false;48) 49) return true;50) 51) 4.先對數(shù)組1,3,9,5,6,7,1,5,4,8進行排序,然后二分查找法找出數(shù)組中的元素8,標出其排序后其下標的位置。1) public class Test 2) public static void main(String args) 3) int numbers = 1,3,9,5,6,7,1,5,4,8;4) java.util.Arrays.sort(numbers);5)6) System.out.println(排序后的數(shù)組為:);7) for (int i=0

20、; i low) mid = (mid = (low + high) / 2) ? 22) (mid + 1) : (low + high) / 2);23) if (key = 0);15)16) double average = (sum - scorescount) / (count - 1);17)18) int numOfAbove = 0;19) int numOfBelow = 0;20) for (int i = 0; i = average)22) numOfAbove+;23) else24) numOfBelow+;25)26) System.out.println(Av

21、erage is + average);27) System.out.println(Number of scores above or equal to the average 28) + numOfAbove);29) System.out.println(Number of scores below the average 30) + numOfBelow);31)32) System.exit(0);33) 34) 6.編寫一個程序,生成0-9之間的100個隨機整數(shù)并且顯示每一個數(shù)的個數(shù)。1) public class exercise7 2) public static void m

22、ain(String args) 3) int numbers = new int100;4) int counts = new int10;5) 6) int k;7) for (int i = 0; i 100; i+)8) 9) numbersi = (int)(Math.random() * 10);10) countsnumbersi+;11) 12) 13) System.out.println(the 100 numbers is :);14) for(int i = 1; i = numbers.length; i+)15) 16) if (i % 10 != 0)17) Sy

23、stem.out.print(numbersi-1 + );18) else19) System.out.println(numbersi-1);20) 21) 22) System.out.println(the counts of each number is :);23) for(int i = 1; i = counts.length; i+)24) System.out.print(countsi-1 + );25) 26) 7. 求平均值和方差。利用 mean(int numbers)求平均值,利用 deviation(int numbers)求標準差。1) import java

24、.util.Scanner;2) public class MeanDeviation 3) public static void main(String args) 4) Scanner in = new Scanner(System.in);5) 6) System.out.print(輸入數(shù)組的長度:);7) int n = in.nextInt();8) 9) System.out.print(輸入數(shù)組元素:);10) int array = new intn;11) for (int i = 0; i array.length; i+)12) 13) arrayi = in.nextInt();14) 15) 16) System.out.print(The mean is: + mean(array) + n17) + The deviation is: + deviation(array);18) 19)20) publi

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 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

提交評論