A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

顺便求高手解答我的疑问
能给我解释一下选择题的第9题为什么args[5]也行
写出结果题第2题 为什么是先Test后Demo
选择题
1. Java中用来实现继承的关键字是____A
        A、extends                B、implements                C、public                D、protected
2.如果要用到一个接口的关键字是____B
        A、import                B、implements                C、extends                D、final
3.接口是Java面向对象的实现机制之一,以下说法正确的是b
        A、Java支持多重继承,一个类可以实现多个接口
        B、Java只支持单重继承,一个类可以实现多个接口
        C、Java只支持单重继承,一个类只可以实现一个接口
        D、Java支持多重继承,但一个类只可以实现一个接口
4.请分析以下代码,请问下面____不可以产生Q8对象?b        public class Q8{
                public Q8(int i){}
                public Q8(int i,float f){}
        }
        A、Q8 q=new Q8();                        B、Q8 q=new Q8(10);
        C、Q8 q=new Q8(10,10f);                D、以上方法均不可以
5.设有下面两个类的定义:
        class  Person {
                long     id;
                String   name;
        }
        Class Student extends Person{
                int scroe;
                int  getScore(){}
        }
则类Student和类Person的关系是__b
        A、包含关系                B、继承关系                C、关联关系                D、上述类定义有语法错误
6. 以下代码__c__语句会被执行?
        ①        int i = 10;
                try {
        ②                System.out.println(i);
        ③                return;
                } catch (Exception e) {
        ④                System.out.println("程序运行出现异常");
                } finally {
        ⑤                System.out.println("程序运行到这里");
                }
        ⑥        System.out.println("Bye Bye");
        A、①②③④⑥        B、编译出错                C、①②③⑤                D、①②③                E、①②⑤⑥
7. __C__类是JAVA语言中所有类的父类。
        A、class                B、java.lang                C、Object                D、以上都不正确
8. 下列关于抽象类描述正确的是____C
        A、某个抽象类的父类是抽象类,则这个类必须要重写父类中的所有抽象方法
        B、接口和抽象类是一回事
        C、绝对不能使用抽象类去创建对象
        D、抽象类中不可以有非抽象方法
9.下面是在命令行中运行JAVA程序A, //为什么5也能
        java  A  the  first  snow , the  first  snow  came
怎样才能在main(String[]  args)方法中访问单词"first"_B_d__?
        A、args[0]        B、args[1]        C、args[2]        D)args[5]        E、以上都不正确
10. 下面的程序输出的结果是___b
        public class A implements B {
                int k=20;
                public static void main(String args[])        {
                        int i;
                        B c1 = new A();
                        i= c1.k;
                        System.out.println("i="+i);
                }       
        }
        interface B {
                int k = 10;
        }
        A) i=20                        B) i=10      C) 程序有编译错误            D) i=true      
____________________________________________________________________________
写出程序结果
1 class Demo
{       
        public static void func()//如果加上这个声明throws Exception 结果是:        {
                try
                {
                        throw  new Exception();
                }
                finally
                {
                        System.out.println("B");
                }
        }
        public static void main(String[] args)
        {
                try
                {
                        func();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("C");
                }
                System.out.println("D");
        }
}
1:未加 编译失败--未报告的错误或异常---------
2:B
C
d
-----------------------------------------------------------
2. class Test
{
        Test()
        {
                System.out.println("Test");
        }
}
class Demo extends Test
{
        Demo()
        {
                System.out.println("Demo");
        }
        public static void main(String[] args)
        {
                new Demo();
                new Test();
        }
}
Test
Demo
3. interface A{}  
class B implements A
{
        public String func()
        {
                return "func";
        }
}
class Demo
{
        public static void main(String[] args)
        {
                A a=new B();
                System.out.println(a.func());。
        }
}
编译失败 接口中没有定义func方法

4. class Fu
{
        boolean show(char a)
        {
                System.out.println(a);
                return true;
        }
}
class Demo extends Fu
{
        public static void main(String[] args)
        {
                int i=0;
                Fu f=new Demo();
                Demo d=new Demo();
                for(f.show('A'); f.show('B')&&(i<2);f.show('C'))
                {
                        i++;
                        d.show('D');
                }       
        }
        boolean show(char a)
        {
                System.out.println(a);
                return false;
        }
}
A
B

5 interface A{}
class B implements A
{
        public String test()
        {
                return "yes";
        }
}
class Demo
{
        static A get()
        {
                return new B();
        }
        public static void main(String[] args)
        {
                A a=get();
                System.out.println(a.test());        }
}
编译失败。多态调用了实现类中独有的方法
====================================================================
6.
写出程序结果:   
class Super
{
        int i=0;
        public Super(String a)
        {
                System.out.println("A");
                i=1;       
        }
        public Super()
        {
                System.out.println("B");
                i+=2;
        }
}
class Demo extends Super
{
        public Demo(String a)
        {
                System.out.println("C");
                i=5;                               
        }
        public static void main(String[] args)
        {
                int i=4;
                Super d=new Demo("A");
                System.out.println(d.i);
        }
}
B
C
5

