笔试题 Java 继承

Question 1 Output of following Java Program?

  1. class Base {
  2. public void show() {
  3. System.out.println("Base::show() called");
  4. }
  5. }
  6. class Derived extends Base {
  7. public void show() {
  8. System.out.println("Derived::show() called");
  9. }
  10. }
  11. public class Main {
  12. public static void main(String[] args) {
  13. Base b = new Derived();
  14. b.show();
  15. }
  16. }

A. Derived::show() called

B. Base::show() called

参考答案

```
A
```

Question 2

  1. class Base {
  2. final public void show() {
  3. System.out.println("Base::show() called");
  4. }
  5. }
  6. class Derived extends Base {
  7. public void show() {
  8. System.out.println("Derived::show() called");
  9. }
  10. }
  11. public class Main {
  12. public static void main(String[] args) {
  13. Base b = new Derived();
  14. b.show();
  15. }
  16. }

A. Base::show() called

B. Derived::show() called

C. Compiler Error

D. Runtime Error

参考答案

```
C
```

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

Question 3

  1. class Base {
  2. public static void show() {
  3. System.out.println("Base::show() called");
  4. }
  5. }
  6. class Derived extends Base {
  7. public static void show() {
  8. System.out.println("Derived::show() called");
  9. }
  10. }
  11. public class Main {
  12. public static void main(String[] args) {
  13. Base b = new Derived();
  14. b.show();
  15. }
  16. }

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?

  1. class Base {
  2. public void Print() {
  3. System.out.println("Base");
  4. }
  5. }
  6. class Derived extends Base {
  7. public void Print() {
  8. System.out.println("Derived");
  9. }
  10. }
  11. public class Main {
  12. private static void DoPrint(Base o) {
  13. o.Print();
  14. }
  15. public static void main(String[] args) {
  16. Base x = new Base();
  17. Base y = new Derived();
  18. Derived z = new Derived();
  19. DoPrint(x);
  20. DoPrint(y);
  21. DoPrint(z);
  22. }
  23. }

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.

  1. class Base {
  2. public void foo() {
  3. System.out.println("Base");
  4. }
  5. }
  6. class Derived extends Base {
  7. private void foo() {
  8. System.out.println("Derived");
  9. }
  10. }
  11. public class Main {
  12. public static void main(String args[]) {
  13. Base b = new Derived();
  14. b.foo();
  15. }
  16. }

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?

  1. class Grandparent {
  2. public void Print() {
  3. System.out.println("Grandparent's Print()");
  4. }
  5. }
  6. class Parent extends Grandparent {
  7. public void Print() {
  8. System.out.println("Parent's Print()");
  9. }
  10. }
  11. class Child extends Parent {
  12. public void Print() {
  13. super.super.Print();
  14. System.out.println("Child's Print()");
  15. }
  16. }
  17. public class Main {
  18. public static void main(String[] args) {
  19. Child c = new Child();
  20. c.Print();
  21. }
  22. }

A. Compiler Error in super.super.Print()

B. Grandparent's Print()

   Parent's Print()

   Child's Print()

C. Runtime Error

参考答案

```
A
```

解决方案

Java 代码

  1. class Grandparent {
  2. public void Print() {
  3. System.out.println("Grandparent's Print()");
  4. }
  5. }
  6. class Parent extends Grandparent {
  7. public void Print() {
  8. super.Print();
  9. System.out.println("Parent's Print()");
  10. }
  11. }
  12. class Child extends Parent {
  13. public void Print() {
  14. super.Print();
  15. System.out.println("Child's Print()");
  16. }
  17. }
  18. public class Main {
  19. public static void main(String[] args) {
  20. Child c = new Child();
  21. c.Print();
  22. }
  23. }

运行结果

  1. Grandparent's Print()
  2. Parent's Print()
  3. Child's Print()

Question 9

  1. final class Complex {
  2. private final double re;
  3. private final double im;
  4. public Complex(double re, double im) {
  5. this.re = re;
  6. this.im = im;
  7. }
  8. public String toString() {
  9. return "(" + re + " + " + im + "i)";
  10. }
  11. }
  12. class Main {
  13. public static void main(String args[]) {
  14. Complex c = new Complex(10, 15);
  15. System.out.println("Complex number is " + c);
  16. }
  17. }

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. 【题解】期末考试 六省联考 2017 洛谷 P3745 BZOJ 4868 贪心 三分

    题目传送门:这里是萌萌哒传送门(>,<) 啊♀,据说这题有个完全贪心的做法,但是要维护太多东西好麻烦的(>,<),于是就来口胡一发三分的做法. 思路很简单,假设我指定了一个x, ...

  2. Linux安全之SYN攻击原理及处理

    TCP自从1974年被发明出来之后,历经30多年发展,目前成为最重要的互联网基础协议,但TCP协议中也存在一些缺陷. SYN攻击就是利用TCP协议的缺陷,来导致系统服务停止正常的响应. SYN攻击原理 ...

  3. mysql 自动记录数据插入及最后修改时间

    总结: `uptime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 原文 应用场景: 1.在数据 ...

  4. windows如何要查看IIS连接数

    1.运行,输入,perfmon.msc 2.在系统监视器,区域点击,添加计数器. 3.在“添加计数器”窗口,“性能对象”选择Web Service,“从列表选择计数器”选中Current Connec ...

  5. Asp.Net MVC +EF CodeFirst+多层程序设计

    1.概述 这是一个基于个人博客的一个项目,虽然博客根本没必要做这么复杂的设计.但是公司有需求,所以先自己弄个项目练练手.项目需要满足下列需求 1.层与层之间需要解耦,在后期上线更新维护时不需要覆盖,只 ...

  6. web启动@Autowired不能自动注入

    使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...

  7. [洛谷P4609] [FJOI2016]建筑师

    洛谷题目链接:[FJOI2016]建筑师 题目描述 小 Z 是一个很有名的建筑师,有一天他接到了一个很奇怪的任务:在数轴上建 \(n\) 个建筑,每个建筑的高度是 \(1\) 到 \(n\) 之间的一 ...

  8. Shiro实战教程(一)

    Shiro完整架构图 Shiro认证过程 Shiro授权的内部处理机制 Shiro 支持三种方式的授权 1.编程式:通过写if/else 授权代码块完成: Subject subject = Secu ...

  9. E.Text Editor (Gym 101466E + 二分 + kmp)

    题目链接:http://codeforces.com/gym/101466/problem/E 题目: 题意: 给你s串和t串,一个数k,求t的最长前缀串在s串中出现次数不少于k. 思路: 一眼二分+ ...

  10. NYOJ 93 汉诺塔 (数学)

    题目链接 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片 ...