程序语言与编程实践7-> Java实操4 | 第三周作业及思路讲解 | 异常处理考察
第三周作业,可能是异常那一章当时没怎么听,此前也不怎么接触,感觉还挺陌生的。
00 第1题
00-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
public class Homework_week3 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int a=0, b=5;
        try{
            System.out.print(a/b+b/a);
        }
        catch
        {
            System.out.println("Exceptions!!!");
        }
    }
}
编译以上代码,会出现什么错误?
A. Prints: Exceptions!!!
B. Prints Nothing
C. Syntax error
D. Runtime Error
E. None of the above
00-2 解答
Answer: D
catch处语法出错,导致不能编译: Uncompilable source code - 非法的类型开始;
如果改为:
catch (ArithmeticException e){
	System.out.println("Exceptions!!!");
}
那么就会输出Exceptions!!!。
考察的是try + catch + finally异常捕获机制的语法:如果try{}抛出的对象属于 catch() 括号内欲捕获的异常类,则 catch() 会捕捉此异常,然后进到 catch() 的块里继续运行。
01 第2题
01-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
public class Homework_week3 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int a=0, b=5;
        String c[] = {"A","B","C"};
        try{
            for(int i = 1;i < 4; i++){
                System.out.print(c[i]);
            }
            System.out.print(a/b+b/a);
        }
        catch (ArithmeticException e)
        {
            System.out.println("D");
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
            System.out.println("E");
        }
    }
}
编译并执行代码,会出现哪种结果?
A. Prints: ABC
B. Prints: ABD
C. Prints: BCE
D. Prints: BCDEE. Compiler Error
01-2 解答
**Answer: **C
这道题就考察了try + catch + finally的运行机制,检测到for循环里出现数组下标溢出,就抛出异常,产生中断,接着匹配到第二个catch语句块catch(ArrayIndexOutOfBoundsException),输出E。
考察的是try + catch + finally异常捕获机制的捕获到异常立刻中断,进入异常处理。
02 第3题
02-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
public class Homework_week3 {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int a=0, b=5;
        String c[] = {"A","B","C"};
        try{
            System.out.print(c[a/b]);
            try{
                for(int i = 1;i < 4; i++){
                    System.out.print(c[i]);
                }
            }
            catch (Exception e)
            {
                System.out.println("D");
            }
            finally{
                System.out.println("E");
            }
        }
        catch(Exception e)
        {
            System.out.println("F");
        }
        finally{
            System.out.println("G");
        }
    }
}
编译并执行代码,会出现哪种结果?
A. Prints: AABCG
B. Prints: ABCDG
C. Prints: AABCDG
D. Prints: AABCDEG
E. Prints: AABCDEFG
02-2 解答
Answer: D
两个try + catch + finally的嵌套,考察的知识是:
" 无论 try 程序块是否有捕捉到异常,或者捕捉到的异常是否与 catch()括号里的异常相同,最后一定会运行 finally 块里的程序代码。finally 的程序代码块运行结束后,程序再回到 try-catch-finally 块之后继续执行。"
03 第4题
03-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
public class Homework_week3 {
    /**
     * @param args the command line arguments
     */
    public static void main( String[] args ) {
         int src[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
         int res[] = {1, 2, 3, 4, 5};
         System.arraycopy(src, 0, res, 0, src.length);
         for(int i=0; i<res.length; i++) {
             System.out.print(res[i]);
         }
     }
}
What is the result?
A. 10987654321
B. 10987612345
C. 12345612345
D. Compiler error
E. Runtime exception
03-2 解答
Answer: E
很明显,这里的语句都符合语法,所以Compiler error可以排除;
而arraycopy()函数的语法:
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
参数说明:
- Object src : 原数组
- int srcPos : 拷贝到原数组的起始位置
- Object dest : 目标数组
- int destPos : 目标数组的开始起始位置
- int length : 要copy的数组的长度
题目中,要向长度为5的数组放入10个元素,所以运行到这个函数的时候,会发生越界错误,采取java默认的异常处理机制,直接抛出异常中断:ArrayIndexOutOfBoundsException。
04 第5题
04-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
public class Homework_week3 {
    /**
     * @param args the command line arguments
     */
    public static void main( String[] args ) {
         byte a[] = new byte[2];
         long b[] = new long[2];
         float c[] = new float[2];
         Object d[] = new Object[2];
         System.out.print(a[1]+","+b[1]+","+c[1]+","+d[1]);
     }
}
编译并执行代码,会出现哪种结果?
A. Prints: 0,0,0,null
B. Prints: 0,0,0.0,null
C. Prints: 0,0,0,0
D. Prints: null,null,null,null
E. The code runs with no output.
04-2 解答
Answer: B
在new一个数组的时候,默认初始化为0,当然,这种初始化契合于数据类型,比如float要初始化为0.0,Object初始化为null。
05 第6题
05-1 题目
1. class A {
2.     public static void main(String[] args) {
3.         int[ ] var1;
4.         int[5] var2;
5.         int[] var3;
6.         int var4[];
7.         }
8.     }
编译并执行代码,会出现哪种结果?
A. compile-time errors occur at line 3
B. compile-time errors occur at line 4
C. compile-time errors occur at line 5
D. compile-time errors occur at line 6
E. None of the above
05-2 解答
Answer: B
这两道题考察的都是数组的声明、分配内存和初始化,
一维数组的声明与分配内存:
数据类型 数组名[ ] ; // 声明一维数组
数组名 = new 数据类型[个数] ; // 分配内存给数组声明数组的同时分配内存:
数据类型 数组名[] = new 数据类型[个数]
06 第7题
06-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
public class Homework_week3 {
    /**
     * @param args the command line arguments
     */
    static void my() throws ArithmeticException {
         System.out.print("A");
         throw new ArithmeticException("A");
          }
     public static void main (String args []) {
         try {
             my();
         }
         catch (Exception e) {
             System.out.print("B");
             }
         finally {
             System.out.print("C");
             }
         }
}
编译并执行代码,会出现哪种结果?
A. Prints: A
B. Prints: AC
C. Prints: ABC
D. Prints: AABC
E. Prints: C
06-2 解答
Answer: C
考察的是throw指定方法抛出异常的知识:
用处:
如果方法内的程序代码可能会发生异常,且方法内又没有使用任何的代码块来捕捉这些异常时,则必须在声明方法时一并指明所有可能发生的异常,以便让调用此方法的程序得以做好准备来捕捉异常。也就是说,如果方法会抛出异常,则可将处理这个异常的
try-catch-finally块写在调用此方法的程序代码内。
语法:
方法名称(参数…) throws 异常类 1,异常类 2,…
具体执行:
throws在指定方法中不处理异常,在调用此方法的地方处理,如果指定方法中异常,则在调用处进行处理(进入catch())
所以这道题的运行过程为:main函数-> try()块-> my函数-> 输出A -> new ArithmeticException(“A”)-> catch()-> 输出B-> finally()-> 输出C
07 第8题
07-1 题目
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author zzrs123
 */
class B extends Exception {}
class C extends B {}
class D extends C {}
public class Homework_week3 {
     /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        int a,b,c,d,x,y,z;
        a = b = c = d = x = y = 0;
        z = 1;
        try {
            try {
                switch(z) {
                   case 1: throw new B();
	           case 2: throw new C();
                   case 3: throw new D();
                   case 4: throw new Exception();
                }
                a++;//a=0
            }
            catch ( C e ) {b++;}//b=0
            finally{c++;}//c=1
        }
        catch ( B e ) {d++;}//d=1
        catch ( Exception e ) {x++;}//x=0
        finally {y++;}//y=1
        System.out.print(a);
        System.out.print(b);
        System.out.print(c);
        System.out.print(d);
        System.out.print(x);
        System.out.print(y);
    }
}
编译并执行代码,会出现哪种结果?
A. 0,0,1,1,0,1
B. 0,1,0,1,1,0
C. 0,0,1,1,0,1
D. 0,1,1,1,1,1
E. 1,1,0,1,0,0
07-2 解答
Answer: AC
这道题的亮点是异常类的层层继承,人脑运行的关键在于:如果报了一个类的错误,那么catch其父类和子类的块会不会响应。
答案是不会,catch只能识别错误本身。
此外就是运行到case 1: throw new B();时,程序就中断,直接进入catch()区,不会运行a++。
08 第9题
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package homework_week3;
/**
 *
 * @author shandaiwang
 */
