常见面试题

 

abstract class Name { 
private String name; 
public abstract boolean isStupidName(String name) {} 
} 

//抽象类中的方法不能有方法体

 

abstract class Something { 
private abstract String doSomething (); 
} 

//抽象方法的访问标识符

 

public class Something { 
public int addOne(final int x) { 
return ++x; 
}  } 

//有final修饰的变量相当于常量

 

public class Something { 
public static void main(String[] args) { 
Other o = new Other(); 
new Something().addOne(o); } 
public void addOne(final Other o) { 
o.i++; 
}  } 
class Other { 
public int i; } 

 

public class Something { 
public static void main(String[] args) { 
Something s = new Something(); 
System.out.println("s.doSomething() returns " + doSomething()); 
} 
public String doSomething() { 
return "Do something ..."; 
}  } 

//静态方法中不能调用实例方法

 

interface A {
    int x = 0;//相当于常量
}

class B {
    int x = 1;
}

class C extends B implements A {
    public void pX() {
        System.out.println(super.x); //访问父类中的变量
        System.out.println(A.x); //访问接口中的变量
    }

    public static void main(String[] args) {
        new C().pX();
    }
}

 

class  Test4  {
       public static void main (String []  args)  {
           boolean x=true;
           boolean y=false;
           short z=42;

          if((z++==42)&&(y=true))z++;
           if((x=false) || (++z==45))  z++;
           System. out.println(“z=”+z);
        }
  }

z=46

 

public class ReturnExceptionDemo {
    
    
    static void methodA() {
        try {
            System.out.println("进入方法A");
            throw new RuntimeException("制造异常");
        } finally {
            System.out.println("用A方法的finally");
        }
    }

    static int methodB() {
        try {
            System.out.println("进入方法B");
            // throw new Exception();
            return 1;
        } catch (Exception e) {
            return 3;
        } finally {
            System.out.println("调用B方法的finally");
            // return 2;
        }
    }

    public static void main(String[] args) {
        try {
            methodA();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        int i = methodB();
        System.out.println(i);
    }
}

进入方法A
用A方法的finally
制造异常
进入方法B
调用B方法的finally
1

 

//题目1:一个数组,让数组的每个元素去除第一个元素,得到的商作为被除数所在位置的新值。
public class Test1 {
    public static void main(String[] args) {
        int[] arr = new int[]{12,43,65,3,-8,64,2};
        
//        for(int i = 0;i < arr.length;i++){
//            arr[i] = arr[i] / arr[0];
//        }
        for(int i = arr.length -1;i >= 0;i--){
            arr[i] = arr[i] / arr[0];
        }
        //遍历arr
        for(int i = 0;i < arr.length;i++){
            System.out.print(arr[i] + " ");
        }
    }
}

 

//输入两个正整数m和n,求其最大公约数和最小公倍数。
public class Test2 {
    public static void main(String[] args) {
        int m = 12;
        int n = 28;
        int max = (m > n)? m : n;
        int min = (m < n)? m : n;
        //最大公约数
        for(int i = min;i >= 1;i--){
            if( m % i == 0 && n % i == 0){
                System.out.println(i);
                break;
            }
        }
        //最小公倍数
        for(int i = max;i <= m * n;i++){
            if(i % m == 0 && i % n == 0){
                System.out.println(i);
                break;
            }
        }
    }
}

 

/*
 * 输入一个整形数组,数组里有正数也有负数。数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。
 * 求所有子数组的和的最大值。要求时间复杂度为O(n)。
 例如:输入的数组为1, -2, 3, -10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,
 因此输出为该子数组的和18。

 */
public class Test3 {
    public static void main(String[] args) {
        int[] arr = new int[]{1, -2, 3, 10, -4, 7, 2, -5};
        int i = getGreatestSum(arr);
        System.out.println(i);
    }
    
