黑马程序员技术交流社区

标题: Java面试题 行就给个黑马币-_- [打印本页]

作者: keto    时间: 2015-6-15 14:34
标题: Java面试题 行就给个黑马币-_-

16.写出程序结果
[java] view plaincopy
class Demo  
{  
    public static void func()  
    {  
        try  
        {  
            throw new Exception;  
            System.out.println("A");  
        }  
        catch (Exception e)  
        {  
            System.out.println("B");  
        }  
    }  
    public static void main(String [] args)  
    {  
        try  
        {  
            func();  
        }  
        catch (Exception e)  
        {  
            System.out.println("C");  
        }  
        System.out.println("D");  
    }  
}  
结果是:编译失败,因为打印"A"的语句执行不到。记住:throw单独存在,下面不要定义语句,因为执行不到。


17.选择题
[java] view plaincopy
class Demo   
{  
    public void func()  
    {  
        //位置1;  
    }  
    class Inner  
    {  
    }  
    public static void main(String [] args)  
    {  
        Demo D=new Demo();  
        //位置2;  
    }  
}  

A 在位置1写 new Inner();
B 在位置2写 new Inner();
C 在位置2写 new d.Inner();
D 在位置2写 new Demo.Inner();
结果是:  A对,
B错 因为主函数是静态的如果要访问Inner需要被static修饰,
C错 格式错误,new new demo().Inner();
D错 因为Inner不是静态的
格式  Outer.Inner in=new Outer().new Inner()




18.写出程序结果
[java] view plaincopy
class Exc0 extends Exception  
{}  
class Exc1 extends Exc0  
{}  
class Demo  
{  
    public static void main(String [] args)  
    {  
        try  
        {  
            throw new Exc1;  
        }  
        catch (Exception e)  
        {  
            System.out.println("Exception");  
        }  
        catch(Exc0 e)  
        {  
            System.out.println("Exc0");  
        }  
    }  
}  

结果是:编译失败,多个catch时,父类的catch要放到下面。



19.补足代码
[java] view plaincopy
interface Test  
{  
    void func();  
}  
class Demo  
{  
    public static void main(String[] args)   
    {  
        //补足代码,(匿名内部类)  
        new Demo().show(new Test()  
        {  
            public void func(){}  
        }  
    });  
    void show(Test t)  
    {  
        t.func();  
    }  
}  



20.写出程序结果
[java] view plaincopy
class Test  
{  
    public static String output="";  
    public static void foo(int i)  
    {  
        try  
        {  
            if(i==1)  
                throw new Exception();  
            output+="1";  
        }  
        catch (Exception e)  
        {  
            output+="2";  
            return;  
        }  
        finally  
        {  
            output+="3";  
        }  
        out+=4;  
    }  
    public static void main(String args[]);  
    {  
        foo(0);  
        System.out.println(output)  
        foo(1);  
        System.out.println(output)  
    }  
}  

结果是:134
    13423



21.补足compare函数内的代码,不许添加其他函数
[java] view plaincopy
class Circle  
{  
    private static double pi=3.14;  
    private double radius;  
    public static double compare(Circle[] cir)  
    {  
        //程序代码//其实就是在求数组中的最大值  
        int max=0;  
        for(int x=1;x<cir.length;x++)  
        {  
            if(cir[x].radius>cir[max])  
                max=x;  
        }  
        return cir[max].radius;  
    }  
}  
class TC  
{  
    public static void main(String [] args)  
    {  
        Circle cir []=new Circle[3];//创建了一个类类型数组  
        Cir[0]=new Circle(1.0);  
        Cir[1]=new Circle(2.0);  
        Cir[2]=new Circle(4.0);  
        System.out.println("最大的半径值是:"+Circle.compare(cir));  
    }  
}  



22.写出程序结果
[java] view plaincopy
public class Demo  
{  
    private static int j=0;  
    private static boolean methodB(int k)  
    {  
        j+=k;  
        return true;  
    }  
    public static void methodA(int i)  
    {  
        boolean b;  
        b=i<10|methodB(4);  
        b=i<10||methodB(8);  
    }  
    public static void main(String[] args)   
    {  
        methodA(0);  
        System.out.println(j);  
    }  
}  

结果是:4



23.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在数组,第一次出现的位置(序号从0开始计算)否则返回-1,要搜索的字符数组和字符都以参数形式传递给该方法,如果传入的数组为null,应该抛出IllegalArgumentsException异常在类的main方法中以各种可能出现的情况测试验证该方法编写的是否正确,例如,字符不存在,字符存在,传入的数组为null等。
[java] view plaincopy
getIndex(null,'a');  
public int getIndex(char [] arr,char key)  
{  
    if(arr==null)  
        throw new IllegalArgumentsException("数组为null");  
    for(int x=0;x<arr.length;x++)  
    {  
        if(arr[x]==key)  
            return x;  
    }  
    return -1;  
}  




24.补足compare函数的代码,不许添加其他函数
[java] view plaincopy
class Circle  
{  
    private double radius;  
    public Circle(double r)  
    {  
        radius=r;  
    }  
    public Circle compare(Circle cir)  
    {  
    //程序代码  
        return (this.radius>cir.radius)?this:cir;  
  
    }  
}  
class TC  
{  
    public static void main(String[] args)   
    {  
        Circle cir1=new Circle(1,0);  
        Circle cir2=new Circle(2,0);  
        Circle cir;  
        if(cir1==cir)  
        System.out.println("圆1的半径比较大");  
        else  
        System.out.println("圆2的半径比较大");  
    }  
}  
作者: 给点正能量    时间: 2015-6-15 15:08
感觉好难,都不怎么会
作者: 前进的途中    时间: 2015-6-15 15:45
看来任重而道远啊
作者: 古风_菜鸟    时间: 2015-6-15 21:23
感谢分享,学习。
作者: hellotaomi    时间: 2015-6-15 22:19
楼主你黑马币怎么会负10的,这个也能赊账的吗??
作者: shuaiqi_P_D    时间: 2015-6-15 22:20

楼主你黑马币怎么会负10的,这个也能赊账的吗???????
作者: 王骥    时间: 2015-6-15 23:01
收藏学习下
作者: 我的最优解    时间: 2015-6-15 23:05
面试还问这个。。。。
作者: q757571446    时间: 2015-6-15 23:18
这尼玛涨姿势了!黑马币海带赊账的

作者: hieiyukina    时间: 2015-6-15 23:46
谢谢分享 学习一下




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2