1.class B extends Exception {
2.    public void myMethod( ) throws RuntimeException {}
3.}
4.
5.public class Homework_week3 extends B {
     /**
     * @param args the command line arguments
     */
6.      public void myMethod( ) throws Exception {}
7.      public static void main (String[] args) {}
8.}
编译错误会出现在哪一行?
A. 1
B. 2
C. 6
D. 7
E. None of the above
08-2 解答
Answer: C
0330 这个问题不是很明白,还是编译错误,所以也很难从IDE得到点什么,查了查,有两点:
- 子类中覆盖方法的访问权限不能比父类小,比如父类的myMethod( )是public类型,子类的不能是private。
- 子类中覆盖方法抛出的异常只能比父类中原方法抛出的异常低级,比如父类抛出Exception,而子类的覆盖方法只能抛出RuntimeException之类的子异常。
09 第10题
09-1 题目
class A {
    public static void main (String[] args) {
        int a=1, b=0;
        int c[] = {1,2,3};
        try {
            System.out.print(c[1]);
            try {
                System.out.print(a/b+b/a);
            }
            catch (ArithmeticException e){
                System.out.print(“C”);
            }
        }
        catch (ArrayIndexOutOfBoundsException e) {
            System.out.print(“A”);
        }
        finally {
            System.out.print(“B”);
        }
    }
}
编译并执行代码,会出现哪种结果?
(A) 1BC
(B) 1CB
(C) 2BC
(D) 2CB
(E) 2AC
09-2 解答
Answer: D
这道题相对常规,是一个try-catch-finally的嵌套问题,
System.out.print(c[1]);
try {
    System.out.print(a/b+b/a);
}
catch (ArithmeticException e){
    System.out.print(“C”);
}
正常输出c[1] 为2,然后进入内层的 try,发现异常,捕获输出 C,接着出来进入外层 try 的catch,未捕获异常,进入外层的 finally ,输出 B 。
程序语言与编程实践7-> Java实操4 | 第三周作业及思路讲解 | 异常处理考察的更多相关文章
- 程序语言与编程实践2-> 蓝桥杯C/C++备赛记录1 | 入门了解与首周训练
		寒假前班主任帮我们报了名,是得好好准备准备.作为一个CSer,coding能力一定不能太弱.我反思,好久没写C/C++代码了,净是些随手写的python脚本,刚开始上手题目bug一大堆. 由于也不是啥 ... 
