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

课堂上的练习题:
注:按Java规范书写程序代码,如果你认为程序有错误,请指出,并说明程序错误原因。

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");
        }
}
编译失败:
如果func放上声明了该异常。结果是?B C D

====================================================================
2.
写出程序结果       
class Test
{
        Test()
        {
                System.out.println("Test");
        }
}
class Demo extends Test
{
        Demo()
        {
                //super();
                System.out.println("Demo");
        }
        public static void main(String[] args)
        {
                new Demo();
                new Test();
        }
}
Test
Demo

Test

考的子类的实例化过程。


====================================================================
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());
        }
}
编译失败:因为A接口中并未定义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());
        }
}
编译失败,因为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)
        {
                //super();
                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(4,5);
                in.func();
               
        }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马