Assignment Help logo
在线聊天

Loading...

Score %0 (0 correct0 incorrect20 unanswered)

Q1. 给定保存在名为fruit的变量中的字符串 "strawberries",fruit.substring(2, 5)将返回什么?

  • rawb
  • raw
  • awb
  • traw

推理: substring方法接受两个参数。

  • 第一个参数是开始索引(包括2处的字符)
  • 第二个是字符串的结束索引(不包括5处的字符)。
  • 在Java中,字符串就像字符数组一样。
  • 因此,该方法将返回 "raw",因为这些字符位于索引2、3和4处。
  • 您还可以将结束索引减去开始索引,以确定子字符串中将包含多少个字符(5-2=3)。

Q2. 如何在Java中实现运行时多态性?

  • 方法重载
  • 方法过度运行
  • 方法重写
  • 方法调用

Q3. 鉴于以下定义,哪个表达式不会评估为true?

boolean b1 = true, b2 = false; int i1 = 1, i2 = 2;

  • (i1 | i2) == 3
  • i2 && b1
  • b1 || !b2
  • (i1 ^ i2) < 4

推理: i2 && b1在int和boolean之间不允许。

Q4. 这段代码的输出是什么?

class Main {
  public static void main (String[] args) {
    int array[] = {1, 2, 3, 4};
    for (int i = 0; i < array.size(); i++) {
       System.out.print(array[i]);
    }
  }
}
  • 由于第4行,它不会编译。
  • 由于第3行,它不会编译。
  • 123
  • 1234

推理: array.size()是无效的,要获取数组的大小或长度,可以使用array.length。

Q5. 以下哪个可以替换代码片段以使下面的代码打印 "Hello World"?

interface Interface1 {
    static void print() {
        System.out.print("Hello");
    }
}

interface Interface2 {
    static void print() {
        System.out.print("World!");
    }
}
  • super1.print(); super2.print();
  • this.print();
  • super.print();
  • Interface1.print(); Interface2.print();

参考

Q6. 以下代码打印什么?

String str = "abcde";
str.trim();
str.toUpperCase();
str.substring(3, 4);
System.out.println(str);
  • CD
  • CDE
  • D
  • "abcde"

推理: 您应该将trim的结果分配回String变量。否则,它将无法工作,因为Java中的字符串是不可变的。

Q7. 这段代码的结果是什么?

class Main {
    public static void main (String[] args){
        System.out.println(print(1));
    }
    static Exception print(int i){
        if (i>0) {
            return new Exception();
        } else {
            throw new RuntimeException();
        }
    }
}
  • 它将显示带有运行时异常的堆栈跟踪。
  • "java.lang.Exception"
  • 它将运行并抛出异常。
  • 它将无法编译。

Q8. 给定这些声明,哪个类可以编译?

interface One {
    default void method() {
        System.out.println("One");
    }
}

interface Two {
    default void method () {
        System.out.println("One");
    }
}
  • A
class Three implements One, Two {
    public void method() {
        super.One.method();
    }
}
  • B
class Three implements One, Two {
    public void method() {
        One.method();
    }
}
  • C
class Three implements One, Two {
}
  • D
class Three implements One, Two {
    public void method() {
        One.super.method();
    }
}

Q9. 这段代码的输出是什么?

class Main {
    public static void main (String[] args) {
        List list = new ArrayList();
        list.add("hello");
        list.add(2);
        System.out.print(list.get(0) instanceof Object);
        System.out.print(list.get(1) instanceof Integer);
    }
}
  • 代码无法编译。
  • truefalse
  • truetrue
  • falsetrue

Q10. 给定以下两个类,Main类的输出将是什么?

package mypackage;
public class Math {
    public static int abs(int num){
        return num < 0 ? -num : num;
    }
}
package mypackage.elementary;
public class Math {
    public static int abs (int num) {
        return -num;
    }
}
import mypackage.Math;
import mypackage.elementary.*;

class Main {
    public static void main (String args[]){
        System.out.println(Math.abs(123));
    }
}
  • 第1行和第2行由于类名冲突而产生编译错误。
  • "-123"
  • 第5行将抛出异常。
  • "123"

解释: 答案是 "123"。abs()方法计算为mypackage.Math类中的方法,因为导入语句形式为:

import packageName.subPackage.*

按需导入声明永远不会导致任何其他声明被屏蔽

Q11. 这段代码的结果是什么?

class MainClass {
    final String message()

 {
        return "Hello!";
    }
}

class Main extends MainClass {
    public static void main(String[] args) {
        System.out.println(message());
    }

     String message() {
         return "World!";
     }
 }
  • 由于第10行,它将无法编译。
  • "Hello!"
  • 由于第2行,它将无法编译。
  • "World!"

解释: 在第10行有编译错误,因为final方法不能被重写,这里的message()是final方法。此外,请注意,非静态方法message()无法从静态上下文中引用。

Q12. 填写下划线以创建一段代码,该代码将告诉 int0 是否可以被 5 整除:

boolean isDivisibleBy5 = _____

  • int0 / 5 ? true: false
  • int0 % 5 == 0
  • int0 % 5 != 5
  • Math.isDivisible(int0, 5)

Q13. 这段代码的输出是什么?

class Main {
    public static void main(String[] args){
        int a = 123451234512345;
        System.out.println(a);
    }
}
  • "123451234512345"
  • 没有任何输出 - 这将无法编译。
  • 负整数值
  • "12345100000"

推理: 在Java中,int类型可用于表示从 -2147483648 到 2147483647 的任何整数。因此,此代码将无法编译,因为分配给'a'的数字大于int类型可以容纳的范围。

Q14. 这段代码的输出是什么?

class Main {
    public static void main (String[] args) {
        String message = "Hello world!";
        String newMessage = message.substring(6, 12)
            + message.substring(12, 6);
        System.out.println(newMessage);
    }
}
  • 代码无法编译。
  • 将引发运行时异常。
  • "world!!world"
  • "world!world!"

Q15. 如何编写一个 for-each 循环,它将迭代ArrayList<Pencil>pencilCase?

  • for (Pencil pencil : pencilCase) {}
  • for (pencilCase.next()) {}
  • for (Pencil pencil : pencilCase.iterator()) {}
  • for (pencil in pencilCase) {}

Q16. 这段代码打印什么?

System.out.print("apple".compareTo("banana"));
  • 0
  • 正数
  • 负数
  • 编译错误

Q17. 您有一个名称的ArrayList,您想按字母顺序对其进行排序。哪种方法不会起作用?

  • names.sort(Comparator.comparing(String::toString))
  • Collections.sort(names)
  • names.sort(List.DESCENDING)
  • names.stream().sorted((s1, s2) -> s1.compareTo(s2)).collect(Collectors.toList())

参考

Q18. 通过实现封装,您无法直接访问类的_属性,除非您正在类本身的代码中编写代码。

  • private
  • protected
  • no-modifier
  • public

Q19. 实例化当前日期的最新方式是什么?

  • new SimpleDateFormat("yyyy-MM-dd").format(new Date())
  • new Date(System.currentTimeMillis())
  • LocalDate.now()
  • Calendar.getInstance().getTime()

解释: LocalDate是Java 8中添加的最新类。

Q20. 填写下划线以创建一段代码,该代码将告诉 int0 是否可以被 5 整除:

boolean isDivisibleBy5 = _____

  • int0 / 5 ? true: false
  • int0 % 5 == 0
  • int0 % 5 != 5
  • Math.isDivisible(int0, 5)