笔试题 Java 继承

Question 1 Output of following Java Program?

class Base {
public void show() {
System.out.println("Base::show() called");
}
} class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
} public class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}

A. Derived::show() called

B. Base::show() called

参考答案

```
A
```

Question 2

class Base {
final public void show() {
System.out.println("Base::show() called");
}
} class Derived extends Base {
public void show() {
System.out.println("Derived::show() called");
}
} public class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}

A. Base::show() called

B. Derived::show() called

C. Compiler Error

D. Runtime Error

参考答案

```
C
```

解析:Java 中 final 修饰方法不允许被子类重写,但是可以被子类继承,final 不能修饰构造方法。

Question 3

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");
}
} public class Main {
public static void main(String[] args) {
Base b = new Derived();
b.show();
}
}

A. Base::show() called

B. Derived::show() called

C. Compiler Error

参考答案

```
A
```

解析:Java 中静态属性和静态方法可以被继承,但是没有被重写 (overwrite) 而是被隐藏。

Question 4 **Which of the following is true about inheritance in Java?

  1. Private methods are final.
  2. Protected members are accessible within a package and inherited classes outside the package.
  3. Protected methods are final.
  4. We cannot override private methods.**

A. 1, 2 and 4

B. Only 1 and 2

C. 1, 2 and 3

D. 2, 3 and 4

参考答案

```
A
```

Question 5 Output of following Java program?

class Base {
public void Print() {
System.out.println("Base");
}
} class Derived extends Base {
public void Print() {
System.out.println("Derived");
}
} public class Main {
private static void DoPrint(Base o) {
o.Print();
} public static void main(String[] args) {
Base x = new Base();
Base y = new Derived();
Derived z = new Derived();
DoPrint(x);
DoPrint(y);
DoPrint(z);
}
}

A. Base

   Derived

   Derived

B. Base

   Base

   Derived

C. Base

   Derived

   Base

D. Compiler Error

参考答案

```
A
```

Question 6 Predict the output of following program. Note that fun() is public in base and private in derived.

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. Compiler Error

D. Runtime Error

参考答案

```
C
```

参考解析

重写要遵循**"两同两小一大"**原则:
**1) 两同:**
* 1.1) 方法名相同
* 1.2) 参数列表相同

2) 两小:

  • 2.1) 子类方法的返回值类型小于或等于父类的

    • 2.1.1) void 时,必须相同
    • 2.1.2) 基本类型时,必须相同
    • 2.1.3) 引用类型时,小于或等于
  • 2.2) 子类方法抛出的异常小于或等于父类的(异常之后)

3) 一大:

  • 3.1) 子类方法的访问权限大于或等于父类的(访问控制修饰符后)

Question 7 **Which of the following is true about inheritance in Java.

  1. In Java all classes inherit from the Object class directly or indirectly. The Object class is root of all classes.
  2. Multiple inheritance is not allowed in Java.
  3. Unlike C++, there is nothing like type of inheritance in Java where we can specify whether the inheritance is protected, public or private.**

A. 1, 2 and 3

B. 1 and 2

C. 2 and 3

D. 1 and 3

参考答案

```
A
```

Question 8 Predict the output of following Java Program?

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. Compiler Error in super.super.Print()

B. Grandparent's Print()

   Parent's Print()

   Child's Print()

C. Runtime Error

参考答案

```
A
```

解决方案

Java 代码

class Grandparent {
public void Print() {
System.out.println("Grandparent's Print()");
}
} class Parent extends Grandparent {
public void Print() {
super.Print();
System.out.println("Parent's Print()");
}
} class Child extends Parent {
public void Print() {
super.Print();
System.out.println("Child's Print()");
}
} public class Main {
public static void main(String[] args) {
Child c = new Child();
c.Print();
}
}

运行结果

Grandparent's Print()
Parent's Print()
Child's Print()

Question 9

final class Complex {

    private final double re;
private final double im; public Complex(double re, double im) {
this.re = re;
this.im = im;
} public String toString() {
return "(" + re + " + " + im + "i)";
}
} class Main {
public static void main(String args[]) {
Complex c = new Complex(10, 15);
System.out.println("Complex number is " + c);
}
}

A. Complex number is (10.0 + 15.0i)

B. Compiler Error

C. Complex number is SOME_GARBAGE

D. Complex number is Complex@8e2fb5(Here 8e2fb5 is hash code of c)

参考答案

```
A
```

参考链接

