1. 程式人生 > >Java基礎語法試卷5(2017/10/16 第一階段考試)

Java基礎語法試卷5(2017/10/16 第一階段考試)

一、選擇題(以下選項中,ABCD四項中至少有一項或多項是正確的(多選、少選、錯選均沒分),共30題,每題2分,共60分)

1、Java原始檔和編譯後的副檔名分別為( B )
A、.class和.java
B、.java和.class
C、.class和.class
D、.java和.java
2、下列表達式1+2+”aa”+3的值是( C )
A、12aa3
B、12aa
C、3aa3
D、aa3
3、以下的變數定義語句中,合法的是( C )
A、float _*5=123.456F
B、byte $_b1=12345
C、double d = 9.88
D、int long

= 123456L
4、在JAVA中,以下程式碼執行的結果是( C )
public class BWF {
public static long add(double a,double b){
return (long)(a+b);
}
public static void main(String[] args){
Double sum=add(12.3, 4.6);
System.out.println(sum);
}
}
A、16.9
B、17
C、16.0
D、16
5、在Java下列變數名不符合命名規則的是( AC )(多選)
A、int ^abc
B、String _abc
C、double 32b
D、char $abc
6、開發Java程式有三個步驟:A.編譯B.編寫源程式C.執行,開發Java程式的正確順序是( D )
A、ABC
B、ACB
C、BCA
D、BAC
7、給定Java程式碼,如下:編譯執行,結果是( A )
public static void main(String[] args)
{
String s;
System.out.println(“s=”+s);
}
A、編譯錯誤
B、編譯通過,但出現執行時錯誤
C、正常執行,輸出s=null
D、正常執行,輸出s=
8、給定某Java程式片段,該程式執行後,i的輸出結果為( B )

public static void main(String[] args)
{
int i=1;
int j=i++;//j=1
if((i>++j)&&(i++==j))
i+=j;
System.out.println(i);
}
A、1
B、2
C、3
D、4

注:前運算子:先自運算,當次取運算後的值;
後運算子:當次取運算前的值,然後運算,下次碰到該變數取運算後的值。

9、關於Java程式設計規範,說法錯誤的是( D )
A、變數的命名一般要求“見名知義”,提高程式碼的可讀性
B、每行推薦只寫一條語句
C、”{“一般放在結構開始行的最末,”}”與該結構的第一個字母對齊,並單獨佔一行
D、類的命名推薦使用小寫。例如動物類:animal
10、在Java中,以下程式的編譯執行結果是( C )


public class BWF {
static int x;
static boolean z;
public static void main(String args[])
{
System.out.println(x);
System.out.println(z);
}
}
A、執行時錯誤
B、輸出:0
true
C、輸出:0
false
D、輸出:0
0

注:成員變數不賦初始值,值型別預設值為0boolean型別預設值為false

11、下列哪些是Java的關鍵字( C )
A、class main string
B、If double class main string
C、static void public int
D、以上都對

注:main是主函式名,不是java佔用的關鍵字。

12、下列說法中對while和do-while描述錯誤的是( D )
A、While迴圈和do-while迴圈語法不同
B、While迴圈先判斷再執行
C、Do-while迴圈至少執行1次在判斷條件
D、Do-while迴圈是先判斷在迴圈

注:do-while迴圈是先執行一次迴圈體,再判斷。

