- 1、本文档共16页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
JAVA基础面试题答案版
JAVA语言基础笔试题-2
Question 1
Given:
11.classA {
12. public void process() { System.out.print(“A “)} }
13. class B extends A {
14. public void process() throws RuntimeException {
15. super.process();
16. if (true) throw new RuntimeException();
17. System.out.print(“B”)
}}
18. public static void main(String[] args) {
19. try { ((A)new B()).process(); }
20. catch (Exception e) { System.out.print(“Exception “)}
21. }
What is the result?
A. Exception
B. A Exception
C. A Exception B
D. A B Exception
E. Compilation fails because of an error in line 14.
F. Compilation fails because of an error in line 19.
答案:B
考点:方法的重写(重写方法异常抛出部分的理解)
多态
异常处理
说明:
子类重写父类方法,不能抛出比父类方法更多的异常,但此处子类重写方法声明抛出了RuntimeException,不算多抛,算是平抛,是可以的。
RuntimeException是Exception的子类,可以被Exception捕获。
Question 2
Given:
11. static class A {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println(“B”)}
16. }
17. public static void main(String[] args) {
18 .A a=new B();
19. a.process();
20.}
What is the result?
A. B
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 15.
E. Compilation fails because of an error in line 18.
F. Compilation fails because of an error in line 19.
答案:F
考点:方法的重写(重写方法异常抛出部分的理解)
多态
静态内部类以及其实例的创建
说明:
a.process(); 是多态调用,调用的应该是类B的process方法,这个方法只是允许抛出RuntimeException, 所以19行在理论上不需要进行异常相关处理,系统会自动抛出该异常,但是多态只是在运行时,系统方能识别,在编译的时候,系统还是按照类A的process方法来进行验证,所以出现错误,因为类A抛出的是检查异常,必须显式被捕获或者抛出。
Question 3
Given:
11. static classA {
12. void process() throws Exception { throw new Exception(); }
13. }
14. static class B extends A {
15. void process() { System.out.println(“B”);
16. }
17. public static void main(String[] args) {
18. new B().process();
19. }
What is the result?
A. B
B. The code runs with no output.
C. Compilation fails because of an error in line 12.
D. Compilation
文档评论(0)