====================================================================
7.
interface Inter
{
        void show(int a,int b);
        void func();
}
class Demo
{
        public static void main(String[] args)
        {
                //补足代码;调用两个函数,要求用匿名内部类
                Inter in = new Inter(){
        Public void show(int a,int b){}
        Public void func(){}
        };
        In.show(1,2);
        In.func();
               

        }
}


====================================================================
8.
写出程序结果
class TD
{
        int y=6;
        class Inner
        {
                static int y=3;  
                void show()
                {
                        System.out.println(y);
                }
        }
}
class TC
{
        public static void main(String[] args)
        {
                TD.Inner ti=new TD().new Inner();
                ti.show();
        }
}
内部类中有static成员,内部类必须是static
====================================================================
9
class Fu
{
        int num=4;
        void show()
        {
                System.out.println("showFu");
        }
}
class Zi extends Fu
{
        int num=5;
        void show()
        {
                System.out.println("showZi");
        }
}
class T
{
        public static void main(String[] args)
        {
                Fu f=new Zi();
                Zi z=new Zi();
                System.out.println(f.num);
                System.out.println(z.num);
                f.show(); //showZi
                z.show();         //showFu
        }
}

10
class Demo
{
        public static void main(String[] args)
        {
                try
                {
                        showExce();
                        System.out.println("A");
                }
                catch(Exception e)
                {
                        System.out.println("B");
                }
                finally
                {
                        System.out.println("C");
                }
                System.out.println("D");
        }
        public static void showExce()throws Exception
        {
                throw new Exception();
        }
}
B
C
D




补充题:

1.写出程序结果
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";
                }
                output+="4";
        }
        public static void main(String args[])
        {
                foo(0);
                System.out.println(output);
                foo(1);
                System.out.println(output);
        }
}
134
13423

2.写出程序结果
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");
                }
        }
}
编译失败 因为先捕获了父类异常
3
写出程序结果
class Super
{
        public int get(){return 4;}
}
class Demo15 extends Super
{
        public long get(){return 5;}       
        public static void main(String[] args)
        {
                Super s=new Demo15();
                System.out.println(s.get());
        }
}

_____编译失败
在Demo15类中,出现了有冲突的同名方法

评分

参与人数 1黑马币 +30 收起 理由
刘芮铭 + 30 赞一个!

查看全部评分

6 个回复

倒序浏览
2. class Test
{
        Test()
        {
                System.out.println("Test");
        }
}
class Demo extends Test
{
        Demo()
        {      super();                                                   、// 因为构造函数的第一行默认会有一个super();会访问父类的构造函数,所以先输出的是Test!!!
                System.out.println("Demo");
        }
        public static void main(String[] args)
        {
                new Demo();
                new Test();
        }
}
Test
Demo


回复 使用道具 举报
第9题为什么args[5]也行 ——————————————————亮点在于逗号也占了一位,所以第二个first在角标5位置
写出结果题第2题 为什么是先Test后Demo-------------------------------子类初始化,先要运行父类的构造方法,因为子类构造函数第一行默认super();
回复 使用道具 举报
能给我解释一下选择题的第9题为什么args[5]也行
9.下面是在命令行中运行JAVA程序A, //为什么5也能
        java  A  the  first  snow , the  first  snow  came
怎样才能在main(String[]  args)方法中访问单词"first"_B_d__?
        A、args[0]        B、args[1]        C、args[2]        D)args[5]        E、以上都不正确
解答:the first snow, this first soow came 如果把每个单词看做一维数组的中的元素,那么first的下标号为1和5,因为String[] args接受的是数组,所以args[1]和args[5]都可以
---------------------------------------------------------------------------------------------------------------
写出结果题第2题 为什么是先Test后Demo
2. class Test
{
        Test()
        {
                System.out.println("Test");
        }
}
class Demo extends Test
{
        Demo()
        {      
                System.out.println("Demo");
        }
        public static void main(String[] args)
        {
                new Demo();
                new Test();
        }
}

结果为

Test
Demo
Test
因为子类Demo继承与父类Test,即子类对象在实例化之前必须首先调用父类的中的构造方法后再调用子类自己的构造方法(现有父类再有子类)
回复 使用道具 举报
杨锦 发表于 2012-9-5 20:06
2. class Test
{
        Test()

谢谢!谢谢各位!
回复 使用道具 举报
尤洋 发表于 2012-9-5 20:27
第9题为什么args[5]也行 ——————————————————亮点在于逗号也占了一位,所以第二个first在 ...

大哥你牛。给力
回复 使用道具 举报
本帖最后由 王舜民 于 2012-9-6 13:45 编辑

这个的分析过程 是怎么样的?
public class A implements B {
                int k=20;
                public static void main(String args[])        {
                        int i;
                        B c1 = new A();
                        i= c1.k;
                        System.out.println("i="+i);
                }       
        }
        interface B {
                int k = 10;
        }

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马