练习11 Java常用类练习题

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
3.给出如下代码: String s1=”ab”; String s2=”cd”; String ss1=”abcd”; String ss2=”ab”+”cd”; String ss3=s1+”cd”; String ss4=”ab”+s2; String ss5=s1+s2; 请问如下哪个表达式为true? A: ss1= = ss2 B: ss2 = = ss3 C: ss3 = = ss4 D: ss4 = = ss5 E: ss4.equals(ss5) 答:
} } 运行的打印结果是什么? A:5 B:6 C:6.1 D:7 E:9 答:
9. 请问如下哪些不是基本类型的封装类? A: Integer B: String C: Char D: Double E: Boolean 答:
10. 如下哪些语句编译无错误? A: Byte b=new Byte(“123”); B: Byte b=new Byte(123); C: Byte b=new Byte(); D: Byte b=new Byte((int)123.4); E: Byte b=new Byte(0x123); 答:
throws ConvertException //将日期时间:yyyy-mm-dd hh:mi:ss 转换为日期 类型,如果转换失败抛出转换异常。 (7) public static String convertToDateString(java.sql.Date date) //将日期类 型转换为字符串类型,格式:yyyy-mm-dd (8) public static String convertToDateTimeString(java.sql.Date date) //将日 期转换为字符串,格式为:yyyy-mm-dd hh:mi:ss 编写测试类Test,对所有方法进行测试。
[] 编程题 1.编写一个字符串功能类StringFunctuin 有如下方法: (1)方法1:public int getWordNumber(String s) throws Exception
参数是一个英文句子,方法的功能是取得此英文句子的单词个数。 如果参数为空或为空字符串,抛出异常,异常信息为:“字符串为 空”。 (2)方法2:public int getWordNumber(String s1,String s2) throws Exception 此方法传递2个String参数,返回s1中出现s2的次数。 (3)方法3:public boolean contain(String s1, String s2) throws Exception 判断S1中是否含有S2
3.编写一个类型转换功能类Conveter, 有如下方法: (1). public int convertInt(String number) throws ConvertException 将字符串转化为整数,如果转换失败,抛出ConvetException异常。 (2). public double convertDouble(String number) throws ConvertException (3). public float convertFloat(String number) throws ConvertException (4). public short convertShort(String number) throws ConvertException (5). public byte convertByte(String number) throws ConvertException 编写测试主类Test, 对此类进行测试。
5.编写一个随机数生成类 RandomGenerator 有方法: (1)public int generateRandom(int bitnum)
// 参数指定随机数的位数。如bitnum=4, 生成4位的随机数。
8.运行如下Java类: public class Main01 {
public static void main(String[] args) {
int i=0; float f=2.3f; double d=2.7; i=(int)Math.ceil(f)*(int)Math.round(d) ; System.out.println(i);
4.定义在Object类上的hashCode()方法的返回类型是什么? A:char B: long C: int D: float
E: double 答:
5. 如下哪些说法是正确的? A: Class类是Object类的超类。 B: Object是一个final类。 C: Class类可以用于装载其他类。 D: Object类是Class类的超类。 E: 以上说法都不正确。 答:
Java常用类练习题
[] 选择题 1.如何获取一个String类实例s包含的字符个数? A: s.size B: s.length C: s.size() D: s.length() 答:
2.请问如下哪些语句正确? A: String s=”abc”; B: String[] s=”abc”; C: new String s=”abc”; D: String s=new String(“abc”); 答:
6.调用Math.random()方法,有可能输出以下哪些结果? A: -0.12 ~ 0.56E3 B: 0.12 ~ 1.1E1 C: -23.45 ~ 0.0 D: 0.356 ~ 0.03 E: 1.00 ~ 0.99 答:
7.定义在Math类中的round(double d)方法的返回类型是什么? A: char B: short C: int D: long E: float 答:
编写测试主类Test, 对此类进行测试。
2.编写一个字符串处理功能类:StringProcessor 有如下方法: (1) public static String getSubString(String s, int beginNum, int endNum) throws Exception
取得原始字符串s的前beginNum和后endNum个字符之间的子字符 串。 (2) public static String getSubString(String s1, String s2 ) throws Exception 取得S1中第一次出现S2和最后一次出现S2之间的字符串。如果S1中 没有出现S2,或只出现S2一次则抛出异常。 (3) public static String reverse(String s1) throws Exception 将原始的字符串s1所有字符倒排后,返回。 编写测试主类Test, 对此类进行测试。ቤተ መጻሕፍቲ ባይዱ
4.编写一个日期功能类:DateFunction 有如下方法: (1) public static java.uitl.Date getCurrentDate() //取得当前日期 (2 )public static String getCurrentShortDate () //返回当前年月日格式日 期:yyyy-mm-dd (3) public static String getCurrentDateTime() //取得当前日期和时间 格 式:yyyy-mm-dd hh:mi:ss (4) public static String getCurrentChineseDate() //返回 “yyyy年xx月xx 日”格式的日期 (5) public static java.util.Date convertToDate(String currentDate) throws Exception //将字符串日期转换为日期类型, 字符串格式为:yyyy-mmdd, 如果转换失败,抛出转换异常 (6) public static java.util.Date convertToLongDate(String currentDate)
相关文档
最新文档