13、分析如下所示的Java程式碼,編譯執行後的輸出結果是( C )
public class BWF {
public static void main(String args[]){
boolean a=true;
boolean b=false;
if(!a && b){
System.out.print(“!a&&b”);
}else if(!a || b){
System.out.print(“!a||b”);
}else{
System.out.print(“ab”);
}
}
}
A、!a&&b
B、!a||b
C、ab
D、!a||ab
14、在JAVA中,以下功能是查詢Java的陣列num。中的最大值,橫線處的程式碼是( C )
public class BWF {
public static void main(String args[])
{
int[] num = {14,27,9,56,03};
int max=___;
for(int i=1;i<____;i++)
{
if(max

注:第一次i輸出為2

17、下列語句序列執行後,k的值是( D )
int x=6,y=10,k=5;
switch(x % y)
{
case 0: k=x*y;
case 6: k=x/y;
case 12: k=x-y;
default : k=x*y-x;
}
A、60
B、5
C、0
D、54

注:沒有break,所以會一直執行到最後一行default語句。

18、在JAVA中,以下選項會導致死迴圈的是( C )
A、for(int i = 0;i<0;i++){}
B、for(int i = 10;i>0;i–){}
C、for(int i = 0;i<10;i–){}
D、for(int i = 0;i>0;i++){}
19、在Java中如下選項中的程式碼能夠正確執行的有( AB ) (多選)
A、short[] b = new short[10]
B、short b[] = new short[10]
C、short b = new short [10]
D、short b = new short[]
20、在Java的for語句中,對break,continue, return的使用,下列說法不正確的選項為( C )
A、break和return都能結束for迴圈
B、continue只是跳過本次迴圈,不再執行continue關鍵字後面的程式碼,不影響後面的 迴圈次數
C、break、return結束for迴圈的區別在於,break是結束方法的執行,return是結束for 迴圈
D、break、return不僅可以在for迴圈中使用,也可以在switch控制語句中使用
21、在Java中,以下程式碼的輸出結果是( C )
public class BWF {
public static void main(String args[]){
int i=0;
while(i<5){
if(i>3)
break;
i++;
if(i<3){
System.out.println(“i小於3”);
continue;
}
System.out.println(“Final”);
}
}
}
A、i小於3
i小於3
Final,Final
B、i小於3
i小於3
C、i小於3
i小於3
Final
Final
D、編譯錯誤
22、在JAVA中,以下程式碼的執行結果是( D )
public class BWF {
public static void main(String args[]){
int[] num=new int[10];
for(int i=0;i

注:注意沒有break,所以會一直執行到最後一行。

26、下面Java程式碼的橫線處不可以填寫的程式碼是( B )
public static void main(String args[])
{
int a=_(int)__10/3.1
__;
}
A、10%3
B、10/3.1
C、10*-3
D、3-10
27、在Java中,變數命名規範說法正確的是( B )
A、變數由字母、下劃線、數字、$符號隨意組成
B、變數不能以數字作為開頭
C、A和a在Java中是同一個變數
D、不同型別的變數,可以起相同的名字
28、在Java中,下面不屬於Java的基本資料型別的是( D )
A、float
B、int
C、boolean
D、String
29、在Java中,以下賦值語句正確的是( BC )(多選)
A、int num1=(int)”12”;
B、int num2=(int)12.0;
C、double num3=2d;
D、double num4=5;

注:float型別中的f/F必須加,double型別中的d可加可不加。

30、在Java中,命名類時要遵守一些規則,以下說法錯誤的是( C )
A、不能使用Java中的關鍵字
B、不能包含任何嵌入的空格或點號
C、不能包含下劃線和“$”符
D、不能以數字開頭

二、程式設計題(程式設計實現下列問題,共4題,每題10分,共40分)
1、有一對兔子,從出生後第3個月起每個月都生一對兔子,小兔子長到第三個月後每個月 又生一對兔子,假如兔子都不死。問第20個月後兔子總數為多少?(提示://兔子的規律為數列1,1,2,3,5,8,13,21.…)
迴圈:

public static void main(String[] args){
        //定義本月、上月、上上月的兔子對數
        int thisMonth = 0;
        int lastMonth1 = 0;
        int lastMonth2 = 0;
        //觀察規律得出除了第一、二個月都是1對外,
        //本月兔子對數為上月+上上月,做迴圈
        for (int i = 1; i <= 20; i++) {
            if (i == 1 || i == 2) {
                thisMonth = 1;
                lastMonth1 = 1;
            } else {
                thisMonth = lastMonth1 + lastMonth2;
            }
            //每輪迴圈把上月的賦給上上月,本月的賦給上月
            lastMonth2 = lastMonth1;
            lastMonth1 = thisMonth;
        }
        System.out.println(thisMonth * 2);
    }

遞迴函式:

    public static long sum(int month) {
        // 定義兔子總數sum,第一、二個月sum為1*2,
        // 第三個月開始本月的兔子數為上月+上上月,使用遞迴函式
        long sum = 0;
        if (month == 1 || month == 2) {
            return sum = 1 * 2;
        } else {
            return sum = (sum(month - 1) + sum(month - 2)) * 2;
        }
    }

2、一球從100米高度自由落下,每次落地後反跳回原高度的一半(即一回合);再落下,求它在第10次落下時彈到最高處,共經過多少米?第10次反彈多高?
迴圈:

public static void main(String[] args) {
        // 定義總距離s,彈起距離t,落下距離luoxia
        double s = 100;
        double t = 100;
        double luoxia = 0;
        for (int i = 1; i <= 10; i++) {
            t = t / 2;
            // 從第二次開始,需要加落下的距離
            if (i > 1) {
                luoxia = t * 2;
            }
            s = s + t + luoxia;
        }
        System.out.println(s);
        System.out.println(t);
    }

遞迴函式:

    public static void main(String[] args) {
        System.out.println(distanceS(10));
        System.out.println(distance(10) / 3);
    }

    // 第十次的距離
    public static double distance(int cishu) {
        double d1 = 150;
        if (cishu == 1) {
            return d1 = 150;
        } else {
            return d1 = distance(cishu - 1) / 2;
        }
    }

    // 總距離
    public static double distanceS(int cishu) {
        double dS = 0;
        if (cishu == 1) {
            return dS = distance(1);
        } else {
            return dS = distance(cishu) + distanceS(cishu - 1);
        }
    }

3、給出年月日,計算改日是該年的第幾天。

public static void main(String[] args) {
        System.out.println(days(2017, 3, 1));
    }

    public static int days(int year, int month, int day) {
        int days = 0;
        for (int i = 1; i < month; i++) {
            switch (i) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days += 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days += 30;
                break;
            case 2: {
                if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0) {
                    days += 29;
                    break;
                } else {
                    days += 28;
                    break;
                }
            }
            }
        }
        return days = days + day;
    }

4、在控制檯輸入3個整數a,b,c,按大小順序輸出。

public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int[] a = new int[3];
        // 迴圈三次把控制檯輸入的數賦給陣列a
        for (int i = 0; i < 3; i++) {
            a[i] = scan.nextInt();
        }
        // 使用氣泡排序對陣列a排序
        for (int i = 1; i < a.length; i++) {
            for (int j = 0; j < a.length - i; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(a));
    }