黑马程序员技术交流社区

标题: 前12天面试题总结 [打印本页]

作者: yuanzhen    时间: 2016-5-9 22:03
标题: 前12天面试题总结
面试题总结:
Day02:看下面的程序是否有问题,如果有问题,请指出并说明理由。
        * byte b1 = 3;
        * byte b2 = 4;
        * byte b3 = b1 + b2;
        // 从两方面去回答这个题
        // b1和b2是两个变量,变量里面存储的值都是变化的,所以在程序运行中JVM是无法判断里面具体的值
        // byte类型的变量在进行运算的时候,会自动类型提升为int类型
         byte b4 = 3 + 4;
        // 3和4都是常量,java有常量优化机制,就是在编译的的时候直接把3和4的结果赋值给b4了
Day02:面试题
        * byte b = 10;
        * b++;
        * b = b + 1;
        * 问哪句会报错,为什么
        //b = b + 1 会报错,因为b + 1 类型提升为int类型.
Day02:面试题:看下面的程序是否有问题,如果有问题,请指出并说明理由。
        * short s=1;s = s+1;//有  s = (short)(s +1);
        * short s=1;s+=1;//没有
Day03:面试题:
        * 请自己实现两个整数变量的交换
        //int  a = a^b;
        //int  b = a^b;
        //int  a = a^b;
        * 注意:以后讲课的过程中,我没有明确指定数据的类型,默认int类型。
Day03:面试题
        * byte可以作为switch的表达式吗?// 可以
        * long可以作为switch的表达式吗?// 不可以
        * String可以作为switch的表达式吗?// 可以
Day08:面试题
   A:看程序写结果  

                class Student {
                        static {
                                System.out.println("Student 静态代码块");
                        }
                       
                        {
                                System.out.println("Student 构造代码块");
                        }
                       
                        public Student() {
                                System.out.println("Student 构造方法");
                        }
                }
       
                class Demo2_Student {
                        static {
                                System.out.println("Demo2_Student静态代码块");
                        }
                       
                        public static void main(String[] args) {
                                System.out.println("我是main方法");
                               
                                Student s1 = new Student();
                                Student s2 = new Student();
                        }
                }
/*
          Demo2_Student静态代码块
          我是main方法
          Student 静态代码块
          Student 构造代码块
          Student 构造方法
          Student 构造代码块
          Student 构造方法
*/


看程序写结果1
                class Fu{
                        public int num = 10;
                        public Fu(){
                                System.out.println("fu");
                        }
                }
                class Zi extends Fu{
                        public int num = 20;
                        public Zi(){
                                System.out.println("zi");
                        }
                        public void show(){
                                int num = 30;
                                System.out.println(num);
                                System.out.println(this.num);
                                System.out.println(super.num);
                        }
                }
                class Test1_Extends {
                        public static void main(String[] args) {
                                Zi z = new Zi();
                                z.show();
                        }
                }
/* 答案:Fu
       zi
           30
           20
           10
*/
                看程序写结果2
                class Fu {
                        static {
                                System.out.println("静态代码块Fu");
                        }
       
                        {
                                System.out.println("构造代码块Fu");
                        }
       
                        public Fu() {
                                System.out.println("构造方法Fu");
                        }
                }
       
                class Zi extends Fu {
                        static {
                                System.out.println("静态代码块Zi");
                        }
       
                        {
                                System.out.println("构造代码块Zi");
                        }
       
                        public Zi() {
                                System.out.println("构造方法Zi");
                        }
                }
       
                Zi z = new Zi(); 请执行结果。
/*答:静态代码块Fu
   静态代码块Zi
   构造代码块Fu
   构造方法Fu
   构造代码块Zi
   构造方法Zi
*/
A:方法重写的面试题
        * Override和Overload的区别?Overload能改变返回值类型吗?
        /* overload可以改变返回值类型,只看参数列表
         方法重写:子类中出现了和父类中方法声明一模一样的方法。与返回值类型有关,返回值是一致(或者是子父类)的
       
         方法重载:本类中出现的方法名一样,参数列表不同的方法。与返回值类型无关。

         子类对象调用方法的时候:
                 先找子类本身,再找父类。
                */
