2011年计算机二级Java对象、类章节练习题及答案
1.java的jvm虚拟机可自动处理__________.
A) 抛出异常
B)捕获异常
C)运行异常
D)声明异常
2.try与catch的匹配是___________.
A)catch中对象名指向try中可能抛出的异常类或父类
B)catch中对象名指向try中可能抛出的异常类的子类
C)catch中对象名指向try中可能抛出的异常的不同类
D)catch中对象名指向try中可能抛出的异常的不同类的父类
3.catch(Exception e){语句的正确排列次序是__________.
A)最前
B)最后
C)中间
D)任意位置
4.一个try语句后必须在catch和__________.
A)break语句
B)continue语句
C)return 语句
D)finally语句
5.为获得异常堆栈使用轨迹,应该利用_____________.
A)fillinStack()方法
B)printStackTrace()方法
C)fillinStackTrace()方法
D)toString()方法
6.请利用Java类为中java.util.GregorianCalendar这个已有的类,编出一个可根据命令行参数打印月历的Java程序.(使用已有类)
7.请根据教程74页描述的Employee和Manager类,并(继承)补充条件:
姓名 年薪 加入公司时间 经理年津贴
Manager: CarlCraker $80 000 1987.12.15 $5 000
Employee: Harry Hacker $50 000 1989.10.1
Employee: Tommy Tester $40 000 1990.3.15
编写一个完整的JAVA程序,打印出按照5%增加年薪后的工资表。
8.改写上一道题的程序,在打印工资表之前按照年薪自低到高排序后打印出来。(接口、对象数组排序)
9.请编写一个完整的JAVA程序,依次打印出命令行参数字符串数组各元素。(字符串数组)
考试大编辑整理编辑推荐:
2011年计算机二级考试JAVA知识点整理汇总
计算机等级考试二级Java模拟试题及答案汇总
计算机等级考试二级Java练习题及解析汇总
全国计算机等级考试二级笔试样卷Java语言程序设计
答案
1,c 2,a 3.b 4,d 5.b
6.本程序可利用的方法如下:
GregorianCalendar.get(Calendar.DAY_OF_MONTH)
.get(Calendar.MONTH)
.get(Calendar.DAY_OF_WEEK)
.get(Calendar.DAY_OF_MONTH ,1)
可利用计数循环、条件循环打印月历。
7.程序代码:
Import java.util.*;
Public class ManagerTest1
{
Public static void main(String[] args)
{
Manager boss=new Manager(“Carl Cracker”,80000,1987,12,15);
Boss.setBonus(5000);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee(“Harry Hacker”,50000,1989,10,1);
staff[2]=new Employee(“Tommy Tester”,40000,1990,3,15);
for(int i=0; i<=staff.length; i++)
{ Employee e=staff[i];
e.raiseSalery(5); System.out.println(“name”=+e.getName()+”,salary=”+e.getSalary());
}
}
}
class Employee
{
Public Employee(String n, double s, int year, int month, int dat)
{
name=n;
salary=s;
GregorianCalendar calendar=new GregorianCaledar(year, month-1, day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date get HireDay()
{
Return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise=salary*byPercent/100;
salary+=raise;
}
private String name;
private double salary;
private Date hireDay;
}
class Manager estends Employee
{
public Manager(String n, double s, int year, int month, int day)
{
super(n, s, year, month, day);
bonus=0 ;
}
public double getSalary()
{
double baseSalary=super.getSalary();
return baseSalary+hours;
}
public void setBonus(double b)
{
bonus=b;
}
private double bonus;
}
程序运行结果为:
D:\code>java ManagerTest1
name=Car1 Cracker,salary=89000.0
name=Harry Hacker,salary=52500.0
name=Tommy Tester,salary=42000.0
考试大编辑整理编辑推荐:
2011年计算机二级考试JAVA知识点整理汇总
计算机等级考试二级Java模拟试题及答案汇总
计算机等级考试二级Java练习题及解析汇总
全国计算机等级考试二级笔试样卷Java语言程序设计
8.定义一个含有主方法的类,定义Manager对象,定义Employee对象数组。为对象数组赋值。用计数循环加薪并打印姓名和年薪。
程序代码:
import java.util.*;
public class ManagerTest2
{ public static void main(String) args)
{ Manager boss=new Manager(“Carl Cracker”, 80000, 1987,12,15);
boss.setBonus(5000);
Employee[] staff=new Employee[3];
staff[0]=boss;
staff[1]=new Employee(“Harry Hacker”, 50000, 1989,10, 1);
staff[2]=new Employee(“Tommy Tester”, 40000, 1990,3,15);
Array.sort(staff);
For(int i=0; i<=staff.length; i++)
{ Employee e= staff[i];
e.raiseSalary(5);
System.out.println(“name=”+e.getName()+”,salary=”+e.getSalary());
}
}
}
class Employee implements Comparable
{ public Employee(String n, double s, int year, int month, int day)
{ name =n;
salary=s;
GregorianCalendar calendar=new GregorianCalendar(year, month-1, day);
hireDay=calendar.getTime();
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public Date get HireDay()
{
Return hireDay;
}
public void raiseSalary(double byPercent)
{ double raise=salary*byPercent/100;
salary+=raise;
}
public int compareTo(Object otherobject)
{ Employee other=(Enployee)otherObject;
if(salary if(salary>other.salary) return 1; return 0; } private String name; private double salary; private Date hireDay; } class Manager estends Employee { public Manager(String n, double s, int year, int month, int day) { super(n, s, year, month, day); bonus=0 ; } public double getSalary() { double baseSalary=super.getSalary(); return baseSalary+hours; } public void setBonus(double b) { bonus=b; } private double bonus; } 程序运行结果为: D:\code>java ManagerTest2 name= Tommy Tester,salary =42000.0 name=Harry Hacker,salary=52500.0 name= Car1 Cracker,salary =89000.0 排序方法可用 java.util.Arrays.sort(staff)给对象数组排序,其中staff是雇员对象数组名。 Employee类要实现java.lang.Comparable接口,要实现接口内的抽象方法comparaTo()方法。 9.主方法的命令行参数是一个字符数组,即:String[] args 程序代码: public class ConLinePara { public static void main(String[] args) ( if(args.length<1) System.exit(0); else System.out.println(“The conmand line parameters:”); for(int i=0; i
{ System.out.println(“ “); System.out.println(args[i]); } } } 程序运行结果为: D:\code\java CommLinePara How are you! The command line parameters: How are you! D:\code\ 编辑推荐: 2011年计算机二级考试JAVA知识点整理汇总 计算机等级考试二级Java模拟试题及答案汇总 计算机等级考试二级Java练习题及解析汇总 全国计算机等级考试二级笔试样卷Java语言程序设计相关阅读