首页 03_JAVA常用类及异常集合考试

03_JAVA常用类及异常集合考试

举报
开通vip

03_JAVA常用类及异常集合考试常用类及异常集合考试 一、 选择题 1、 下列的哪个程序段可能导致错误? A)String s = " hello"; String t = " good "; String k = s + t; B)String s = " hello"; String t; t = s[3] + "one"; C)String s = " hello"; String standard = s.toUpperCase(); D) String s = "hello"; String t = s +...

03_JAVA常用类及异常集合考试
常用类及异常集合考试 一、 选择 快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题 1、 下列的哪个程序段可能导致错误? A)String s = " hello"; String t = " good "; String k = s + t; B)String s = " hello"; String t; t = s[3] + "one"; C)String s = " hello"; String standard = s.toUpperCase(); D) String s = "hello"; String t = s + "good";   答案 八年级地理上册填图题岩土工程勘察试题省略号的作用及举例应急救援安全知识车间5s试题及答案 :B 2、 顺序执行下列程序语句后,则b的值是 String a="Hello"; String b=a.substring(0, 2); A)Hello B)hello C)He D)null         答案:C 3、 顺序执行下列程序语句后,结果是 public class Foo { public static void main(String[] args) { String s; System.out.println("s=" + s); } } A)打印 s= B)打印 s=null C)编译错误 D)空指针异常         答案:C 4、 下列代码的执行结果是: class Test5 { public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); System.out.print(s1 == s2); System.out.print(","); System.out.println(s1.equals(s2)); } } A)true,false B)true,true C)false,true D)false,false         答案:C 5、 下列代码的执行结果是: public static void main(String arg[]) { StringBuffer a = new StringBuffer("A"); StringBuffer b = new StringBuffer("B"); operate(a, b); System.out.println(a + "," + b); } static void operate(StringBuffer x, StringBuffer y) { x.append(y); y = x; } A)AB,B B)A,B C)AB,A D)AB,AB         答案:A 6、 下列代码的执行结果是: String str1 = "This is a test!"; StringBuffer str2 =new StringBuffer( "This is a test!"); str1 = str1+"Hi"; str2.append("Hi"); System.out.println("str1 == " + str1); System.out.println("str2 == " + str2); A)This is a test! This is a test! Hi B)This is a test! Hi This is a test! C)This is a test! This is a test! D)This is a test! Hi This is a test! Hi   答案:D 7、 下列代码的执行结果是: public class IntegerTest { int i; IntegerTest(int i) { this.i = i; } } public class TestString { public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("abc"); IntegerTest i1 = new IntegerTest(100); IntegerTest i2 = new IntegerTest(100); System.out.println("s1==s2:"+(s1 == s2)); System.out.println("s1.equals(s2):"+(s1.equals(s2))); System.out.println("i1==i2:"+(i1 == i2)); System.out.println("i1.equals(i2):"+(i1.equals(i2))); } } A)false true false false B)false true false true C)false false false false D)false true true false   答案:A 8、 下面代码能最后打印的值是? public class TestValue { private static int a; public static void main(String[] args) { modify(a); System.out.println(a); } public static void modify(int a) { a++; } } A)编译错误 B)null C)0 D)1         答案:C 9、 下列关键字中那些不属于异常处理的 A)try{}catch(){} B)throws C)Exception D)final         答案:D 10、 下面说法正确的是 A)finally块只有在出现异常的情况下执行 B)如果在try块中包含return语句,那么finally块将不会执行 C)finally块代码总是会运行的 D)finally块必须跟在catch块之后         答案:C 11、 阅读下面代码 class Input { public static void main(String[] args) { String s = “-”; try { doMath(args[0]); s += “t” // 第6行 } finally  { System.out.println(s+=”f ”);} } public static void doMath(Stirng a) { int y = 7 / Integer.parsetInt(a); } } 当在控制台执行 java Input java Input 0    后,下面说明正确的是: A)第6行执行了1次 B)第6行执行了2次 C)第6行执行了0次 D)finally块执行了0次         答案:C 12、 try{ int a = Integer.parseInt(“two”) } 可以用下面哪种异常来捕捉 A)NullPonintException B)ClassCastException C)NumberFormatException D)SQLException         答案: 13.可实现有序对象的操作有哪些?(  CD ) A.HashMap    B.HashSet      C.TreeMap      D.LinkedList 14.迭代器接口(Iterator)所定义的方法是(  ABC )。 A.hasNext()                B.next() C.remove()                D.nextElement() 14.下列方法属于java.lang.Math类的有(方法名相同即可)( ABCD )。 A.random()    B.abs()    C.sqrt()        D.pow() 15.指出正确的 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 达式有(  AB )。 A.double a=2.0;        B.Double a=new Double(2.0); C.byte A= 350;        D.Byte a = 120; 16.System类在哪个包中?( B ) A.java.awt    B.java.lang  C.java.util  D.java.io 17.关于Float,下列说法正确的是(  ABC )。 A.Float在java.lang包中      B.Float a=1.0是正确的赋值方法 C.Float是一个类            D.Float a= new Float(1.0)是正确的赋值方法 18、请问所有的异常类皆继承哪一个类?( A  ) A.java.lang.Throwable          B.java.lang.Exception C.java.lang.Error              D.java.io.Exception 19.哪个关键字可以抛出异常?( B ) A.transient      B.throw    C.finally      D.catch 20.对于已经被定义过可能抛出异常的语句,在编程时(  A )。 A.必须使用try/catch语句处理异常,或用throw将其抛出。 B.如果程序错误,必须使用 try/catch语句处理异常。 C.可以置之不理。 D.只能使用try/catch语句处理。 21.下面程序段的执行结果是什么?(  B ) public class Foo{ public static void main(String[] args){ try{ return;} finally{System.out.println("Finally"); } } } A.编译能通过,但运行时会出现一个例外。  B.程序正常运行,并输出 "Finally"。 C.程序正常运行,但不输出任何结果。 D.因为没有catch语句块,所以不能通过编译。 22.下列程序中构造了一个set并且调用其方法add(),输出结果是(  B )。 import java.util.*; public class T1_2 { public int hashCode() { return 1; } public boolean equals(Object b) { return true; } public static void main(String args[]) { Set set = new HashSet(); set.add(new T1_2()); set.add(new String("ABC")); set.add(new T1_2()); System.out.println(set.size()); } } A.1  B . 2  C.3  D .无结果 2、上机题 1、函数y=x*x(x是整形,0= itemMap; //商品编号与订单项的键值对 public ShoppingCar(){ //初始化购物车 } public void buy(int nid){ //如果该商品是第一次购买,商品的信息从数据库获取,数据库的模拟代          //码见附录。itemMap增加一对键值对。 //如果不是第一次购买,则通过商品编号找到对应的定单项,然后更新定 //单项的商品数量。 //不管是否第一次购买,都得更新购物车的总价格和总数量。 } public void delete(int nid){ //通过商品编号删除的对应的定单项,然后从更新购物车的总价格和总数量。 } public void update(int nid, int count){ ///通过商品编号找到对应的对应的定单项,修改商品数量。然后从更新购物车的总价格和总数量。 } public void clear(){ //清空定单项,购物车的总价格和总数量清零。 } public void show(){ //显示购物车的商品,格式如下: 商品编号  商品名称  单价  购买数量  总价 1          地瓜      2.0    2          4.0 2          衣服      30    5          150 … 合计:总数量:5  总价格:20元 } } 4) 编写用户程序TestShoppingCar,模拟购买,删除,修改,清空购物车等动作,在这个过程中,调用show方法来查看购物车的情况。 附:模拟数据库的代码: public class Database{ private Map data = new HashMap(); public Database(){ McBean bean = new McBean(); bean.setNid(1); bean.setSname("地瓜"); bean.setNprice(2.0); bean.setSdescription("新鲜的地瓜"); data.put(1, bean); bean = new McBean(); bean.setNid(2); bean.setSname("土豆"); bean.setNprice(1.2); bean.setSdescription("又好又大的土豆"); data.put(2, bean); bean = new McBean(); bean.setNid(3); bean.setSname("丝瓜"); bean.setNprice(1.5); bean.setSdescription("本地丝瓜"); data.put(3, bean);      } public McBean getMcBean(int nid){ return data.get(nid); } 7、附加题:写一个JAVA自定义栈,用数组实现或用链表实现。 数组实现思路:使用Object数组来实现栈的存取,由于栈的FILO的特性,其用链表更加节省空间;首先定义一个栈接口,用来描述栈的功能,提供出栈、入栈、获取栈顶元素、判断是否为空以及清空栈;然后,根据栈接口的描述来实现该栈,这里用Object数组的方式来实现; 链表实现思路:首先定义一个栈接口,用来描述栈的功能,提供出栈、入栈、获取栈顶元素、判断是否为空以及清空栈;定义一个Node类,用于保存链中点的信息,然后再实现该IStack接口; 1: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; public class Test1 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); HashMap map = new HashMap<>(); int y=0; while(true){ System.out.println("请输入"); String str = br.readLine(); int m = Integer.parseInt(str); if(m>=0&&m<=100){ Integer x = Integer.parseInt(str); y=m*m; System.out.println("x="+m+"y="+y); map.put(m, y); System.out.println(map); Iterator> it = map.entrySet().iterator(); it = map.entrySet().iterator(); while(it.hasNext()){ Entry e = it.next(); System.out.println(e); } Collection c = map.values(); System.out.println(c); }else{ br.close(); break; } } } } 2: public class Test2 { public static void main(String[] args) { String str1; str1="this is my homework! I must finish it!"; String str2[]=str1.split(" "); System.out.println("单词个数为"+str2.length); } } 3: import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPasswordField; import javax.swing.JTextField; public class Test3 extends JFrame implements MouseListener{ private JLabel logonLabel = new JLabel("User"); private JLabel passwordLabel = new JLabel("Password"); private JTextField user = new JTextField(); private JPasswordField password = new JPasswordField(); private JButton enter = new JButton("Logon On"); public Test3(){ super("Logon on window"); super.setLayout(new GridLayout(3, 2)); super.add(logonLabel); super.add(user); super.add(passwordLabel); super.add(password); super.add(enter); enter.addMouseListener(this); super.pack(); super.setVisible(true); super.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void mouseClicked(MouseEvent e){ if(!user.getText().matches("\\d{1,10}") || password.getPassword().length != 6){ JOptionPane.showMessageDialog(null, "Username/password incorrect format!"); }else{ if(user.getText().equals("1234567890") && password.getText().equals("mypass")){ JOptionPane.showMessageDialog(null, "Logon success. Welcome " + user.getText() + " back"); }else{ JOptionPane.showMessageDialog(null, "Logon failure for user/password incorrect. Please try again"); } } } public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub } public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub } public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub } /** * @param args */ public static void main(String[] args) { new Test3(); } } 4: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Test4 { public static void main(String[] args) { System.out.println("请输入一个数:"); double input = 0; boolean flag=true; while(flag) { try{ BufferedReader in=new BufferedReader(new InputStreamReader(System.in)); input=Double.valueOf(in.readLine()); if(input<0) { System.out.println("输入的是负数!"); } else flag=false; } catch(NumberFormatException e) { System.out.println("输入格式有误!请重新输入!"); }catch(IOException e) { e.printStackTrace(); } } System.out.println("输入的数为:"+input); System.out.println(input+"的为:"+Math.sqrt(input)); } } 5: import java.text.ParseException;  import java.text.SimpleDateFormat;  import java.util.Calendar;  import java.util.Date;  public class test5{  public static void main(String[] args) throws ParseException {  // TODO Auto-generated method stub  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  Date d1=sdf.parse("2012-09-08 10:10:10");  Date d2=sdf.parse("2012-09-15 00:00:00");  System.out.println(daysBetween(d1,d2));  System.out.println(daysBetween("2012-09-08 10:10:10","2012-09-15 00:00:00"));  } public static int daysBetween(Date smdate,Date bdate) throws ParseException    {  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  smdate=sdf.parse(sdf.format(smdate));  bdate=sdf.parse(sdf.format(bdate));  Calendar cal = Calendar.getInstance();    cal.setTime(smdate);    long time1 = cal.getTimeInMillis();                cal.setTime(bdate);    long time2 = cal.getTimeInMillis();        long between_days=(time2-time1)/(1000*3600*24);  return Integer.parseInt(String.valueOf(between_days));          }    public static int daysBetween(String smdate,String bdate) throws ParseException{  SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  Calendar cal = Calendar.getInstance();    cal.setTime(sdf.parse(smdate));    long time1 = cal.getTimeInMillis();                cal.setTime(sdf.parse(bdate));    long time2 = cal.getTimeInMillis();        long between_days=(time2-time1)/(1000*3600*24);  return Integer.parseInt(String.valueOf(between_days));    }  }  6: class McBean { private int id; private String name; private double price; private String instuction; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String getInstuction() { return instuction; } public void setInstuction(String instuction) { this.instuction = instuction; } } class ShoppingCar { private double totalPrice; private int totalCount; private Map itemMap; public ShoppingCar() { itemMap = new HashMap(); } public void buy(int nid) { OrderItemBean order = itemMap.get(nid); McBean mb; if (order == null) { mb = new Test().getMcBean(nid); order = new OrderItemBean(mb, 1); itemMap.put(nid, order); update(nid, 1); } else { order.setCount(order.getCount() + 1); update(nid, 1); } } public void delete(int nid) { OrderItemBean delorder = itemMap.remove(nid); totalCount = totalCount - delorder.getCount(); totalPrice = totalPrice - delorder.getThing().getPrice() * delorder.getCount(); } public void update(int nid, int count) { OrderItemBean updorder = itemMap.get(nid); totalCount = totalCount + count; totalPrice = totalPrice + updorder.getThing().getPrice() * count; } public void clear() { itemMap.clear(); totalCount = 0; totalPrice = 0.0; } public void show() { DecimalFormat df = new DecimalFormat("¤#.##"); System.out.println("商品编号\t商品名称\t单价\t购买数量\t总价"); Set set = itemMap.keySet(); Iterator it = set.iterator(); while (it.hasNext()) { OrderItemBean order = itemMap.get(it.next()); System.out.println(order.getThing().getId() + "\t" + order.getThing().getName() + "\t" + df.format(order.getThing().getPrice()) + "\t" + order.getCount() + "\t" + df.format(order.getCount() * order.getThing().getPrice())); } System.out.println("合计: 总数量: " + df.format(totalCount) + " 总价格: " + df.format(totalPrice)); System.out.println("**********************************************"); } } class OrderItemBean { private McBean thing;//商品的实体 private int count;//商品的数量 public OrderItemBean(McBean thing, int count) { super(); this.thing = thing; this.count = count; } public McBean getThing() { return thing; } public void setThing(McBean thing) { this.thing = thing; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } } class TestShoppingCar { public static void main(String[] args) { ShoppingCar s = new ShoppingCar(); s.buy(1);//购买商品编号1的商品 s.buy(1); s.buy(2); s.buy(3); s.buy(1); s.show();//显示购物车的信息 s.delete(1);//删除商品编号为1的商品 s.show(); s.clear(); s.show(); } } 7: class ArrayStack {  public static final int DEFAULT_CAPACITY = 10;  private AnyType[] theArray;  private int topOfStack;  private int size;  public ArrayStack() {  clear();  topOfStack = -1;  }  public void push(AnyType x) {  if (size == theArray.length)  ensureCapacity(2 * size + 1);  theArray[++topOfStack] = x;  size++;  }  public AnyType top() {  if (topOfStack != -1)  return theArray[topOfStack];  return null;  }  public AnyType pop() {  if (topOfStack != -1)  size--;  return theArray[topOfStack--];  }  public void clear() {  topOfStack = -1;  ensureCapacity(DEFAULT_CAPACITY);  }  public boolean isEmpty() {  return topOfStack == -1;  }  public void ensureCapacity(int newCapacity) {  if (newCapacity < size)  return;  AnyType[] old = theArray;  theArray = (AnyType[]) new Object[newCapacity];  for (int i = 0; i < size; i++)  theArray[i] = old[i];  }  }  class LinkedStack {  private static class Node {  Node(AnyType data, Node prev) {  this.data = data;  this.prev = prev;  }  private AnyType data;  private Node prev;  }  private Node bottom;  public LinkedStack() {  clear();  }  public void clear() {  bottom = new Node(null, null);  }  public void push(AnyType x) {  bottom = new Node(x, bottom);  }  public AnyType top() {  if (bottom != null)  return bottom.data;  return null;  }  public AnyType pop() {  if (bottom != null) {  bottom = bottom.prev;  return bottom.data;  }  return null;  }  public boolean isEmpty() {  return bottom.prev == null;  }  }  class StackTest { 
本文档为【03_JAVA常用类及异常集合考试】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: 免费 已有0 人下载
最新资料
资料动态
专题动态
is_105949
暂无简介~
格式:doc
大小:95KB
软件:Word
页数:40
分类:互联网
上传时间:2019-01-17
浏览量:87