    public static int getGreatestSum(int[] arr){
        int greatestSum = 0;
        if(arr == null || arr.length == 0){
            return 0;
        }
        int temp = greatestSum;
        for(int i = 0;i < arr.length;i++){
            temp += arr[i];
            
            if(temp < 0){
                temp = 0;
            }
            
            if(temp > greatestSum){
                greatestSum = temp;
            }
        }
        if(greatestSum == 0){
            greatestSum = arr[0];
            for(int i = 1;i < arr.length;i++){
                if(greatestSum < arr[i]){
                    greatestSum = arr[i];
                }
            }
        }
        return greatestSum;
    }
}

 

/*
 * 将一个字符串进行反转。将字符串中指定部分进行反转。比如将“abcdefg”反转为”abfedcg”

 */
public class Test5 {
    public static void main(String[] args) {
        String str = new String("abcdefg");
        str = reverseString(str,2,5);
        System.out.println(str);
        
    }
    
    public static String reverseString(String str,int start,int end){
        char[] c = str.toCharArray();
        
        return reverseArray(c,start,end);
    }
    
    public static String reverseArray(char[] c,int start,int end){
        for(int x = start,y = end;x < y;x++,y--){
            char temp = c[x];
            c[x] = c[y];
            c[y] = temp;
        }
        return new String(c);
    }
}

 

/*
 * 古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,
 * 假如兔子都不死,问每个月的兔子总数为多少?   
//这是一个菲波拉契数列问题

 */
public class Test7 {
    public static void main(String[] args) {
        System.out.println("第1个月:" + 1);
        System.out.println("第2个月:" + 1);
        int M = 24;
        int f1 = 1,f2 = 1;
        int f;
        for(int i = 3;i <= M;i++){
            f = f2;
            f2 = f1 + f2;
            f1 = f;
            System.out.println("" + i + "个月:" + f2);
        }
    }
}

 

/*
 * 已知有一个数列:f(0) = 1,f(1) = 4,
 * f(n+2)=2*f(n+1) + f(n),==>f(n) = 2*f(n-1) + f(n-2)
 * 其中n是大于0的整数,求f(10)的值。

 */
public class Test8 {
    public static void main(String[] args) {
        Test8 t = new Test8();
        int i = t.func(10);
        System.out.println(i);
    }
    
    public int func(int n){
        if(n == 0)
            return 1;
        else if(n == 1)
            return 4;
        else{
            //return 2*func(n-1) + func(n-2);
            return func(n+2)-2*func(n+1);
        }
    }
}

 

//1.Java中的“==”和equals()方法有什么区别?
public class Test10 {
    public static void main(String[] args) {
        int i = 10;
        int j = 10;
        System.out.println(i == j);
        
        Person p1 = new Person("韩梅梅");
        Person p2 = new Person("韩梅梅");
        System.out.println(p1 == p2);//false
        System.out.println(p1.equals(p2));//false
        
        String str1 = new String("abc");
        String str2 = new String("abc");
        System.out.println(str1 == str2);//false
        System.out.println(str1.equals(str2));//true
        
        
        System.out.println(p1.name == p2.name);//true
    }
}

class Person{
    String name;
    
    public Person(String name){
        this.name = name;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
    
}

 

public class SelectSort {
    public static void main(String[] args) {
        int[] arr = new int[]{1, -2, 3, 10, -4, 7, 2, -5};
        arr = selectSort(arr);
        for(int i = 0;i < arr.length;i++){
            System.out.println(arr[i] + " ");
        }
    }
    public static int[] selectSort(int[] arr){
        for(int i = 0;i < arr.length -1;i++){
            int min = i;
            for(int j = i+1;j < arr.length;j++){
                if(arr[min] > arr[j]){
                    min = j;
                }
            }
            if(min != i){
                int temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
            
        }
        return arr;
        
    }
}

 

 

 Object:
public boolean equals(Object obj) {
        return (this == obj);
    }


String:
public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

 

 

                     

                              

posted @ 2018-05-24 14:25  coderlzb  阅读(309)  评论(0编辑  收藏  举报