笔试题 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. qq快速登陆

    http://www.cnblogs.com/1996V/p/7481823.html qq快速登陆

  2. 这年头不会点Git真不行!!!

    版本控制 说到版本控制,脑海里总会浮现大学毕业是写毕业论文的场景,你电脑上的毕业论文一定出现过这番景象! 1 2 3 4 5 6 7 8 9 10 11 毕业论文_初稿.doc 毕业论文_修改1.do ...

  3. Vue.js随笔四(方法的声明和使用)

    1.首先你需要新建路由,这个就不多说了 2.然后在你的新的.vue里面需要如下所示的添加methods:{方法},然后按钮的里面你会看到v-on:click,这就是点击这个按钮会触发的动作,这个就是触 ...

  4. NOIP 2014 提高组 Day2

    期望得分:100+60+30=190 实际得分:70+60+30=160 https://www.luogu.org/problem/lists?name=&orderitem=pid& ...

  5. Codeforces 620C EDU C.Pearls in a Row ( set + greed )

    C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...

  6. Linux更改文件及目录权限问题

    1. 快速实例学习: 修改某个目录下的所有文件的权限,包括子目录中的文件,例子如下: # /home/user 注:仅把/home/user目录的权限设置为rwxrwxrwx # /home/user ...

  7. java多线程机制1(线程创建的两种方式)

    进程:正在运行的程序.(即程序在内存中开辟了一片空间) 线程:是进程的执行单元. 一个进程至少包含了一个多个线程. 多线程是不是可以提高效率:多线程可以合理的利用系统的资源,提高效率是相对的.因为cp ...

  8. ASP.NET Session详解笔记

    (一) 描述 当用户在 Web 应用程序中导航 ASP.NET 页时,ASP.NET 会话状态使您能够存储和检索用户的值.HTTP 是一种无状态协议.这意味着 Web 服务器会将针对页面的每个 HTT ...

  9. 如何写出高性能SQL语句

    优化SQL查询:如何写出高性能SQL语句 1.首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生欀如一条SQL语句如果 ...

  10. 表格标签(table、行、列、表头)

    表格标签 一.<table> <table>代表表格标签.   <table></table> 1.width  表示表格宽度,宽度表达方式有像素和百分 ...