笔试题 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. 「Django」与mysql8连接的若干问题

    1.setting配置 DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', #数据库驱动名 'NAME': 'my_tes ...

  2. [DeeplearningAI笔记]卷积神经网络1.2-1.3边缘检测

    4.1卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.2边缘检测示例 边缘检测可以视为横向边缘检测和纵向边缘检测如下图所示: 边缘检测的原理是通过一个特定构造的卷积核对原始图 ...

  3. .NET面试题系列(一)基本概念

    什么是CLR CLR常用简写词语,CLR是公共语言运行库(Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集等),并保证应用和底 ...

  4. Javascript的执行过程详细研究

    下面我们以更形象的示例来说明JavaScript代码在页面中的执行顺序.如果说,JavaScript引擎的工作机制比较深奥是因为它属于底层行为,那么JavaScript代码执行顺序就比较形象了,因为我 ...

  5. 游戏AI:行为树

    Behavior Tree 行为树通过子Task的返回值决定整棵树的走向 Task 行为树上的每个节点都称为一个Task, 每个Task存在三种状态, success, failure, runnin ...

  6. windos下创建软链接,附Linux下创建软链接

    用过好多次老是忘记: 写在这里忘了就来看下 Windows下(win7) mklink /D D:\phpStudy\WWW\yii\school\teacher\web\uploads\public ...

  7. G6踩坑日记

    用G6去完成一整个图例的时候,当包裹它的容器满足不了包裹的需求时,我们就需要引入缩略图来解决问题了 缩略图使用方式很简单 引入插件配置就可以了 当我们使用多张图片进行绘图(G6支持使用图片进行构图,原 ...

  8. ORA-01552 非系统表空间不能使用系统回滚段处理

    今天新搭建了一个10g的测试数据库,运行都很正常,但是在打开autotrace功能后执行语句,报错 SQL> set autotrace on SQL> select username,s ...

  9. sqlmap tamper编写

    #!/usr/bin/env python """ Copyright (c) 2006-2017 sqlmap developers (http://sqlmap.or ...

  10. imperva agent 的重新注册

    情况是这样 公司搭了一个环境有mysql的数据库并且安装了agent,imperva管理平台上也可以看到agent的注册信息,但是没想到的是有人把我的虚机给还原快照了,而且还没保存..... 这次写个 ...