Day09 面试题
A:看下面程序是否有问题,如果没有,说出结果

                class Fu {
                        public void show() {
                                System.out.println("fu show");
                        }
                }
       
                class Zi extends Fu {
                        public void show() {
                                System.out.println("zi show");
                        }
       
                        public void method() {
                                System.out.println("zi method");
                        }
                }
       
                class Test1Demo {
                        public static void main(String[] args) {
                                Fu f = new Zi();
                                f.method();
                                f.show();
                        }
                }
//f.method() 编译失败
* B:看下面程序是否有问题,如果没有,说出结果
                class A {
                        public void show() {
                                show2();
                        }
                        public void show2() {
                                System.out.println("我");
                        }
                }
                class B extends A {
                        public void show2() {
                                System.out.println("爱");
                        }
                }
                class C extends B {
                        public void show() {
                                super.show();
                        }
                        public void show2() {
                                System.out.println("你");
                        }
                }
                public class Test2DuoTai {
                        public static void main(String[] args) {
                                A a = new B();
                                a.show();
                                B b = new C();
                                b.show();
                        }
                }

// 爱 你
A:面试题1
        * 一个抽象类如果没有抽象方法,可不可以定义为抽象类?如果可以,有什么意义?
        //可以
        // 这么做目的只有一个,就是不让其他类创建本类对象,交给子类完成
* B:面试题2
        * abstract不能和哪些关键字共存
        /* static  static 修饰可以直接类名.调用,不用创建对象,abstract必须由子类创建对象调用
           final   final 修饰的不可以重写,abstract修饰的强制让子类重写,矛盾
           private private 修饰的不可以被其他类访问,无法被子类重写
           */
Day10面试题
要求:使用已知的变量,在控制台输出30,20,10。
               
                class Outer {
                        public int num = 10;
                        class Inner {
                                public int num = 20;
                                public void show() {
                                        int num = 30;
                                        System.out.println(?);     //num
                                        System.out.println(??);    //this.num
                                        System.out.println(???);   //Outer.this.num
                                }
                        }
                }
                class InnerClassTest {
                        public static void main(String[] args) {
                                Outer.Inner oi = new Outer().new Inner();
                                oi.show();
                        }       
                }
面试题
*
                按照要求,补齐代码
                interface Inter { void show(); }
                class Outer { //补齐代码 }
                class OuterDemo {
                        public static void main(String[] args) {
                                  Outer.method().show();
                          }
                }
                要求在控制台输出”HelloWorld”

/*
public static Inter method(){
    return new Inter(){
          public void show(){
             System.out.println("HelloWorld");
           }
        };
}
*/
Day12 面试题
         1.判断定义为String类型的s1和s2是否相等
        * String s1 = "abc";
        * String s2 = "abc";
        * System.out.println(s1 == s2);                 //true                       
        * System.out.println(s1.equals(s2));         //true       
* 2.下面这句话在内存中创建了几个对象?
        * String s1 = new String("abc");                //        两个
* 3.判断定义为String类型的s1和s2是否相等
        * String s1 = new String("abc");                       
        * String s2 = "abc";
        * System.out.println(s1 == s2);                   //false
        * System.out.println(s1.equals(s2));   //true
* 4.判断定义为String类型的s1和s2是否相等
        * String s1 = "a" + "b" + "c";
        * String s2 = "abc";
        * System.out.println(s1 == s2);                //true
        * System.out.println(s1.equals(s2));//true
* 5.判断定义为String类型的s1和s2是否相等
        * String s1 = "ab";
        * String s2 = "abc";
        * String s3 = s1 + "c";
        * System.out.println(s3 == s2);      //false
        * System.out.println(s3.equals(s2)); //true



作者: dd6434541    时间: 2016-5-9 22:07
66666666666666666
作者: liurongcheng    时间: 2016-5-9 22:10
感觉这个可以有.......66666666666666666666
作者: wubo46    时间: 2016-5-9 22:58
默默的收下,应该用的上
作者: johnli    时间: 2016-5-9 23:04
谢谢分享
作者: runner    时间: 2016-5-9 23:28
好东西.谢谢!!!
作者: 梦想与渴望    时间: 2016-5-9 23:35
已收下,谢谢
作者: su3356859    时间: 2016-5-9 23:38
已收下,谢
作者: okchenyang44    时间: 2016-5-10 07:06
每个知识点都有对应的习题,习题能做出,知识点就掌握的差不多了,老师说得对,你看懂知识点,但是不懂怎么用还是等于零,所以得多敲代码!

作者: 女神之泪    时间: 2016-5-10 07:19
666666*已收藏




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