有几道练习题不懂,请教大家,谢谢。
大家给我解释一下这几道题。
虽然都有答案,但是答案不一定对。
package com.abe.pkg1;
public class ClassOne{
private char var = 'a';
char getVar(){ return var;}
}
package com.abe.pkg2;
improt com.abc.pkg1.ClassOne;
public class ClassTest extends ClassOne{
public static void main(String[] args){
char a = new ClassOne().getVar();
char a = new ClassTest().getVar();
}
}
what's the result?
a.compilation fails
b.compilation succeeds and no exceptions are
thrown.
c.an exception is thrown at line 5 in
ClassTest.java
答案是A 请解释一下
class TestA{
TestB b;
TestA(){
b = new TestB(this);
}
}
class TestB{
TestA a;
TestB(TestA a){
this.a = a;
}
}
class TestAll{
public static void main(String args[]){
new TestAll().makeThings();
// code on...
}
void makeThings(){
TestA test = new TestA();
}
}
Question:
which two statements are true after line15 ?
a.line 15 causes a stack overflow.
b.an exception is thrown at runtime.
c.the object referenced by a is eligible for
garbage collection.
d.the object referenced by b is eligible for
garbage collection.
答案c d 请解释。
public class Test{
public void foo(){
assert false;
assert false;
}
public void bar(){
while(true){
assert false;
}
assert false;
}
}
what causes compilation to fail?
a.line 13
b. line 14
c. line 18
d. line 20
答案 D 请解释
Given that b and c refer to instances of wrapper
classes, which tow statements are true?
a. b.equals(b) returns true.
b. b.equals(c) returns the same result as b==c
c. b.equals(c) can return false even if
c.equals(b) returns true.
d. b.equals(c) throws an exception if b and c
are different wrpper types.
e. b.equals(c) returns fals if the type of
wrpper objects being compared are different.
答案:b,c 请解释
which statement si true?
a. The Error class is a runtimeException.
b. No exceptions are subclases of Error.
c. Any staement taht may throw an Error must be enclosed in a try block.
d. Any statement that may throw an Exception must be enclosed in a try block.
e. Any statement that may throw a runtimeException must be enclosed in a try block
答案d 我怎么觉得b才对