1)What is the representation of the third element in an array called a? 1) _______

A)a(3) B) a(2) C) a[2] D) a[3]

2)If you declare an array double[ ] list = {3.4, 2.0, 3.5, 5.5}, list[1] is ________. 2) _______

A)3.4  B)5.5  C)2.0  D)3.4  E)undefined

3)Which of the following is incorrect? (Choose all that apply.) 3) _______

A)int a = new int[2]; B)int a[ ] = new int[2];

C)int[ ] a = new int[2]; D)int a() = new int[2]; E)int[ ] a = new int(2);

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

A)3 B) 2 C) 4 D) 1 E) 0

5)How many elements are in array double[ ] list = new double[5]? 5) _______

A)6 B) 5 C) 4 D) 0

6)What is the correct term for numbers[99]? 6) _______

A)array variable

B)index variable

C)array

D)indexed variable

E)index

7)Suppose int i = 5, which of the following can be used as an index for array double[ ] t = new double[100]? (Choose all that apply.) 7) _______

A)i

B)i + 10

C)(int)(Math.random() * 100))

D)Math.random() * 100

E)i + 6.5

int型整数

8)Analyze the following code.

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 runs fine and displays x[0] is 0.
B)The program has a runtime error because the array elements are not initialized.
C)The program has a runtime error because the array element x[0] is not defined.
D)The program has a compile error because the size of the array wasn't specified when declaring the array.

9)Which of the following statements is valid? (Choose all that apply.) 9) _______
A)char[ ] c = new char();
B)int i = new int(30);
C)int[ ] i = {3, 4, 3, 2};
D)char[ ] c = new char[4]{'a', 'b', 'c', 'd'};
E)double d[ ] = new double[30];

10)How can you initialize an array of two characters to 'a' and 'b'? (Choose all that apply.)
A)char[ ] charArray = {'a', 'b'};
B)char[2] charArray = {'a', 'b'};
C)char[ ] charArray = new char[2]; charArray = {'a', 'b'};
D)char[ ] charArray = new char[ ]{'a', 'b'};

11)What would be the result of attempting to compile and run the following code?

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 new double[ ]{1.0, 2.0, 3.0};
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 {1, 2, 3}.
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.  

12)Assume int[ ] t = {1, 2, 3, 4}. What is t.length? 12) ______
A)3  B) 5 C) 4 D) 0

13)Analyze the following code:

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 has a compile error because i is not defined in the last statement in the main method.
B)The program displays 0 1 2 3 4.
C)The program has a runtime error because the last statement in the main method causes ArrayIndexOutOfBoundsException.
D)The program displays 4.

14)(Tricky) What is output of the following code:

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 20  
B)120 200 16  
C)120 200 14   
D)016 is a compile error. It should be written as 16.

0开头表示八进制整数直接量!!!

15)In the following code, what is the printout for list2?

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)0 1 3 B) 0 1 2 C) 1 2 3 D) 1 1 1

16)In the following code, what is the printout for list1?

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) 0 1 2 C) 0 1 3 D) 1 1 1

将list1的引用赋给list2,list1和list2指向内存中的同一块区域

17)Analyze the following code:

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 0 0 0 0
B)The program displays 0 0 3 4
C)The program displays 0 0
D)The program displays 1 2 3 4

18)Analyze the following code:

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 0 0
B)The program displays 0 0 3 4
C)The program displays 1 2 3 4
D)The program displays 0 0 0 0

19)Analyze the following code:

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 0 0
B)The elements in the array x cannot be changed, because x is final.
C)The program has a compile error on the statement x = new int[2], because x is final and cannot be changed.
D)The program displays 1 2 3 4

20)Analyze the following code.

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

A)The code can compile and run fine. The second line assigns a new array to list.
B)The code has compile errors because the variable list cannot be changed once it is assigned.
C)The code has compile errors because you cannot assign a different size array to list.
D)The code has runtime errors because the variable list cannot be changed once it is assigned.

第二行代码重新为list分配了一个新的数组

21)Analyze the following code:

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 displays a[1] is 1.
C)The program displays a[1] is 0.
D)The program has a runtime error because a[1] is not initialized.

22)The ________ method copies the sourceArray to the targetArray. 22) ______
A)System.arrayCopy(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.copyArrays(sourceArray, 0, targetArray, 0, sourceArray.length);

arraycopy方法违反了Java的命名方式,根据命名习惯,应该命名为arrayCopy。arraycopy方法没有给目标数组分配空间,复制前必须创建目标数组以及分配给它的内存空间,复制完成后,sourceArray 和 targetArray具有相同的内容。

23)When you pass an array to a method, the method receives ________. 23) ______
A)the length of the array B) a copy of the first element
C)a copy of the array D) the reference of the array

