PTA-C++(异常处理2)

一.判断题

1.If you are not interested in the contents of an exception object, the catch block parameter may be omitted.。(T)

2.catch (type p) acts very much like a parameter in a function. Once the exception is caught, you can access the thrown value from this parameter in the body of a catch block.。(T)

二.单选题

2-1
Suppose Exception2 is derived from Exception1. Analyze the following code.(A)

try {

statement1;

statement2;

statement3;
}

catch (Exception1 ex1)
{
}

catch (Exception2 ex2)
{
}

A.If an exception of the Exeception2 type occurs, this exception is caught by the first catch block.
B.If an exception of the Exeception2 type occurs, this exception is caught by the second catch block.
C.The program has a compile error because these two catch blocks are in wrong order.
D.The program has a runtime error because these two catch blocks are in wrong order.

2-2
Suppose that statement2 throws an exception of type Exception2 in the following statement:(D)

try {

statement1;

statement2;

statement3;
}

catch (Exception1 ex1)
{
}

catch (Exception2 ex2)
{
}

catch (Exception3 ex3)
{
statement4;
throw;
}

statement5;

A.statement2
B.statement3
C.statement4
D.statement5

2-3
Suppose that statement3 throws an exception of type Exception3 in the following statement:

try {

statement1;

statement2;

statement3;

}

catch (Exception1 ex1)
{
}

catch (Exception2 ex2)
{
}

catch (Exception3 ex3)
{
statement4;
throw;
}

statement5;

Which statements are executed after statement3 is executed?( C)

A.statement2
B.statement3
C.statement4
D.statement5

2-4
下列关于异常的描述中,错误的是(A)。

A.编译错属于异常,可以抛出
B.运行错属于异常
C.硬件故障也可当异常抛出
D.只要是编程者认为是异常的都可当异常抛出

2-5
下列关于异常类的说法中,错误的是。(A)

A.异常类由标准库提供,不可以自定义
B.C++的异常处理机制具有为抛出异常前构造的所有局部对象自动调用析构函数的能力
C.若catch块采用异常类对象接收异常信息,则在抛出异常时将通过拷贝构造函数进行对象复制,异常处理完后才将两个异常对象进行析构,释放资源
D.异常类对象抛出后,catch块会用类对象引用接收它以便执行相应的处理动作

2-6
C++处理异常的机制是由(B)3部分组成。

A.编辑、编译和运行
B.检查、抛出和捕获
C.编辑、编译和捕获
D.检查、抛出和运行

2-11
One of the major features in C++ is (D ) handling,which is a better way of handling errors.

A.data
B.pointer
C.test
D.exception

2-12
What is wrong in the following code?( C)

vector v;
v[0] = 2.5;

A.The program has a compile error because there are no elements in the vector.
B.The program has a compile error because you cannot assign a double value to v[0].
C.The program has a runtime error because there are no elements in the vector.
D.The program has a runtime error because you cannot assign a double value to v[0].

2-13
If you enter 1 0, what is the output of the following code?(D)

#include “iostream”
using namespace std;

int main()

{
// Read two integers

cout << "Enter two integers: ";

int number1, number2;

cin >> number1 >> number2;

try
{
if (number2 == 0)
throw number1;

cout << number1 << " / " << number2 << " is "
  << (number1 / number2) << endl;

cout << "C" << endl;

}
catch (int e)
{
cout << “A” ;
}

cout << “B” << endl;

return 0;
}

A.A
B.B
C.C
D.AB

2-14
The function what() is defined in A______.

A.exception
B.runtime_error
C.overflow_error
D.bad_exception

三.函数题

6-1 求两个数的调和平均数(异常处理)

分数 6
作者 张德慧
单位 西安邮电大学
两个数a和b的调和平均数的计算公式为:2×a×b/(a+b),下列函数hmean计算a和b的调和平均数并进行异常处理。请完成该函数并提交。

函数接口定义:
double hmean(double a, double b) throw(const char );
在这里解释接口参数。例如:其中 a 和 b 都是用户传入的参数。函数须返回 a 和 b 的调和平均数。
裁判测试程序样例:
#include
using namespace std;
/
你提交的代码将嵌入到这里 */

int main()
{
double x, y;
cin >> x >> y;
try {
cout<<hmean(x,y);
}
catch (const char * s)
{
cout << s << endl;
}
return 0;
}
输入样例:
2 -2
输出样例:
bad hmean() arguments: a = -b not allowed

 double hmean(double a, double b) {
    if(a+b==0){
        throw("bad hmean() arguments: a = -b not allowed");
    }
    return (2*a*b)/(a+b);
 }

6-15 除数为零异常

分数 10
作者 张德慧
单位 西安邮电大学
下面是这个程序处理除数为零的异常,在函数 division( )中抛出一个除以零的异常,并在main函数中的 catch 块中捕获该异常。

函数接口定义:
double division(int a, int b);
其中a为被除数,b为除数。

裁判测试程序样例:
#include
using namespace std;

/* 请在这里填写答案 */

int main ()
{
int x,y;
double z = 0;

cin>>x>>y;
try {
    z = division(x, y);
    cout << z << endl;
}catch (const char* msg) {
    cout << msg << endl;
}
return 0;

}
输入样例:
在这里给出一组输入。例如:

2 0
输出样例:
在这里给出相应的输出。例如:

Divided by zero!

double division(int a, int b){
    if(b==0){
        throw("Divided by zero!");
    }
    return a/b;
}

四.编程题

7-1 数字格式异常

分数 10
作者 张德慧
单位 西安邮电大学
(NumberFormatException数字格式异常)编写一个程序,提示用户读取两个整数,然后显示他们的和。程序应该在输入不正确时提示用户再次输入数字。