- 程序语言与编程实践4-> 蓝桥杯C/C++备赛记录2 | 第二周学习训练
		0323,又是一周星期三,按道理该总结了.这周前几天写题比较多,后面事情多了起来,就没怎么写了.主要方向是洛谷的基本语法熟悉,PTA平台数据结构的一些题目. 0323附上: 题目比较多,所以文章可能有 ... 
- 1903021121—刘明伟—Java第三周作业—学习在eclipse上创建并运行java程序
		项目 内容 课程班级博客链接 19信计班(本) 作业要求链接 第三周作业 作业要求 每道题要有题目,代码,截图 扩展阅读 eclipse如何创建java程序 java语言基础(上) 扩展阅读心得: 想 ... 
- 2017-2018-1 JAVA实验站 第三周作业
		2017-2018-1 JAVA实验站 第三周作业 团队展示 队名 JAVA实验站 拟作的团队项目描述 (2048)增加其他模式,使得2048更加丰富多彩 团队的首次合照 团队的特色描述 团队内部很团 ... 
- JAVA第三周作业(从键盘输入若干数求和)
		JAVA第三周作业(从键盘输入若干数求和) 在新的一周,我学习了JAVA的IO编程.下面的代码实现了从键盘输入若干数求和的目标.import java.util.Scanner; public cla ... 
