【笔试题】Java 继承知识点检测
笔试题 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?
- Private methods are final.
- Protected members are accessible within a package and inherited classes outside the package.
- Protected methods are final.
- 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.
- In Java all classes inherit from the Object class directly or indirectly. The Object class is root of all classes.
- Multiple inheritance is not allowed in Java.
- 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
```
解决方案
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 继承知识点检测的更多相关文章
- 剑指Offer——网易校招内推笔试题+模拟题知识点总结
剑指Offer--网易校招内推笔试题+模拟题知识点总结 前言 2016.8.2 19:00网易校招内推笔试开始进行.前天晚上利用大约1小时时间完成了测评(这个必须做,关切到你能否参与面试).上午利用2 ...
- Java面试中笔试题——Java代码真题,这些题会做,笔试完全可拿下!
大家好,我是上海尚学堂Java培训老师,以下这些Java笔试真题是上海尚学堂Java学员在找工作中笔试遇到的真题.现在分享出来,也写了参考答案,供大家学习借鉴.想要更多学习资料和视频请留言联系或者上海 ...
- Android开发面试经——3.常见Java基础笔试题
Android开发(29) 版权声明:本文为寻梦-finddreams原创文章,请关注:http://blog.csdn.net/finddreams 关注finddreams博客:http:/ ...
- 2016最新Java笔试题集锦
更新时间:2015-08-13 来源:网络 投诉删除 [看准网(Kanzhun.com)]笔试题目频道小编搜集的范文“2016最新Java笔试题集锦”,供大家阅读参考, ...
- 【笔试题】Java 中如何递归显示一个目录下面的所有目录和文件?
笔试题 Java 中如何递归显示一个目录下面的所有目录和文件? import java.io.File; public class Test { private static void showDir ...
- 【笔试题】Java 易错题精选
笔试题 Java 易错题精选 1.写出下列程序的运行结果( )String 不变性Java 值传递 public class Test { public static void main(String ...
- [原创]Java常见笔试题知识点汇总
前天数梦工厂来学校招聘,笔试题比较有特点,全是Java题,基本就是Java的一些特点.凭记忆按照题目找到一些必备知识点 (1). try {}里有一个return语句,那么紧跟在这个try后的fina ...
- 【笔试题】Java笔试题知识点
Java高概率笔试题知识点 Java语法基础部分 [解析]java命令程序执行字节码文件是,不能跟文件的后缀名! 1.包的名字都应该是由小写单词组成,它们全都是小写字母,即便中间的单词亦是如此 2.类 ...
- JAVA 150道笔试题知识点整理
JAVA 笔试题 整理了几天才整理的题目,都是在笔试或者面试碰到的,好好理解消化下,对你会有帮助,祝你找工作顺利,收到满意的 offer . 1.Java 基础知识 1.1 Java SE 语法 &a ...
随机推荐
- 「Linux」centos7更新python3.6后yum报错问题
1. #vi /usr/bin/yum 因为我的旧版本是2.7,所以将#!/usr/bin/python改为#!/usr/bin/python2.7就可以了! 退出保存 2.可能还会报错 就修改/us ...
- [Java多线程]-Thread和Runable源码解析之基本方法的运用实例
前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...
- K8S dashboard 创建只读账户
1.创建名字为“Dashboard-viewonly“的Cluster Role,各种资源只给予了list,get,watch的权限.dashboard-viewonly.yaml --- apiVe ...
- java project转变成java web project
首先,你的eclipse必须得装有web插件 1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下: 找到:<natures> </natures&g ...
- C++对象与其第一个非静态成员地址相同
由于对象的内存空间里是按顺序依次存储了它的非静态成员变量,因此对象和它的第一个非静态成员变量的地址是相同的: class A { private: int i; } int main() { A a; ...
- SQLServer字符串的一些截取技巧
先看一张科学家的生卒年月表 截取科学家的出生年份可以很方便的用left函数截取,如果要截取去世年份,需要先获取字符“—”的位置. select substring(c,charindex('—',c) ...
- 使用HttpClient4来构建Spring RestTemplate
Spring RestTemplate简单说明 现在REST服务已经很普及了,在我们的程序中,经常会需要调用REST API,这时候会有很多选择,原始一点的JDK自带的,再进一步点使用HttpClie ...
- Linux 操作系统下 VI 编辑器常用命令详细介绍
一.Vi 简介 vi是unix世界中最通用的全屏编辑器,linux中是用的是vi的加强版vim,vim同vi完全兼容,vi就是"visual interface"的缩写.它可以执行 ...
- HTML DOM 节点介绍(nodeName,nodeValue,nodeType)
对于初学者来说,HTML DOM 里面的 nodeName.nodeValue 以及 nodeType 三个属性的作用和取值不是很清楚.下面整理的信息包含有关于节点的详细信息,供参考. 节点信息 每个 ...
- CodeForces 990C
Description A bracket sequence is a string containing only characters "(" and ")" ...