Java-基础题目集-Chapter 7 Single-Dimensional Arrays

目录

一.选择题

二.编程题

一.选择题

1.Assume int[] scores = {1, 20, 30, 40, 50}, what is the output of System.out.println(java.util.Arrays.toString(scores))B

A.{1, 20, 30, 40, 50}

B.[1, 20, 30, 40, 50]

C.{1 20 30 40 50}

D.[1 20 30 40 50]

解析:假设 int[] scores = {1, 20, 30, 40, 50},则系统输出是什么?

2.What is the representation of the third element in an array called a?A

A.a[2]

B.a(2)

C.a[3]

D.a(3)

解析:数组中第三个元素的表示形式是什么?

3.If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ___(B__.

A.3.4

B.2.0

C.3.5

D.5.5

E.undefined

解析:如果声明数组 double[] list = {3.4, 2.0, 3.5, 5.5},则list [1] 为 ___。

4.If you declare an array double[] list = {3.4, 2.0, 3.5, 5.5}, the highest index in array list is _____(D)_____.

A.0

B.1

C.2

D.3

E.4

解析:如果声明数组 double[] list = {3.4, 2.0, 3.5, 5.5},则数组列表中的最高索引为 。

5.How many elements are in array double[] list = new double[5]?(B

A.4

B.5

C.6

D.0

解析:数组double[]list中有多少元素?

6.What is the correct term for numbers[99]?(D

A.index

B.index variable

C.indexed variable

D.array variable

E.array

解析:正确术语是什么:number[99];

注意数组变量与数组的区别:数组变量是一个地址,数组是一系列连续分布的内存。

7.Analyze the following code.(C

public class Test {
  public static void main(String[] args) {
    int[] x = new int[3];
    System.out.println("x[0] is " + x[0]);
  }
}

A.The program has a compile error because the size of the array wasn't specified when declaring the array.

B.The program has a runtime error because the array elements are not initialized.

C.The program runs fine and displays x[0] is 0.

D.The program has a runtime error because the array element x[0] is not defined.

解析:A.程序出现编译错误,因为在声明数组时未指定数组的大小。

B.程序出现运行时错误,因为数组元素未初始化。

C.程序运行正常,显示 x[0] 为 0。

D.程序出现运行时错误,因为数组元素 x[0] 未定义。

8.What would be the result of attempting to compile and run the following code?(E

public class Test {
  public static void main(String[] args) {
    double[] x = new double[]{1, 2, 3};
    System.out.println("Value is " + x[1]);
  }
}

A.The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by {1, 2, 3}.

B.The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[3]{1, 2, 3};

C.The program has a compile error because the syntax new double[]{1, 2, 3} is wrong and it should be replaced by new double[]{1.0, 2.0, 3.0};

D.The program compiles and runs fine and the output "Value is 1.0" is printed.

E.The program compiles and runs fine and the output "Value is 2.0" is printed.

解析:A.程序出现编译错误,因为语法new double[]{1, 2, 3} 错误,应将其替换为 {1, 2, 3}。

B.程序有编译错误,因为语法new double[]{1, 2, 3}是错误的,它应该被new double[3]{1,2,3}替换;

C.程序有编译错误,因为语法new double[]{1, 2, 3} 是错误的,它应该被new double[]{1.0, 2.0, 3.0} 替换;

D.程序编译并运行正常,并输出“值为1.0”。

E.程序编译并运行正常,并打印输出“值为2.0”。

9.Assume int[] t = {1, 2, 3, 4}. What is t.length?(C

A.0

B.3

C.4

D.5

10.What is the output of the following code?(B)

double[] myList = {1, 5, 5, 5, 5, 1};
double max = myList[0];
int indexOfMax = 0;
for (int i = 1; i < myList.length; i++) {
  if (myList[i] > max) {
    max = myList[i];
    indexOfMax = i;
  }
}
System.out.println(indexOfMax);

A.0

B.1

C.2

D.3

E.4

11.Analyze the following code:(C

public class Test {
  public static void main(String[] args) {
    int[] x = new int[5];
    int i;
    for (i = 0; i < x.length; i++)
      x[i] = i;
    System.out.println(x[i]);
  }
}

A.The program displays 0 1 2 3 4.

B.The program displays 4.

C.The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.

D.The program has a compile error because i is not defined in the last statement in the main method.

解析:A.程序显示 0 1 2 3 4。

B.程序显示 4。

C.程序具有运行时错误,因为 main 方法中的最后一个语句会导致数组索引超出边界异常。

D.程序有编译错误,因为 i 未在 main 方法的最后一个语句中定义。

12.Analyze the following code:(C

public class Test {
  public static void main(String[] args) {
    double[] x = {2.5, 3, 4};
    for (double value: x)
      System.out.print(value + " ");
  }
}

A.The program displays 2.5, 3, 4

B.The program displays 2.5 3 4

C.The program displays 2.5 3.0 4.0

D.The program displays 2.5, 3.0 4.0

E.The program has a syntax error because value is undefined.

13.What is the output of the following code?(D

int[] myList = {1, 2, 3, 4, 5, 6};

for (int i = myList.length - 2; i >= 0; i--) {
    myList[i + 1] = myList[i];
}

for (int e: myList)
    System.out.print(e + " ");

A.1 2 3 4 5 6

B.6 1 2 3 4 5

C.6 2 3 4 5 1

D.1 1 2 3 4 5

E.2 3 4 5 6 1

14.What is output of the following code:(B

public class Test {
  public static void main(String[] args) {
    int[] x = {120, 200, 016};
    for (int i = 0; i < x.length; i++)
      System.out.print(x[i] + " ");
  }
}

A.120 200 16

B.120 200 14

C.120 200 20

D.016 is a compile error. It should be written as 16.

解析:016是八进制,转为十进制是14;

15.What is output of the following code:(D

public class Test {
  public static void main(String[] args) {
    int list[] = {1, 2, 3, 4, 5, 6};

    for (int i = 1; i < list.length; i++)
      list[i] = list[i - 1];

    for (int i = 0; i < list.length; i++)
      System.out.print(list[i] + " ");
  }
}

A.1 2 3 4 5 6

B.2 3 4 5 6 6

C.2 3 4 5 6 1

D.1 1 1 1 1 1

16.In the following code, what is the output for list2?(C

public class Test {
  public static void main(String[] args) {
    int[] list1 = {1, 2, 3};
    int[] list2 = {1, 2, 3};
    list2 = list1;
    list1[0] = 0; list1[1] = 1; list2[2] = 2;

    for (int i = 0; i < list2.length; i++)
      System.out.print(list2[i] + " ");
  }
}

A.1 2 3

B.1 1 1

C.0 1 2

D.0 1 3

解析:对于数组的定义,初始化时用new与不用new 没区别,只是两种方式罢了,因为数组是引用数据类型,建立对象时,无论用不用new,数组实体都是放在堆内存中,引用变量放在栈内存。

改变list2也改变list1。

17.In the following code, what is the output for list1?(C)

public class Test {
  public static void main(String[] args) {
    int[] list1 = {1, 2, 3};
    int[] list2 = {1, 2, 3};
    list2 = list1;
    list1[0] = 0; list1[1] = 1; list2[2] = 2;

    for (int i = 0; i < list1.length; i++)
      System.out.print(list1[i] + " ");
  }
}

A.1 2 3

B.1 1 1

C.0 1 2

D.0 1 3

18.Analyze the following code:(A

public class Test {
  public static void main(String[] args) {
    int[] x = {1, 2, 3, 4};
    int[] y = x;

    x = new int[2];

    for (int i = 0; i < y.length; i++)
      System.out.print(y[i] + " ");
  }
}

A.The program displays 1 2 3 4

B.The program displays 0 0

C.The program displays 0 0 3 4

D.The program displays 0 0 0 0

解析:y和x指向同一个数组,打印y就是打印x。

19.Analyze the following code:(B

public class Test {
  public static void main(String[] args) {
    int[] x = {1, 2, 3, 4};
    int[] y = x;

    x = new int[2];

    for (int i = 0; i < x.length; i++)
      System.out.print(x[i] + " ");
  }
}

A.The program displays 1 2 3 4

B.The program displays 0 0

C.The program displays 0 0 3 4

D.The program displays 0 0 0 0

解析:x创建后又改指向另一个大小为2个int的数组,默认初始化0 0.

20.Analyze the following code:(C

public class Test {
  public static void main(String[] args) {
    final int[] x = {1, 2, 3, 4};
    int[] y = x;

    x = new int[2];

    for (int i = 0; i < y.length; i++)
      System.out.print(y[i] + " ");
  }
}

A.The program displays 1 2 3 4

B.The program displays 0 0

C.The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.

D.The elements in the array x cannot be changed, because x is final.

解析:final修饰基本数据类型表示改数值不能被改变,final修饰引用数据类型表示该数据地址值不能改变,但数值可以改变。

21.Analyze the following code.(C)

int[] list = new int[5];
list = new int[6];

A.The code has compile errors because the variable list cannot be changed once it is assigned.

B.The code has runtime errors because the variable list cannot be changed once it is assigned.

C.The code can compile and run fine. The second line assigns a new array to list.

D.The code has compile errors because you cannot assign a different size array to list.

解析:A.代码存在编译错误,因为变量list一旦赋值就无法更改。

B.代码存在运行时错误,因为变量list一旦赋值就无法更改。

C.代码可以编译并运行正常。第二行指定list的新数组。

D.代码存在编译错误,因为您无法为list分配不同大小的数组。

22.Analyze the following code:(C

public class Test {
  public static void main(String[] args) {
    int[] a = new int[4];
    a[1] = 1;
    a = new int[2];
    System.out.println("a[1] is " + a[1]);
  }
}

A.The program has a compile error because new int[2] is assigned to a.

B.The program has a runtime error because a[1] is not initialized.

C.The program displays a[1] is 0.

D.The program displays a[1] is 1

解析:开辟数组a后初始化a[1]为1,但是随后改变a指向新开辟的2个int大小的数组,默认初始化为0 0,故输出a[1]为0。

23.The __________ method copies the sourceArray to the targetArray.(D

A.System.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);

B.System.copyarrays(sourceArray, 0, targetArray, 0, sourceArray.length);

C.System.arrayCopy(sourceArray, 0, targetArray, 0, sourceArray.length);

D.System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

解析:将源数组复制到目标数组。

正确格式:

Arrays.copyOf(dataType[] srcArray,int length);
Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)
System.arraycopy(dataType[] srcArray,int srcIndex,int destArray,int destIndex,int length)

24.

When you pass an array to a method, the method receives __________.

A.a copy of the array

B.a copy of the first element

C.the reference of the array

D.the length of the array

解析:C

25.Show the output of the following code:(D

public class Test {
  public static void main(String[] args) {
    int[] x = {1, 2, 3, 4, 5};
    increase(x);

    int[] y = {1, 2, 3, 4, 5};
    increase(y[0]);

    System.out.println(x[0] + " " + y[0]);
  }

  public static void increase(int[] x) {
    for (int i = 0; i < x.length; i++)
      x[i]++;
  }

  public static void increase(int y) {
    y++;
  }
}

A.0 0

B.1 1

C.2 2

D.2 1

E.1 2

解析:第一个increase方法改变了x数组的数值,第二个increase方法中的y只是临时变量,方法结束后,y的值并未发生改变

26.Do the following two programs produce the same result?(A

Program I:

public class Test {
  public static void main(String[] args) {
    int[] list = {1, 2, 3, 4, 5};
    reverse(list);
    for (int i = 0; i < list.length; i++)
      System.out.print(list[i] + " ");
  }

  public static void reverse(int[] list) {
    int[] newList = new int[list.length];

    for (int i = 0; i < list.length; i++)
      newList[i] = list[list.length - 1 - i];

    list = newList;
  }
}

Program II:

public class Test {
  public static void main(String[] args) {
    int[] oldList = {1, 2, 3, 4, 5};
    reverse(oldList);
    for (int i = 0; i < oldList.length; i++)
      System.out.print(oldList[i] + " ");
  }

  public static void reverse(int[] list) {
    int[] newList = new int[list.length];

    for (int i = 0; i < list.length; i++)
      newList[i] = list[list.length - 1 - i];

    list = newList;
  }
}

A.Yes

B.No

解析:两个程序结果其实都为:1 2 3 4 5 ;因为reverse方法中改变的是newlist的数值,但最后打印的还是oldlist的数值。

27.Analyze the following code:(A

public class Test {
  public static void main(String[] args) {
    int[] oldList = {1, 2, 3, 4, 5};
    reverse(oldList);
    for (int i = 0; i < oldList.length; i++)
      System.out.print(oldList[i] + " ");
  }

  public static void reverse(int[] list) {
    int[] newList = new int[list.length];

    for (int i = 0; i < list.length; i++)
      newList[i] = list[list.length - 1 - i];

    list = newList;
  }
}

A.The program displays 1 2 3 4 5.

B.The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException.

C.The program displays 5 4 3 2 1.

D.The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

28.Analyze the following code:(C

public class Test1 {
  public static void main(String[] args) {
    xMethod(new double[]{3, 3});
    xMethod(new double[5]);
    xMethod(new double[3]{1, 2, 3});
  }

  public static void xMethod(double[] a) {
    System.out.println(a.length);
  }
}

A.The program has a compile error because xMethod(new double[]{3, 3}) is incorrect.

B.The program has a compile error because xMethod(new double[5]) is incorrect.

C.The program has a compile error because xMethod(new double[3]{1, 2, 3}) is incorrect.

D.The program has a runtime error because a is null.

29.The JVM stores the array in an area of memory, called _______, which is used for dynamic memory allocation where blocks of memory are allocated and freed in an arbitrary order.(B

A.stack

B.heap

C.memory block

D.dynamic memory

解析:JVM开辟数组是在堆上。无论用不用new,数组实体都是放在堆内存中。

30.When you return an array from a method, the method returns _____(C)_____.

A.a copy of the array

B.a copy of the first element

C.the reference of the array

D.the length of the array

31.Suppose a method p has the following heading:

public static int[] p()

What return statement may be used in p()?(D

A.return 1;

B.return {1, 2, 3};

C.return int[]{1, 2, 3};

D.return new int[]{1, 2, 3};

32.If a key is not in the list, the binarySearch method returns ____(C)_____.

A.insertion point

B.insertion point - 1

C.-(insertion point + 1)

D.-insertion point

解析:

方法的返回值有几种:

1.找到的情况下:如果key在数组中,则返回搜索值的索引。

2.找不到的情况下:

 [1] 搜索值不是数组元素,且在数组范围内,从1开始计数,得“ - 插入点索引值”;
 [2] 搜索值是数组元素,从0开始计数,得搜索值的索引值;
 [3] 搜索值不是数组元素,且大于数组内元素,索引值为 – (length + 1);
 [4] 搜索值不是数组元素,且小于数组内元素,索引值为 – 1。

33.The _____(C)_____ method sorts the array scores of the double[] type.

A.java.util.Arrays(scores)

B.java.util.Arrays.sorts(scores)

C.java.util.Arrays.sort(scores)

D.Njava.util.Arrays.sortArray(scores)

34.Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return?(D

A.0

B.-1

C.1

D.2

E.-2

35.Assume int[] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return?(E

A.0

B.-1

C.1

D.2

E.-2

36.How can you get the word "abc" in the main method from the following call?

java Test "+" 3 "abc" 2   C

A.args[0]

B.args[1]

C.args[2]

D.args[3]

37.Given the following program:(C

public class Test {
  public static void main(String[] args) {
    for (int i = 0; i < args.length; i++) {
      System.out.print(args[i] + " ");
    }
  }
}

What is the output, if you run the program using

java Test 1 2 3

A.3

B.1

C.1 2 3

D.1 2

38.Which code fragment would correctly identify the number of arguments passed via the command line to a Java application, excluding

the name of the class that is being invoked?(A

A.int count = args.length;

B.int count = args.length - 1;

C.int count = 0; while (args[count] != null) count ++;

D.int count=0; while (!(args[count].equals(""))) count ++;

39.Which correctly creates an array of five empty Strings?(B

A.String[] a = new String [5];

B.String[] a = {"", "", "", "", ""};

C.String[5] a;

D.String[ ] a = new String [5]; for (int i = 0; i < 5; a[i++] = null);

40.Identify the problems in the following code.(D

public class Test {
  public static void main(String argv[]) {
    System.out.println("argv.length is " + argv.length);
  }
}

A.The program has a compile error because String argv[] is wrong and it should be replaced by String[] args.

B.The program has a compile error because String args[] is wrong and it should be replaced by String args[].

C.If you run this program without passing any arguments, the program would have a runtime error because argv is null.

D.If you run this program without passing any arguments, the program would display argv.length is 0.

解析:如果在未传递任何参数的情况下运行此程序,则该程序将显示 argv.length 为 0。

二.编程题

1.Print distinct numbers(输出不同的数)

Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once).

输入格式:

输入在一行中给出10个整数。

输出格式:

第一行输出互不相同的整数的个数;

第二行输出互不相同的整数,两个整数之间用一个空格隔开。

输入样例:

1 2 3 2 1 6 3 4 5 2

输出样例:

6
1 2 3 6 4 5 

答案:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int a[]=new int[10];Scanner sc=new Scanner(System.in);

        for(int i=0;i<10;i++){
            a[i]=sc.nextInt();
        }
        int result=0,k=0,b[]=new int[a.length];
        for(int i=0;i<10;i++){
            int sum=0;
            for(int j=0;j<10;j++){
                if(a[i]==a[j])sum++;
            }
            boolean is=true;
            if(sum>1){
                for(int l=0;l<i;l++){
                    if(a[l]==a[i])
                        is=false;
                }
            }
            if(is) {
                b[k++]=a[i];result++;
            }
        }
        System.out.println(result);
        for(int i=0;i<result;i++) System.out.printf(b[i]+" ");
    }
}

2.Sorted?(数组是否有序)

Write a program that prompts the user to enter a list and displays whether the list is sorted in increasing order or not.

输入格式:

输入一行数据,其中第一个数是数组元素的个数(不计入数组),后续输入数组元素。

例如,下面是输入有8个元素的数组:

8 10 1 5 16 61 9 11 1

输出格式:

如果输入的数组是升序排列的,程序输出true,否则输出false。

例如对上面的输入,程序输出:

false

输入样例:

10 1 1 3 4 4 5 7 9 11 21

输出样例:

true

答案:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        int n=sc.nextInt();
        int []a=new int[n];int []b=new int[n];
        for(int i=0;i<n;i++){
            a[i]=sc.nextInt();
            b[i]=a[i];
        }
        Arrays.sort(a);
        boolean equal=true;
        for(int i=0;i<n;i++){
            if(a[i]!=b[i]){
                equal=false;
                break;
            }
        }
        if(equal) System.out.println("true");
        else System.out.println("false");
    }
}

3.Merge two sorted lists(合并有序数组)

Write a program that prompts the user to enter two sorted lists, merges the two sorted lists into a new sorted list and displays the merged list.

输入格式:

输入分为两行:第一行输入第一个数组;第二行输入第二个数组;

每行输入的第一个数是数组的元素个数(不作为数组的一部分),随后是数组的元素。

输出格式:

在一行中输出两个数组合并后的新有序数组,数组元素之间用一个空格分隔。

输入样例:

5 1 5 16 61 111
4 2 4 5 6

输出样例:

1 2 4 5 5 6 16 61 111 

重要提示:

主类名字必须是Main。

答案:

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a_len=sc.nextInt();int[] b = new int[a_len];
        for(int j = 0; j<b.length;j++) {
            b[j]=sc.nextInt();
        }
        int b_len=sc.nextInt();
        int []c=new int[b_len];
        for(int i=0;i<c.length;i++){
            c[i]=sc.nextInt();
        }
        int []result=new int[b_len+a_len];int k=0;
        for(int j = 0; j<b.length;j++) {
            result[k++]=b[j];
        }
        for(int i=0;i<c.length;i++){
            result[k++]=c[i];
        }
        Arrays.sort(result);
        for(int i=0;i<result.length;i++){
            System.out.printf(result[i]+" ");
        }
    }
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xxx_xiyuyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值