24)Show the output of the following code:

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)2 2 B) 1 1 C) 0 0 D) 1 2 E) 2 1

前者引用传递,后者值传递。

25)Do the following two programs produce the same result?
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

26)Analyze the following code:

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 and then raises an ArrayIndexOutOfBoundsException.
B)The program displays 1 2 3 4 5.
C)The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
D)The program displays 5 4 3 2 1.

27)Analyze the following code:

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]{1, 2, 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, 3}) is incorrect.
D)The program has a runtime error because a is null.

28)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. A)dynamic memory B) memory block C)stack D) heap

Java中的数组可以看出一个特殊的对象,声明时放在栈中,分配的空间存储在堆中。
29)When you return an array from a method, the method returns ________. 29) ______
A)the reference of the array B) the length of the array
C)a copy of the first element D) a copy of the array

30)Suppose a method p has the following heading:
public static int[ ] p()
What return statement may be used in p()? 30) ______
A)return 1; B) return new int[ ]{1, 2, 3}; C)return int[ ]{1, 2, 3}; D) return {1, 2, 3};

31)The reverse method is defined in the textbook. What is list1 after executing the following statements?
int[ ] list1 = {1, 2, 3, 4, 5, 6};
list1 = reverse(list1);
A)list1 is 6 5 4 3 2 1 B) list1 is 1 2 3 4 5 6 C)list1 is 0 0 0 0 0 0 D) list1 is 6 6 6 6 6 6

32)The reverse method is defined in this section. What is list1 after executing the following statements?
int[ ] list1 = {1, 2, 3, 4, 5, 6};
int[ ] list2 = reverse(list1);
A)list1 is 0 0 0 0 0 0 B) list1 is 6 6 6 6 6 6 C)list1 is 6 5 4 3 2 1 D) list1 is 1 2 3 4 5 6

33)Which of the following declarations are correct? (Choose all that apply.) 33) ______
A)public static void print(double... numbers, String name)
B)public static double... print(double d1, double d2)
C)public static void print(int n, double... numbers)
D)public static void print(double... numbers)
E)public static void print(String... strings, double... numbers)

34)Which of the following statements are correct to invoke the printMax method in Listing 6.5 in the textbook? (Choose all that apply.) 34) ______
A)printMax(new int[ ]{1, 2, 3});
B)printMax(1, 2, 2, 1, 4);
C)printMax(new double[ ]{1, 2, 3});
D)printMax(1.0, 2.0, 2.0, 1.0, 4.0);

35)For the binarySearch method in Section 6.9.2, what is low and high after the first iteration of the while loop when invoking binarySearch(new int[ ]{1, 4, 6, 8, 10, 15, 20}, 11)? 35) ______
A)low is 0 and high is 5
B)low is 3 and high is 6
C)low is 4 and high is 6
D)low is 0 and high is 3
E)low is 0 and high is 6

36)If a key is not in the list, the binarySearch method returns ________. 36) ______
A)insertion point B) -insertion point C)-(insertion point + 1) D) insertion point - 1

37)The selectionSort method is defined in this section. Assume list is {3.1, 3.1, 2.5, 6.4, 2.1}, what is the content of list after the first iteration of the outer loop in the method? 37) ______
A)3.1, 3.1, 2.5, 6.4, 2.1 B) 2.5, 3.1, 3.1, 6.4, 2.1 C)3.1, 3.1, 2.5, 2.1, 6.4 D) 2.1, 2.5, 3.1, 3.1, 6.4

38)The selectionSort method is defined in this section. What is list1 after executing the following statements?
double[ ] list1 = {3.1, 3.1, 2.5, 6.4};
selectionSort(list1);
A)list1 is 3.1, 2.5, 3.1, 6.4 B) list1 is 3.1, 3.1, 2.5, 6.4
C)list1 is 6.4, 3.1, 3.1, 2.5 D) list1 is 2.5 3.1, 3.1, 6.4

39)The ________ method sorts the array scores of the double[ ] type. 39) ______
A)java.util.Arrays(scores)
B)java.util.Arrays.sorts(scores)
C)Njava.util.Arrays.sortArray(scores)
D)java.util.Arrays.sort(scores)

40)Assume int[ ] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 30) return? 40) ______
A)2 B) 1 C) -1 D) -2 E) 0

41)Assume int[ ] scores = {1, 20, 30, 40, 50}, what value does java.util.Arrays.binarySearch(scores, 3) return? 41) ______
A)1 B) -1 C) -2 D) 2 E) 0

