黑马程序员——面试题小总结1

来源:互联网 发布:数据库应用软件开发 编辑:程序博客网 时间:2024/06/05 00:48

------<a href="http://www.itheima.com"target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------

面试题小总结1

/*

         定义数组的格式

        

                   int[][]arr正确

                   intarr[][]正确

                   int[]arr[]正确

                  

                   int[][]arr = new int[][];//错误

                   int[][]arr = new int[3][2];//正确

                   int[][]arr = new int[3][]; //正确

                   int[][]arr = new int[][2]; //错误

                   int[][]arr = new int[][]{{1,2},{2,3},{4}};//正确

                  

                   //一维数组

                   int[]arr = new int[3];//正确

                   intarr[] = new int[3];//正确

                   int[]arr = new int[]{1,2,3,4};//正确

                   int[]arr = new int[2]{1,2};//错误

                   int[]arr = {1,2,3,4,5};//正确

                  

                   int[]arr;

                   arr= new int[3];//正确

                   arr[0]= 11;

                   arr[1]= 22;

                   arr[2]= 33;

                  

                   int[]arr;

                   arr= {11,22,33}; //错误

                  

                  

*/

/*

         面试题

                   b3=3+434都是常量,所以java在编译时期会检查该常量的和是否超出byte类型的范围。如果没有可以赋值。

                   b4=b1+b2不可以,是因为b1b2是变量,因为变量的值会变化,不确定具体的值,所以默认使用int类型进行存储。

*/

class DataTypeDemo4 {

        

         publicstatic void main(String[] args) {

                   byteb1 = 3;

                   byteb2 = 4;

                   byteb3 = 3+4;

                   byteb4 = b1+b2;//错误:可能损失精度

                  

                   System.out.println(b3);//7

                   System.out.println(b4);

         }

}

/*

         面试题

        

         1:看程序写结果

         classFu{

                   publicint num = 10;

                   publicFu(){

                            System.out.println("fu");

                   }

         }

         classZi extends Fu{

                   publicint num = 20;

                   publicZi(){

                            System.out.println("zi");

                   }

                   publicvoid show(){

                            intnum = 30;

                            System.out.println(num);

                            System.out.println(this.num);

                            System.out.println(super.num);

                   }

         }

         classTest {

                   publicstatic void main(String[] args) {

                            Ziz = new Zi();

                            z.show();

                   }

         }

        

        

         运行的结果:

                   fu

                   zi

                   30

                   20

                   10

        

         ----------------------------------------------

        

         2:面试题

         classFu {

                   static{

                            System.out.println("静态代码块Fu");

                   }

 

                   {

                            System.out.println("构造代码块Fu");

                   }

 

                   publicFu() {

                            System.out.println("构造方法Fu");

                   }

         }

 

         classZi extends Fu {

                   static{

                            System.out.println("静态代码块Zi");

                   }

 

                   {

                            System.out.println("构造代码块Zi");

                   }

 

                   publicZi() {

                            System.out.println("构造方法Zi");

                   }

         }

 

         Ziz = new Zi(); 请执行结果。

                  

                   静态代码块Fu

                   静态代码块Zi

                   构造代码块Fu

                   构造方法Fu

                   构造代码块Zi

                   构造方法Zi

                  

         A:静态随着类的加载而加载。

         B:静态代码块 --构造代码块 --构造方法的执行流程

                   静态代码块 --构造代码块 --构造方法

         C:只要有子父关系,肯定先初始化父亲的数据,然后初始化子类的数据。

                  

----------------------------------------------------------

*/

packagecn.itcast_04_Exception_TryCatchFinally;

/*

 * 面试题

 */

public class FinallyTest {

         publicstatic void main(String[] args) {

                  

                   intx = 20;

                   try{

                            x= 10;

                            System.out.println(x/0);

                  } catch (ArithmeticException e) {

                            System.out.println("除数不能为零");

                            x= 5;

                   }finally {

                            x= 3;

                            System.out.println("除法运算结束");

                   }

                  

                   System.out.println("over");

                   System.out.println("x="+ x);

                  

         }

}

/*

         读程序写结果

*/

class IfDemo9 {

         publicstatic void main(String[] args){

                   booleanflag = false;

        

                   if(flag == false) {

                            System.out.println(1);

                   }

                  

                   if(flag = false) {

                            System.out.println(2);

                   }

                  

                   if(flag = false) {

                            System.out.println(3);

                   }else if (flag) {

                            System.out.println(4);

                   }else {

                            System.out.println(5);

                   }

                  

                   if(flag = false) {

                            System.out.println(3);

                   }else if (!flag) {

                            System.out.println(4);

                   }else {

                            System.out.println(5);

                   }

         }

}

/*

         成员内部类面试题

*/

class Outer {

                   //this对象

                   //不是真的这样,方便大家理解public static Outer this = new Outer();

                  

                   publicint num = 10;

                  

                   classInner {

                            //this对象

                            publicint num = 20;

                           

                            publicvoid show() {

                                     intnum = 30;

                                     System.out.println(num);

                                     System.out.println(this.num);

                                     //System.out.println(super.num);//错误, super.num ,找不到变量 num

                                     //System.out.println(newOuter().num);

                                     System.out.println(Inner.this.num  );

                                     System.out.println(Outer.this.num  );

                            }

                   }

         }

//在控制台分别输出 30,20,10

 

class InnerClassDemo3 {

         publicstatic void main(String[] args) {

                   //创建内部类对象

                   Outer.Inneroi = new Outer().new Inner();

                   oi.show();

         }

}

/*

         匿名内部类面试题

                   要求在控制台输出”HelloWorld

                  

         Outer.method(),在Outer类中有一个静态method方法,

         返现Outer.method()的结果可以调用show(),说明这个结果是一个对象,并且该对象可以调用show方法

        

 

*/

interface Inter {

         publicabstract void show();

}

 

class Outer {

         //补齐代码

         publicstatic Inter method() {

        

                   returnnew Inter(){//接口子类对象

                            //重写接口中的抽象方法

                            publicvoid show(){

                                     System.out.println("HelloWorld");

                            }

                   };

         }

        

}

        

class InnerClassDemo7 {

         publicstatic void main(String[] args) {

                     Outer.method().show();

           }

}

package cn.itcast_04_Integer;

/*

 * 面试题

 */

public class IntegerDemo6 {

         publicstatic void main(String[] args) {

                   Integeri1 = new Integer(127);

                   Integeri2 = new Integer(127);

                   System.out.println(i1== i2);//false

                   System.out.println(i1.equals(i2));//true

 

                   Integeri3 = new Integer(128);

                   Integeri4 = new Integer(128);

                   System.out.println(i3== i4);//false

                   System.out.println(i3.equals(i4));//true

 

                   Integeri5 = 127;//Integer i5 = Integer.valueOf(127);

                   Integeri6 = 127;//Integer i6 = Integer.valueOf(127);

                   System.out.println(i5== i6);//true

                   System.out.println(i5.equals(i6));//true

 

                   Integer.valueOf(127);

 

                   Integeri7 = 128;//Integer i7 = Integer.valueOf(128);

                   Integeri8 = 128;//Integer i8 = Integer.valueOf(128);

                   System.out.println(i7== i8);//false

                   System.out.println(i7.equals(i8));//true

 

         }

}

 

 

 

0 0
原创粉丝点击