All objects are allocated on heap in Java
问题1:(继承)
class Base {
public static void show() {
System.out.println("Base::show() called");
}
}
class Derived extends Base {
public static void show() {
System.out.println("Derived::show() called");
}
}
class Main {
public static void main(String[] args) {
Base b = new Derived();;
b.show();
}
}
输出结果: (A)
Base::show() called (B)
Derived::show() calle (C) 编译错误
问题2:(继承)
class Base {
public void foo() { System.out.println("Base"); }
}
class Derived extends Base {
private void foo() { System.out.println("Derived"); }
}
public class Main {
public static void main(String args[]) {
Base b = new Derived();
b.foo();
}
}
输出结果:(A) Base (B) Derived (C) 编译错误 (D)运行错误
问题3:(继承)
class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
}
class Parent extends Grandparent {
public void Print() {
System.out.println("Parent's Print()");
}
}
class Child extends Parent {
public void Print() {
super.super.Print();
System.out.println("Child's Print()");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}
输出结果:(A) 编译错误 (B)Grandparent's Print() Parent's Print() Child's Print() (C) 运行错误
问题4:(方法)
class Test {
public static void swap(Integer i, Integer j) {
Integer temp = new Integer(i);
i = j;
j = temp;
}
public static void main(String[] args) {
Integer i = new Integer(10);
Integer j = new Integer(20);
swap(i, j);
System.out.println("i = " + i + ", j = " + j);
}
}
输出结果:(A)i = 10, j = 20 (B)i = 20, j = 10 (C)i = 10, j = 10 (D)i = 20, j = 20
问题5:(异常)
class Base extends Exception {}
class Derived extends Base {}
public class Main {
public static void main(String args[]) {
// some other stuff
try {
// Some monitored code
throw new Derived();
}
catch(Base b) {
System.out.println("Caught base class exception");
}
catch(Derived d) {
System.out.println("Caught derived class exception");
}
}
}
输出结果:(A)Caught base class exception (B)Caught derived class exception (C)编译错误 because derived is not throwable (D)编译错误 because base class exception is caught before derived class
问题6:(操作符)
class Test {
public static void main(String args[]) {
int x = -4;
System.out.println(x>>1);
int y = 4;
System.out.println(y>>1);
}
}
输出结果是?
问题7:(操作符)
class Base {}
class Derived extends Base {
public static void main(String args[]){
Base a = new Derived();
System.out.println(a instanceof Derived);
}
}
输出结果是?
解析
问题1:选A。方法是static的,不会出现运行时多态Like C++, when a function is static, runtime polymorphism doesn't happen.
问题2:选C。子类覆盖父类的方法,不应比父类方法的限定修饰词更严格。It is compiler error to give more restrictive access to a derived class function which overrides a base class function.
问题3:选A。JAVA里 super.super是不允许的。In Java, it is not allowed to do super.super. We can only access Grandparent's members using Parent.
问题4:选A。JAVA中参数通过值传递。Parameters are passed by value in Java
问题5:选D。
Main.java:12: error: exception Derived has already been caught catch(Derived d) { System.out.println("Caught derived class exception"); }
问题6:-2 2 带符号右移(算术右移)。知乎有个答案不错:https://www.zhihu.com/question/38051371
问题7:true 当引用为父类类型时,instanceof操作符同样成立。
(未完待续)