【笔试题】Java 继承知识点检测的更多相关文章

  1. 剑指Offer——网易校招内推笔试题+模拟题知识点总结

    剑指Offer--网易校招内推笔试题+模拟题知识点总结 前言 2016.8.2 19:00网易校招内推笔试开始进行.前天晚上利用大约1小时时间完成了测评(这个必须做,关切到你能否参与面试).上午利用2 ...

  2. Java面试中笔试题——Java代码真题,这些题会做,笔试完全可拿下!

    大家好,我是上海尚学堂Java培训老师,以下这些Java笔试真题是上海尚学堂Java学员在找工作中笔试遇到的真题.现在分享出来,也写了参考答案,供大家学习借鉴.想要更多学习资料和视频请留言联系或者上海 ...

  3. Android开发面试经——3.常见Java基础笔试题

      Android开发(29)  版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...

  4. 2016最新Java笔试题集锦

    更新时间:2015-08-13         来源:网络         投诉删除 [看准网(Kanzhun.com)]笔试题目频道小编搜集的范文“2016最新Java笔试题集锦”,供大家阅读参考, ...

  5. 【笔试题】Java 中如何递归显示一个目录下面的所有目录和文件?

    笔试题 Java 中如何递归显示一个目录下面的所有目录和文件? import java.io.File; public class Test { private static void showDir ...

  6. 【笔试题】Java 易错题精选

    笔试题 Java 易错题精选 1.写出下列程序的运行结果( )String 不变性Java 值传递 public class Test { public static void main(String ...

  7. [原创]Java常见笔试题知识点汇总

    前天数梦工厂来学校招聘,笔试题比较有特点,全是Java题,基本就是Java的一些特点.凭记忆按照题目找到一些必备知识点 (1). try {}里有一个return语句,那么紧跟在这个try后的fina ...

  8. 【笔试题】Java笔试题知识点

    Java高概率笔试题知识点 Java语法基础部分 [解析]java命令程序执行字节码文件是,不能跟文件的后缀名! 1.包的名字都应该是由小写单词组成,它们全都是小写字母,即便中间的单词亦是如此 2.类 ...

  9. JAVA 150道笔试题知识点整理

    JAVA 笔试题 整理了几天才整理的题目,都是在笔试或者面试碰到的,好好理解消化下,对你会有帮助,祝你找工作顺利,收到满意的 offer . 1.Java 基础知识 1.1 Java SE 语法 &a ...

随机推荐

  1. Rabbitmq--topic

    一.前言 前面讲到direct类型的Exchange路由规则是完全匹配binding key与routing key,但这种严格的匹配方式在很多情况下不能满足实际业务需求.topic类型的Exchan ...

  2. 封装-python

    六 封装 从封装本身的意思去理解,封装就好像是拿来一个麻袋,把小猫,小狗,小王八,还有alex一起装进麻袋,然后把麻袋封上口子.但其实这种理解相当片面 首先我们要了解 回到顶部 6.1 要封装什么 你 ...

  3. Emacs ^ Vim

    Emacs存活: http://files.cnblogs.com/files/TheRoadToTheGold/Emacs%E2%80%94%E2%80%94%E5%AD%98%E6%B4%BB.z ...

  4. Python学习笔记(五十)爬虫的自我修养(三)爬取漂亮妹纸图

    import random import urllib from urllib import request import os ################################### ...

  5. 关于Cookie跨域的问题研究

    Cookie是一个伟大的发明,它允许Web开发者保留他们的用户的登录状态.但是当你的站点有一个以上的域名时就会出现问题了.在Cookie规范上说,一个cookie只能用于一个域名,不能够发给其它的域名 ...

  6. Lua只读表

    利用Lua的元表(metatable)和元函数(metafunction)可以很简单的实现此功能. 其实现大致分为三个部分 1.禁止在表中创建新值 2.禁止改变已有的值 3.将子表也变为只读 1.禁止 ...

  7. PHP分页类分享

    /** * 获取分页的HTML内容 * @param integer $page 当前页 * @param integer $pages 总页数 * @param string $url 跳转url地 ...

  8. Git学习笔记3 git revert

    我们难免会因为种种原因执行一些错误的commit / push,git提供了revert命令帮助程序员修复这样的错误. 举个例子,下图是git commit 的历史记录 git revert 命令会通 ...

  9. [转载]Windows服务编写原理及探讨(1)

    有那么一类应用程序,是能够为各种用户(包括本地用户和远程用户)所用的,拥有用户授权级进行管理的能力,并且不论用户是否物理的与正在运行该应用程序的计算机相连都能正常执行,这就是所谓的服务了. (一)服务 ...

  10. 在 ASP.NET Core 具体使用文档

    https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/hosting?tabs=aspnetcore2x