黑马程序员——Java基础
------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ------- }public void func(){ }};in.show(4,5)in.funct();}}
Java基础---练习题
1.写出程序结果class Demo{public static void func(){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(){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){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);}}
结果是:BC5
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){
13.写出程序结果
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()throw Exception {throw new Exception();}} 结果是: BCD 14.写出程序结果class Super{int i=0;//Super(){}public Super(String s){i=1;}}class Demo extends Super{public Demo(String s){i=2;}public static void main(String[] args) {Demo d=new Demo("yes");System.out.println(d.i);}} 结果是:编译失败,因为父类中缺少空参数的构造函数或者子类应该通过Super语句指定要调用的父类中的构造 函数 15.写出程序结果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());}} 结果是:编译失败,因为子类父类中的get方法没有覆盖但是子类调用时候不能明确返回值是什么类型的。 所以这样的函数不能存在于子父类中。 16.写出程序结果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.选择题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.写出程序结果 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.补足代码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.写出程序结果 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 1342321.补足compare函数内的代码,不许添加其他函数
class Circle{private static double pi=3.14;private double radius;public static double compare(Circle[] cir){//程序代码//其实就是在求数组中的最大值int max=0;for(int x=1;x23.在一个类中编写一个方法,这个方法搜索一个字符数组中是否存在某个字符,如果存在,则返回这个字符在数组,第一次出现的位置(序号从0开始计算)否则返回-1,要搜索的字符数组和字符都以参数形式传递给该方法,如果传入的数组为null,应该抛出IllegalArgumentsException异常在类的main方法中以各种可能出现的情况测试验证该方法编写的是否正确,例如,字符不存在,字符存在,传入的数组为null等。
getIndex(null,'a');public int getIndex(char [] arr,char key){if(arr==null)throw new IllegalArgumentsException("数组为null");for(int x=0;x}}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的半径比较大");elseSystem.out.println("圆2的半径比较大");}}