class MyException extends Exception { }
class Tire {
void doStuff() { } // #4
}
public class Retread extends Tire {
public static void main(String[] args) {
new Retread().doStuff();
}
// insert code here
System.out.println(7/0);
}
Run Code Online (Sandbox Code Playgroud)
并给出以下四个代码片段:
void doStuff() {void doStuff() throws MyException {void doStuff() throws RuntimeException {void doStuff() throws ArithmeticException {当在第10行独立地添加片段1-4时,这是真的吗?(选择所有适用的选项.)
答案:C和D是正确的.重写方法不能抛出比重写方法抛出的更广泛的已检查异常.但是,重写方法可能会抛出被重写方法抛出的RuntimeExceptions.基于上述情况,A,B,E和Fare不正确.(目标2.4)
在这种情况下,我没有得到BoldItalic标记所说的内容.重写方法(#4)不会抛出任何异常,因此我们如何知道我们添加到重写方法(选项2)的那个(在这种情况下是MyException)是否比重写方法更广泛.Arithmetic异常是如何运行的,没有错误.它如何不比最重要的方法中的不知道哪个例外更广泛.
问:02鉴于:
11. public void genNumbers() {
12. ArrayList numbers = new ArrayList();
13. for (int i=0; i<10; i++) {
14. int value = i * ((int) Math.random());
15. Integer intObj = new Integer(value);
16. numbers.add(intObj);
17. }
18. System.out.println(numbers);
19. }
Run Code Online (Sandbox Code Playgroud)
哪一行代码标志着intObj引用的对象成为垃圾收集候选者的最早点?
A. Line 16
B. Line 17
C. Line 18
D. Line 19
E. The object is NOT a candidate for garbage collection.
Run Code Online (Sandbox Code Playgroud)
答案:D
困惑为什么答案是D而不是B.请帮助我理解.提前致谢!
import java.util.*;
class KeyMaster {
public int i;
public KeyMaster(int i) { this.i = i; }
public boolean equals(Object o) { return i == ((KeyMaster)o).i; }
public int hashCode() { return i; }
}
public class MapIt {
public static void main(String[] args) {
Set<KeyMaster> set = new HashSet<KeyMaster>();
KeyMaster k1 = new KeyMaster(1);
KeyMaster k2 = new KeyMaster(2);
set.add(k1); set.add(k1);
set.add(k2); set.add(k2);
System.out.print(set.size() + “:”);
k2.i = 1;
System.out.print(set.size() + “:”);
set.remove(k1);
System.out.print(set.size() + “:”);
set.remove(k2);
System.out.print(set.size());
}
} …Run Code Online (Sandbox Code Playgroud) 为什么以下代码只输出3 Thread-0时输出6行Thread-1?
public class NameList{
private List names = new ArrayList();
public synchronized void addName(String name){
names.add(name);
}
public synchronized void print(){
for (int i = 0; i < names.size(); i++) {
System.out.print(names.get(i)+" ");
System.out.println(Thread.currentThread().getName());
}
}
public static void main(String args[]){
final NameList nl = new NameList();
for (int i = 0; i <2; i++) {
new Thread(){
public void run(){
nl.addName("A");
nl.addName("B");
nl.addName("C");
nl.print();
}
}.start();
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
A Thread-1
B Thread-1
C …Run Code Online (Sandbox Code Playgroud)