binarySearch()方法的返回值为:1、如果找到关键字,则返回值为关键字在数组中的位置索引,且索引从0开始2、如果没有找到关键字,返回值为负的插入点值,所谓插入点值就是第一个比关键字大的元素在数组中的位置索引,而且这个位置索引从1开始。

Java题库——Chapter6 一维数组的更多相关文章

  1. Java题库——chapter7 多维数组

    1)Which of the following statements are correct? 1) _______ A)char[ ][ ] charArray = {{'a', 'b'}, {' ...

  2. Java连载67-深入一维数组、main方法中的args参数详解

    一.复习了一维数组,还复习了强制类型转换的注意点. package com.bjpowernode.java_learning; public class D67_1_GoDeepIntoArrays ...

  3. leetcode题库练习_数组中重复的数字

    题目:数组中重复的数字 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次 ...

  4. Openjudge-NOI题库-二维数组回形遍历

    题目描述 Description 给定一个row行col列的整数数组array,要求从array[0][0]元素开始,按回形从外向内顺时针顺序遍历整个数组.如图所示:  输入输出格式 Input/ou ...

  5. Java题库——Chapter13抽象类和接口

    )What is the output of running class Test? public class Test { public static void main(String[ ] arg ...

  6. Java题库——Chapter12 异常处理和文本IO

    异常处理 1)What is displayed on the console when running the following program? class Test { public stat ...

  7. Java题库——Chapter9 String的用法

    1)Which code fragment would correctly identify the number of arguments passed via the command line t ...

  8. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  9. Openjudge-NOI题库-蛇形填充数组

    题目描述 Description 用数字1,2,3,4,...,n*n这n2个数蛇形填充规模为n*n的方阵. 蛇形填充方法为: 对于每一条左下-右上的斜线,从左上到右下依次编号1,2,...,2n-1 ...

随机推荐

  1. django----Sweetalert bulk_create批量插入数据 自定义分页器

    目录 一.Sweetalert使用AJAX操作 二.bulk_create 三.分页器 divmod 分页器组件 自定义分页器的使用 一.Sweetalert使用AJAX操作 ​ sweetalert ...

  2. [修仙之路]React-Redux 金丹篇

    作者:水涛追求技术,但又不失生活的乐趣,过自己想要的生活 React-Redux简介 React-Redux可以使你的React项目拥有全局数据,可以使多个React组件读取到全局数据并且组件中也可修 ...

  3. 《Java基础知识》Java类的定义及其实例化

    类必须先定义才能使用.类是创建对象的模板,创建对象也叫类的实例化. 下面通过一个简单的例子来理解Java中类的定义: public class Dog { String name; int age; ...

  4. JavaScript图形实例:七彩线团

    1.线团图案 设立坐标计算公式为: X=R1*COS(3α)+R2*COS(14α)) Y=R1*SIN(3α)+R2 *SIN(14α)) 再用循环依次取α值为0~2π(每次增量为0.01),计算出 ...

  5. IO测试工具 - 用于IO测试 ; linux benchmarks

    IO测试工具,用于磁盘IO测试,下面进行使用列表进行记录: iozone fio dd ioping iotop iostat bonnie++ crystalDisk Atto as-ssd-ben ...

  6. 查看python版本多少位的

    正常我们在cmd终端输入python之后,如果有安装python,就会在回车之后出来关于你安装的python版本信息,几版本,多少位的,但是还有一种,像我这样只显示了python版本是3.7.5,并没 ...

  7. PHP使用递归按层级查找数据

    今天主要介绍一下使用递归来按层级查找数据.原理挺简单的,主要是通过父级id一级一级的循环查找子级,使用PHP循环代码也很容易实现,不过如果层级越多,PHP重复代码也越多,这时可以使用递归来实现这功能. ...

  8. Jenkins之自动部署、代码安全扫描、自动化接口测试

    搭建Jenkins wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.reporpm --i ...

  9. vue/cli新旧版本安装方式

    一.老版本安装  Shift+鼠标右键 选择打开命令窗口 1.创建项目之前,需先确保本机已经安装node 在命令窗口中执行node -v npm -v 2.一般情况下用npm安装东西比较慢,可以使用淘 ...

  10. php 将科学计算法得出的结果转换成原始数据 NumToStr

    由于php最大只支持显示 15位因的数据运算,大于15位的2数加减乘除的数据的结果,会直接用科学计数法显示, 但在现实生活中,科学计数法不利于普通人识别,所以,本函数将:科学计数法的出的结果转换成原始 ...