输入格式:
i 9 (第1次输入)
l 8 (第2次输入)
5 6 (第3次输入)

输出格式:
Incorrect input and re-enter two integers: (第1次输出提示)
Incorrect input and re-enter two integers: (第2次输出提示)
Sum is 11 (输出结果)

输入样例:
i 9
l 8
5 6
输出样例:
Incorrect input and re-enter two integers:
Incorrect input and re-enter two integers:
Sum is 11

暂无

7-16 求平方根函数mySqrt的异常处理

分数 10 作者 张德慧 单位 西安邮电大学 改造下面的求平方根函数mySqrt,当x小于0时,输出错误信息:“Error: Can not take sqrt of negative number”;当x不小于0时,输出x的平方根。要求在main函数中采用C++的异常处理方法。

double mySqrt(double x)
{
return sqrt(x);
}

输入格式:
4

输出格式:
The sqrt of 4 is 2

输入样例:
-9
输出样例:
Error: Can not take sqrt of negative number

if-else语句实现

#include<iostream>
#include<cmath>
using namespace std;

double mySqrt(double x)
{ 
    if (x>=0)
        return sqrt(x); 
    else 
        return -1;
}
int main()
{
    double x,y;
    cin>>x;
    y = mySqrt(x);
    if (y==-1)
        cout<<"Error: Can not take sqrt of negative number";
    else
        cout<<"The sqrt of "<<x<<" is "<<y;
    return 0;
}

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实验1 类的定义、对象数组的使用 1. 定义一个学生类(Student), 属性有 1)非静态属性String studentNumber 2)非静态属性String studentName 3)非静态属性int markForMaths 4)非静态属性int markForEnglish 5)非静态属性int markForScience 方法有: 1)构造方法Student(String number, String name) 2)构造方法Student() 3)String getNumber() 4)String getName() 5)void enterMarks(int markForMaths, int markForEnglish, int markForScience) 6)int getMathsMark() 7)int getEnglishMark() 8)int getScienceMark() 9)double calculateAverage() 10)String toString() 返回学生信息,包括学号、姓名、数学成绩、英语成绩、科学成绩、平均成绩。 注意:为了保证calculateAverage返回double类型,需要把三个分数的和除以3.0,而不是3. 另外,分数的初始值是什么?如果每个分数初始值为0,会造成混淆,分数为0表示还没有输入分数,还是分数确实为0?有更好的初始值吗? 编写Student类,并且编写一个StudentTest类,对Student类进行测试。 StudentTest类运行效果如下: 输入学生学号:2011211301 输入学生姓名:王晓 输入学生三门课成绩(数学,英语,科学):88,79,90 学生信息如下: 学号:2011211301 姓名:王晓 数学成绩:88 英语成绩:79 科学成绩:90 平均成绩:85.66666666666667 2.定义一个StudentList类用来存储Student对象 属性有 1)Student[] list; //list存储学生对象 2)int total; //学生总人数 方法有: 1)StudentList(int length) //length是数组长度 2)boolean add(Student stu) //增加stu到数组中,成功,返回true,否则false 3)boolean remove(int no) //删除第no个数组元素,删除成功,返回true,否则false 4)boolean remove(String number) //删除学号为number的学生,删除成功,返回true,否则false 5)boolean isEmpty() //判断数组是否为空,若是,返回true,否则false 6)Student getItem(int no) //返回第no个学生 7)Student getItem(Student number) //返回学号为number的学生,若该生不存在,返回null。 8) int getTotal() 返回学生总人数 编写StudentList类,并且编写一个StudentListTest类,对StudentList类进行测试。 StudentListTest类运行效果: 菜单如下,输入 1~8代表您要执行的操作: 1. 增加1个学生 2. 根据学号删除学生 3. 根据位置删除学生 4. 判断是否为空 5.根据位置返回学生 6.根据学号返回学生 7. 输出全部学生信息 8.退出程序 输入您的操作:1 输入学生信息: 学号:2011211301 姓名:王晓 数学成绩:88 英语成绩:79 科学成绩:90 ---目前有1个学生,信息为---: 学号:2011211301 姓名:王晓 数学成绩:88 英语成绩:79 科学成绩:90 平均成绩:85.66666666666667 输入您的操作:1 学号:2011211311 姓名:李辉 数学成绩:80 英语成绩:79 科学成绩:93 ---目前有2个学生,信息为---: 学号:2011211301 姓名:王晓 数学成绩:88 英语成绩:79 科学成绩:90 平均成绩:85.66666666666667 姓名:李辉 数学成绩:80 英语成绩:79 科学成绩:93 平均成绩:84.0 输入您的操作:5 输入学生位置:10 对不起,没有对应的学生 输入您的操作:5 输入学生位置:2 学生信息如下: 姓名:李辉 数学成绩:80 英语成绩:79 科学成绩:93 平均成绩:84.0 输入您的操作:3 输入要删除第几个学生:2 删除成功 ---目前有1个学生,信息为:--- 学号:2011211301 姓名:王晓 数学成绩:88 英语成绩:79 科学成绩:90 平均成绩:85.66666666666667 ......//后面的玉运行效果略 作业命名: _学号(8位)_姓名_第几次作业(1位)_类名.java eg: _09211195_王晓_1_Student.java //定义Student类 _09211195_王晓_1_StudentList.java //定义StudentList类 为便于作业批改和查找,上传作业的时候,传.java文件,不要上传压缩包,一次作业可以有多个.java文件 为便于作业批改,要求源文件中的类名也需要和java文件名一致。如: public class _09211195_王晓_1_Student{ //略 }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值