【笔试题】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 ...
随机推荐
- 【题解】期末考试 六省联考 2017 洛谷 P3745 BZOJ 4868 贪心 三分
题目传送门:这里是萌萌哒传送门(>,<) 啊♀,据说这题有个完全贪心的做法,但是要维护太多东西好麻烦的(>,<),于是就来口胡一发三分的做法. 思路很简单,假设我指定了一个x, ...
- Linux安全之SYN攻击原理及处理
TCP自从1974年被发明出来之后,历经30多年发展,目前成为最重要的互联网基础协议,但TCP协议中也存在一些缺陷. SYN攻击就是利用TCP协议的缺陷,来导致系统服务停止正常的响应. SYN攻击原理 ...
- mysql 自动记录数据插入及最后修改时间
总结: `uptime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP 原文 应用场景: 1.在数据 ...
- windows如何要查看IIS连接数
1.运行,输入,perfmon.msc 2.在系统监视器,区域点击,添加计数器. 3.在“添加计数器”窗口,“性能对象”选择Web Service,“从列表选择计数器”选中Current Connec ...
- Asp.Net MVC +EF CodeFirst+多层程序设计
1.概述 这是一个基于个人博客的一个项目,虽然博客根本没必要做这么复杂的设计.但是公司有需求,所以先自己弄个项目练练手.项目需要满足下列需求 1.层与层之间需要解耦,在后期上线更新维护时不需要覆盖,只 ...
- web启动@Autowired不能自动注入
使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...
- [洛谷P4609] [FJOI2016]建筑师
洛谷题目链接:[FJOI2016]建筑师 题目描述 小 Z 是一个很有名的建筑师,有一天他接到了一个很奇怪的任务:在数轴上建 \(n\) 个建筑,每个建筑的高度是 \(1\) 到 \(n\) 之间的一 ...
- Shiro实战教程(一)
Shiro完整架构图 Shiro认证过程 Shiro授权的内部处理机制 Shiro 支持三种方式的授权 1.编程式:通过写if/else 授权代码块完成: Subject subject = Secu ...
- E.Text Editor (Gym 101466E + 二分 + kmp)
题目链接:http://codeforces.com/gym/101466/problem/E 题目: 题意: 给你s串和t串,一个数k,求t的最长前缀串在s串中出现次数不少于k. 思路: 一眼二分+ ...
- NYOJ 93 汉诺塔 (数学)
题目链接 描述 在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针.印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片 ...