黑马程序员java自学之--自测题

来源:互联网 发布:java实现发邮件 编辑:程序博客网 时间:2024/04/27 22:55

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

package com.itheima;/** * 第1题: 1、 以下代码哪个是正确的?为什么? a. byte b = 1 + 1; b. byte b = 1; b = b + 1; c. byte * b = 1; b = b += 1; d. byte b = 1; b = ++b; * @author Administrator * * 第1题选择正确答案a */public class Test1 {    public static void main(String[] args) {        /*//选项a检验        byte b = 1 + 1;//类型自动提升时右边1+1为常量int类型答案为2,byte的范围为-128~127,而2在byte类型的范围中可以给byte        //答案正确        */                /*//选项b检验        byte b = 1;//此处的int为1,在byte的-128~127的数字范围中,可以赋给b        b = b + 1;//此处编译不通过。因为b为一个因为在自动类型提升时b+1结果转换为int,在不损失精度的前提下int类型要强制转换为byte类型        //答案错误        */                /*        //选项c检验        byte b = 1;//此处的int为1,在byte的-128~127的数字范围中,可以赋给b        b = b +=1;//此处运算因为b是一个变量。运算时先算右边b=b+1,再赋给b,在算b=b+1时错误原因同b选项一样。        //答案错误        */                /*选项d检验        byte b = 1;//此处的int为1,在byte的-128~127的数字范围中,可以赋给b        b = ++b;//此处运算为b先加1变为一个byte数b,再把b赋给b,在运算++b的时候错误原因同b选项,可能变量改变时超出byte的-128~127的范围。        //答案错误        */    }}package com.itheima;import java.util.Scanner;/** * 第2题、 从键盘接受一个数字,打印该数字表示的时间,最大单位到天,例如: 键盘输入6,打印6秒; 键盘输入60,打印1分; 键盘输入66,打印1分6秒; * 键盘输入666,打印11分6秒; 键盘输入3601,打印1小时1秒 * * @author Administrator * */public class Test2 {    public static void main(String[] args) {        toTime();    }    public static void toTime() {        int minutes = 0;        int hours = 0;        int days = 0;        Scanner in = new Scanner(System.in);        int seconds = 0;        System.out.println("请输入一个整数(0表示退出)");        while ((seconds = in.nextInt()) != 0) {            days = seconds / (24 * 60 * 60);//获取天            seconds %= (24 * 60 * 60);//得到获取天后的秒数            hours = seconds / (60 * 60);//获取小时            seconds %= (60 * 60);//得到获取小时后的秒数            minutes = seconds / 60;//获取分钟            seconds %= 60;//获取秒            System.out.println((days == 0 ? "" : days + "天")                    + (hours == 0 ? "" : hours + "小时")                    + (minutes == 0 ? "" : minutes + "分")                    + (seconds == 0 ? "" : seconds + "秒"));        }    }}package com.itheima;/** * 第3题。 定义一个二维int数组,编写代码获取最小元素。 * @author Administrator * */public class Test3 {    public static void main(String[] args) {        getMinIntArray();    }    public static void getMinIntArray(){        int[][] arr = {{12,19,0},{59,29,10},{31,-4,90}};//定义一个二维int数组        int min = arr[0][0];        for (int i = 0; i < arr.length; i++) {            for (int j = 0; j < arr[i].length; j++) {                if(arr[i][j] < min){                    min = arr[i][j];                }            }        }        System.out.println("int二维数组中最小的是:" + min);    }}package com.itheima;/** * 第4题。编程计算3乘8等于几,什么方法效率更高? * @author Administrator *思路:3*8.可以看到8=2*2*2,所以可以把3想做左移3位,因为左移一位为乘以2. */public class Test4 {    public static void main(String[] args) {        System.out.println(3<<3);    }}package com.itheima;/** * 第5题.分析运行结果,说明原理。(没有分析结果不得分) * class A { *  void fun1() { * System.out.println(fun2()); } * int fun2() { return 123; } } *  public class B extends A * { int fun2() * { return 456; } *  public static void main(String args[]) * { B b = new B(); * b.fun1(); * A a = b; * a.fun1(); } } * * @author Administrator * 分析结果: * 类A有fun1,fun2两个方法,类B是继承的类A并且类B覆盖了父类A的fun2; * 在产生一个一个B类的对象b时,b调用fun1,而在类B中没有直接写fun1方法,但是父类B写了这个方法 * 所以使用父类A的fun1,而在父类A中的这个fun1函数在内部调用调用fun2方法,而在类B中覆盖了此方法,所以使用类B的fun2方法、 * 在产生一个A类的对象a时,是引用a指向对象b,而b对象是B类的一个对象,这里使用了多态性,父类的引用指向子类的实例 * 所以对象a在调用fun1时使用的还是父类A类的fun1方法,而在A类中调用fun2方法,此时使用的是B类覆盖的fun2方法。 */public class Test5 extends A{    int fun2()     { return 456;     }     public static void main(String args[])    {         Test5 b = new Test5();     b.fun1(); //打印456;     A a = b;     a.fun1(); //打印456     }}class A {     void fun1()       {           System.out.println(fun2());       }     int fun2()       {           return 123;       } }package com.itheima;/** *  第6题. *  定义一个包含私有成员变量和函数的类,再定义一个内部类,在内部类函数中访问外部成员变量,并调用外部函数。 *  在外部类函数中创建内部类对象,调用内部类函数。 * @author Administrator * */public class Test6 {    private int Test6x = 8;    public void showX(){        System.out.println("外部类打印变量:"+Test6x);    }    class A{        private int Ax = Test6x;        public void AShowX() {            showX();            System.out.println("内部类打印变量:"+Ax);        }    }    public static void main(String[] args) {        Test6 test = new Test6();        Test6.A a = test.new A();        a.AShowX();    }}package com.itheima;/** * 第7题. 编写一个可以获取文件扩展名的函数,形参接收一个文件名字符串, *  返回一个扩展名字符串。 * * @author Administrator * */public class Test7 {    public static void main(String[] args) {        getExtensionName("afahgo.fasdf.txt.jpg");    }    public static void getExtensionName(String filename) {        int index = filename.lastIndexOf('.');//获取最后一个“.”的位置        String s = filename.substring(index+1);//获取最后一个"."的之后的子串。        System.out.println(s);    }}package com.itheima;/** * 第8题.throw和throws有什么区别? * try、catch、finally分别在什么情况下使用? * @author Administrator * */public class Test8 {    /*     * throw用于抛出异常对象,后面跟的是异常对象;throw用在函数内。     * throws用于抛出异常类,后面跟的异常类名,可以跟多个,用逗号隔开。throws用在函数上。     * try用来写可能产生异常的代码,     * catch用来捕获try代码中产生的异常,并进行处理或抛出,     * finally是始终执行的代码块,常用于关闭资源。     * catch就是对相应的try的监视,相应的try中产生了异常,catch就会捕获来处理,     * finally是不管try和catch写了什么都会执行代码,就算有return,finally中的代码也会执行,当然强行关闭程序,     * 就可能执行不到。     * */}package com.itheima;/** * 第9题. 写一个正则表达式,可以匹配尾号5连的手机号。 * 规则: 第1位是1,第二位可以是数字3458其中之一,后面4位任意数字,最后5位为任意相同的数字。例如:18601088888、13912366666 * @author Administrator * */public class Test9 {    public static void main(String[] args) {        regxDemo("15343255555");    }    public static void regxDemo(String str){        /*         * [1]表示第一个数字为1         * [3458]表示第二个为数字3或4或5或8         * \d{4}表示四个为数字         * (\d)表示把这个数字封装为一个组         * \1{4}表示取封装的1的组而且出现四次         * */        String reg = "[1][3458]\\d{4}(\\d)\\1{4}";        boolean b = str.matches(reg);        System.out.println(b);    }}package com.itheima;/** * 编写函数,从一个字符串中按字节数截取一部分, 但不能截取出半个中文(GBK码表), 例如:从“HM程序员”中截取2个字节是“HM”, * 截取4个则是“HM程”,截取3个字节也要是"HM" 而不要出现半个中文 * * 思路: *    汉字的默认编码gbk。 *    所以一个汉字两个字节。都是负数。 *    只有判断最后一个字节是否是负数。 *    如果是,就往前继续看有多少负数。如果是偶数个,不舍弃。 *    如果是奇数个,即舍弃最后一个字节。 */public class Test10 {    public static void main(String[] args)    {        System.out.println(getString("abzM你好哈哈哈", 5));    }    public static String getString(String str, int n)    {        byte[] arr = str.getBytes();        int count = 0;        for(int x = n-1; x>=0; x--)        {            if(arr[x]<0)            {                count++;            }            else                break;        }        if(count%2==0)            return new String(arr,0,n);        else            return new String(arr,0,n-1);    }}


0 0
原创粉丝点击