- 2017-2018-1 JAVA实验站 第六、七周作业
		2017-2018-1 JAVA实验站 第六.七周作业 详情请见团队博客 
- 2017-2018-1 JAVA实验站 第四、五周作业
		2017-2018-1 JAVA实验站 第四.五周作业 JAVA实验站小组成员 学号 名字 职务 20162318 张泰毓 组长 20162303 石亚鑫 组员 20162304 张浩林 组员 201 ... 
- Java语言与C语言混合编程(2)--在Java中调用C语言本地库
		在上一篇文章中介绍了Java语言中的native关键字,以及Java语言调用C语言的编译生成本地动态链接库(DLL)实现加法运算的小例子,本文通过一个更加详细的例子,深入讲解Java语言调用C语言的函 ... 
- 【原创】大叔经验分享(17)编程实践对比Java vs Scala
		scala 官方地址 https://www.scala-lang.org/ 本文尽可能包含了一些主要的java和scala在编程实践时的显著差异,展现scala的代码的简洁优雅:scala通吃< ... 
随机推荐
- 使用java程序完成大量文件目录拷贝工作
			java程序完成目录拷贝工作 背景描述:我目录有140多个,每个目录里面都有一个src目录.我现在想要所有的src目录移动到同一个目录中. package com.util.cp; import ja ... 
- nginx域名转发
			场景1:因服务器限制,所以只对外开放了一个端口,但是需要请求不同的外网环境,所以在中转服务器上用nginx做了一次转发 实现: server { listen 8051; server_name lo ... 
- 华为eNSP的防火墙(USG6000V)如何使用Web界面登入
			文章目录 华为eNSP的防火墙(USG6000V)如何使用Web界面登入 前言 一.使用步骤 1.导入USG6000V的镜像包 总结 前言 在华为的eNSP的模拟器上如何使用Web界面去管理与使用模拟 ... 
- 【01】Maven依赖插件之maven-dependency-plugin
			一.插件目标(goal) 1.analyze:分析项目依赖,确定哪些是已使用已声明的,哪些是已使用未声明的,哪些是未使用已声明的 2.analyze-dep-mgt:分析项目依赖,列出已解析的依赖项与 ... 
- 【windows 操作系统】什么是窗口?|按钮也是窗口
			起因 在看操作系统消息机制的时候,看到一句化:全局消息队列把消息发送到窗口所在的线程消息队列.突然就怀疑起了窗口的意思.于是就有这边基类. 文章来源:https://docs.microsoft.co ... 
- 【C# 表达式树 三】ExpressionType 节点类型种类
			// // 摘要: // 描述表达式目录树的节点的节点类型. public enum ExpressionType { // // 摘要: // 加法运算,如 a + b,针对数值操作数,不进行溢出检 ... 
- (一)    operator、explicit与implicit  操作符重载
			原文地址: Click Here 操作符重载必须用public static 应为操作符是用来操作实例的. operator operator ... 
- 图解volatile
			volatile是什么 出去面试的时候,很多面试官都会问你:说说你对volatile的理解. 下面我将用图的方式告诉大家,volatile是什么? 如上图所示:每个线程都有自己的工作内存,同时还能访问 ... 
- MyBatis核心对象
			MyBatis 有三个基本要素: 核心接口和类 MyBatis核心配置文件(mybatis-config.xml) SQL映射文件(mapper.xml) 下面首先介绍 MyBatis 的核心接口和类 ... 
- Shell编程四剑客包括:find、sed、grep、awk
			一.Shell编程四剑客之Find Find工具主要用于操作系统文件.目录的查找,其语法参数格式为: find path -option [ -print ] [ -exec -ok command ... 
