Java题库——chapter7 多维数组

1)Which of the following statements are correct? 1) _______
A)char[ ][ ] charArray = {{'a', 'b'}, {'c', 'd'}};
B)char[2][2] charArray = {{'a', 'b'}, {'c', 'd'}};
C)char[2][ ] charArray = {{'a', 'b'}, {'c', 'd'}};
D)char[ ][ ] charArray = {'a', 'b'};

二维数组静态初始化[ ][ ]不允许出现数字


2)Assume double[ ][ ] x = new double[4][5], what are x.length and x[2].length? 2) _______
A)5 and 5 B) 4 and 4 C) 5 and 4 D) 4 and 5

x.length返回该二维数组中一维数组元素的个数

x[2].length返回一维数组中元素的个数。


3)Analyze the following code:

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

A)The program has a runtime error because x[2][2] is null.
B)The program has a compile error because new boolean[3][ ] is wrong.
C)The program runs and displays x[2][2] is null.
D)The program runs and displays x[2][2] is true.
E)The program runs and displays x[2][2] is false.


4)Suppose a method p has the following heading:

public static int[ ][ ] p()

What return statement may be used in p()?
A)return 1;
B)return int[ ]{1, 2, 3};
C)return {1, 2, 3};
D)return new int[ ][ ]{{1, 2, 3}, {2, 4, 5}};
E)return new int[ ]{1, 2, 3};


5)Assume double[ ][ ][ ] x = new double[4][5][6], what are x.length, x[2].length, and x[0][0].length? 5) _______
A)4, 5, and 4  B) 4, 5, and 6  C)6, 5, and 4  D) 5, 5, and 5


6)Which of the following statements are correct? (Choose all that apply.) 6) _______
A)char[ ][ ][ ] charArray = new char[2][2][ ];
B)char[ ][ ][ ] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};
C)char[2][2][ ] charArray = {'a', 'b'};
D)char[ ][ ][ ] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}};

转载于:https://www.cnblogs.com/wkfvawl/p/11594418.html

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 什么是 Java 异常? Java 异常是指程序执行期间可能发生的错误或异常情况,例如除以零、数组越界、空指针引用等。当这些异常发生时,Java 虚拟机会抛出一个异常对象,并且程序的执行流程将被中断。 2. Java 异常处理机制有哪些关键字和语句? Java 异常处理机制包括以下关键字和语句: - try:用于包含可能会抛出异常的代码块。 - catch:用于捕获指定类型的异常,并在捕获到异常时执行相应的处理代码。 - finally:用于包含无论是否发生异常都需要执行的代码块。 - throw:用于抛出指定的异常对象。 - throws:用于声明可能会抛出指定类型异常的方法。 3. Java 中的异常分为哪几类? Java 中的异常分为两大类:Checked Exception 和 Unchecked Exception。 Checked Exception 是指在编译时就能够检查出来的异常,例如 IOException、ClassNotFoundException 等。程序必须显式地处理这些异常,否则编译不通过。 Unchecked Exception 是指在运行时才能检查出来的异常,例如 NullPointerException、ArrayIndexOutOfBoundsException 等。程序可以选择处理这些异常,但不处理也不会导致编译错误。 4. 请简要说明 try-catch-finally 的执行流程。 当程序执行到 try 块时,Java 会尝试执行其中的代码。如果在 try 块中抛出了异常,则会将异常对象传递给 catch 块进行处理。catch 块会匹配异常类型,如果匹配成功,则执行相应的处理代码。如果 catch 块处理完异常后,程序需要继续执行,则会执行 finally 块中的代码。如果 finally 块中也抛出了异常,则该异常会覆盖 try 或 catch 块中的异常。 如果 try 块中没有抛出异常,则 catch 块不会被执行。如果 finally 块中抛出异常,则该异常会覆盖 try 块中的异常。 5. 什么是异常链? 异常链是指在处理异常时,将一个异常对象作为另一个异常的原因,并将它们组合成一个异常链。这样做的好处是,在抛出异常时可以同时传递多个异常信息,从而更加清晰地表示异常发生的原因。 6. 请简要说明 try-with-resources 的作用和使用方法。 try-with-resources 是 Java 7 中引入的语法,用于自动关闭实现了 AutoCloseable 接口的资源。在 try 块中声明需要使用的资源,Java 会在 try 块执行完毕后自动关闭这些资源,无需手动调用 close 方法。 try-with-resources 的语法如下: ``` try (Resource1 r1 = new Resource1(); Resource2 r2 = new Resource2()) { // 使用资源 } catch (Exception e) { // 处理异常 } ``` 7. 请简要说明 Java 中的文本 IO。 Java 中的文本 IO 主要包括两种类:Reader 和 Writer。Reader 用于读取字符流,而 Writer 用于写入字符流。 Java 中常用的 Reader 类包括 InputStreamReader、FileReader 和 BufferedReader,常用的 Writer 类包括 OutputStreamWriter、FileWriter 和 BufferedWriter。这些类提供了各种方法来读取和写入字符流,并且可以处理多种编码格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值