笔试题 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. centos7安装ZABBIX 3.0+ 邮件报警【OK】

    设置主机名: vi /etc/hosts 10.0.0.252 zabbix-server hostnamectl set-hostname   关闭防火墙: systemctl stop firew ...

  2. ASP.NET 使用ajaxupload.js插件出现上传较大文件失败的解决方法

    在网上下载了一个ajaxupload.js插件,用于无刷新上传图片使的,然后就按照demo的例子去运行了一下,上传啊什么的都OK,但是正好上传的示例图片有一个比较大的,4M,5M的样子,然后上传就会报 ...

  3. HDU 6206 青岛网络赛1001 高精度 简单几何

    给出的数据1e12规模,常规判点是否在圆范围内肯定要用到半径,求得过程中无法避免溢出,因此用JAVA自带的浮点大数运算,和个ZZ一样比赛中eclipse出现问题,而且太久没写JAVA语法都不清楚变量忘 ...

  4. 03.WebView演练-iOS开发Demo(示例程序)源代码

    技术博客http://www.cnblogs.com/ChenYilong/   新浪微博http://weibo.com/luohanchenyilong   //转载请注明出处--本文永久链接:h ...

  5. 深入理解Spring系列之四:BeanDefinition装载前奏曲

    转载 https://mp.weixin.qq.com/s?__biz=MzI0NjUxNTY5Nw==&mid=2247483835&idx=1&sn=276911368d4 ...

  6. 好消息! 不用再羡慕Python有jupyter 我R也有Notebook了【附演示视频】

    熟悉python的朋友可能知道jupyter notebook.它是一个Web应用程序,允许你创建和共享代码,方程,可视化和说明性文本文档.现在,我们可以在RStudio中实现R Notebook的功 ...

  7. 导航狗IT周报第十五期(July 8, 2018)

    摘要:Seclists.Org: 微信支付SDK存在XXE漏洞:WordPress 4.9.6存在文件删除漏洞:linux中常用的文件打包/解包与压缩/解压缩命令总结… 安全播报 Seclists.O ...

  8. 美团实习Java岗面经,已拿offer

    作者:icysnowgx 链接:https://www.nowcoder.com/discuss/71954?type=2&order=3&pos=10&page=1 来源:牛 ...

  9. thinkphp中的验证器

  10. php返回json数据函数实例_php技巧_脚本之家

    本文实例讲述了php返回json数据函数的用法,分享给大家供大家参考.具体方法如下: json_encode()函数用法: echo json_encode(array('a'=>'bbbb', ...