zoukankan      html  css  js  c++  java
  • Java常用工具类题库

     一、    填空题

    1. 在Java中每个Java基本类型在java.lang包中都在一个相应的包装类,把基本类型数据转换为对象,其中包装类Integer是___Number__的直接子类。
    2. 包装类Integer的静态方法可以将字符串类型的数字”123”转换成基本整型变量n,其实现语句是:__ Integer.parseInt(“123”)__。
    3. 在Java中使用java.lang包中的__ StringBuffer/StringBuilder 类来创建一个字符串对象,它代表一个字符序列可变的字符串,可以通过相应的方法改变这个字符串对象的字符序列。
    4. StringBuilder类是StringBuffer类的替代类,两者的共同点是都是可变长度字符串,其中线程安全的类是__StringBuffer____。
    5. DateFormat类可以实现字符串和日期类型之间的格式转换,其中将日期类型转换为指定的字符串格式的方法名是__Format__。
    6. 使用Math.random( )返回带正号的 double值,该值大于等于0.0且小于1.0。使用该函数生成[30,60]之间的随机整数的语句是___(int)(Math.random()*31)+30 
    7. JDK1.5后提供了___enum___关键字,用以定义枚举类。枚举类是一种特殊的类,可以有自己的属性、方法和构造方法。
    8. File对象调用方法   mkdir()   创建一个目录,不包括所有必需但不存在的父目录,当且仅当已创建目录时,返回true;否则返回false。
    9. 将字符串”123”转换成基本数据类型  Interger.parseInt(iStr) 。          
    10. String类的trim()方法作用是   去除字符串首尾的空格     。          
    11. "hamburger".substring(4, 8) 返回的结果是 urge 。         
    12. String s = "a"+”b”+”c” 创建  5  个对象。
    13. System.currentTimeMillis()表示获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离,类型为long    。         

     

    二、    选择题

    1.

    以下选项中关于intInteger的说法错误的是( BD  。(选择二项)

     

     

     

     

    A.

    int是基本数据类型,Integer是int的包装类,是引用数据类型

     

    B.

    int的默认值是0,Integer的默认值也是0

     

    C.

    Integer可以封装了属性和方法提供更多的功能

     

    D.

    Integer i=5;该语句在JDK1.5之后可以正确执行,使用了自动拆箱功能

     

    2.

    分析如下Java代码,该程序编译后的运行结果是(  D  )。(选择一项)

     

    public static void main(String[ ] args) {

             String str=null;

             str.concat("abc");

             str.concat("def");

             System.out.println(str);

    }

     

     

     

     

    A

    Null

     

    B.

    Abcdef

     

    C.

    编译错误

     

    D.

    运行时出现NullPointerException异常

     

    3.

    以下关于String类的代码的执行结果是(  B  。(选择一项)

     

    public class Test2 {

        public static void main(String args[]) {

            String s1 = new String("bjsxt");

            String s2 = new String("bjsxt");

            if (s1 == s2)            System.out.println("s1 == s2");

            if (s1.equals(s2))        System.out.println("s1.equals(s2)");

        }

    }

     

     

     

     

    A.

    s1 == s2

     

    B.

    s1.equals(s2)

     

    C.

    s1 == s2

    s1.equals(s2)

     

    D.

    以上都不对

     

    4.

    以下关于StringBuffer类的代码的执行结果是(  D  。(选择一项)

     

    public class TestStringBuffer {

             public static void main(String args[]) {

                       StringBuffer a = new StringBuffer("A");

                       StringBuffer b = new StringBuffer("B");

                       mb_operate(a, b);

                       System.out.println(a + "." + b);

             }

             static void mb_operate(StringBuffer x, StringBuffer y) {

                       x.append(y);

                       y = x;

             }

    }

     

     

     

     

    A.

    A.B 

     

    B.

    A.A

     

    C.

    AB.AB

     

    D.

    AB.B

     

    5.

    给定如下Java代码,编译运行的结果是(  C  )。(选择一项)

     

    public static void main(String []args){

             String s1= new String("pb_java_OOP_T5");

             String s2 = s1.substring(s1.lastIndexOf("_"));

             System.out.println("s2="+s2);

    }

     

     

     

     

     

    A

    s2=_java_OOP_T5

     

    B.

    s2=_OOP_T5

     

    C.

    s2=_T5

     

    D.

    编译出错

    6.

    对于语句String s="my name is kitty",以下选项中可以从其中截取”kitty”的是(  AB  )(选择二项

     

     

     

     

    A

    s.substring(11,16)

     

    B.

    s.substring(11)

     

    C.

    s.substring(12,17)

     

    D.

    s.substring(12,16)

     

    7.

    分析下面的Java程序段,编译运行后的输出结果是(  D  )。(选择一项)

     

    public class Test {

             public void changeString(StringBuffer sb) {

                       sb.append("stringbuffer2");

             }

             public static void main(String[] args) {

                       Test a = new Test();

                       StringBuffer sb = new StringBuffer("stringbuffer1");

                       a.changeString(sb);

                       System.out.println("sb = " + sb);

             }

    }

     

     

     

     

    A

    sb = stringbuffer2stringbuffer1

     

    B.

    sb = stringbuffer1

     

    C.

    sb = stringbuffer2

     

    D.

    sb = stringbuffer1stringbuffer2

     

    8.

    给定如下Java代码,编译运行的结果是(  A  )。(选择一项)

     

    public static void main(String[] args) {

             StringBuffer sbf = new StringBuffer("java");

             StringBuffer sbf1 = sbf.append(",C#");

             String sbf2 = sbf + ",C#";

             System.out.print(sbf.equals(sbf1));

             System.out.println(sbf2.equals(sbf));

    }

     

     

     

     

    A

    true  false

     

    B.

    true  true

     

    C.

    false  false

     

    D.

    false  true

     

    9.

    分析下面的Java程序,编译运行后的输出结果是(  B  )。(选择一项)

     

    public class Example {

             String str = new String("good");

             char[] ch = { 'a', 'b', 'c' };

             public static void main(String args[]) {

                       Example ex = new Example( );

                       ex.change(ex.str, ex.ch);

                       System.out.print(ex.str + "and");

                       System.out.print(ex.ch);

             }

             public void change(String str, char ch[]) {

                       str = "test ok";

                       ch[0] = 'g';

             }

    }

     

     

     

     

    A

    Goodandabc

     

    B.

    Goodandgbc

     

    C.

    test okandabc

     

    D.

    test okandgbc

     

    10.

    以下程序片段中可以正常编译的是(  C   )。(选择一项)

     

     

     

     

    A

    String s = "Gone with the wind"; 

    String k = s+t;

    String t = "good";

     

    B.

    String s = "Gone with the wind";

    String t;           

    t = s[3]+"one";

     

    C.

    String s = "Gone with the wind";

    String stanfard = s.toUpperCase();

     

    D.

    String s = "home directory";

    String t = s – "directory";

     

    11.

    File类中的(  B  )方法可以用来判断文件或目录是否存在。(选择一项)

     

     

     

     

    A

    exist()

     

    B.

    exists()

     

    C.

    fileExist()

     

    D.

    fileExists()

     

    12.

    Java中,以下File类的方法中(  C  )用来判断是否是目录。(选择一项)

     

     

     

     

    A

    isFile( )

     

    B.

    getFile( )

     

    C.

    isDirectory( )

     

    D.

    getPath( )

     

    13.

    分析下面的Java程序,编译运行后的输出结果是(  B  )。(选择一项)

     

    public class Example {

             String str = new String("good");

             char[] ch = { 'a', 'b', 'c' };

             public static void main(String args[]) {

                       Example ex = new Example( );

                       ex.change(ex.str, ex.ch);

                       System.out.print(ex.str + "and");

                       System.out.print(ex.ch);

             }

             public void change(String str, char ch[]) {

                       str = "test ok";

                       ch[0] = 'g';

             }

    }

     

     

     

     

    A

    Goodandabc

     

    B.

    Goodandgbc

     

    C.

    test okandabc

     

    D.

    test okandgbc

     

    14.

    分析下面代码的结果(  A  )。(选择一项)

     

    public static void main(String args[]) {

                       String s = "abc";

                       String ss = "abc";

                       String s3 = "abc" + "def"; // 此处编译器做了优化!

                       String s4 = "abcdef";

                       String s5 = ss + "def";

                       String s2 = new String("abc");

                       System.out.println(s == ss);

                       System.out.println(s3 == s4);

                       System.out.println(s4 == s5);

                       System.out.println(s4.equals(s5));

             }

     

     

     

     

    A

    true true false true

     

    B.

    true true true false

     

    C.

    true  false    true  true

     

    D.

    false true false true

    三、    判断题

    1. 方法Integer.parseInt()的作用是将一个整数转变成String。( F  )
    2. JK1.5后提供了自动装箱和自动拆箱功能,从而可以实现基本数据类型和对应包装类之间的自动转换,简化了操作。(  T  )
    3. 执行语句String str="abcedf"; int len=str.length; 后,能够得到字符串的长度是6。(  F  )
    4. 运算符“==”用于比较引用时,如果两个引用指向内存同一个对象,则返回true。(  T  )
    5. java.sql.Date类和java.util.Date类的关系是前者是后者的父类,其中前者没有提供无参数构造方法,而后者可以提供无参数构造方法来获取当前时间。(  F  )
    6. 求x的y次方,其表达式为:Math.pow(x,y)。(  T  )
    7. 一个File对象可以代表一个文件或目录,它可以获取文件和目录属性,也可以访问文件内容。(  F  )
    8. 在使用File类中的delete( )方法时,删除时可能文件不存在,所以我们最好先判断一下是否存在,不然会出现NullPointerException异常。(  F  )
    9. Date d = new Date()表示的是当前时间。(  T   )
    10. 递归可以完全使用迭代来代替。(  T  )

    四、    简答题

    1. 自动装箱和自动拆箱
    2. String、StringBuffer、StringBuilder区别与联系。
    3. String str=”bjsxt”;和String str= new String(“bjsxt”);的区别
    4. java.sql.Date和java.util.Date的联系和区别
    5. 为什么要使用包装类,包装类的作用。
    6. File类的方法mkdir跟mkdirs,有什么区别?

    前一个是创建一个文件夹,后一个是把所有的都创建

    1. 简述枚举的使用。
    2. 递归算法的优点是什么?缺点是什么?

    五、    编码题

    1. 验证键盘输入的用户名不能为空,长度大于6,不能有数字。

    提示:使用字符串String类的相关方法完成

     

     1 public class TestCheckUserName
     2 {
     3     public static void main(String[] args)
     4     {
     5         //给出用户名
     6         Scanner input = new Scanner(System.in);
     7         System.out.print("请输入用户名:");
     8         String userName = input.nextLine();
     9         //验证表单输入的用户名不能为空,
    10         if (userName == null || "".equals(userName))
    11         {
    12             System.out.println("用户名不能为空");
    13             return;
    14         }
    15         //长度大于6,
    16         if (userName.length() <= 6)
    17         {
    18             System.out.println("用户名长度必须大于6个字符");
    19             return;
    20         }
    21         //不能有数字
    22         for (int i = 0; i < userName.length(); i++)
    23         {
    24             //取出每个字符
    25             char ch = userName.charAt(i);//'3'  '4'
    26             //判断每个字符是否是数字
    27             //if(ch <='9'  && ch >='0'){
    28             if (ch <= 57 && ch >= 48)
    29             {
    30                 System.out.println("用户名不能有数字");
    31                 //break;//退出for循环
    32                 return;//退出main方法
    33             }
    34         }
    35         System.out.println("用户名符合要求");
    36     }
    37

    2.接收从键盘输入的字符串格式的年龄,分数和入学时间,转换为整数、浮点数、日期类型,并在控制台输出。

    提示:使用包装类Integer、Double和日期转换类DateFormat实现

     

     1 public class TestStringConvert
     2 {
     3     public static void main(String[] args)
     4     {
     5         //从键盘输入字符串形式的年龄,分数和入学时间
     6         Scanner input = new Scanner(System.in);
     7         System.out.print("请输入年龄:");
     8         String sage = input.nextLine();
     9         System.out.print("请输入分数:");
    10         String sscore = input.nextLine();
    11         System.out.print("请输入日期(yyyy/MM/dd):");
    12         String sdate = input.nextLine();
    13         //年龄转换String----int
    14         int age = 0;
    15         age = Integer.parseInt(sage);
    16         //分数转换 String ---double
    17         double score = 0.0;
    18         score = Double.parseDouble(sscore);
    19         //入学时间的转换String ---Date
    20         Date enterDate = null;
    21         DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    22         try
    23         {
    24             enterDate = sdf.parse(sdate);
    25         } catch (ParseException e)
    26         {
    27             e.printStackTrace();
    28             //return;
    29         }
    30         //输出结果
    31         System.out.println(age + "  " + score + "  " + enterDate);
    32     }
    33 }

     3.根据交通信号灯颜色决定汽车停车、行驶和慢行

    提示:使用枚举实现

     

     1 //定义信号灯枚举
     2 public enum Signal
     3 {
     4     红, 绿, 黄
     5 }
     6 public class TestSignal
     7 {
     8     public static void main(String[] args)
     9     {
    10         //指定信号灯颜色
    11         Signal s = Signal.红;
    12         //根据颜色决定汽车下步行动
    13         switch (s)
    14         {
    15             case 红:
    16                 System.out.println("停车");
    17                 break;
    18             case 绿:
    19                 System.out.println("行驶");
    20                 break;
    21             case 黄:
    22                 System.out.println("慢行");
    23                 break;
    24         }
    25     }
    26 }//定义信号灯枚举
    27 public enum Signal
    28 {
    29     红, 绿, 黄
    30 }
    31 public class TestSignal
    32 {
    33     public static void main(String[] args)
    34     {
    35         //指定信号灯颜色
    36         Signal s = Signal.红;
    37         //根据颜色决定汽车下步行动
    38         switch (s)
    39         {
    40             case 红:
    41                 System.out.println("停车");
    42                 break;
    43             case 绿:
    44                 System.out.println("行驶");
    45                 break;
    46             case 黄:
    47                 System.out.println("慢行");
    48                 break;
    49         }
    50     }
    51 }

    4.以树状结构输出计算机某个指定文件夹下的所有的文件和子文件夹名称。

    提示:使用File的方法,并结合递归实现

     

     1 public class TestDirectory
     2 {
     3     public static void main(String[] args)
     4     {
     5         File file = new File("d:/101sxt");
     6         showTree(file, 1);
     7     }
     8     public static void showTree(File file, int level)
     9     {
    10         //获取当前目录下的文件和子文件夹(仅当前层次)
    11         File[] files = file.listFiles();
    12         //输出当前目录下的文件和子文件夹(仅当前层次)
    13         for (File f : files)
    14         {
    15             //根据级别level输出指定个数的-
    16             for (int i = 0; i < level; i++)
    17             {
    18                 System.out.print("-");
    19             }
    20             //输出当前的一个文件或文件夹
    21             if (f.isDirectory())
    22             {
    23                 System.out.println("[" + f.getName() + "]");
    24                 //递归
    25                 showTree(f, level + 1);
    26             } else
    27             {
    28                 System.out.println(f.getName() + "	" + f.length());
    29             }
    30         }
    31     }
    32 }

    4.将1990年3月3日通过Calendar来表示,并得出这天是该年的第几天?将该日期增加35天,是哪一天?使用代码来说明。

     

     1 public class Test
     2 {
     3 
     4     public static void main(String[] args)
     5     {
     6         Calendar calendar = Calendar.getInstance();
     7         //设置年月日
     8         calendar.set(1990, 3, 3);
     9         //得到该年的第几天
    10         int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
    11         System.out.println(dayOfYear);
    12         //增加35天
    13         calendar.add(Calendar.DAY_OF_YEAR, 35);
    14         //得到date对象
    15         Date date = calendar.getTime();
    16         System.out.println(date.toLocaleString());
    17     }
    18 }
    19 //或者采用如下代码实现
    20 public static void main(String[] args) throws ParseException 
    21 {
    22     Calendar ca = Calendar.getInstance();//创建一个日期实例
    23     DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
    24     Date date = df.parse("1990年3月3日");
    25     ca.setTime(date );//实例化一个日期
    26     System.out.println(ca.get(Calendar.DAY_OF_YEAR));                                                         ca.add(Calendar.DAY_OF_YEAR, 35);//加上35天
    27     System.out.println(df.format(ca.getTime()));//获取那一天
    28

    六、    可选题

    1.生成10个[10,23)之间的随机整数

    提示:分别使用Math.random()和Random类的nextDouble()或nextInt()实现

     

     1 public class TestRandom
     2 {
     3     public static void main(String[] args)
     4     {
     5         //实现1:生成10个[10,23)之间的随机整数             
     6         for (int i = 0; i < 10; i++)
     7         {
     8             int r = (int) (Math.random() * 13) + 10;
     9             System.out.print(r + "	");
    10         }
    11         System.out.println();
    12         Random rand = new Random();
    13         //实现2:生成10个[10,23)之间的随机整数
    14         for (int i = 0; i < 10; i++)
    15         {
    16             int r = (int) (rand.nextDouble() * 13) + 10;
    17             System.out.print(r + "	");
    18         }
    19         System.out.println();
    20         //实现3:生成10个[10,23)之间的随机整数
    21         for (int i = 0; i <= 10; i++)
    22         {
    23             int r = rand.nextInt(13) + 10;//[0,13)+10---[10,23)
    24             System.out.print(r + "	");
    25         }
    26         System.out.println();
    27     }
    28 }

    2.手动定义一个枚举,表示十二个月的英文月份。

     

    1 public enum Month
    2 {
    3     January, february, March, April, May, June,
    4     July, August, September, October, November, December
    5 }

    3.打印某个月份的可视化日历

    提示:使用DateFormat、Calendar类实现功能

     1 public class VisualCalendar2
     2 {
     3     public static void main(String[] args) throws ParseException
     4     {
     5         //从键盘输入指定格式的字符串 (Scanner) 2015-12-3
     6         System.out.println("请输入日期(按照格式:2030-3-10):");
     7         Scanner scanner = new Scanner(System.in);
     8         String temp = scanner.nextLine();
     9         //把输入的字符串变成Date(DateFormat)
    10         DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    11         Date date = format.parse(temp);
    12         //把Date变成Calendar(setTime())
    13         Calendar calendar = Calendar.getInstance();
    14         calendar.setTime(date);
    15         //取出日历的日信息
    16         int dateOfMonth = calendar.get(Calendar.DATE);
    17         //输出日历头信息  日         一     二     三     四     五     六
    18         System.out.println("日	一	二	三	四	五	六");
    19         //输出该月1日之前的空白( 要知道该月1日是星期几)
    20         calendar.set(Calendar.DATE, 1);
    21         int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
    22         for (int i = 1; i < weekDay; i++)
    23         {
    24             System.out.print('	');
    25         }
    26         //输出该月的日历从1到最后一天(更多细节)
    27         int maxDay = calendar.getActualMaximum(Calendar.DATE);
    28         for (int i = 1; i <= maxDay; i++)
    29         {
    30             //如果是当天,输出*
    31             if (i == dateOfMonth)
    32             {
    33                 System.out.print("*");
    34             }
    35             //输出每一天
    36             System.out.print(i + "	");
    37             //如果是周六换行
    38             weekDay = calendar.get(Calendar.DAY_OF_WEEK);
    39             if (weekDay == 7)
    40             {
    41                 System.out.println();
    42             }
    43             //日历变成下一天!!!!!!
    44             calendar.add(Calendar.DATE, 1);
    45         }
    46     }
    47 }
  • 相关阅读:
    leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
    leetcode 557. Reverse Words in a String III 、151. Reverse Words in a String
    leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
    leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
    leetcode 162. Find Peak Element
    leetcode 88. Merge Sorted Array
    leetcode 74. Search a 2D Matrix 、240. Search a 2D Matrix II
    Android的API版本和名称对应关系
    spring 定时任务执行两次解决办法
    解析字符串为泛型的方法
  • 原文地址:https://www.cnblogs.com/guanghe/p/6063461.html
Copyright © 2011-2022 走看看