面试题汇总
1、byte b= 255 ; 报错么?
报错,byte的范围是 -128~127
另外int的范围是-2^31 ~ 2^31-1
2、int $x; 和int #x; 会报错么?
1 2
| #x会报错,java命名规范: $ 、字母、下划线开头都行,后面的可以是数字、字母、下划线.
|
3、以下程序不会不会报错?为什么?
1 2 3 4 5 6 7 8 9 10
| public class B5Mtest {
static int k ; public static void main(String[] args) { System.out.println(k);
}
}
|
4、子类能否缩小父类的访问权限?能不能放大?
不能缩小,能够放大。
1 2 3 4 5 6 7 8 9 10 11 12
| public class B5MC1 { protected void printHello(){ System.out.println("Hello class1"); } }
public class B5MC2 extends B5MC1{ public void printHello(){ System.out.println("Hello class1"); } }
|
类2改成private报错。
另外附上访问权限:
1 2 3 4 5
| 包外 子类 包内 类内 public yes yes yes yes protected no yes yes yes default no no yes yes private no no no yes
|
5、static 方法能不能被重载?
能够被重载,也能够被覆盖。
6、以下输出是什么?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class B5MC1 { void show(){ System.out.println("static show C1"); } }
public class B5MC2 extends B5MC1{
static void show(){ System.out.println("static show C2"); } public static void main(String[] args) { B5MC2 b = new B5MC2(); b.show(); } }
|
编译报错。子类不能用static 覆盖父类方法。
7、运行时异常和检查式异常有哪些?
运行时异常:(都继承了RuntimeException)
1 2 3 4
| ClassCastException ConcurrentModificationException IndexOutOfBoundsException NullPointerException
|
检查式异常:
1 2
| IOException SQLException
|
8、下面程序输出是什么?
1 2 3 4 5 6 7 8 9 10 11 12 13
| public static void main(String[] args) { int num1 = 1; Integer num2 = 1; Integer num3 = new Integer(1); Integer num4 = Integer.valueOf(1); System.out.println(num1 == num2); System.out.println(num2 == num3); System.out.println(num3 == num4); System.out.println(num4 == num1); System.out.println(num4 == num2); }
|
输出;
1 2 3 4 5
| true false false true true
|
10、下面程序的输出是什么?
1 2 3 4 5 6 7 8 9 10 11
| public static void main(String[] args) { String s1 = "ab"+"cd"; String s2 = "abcd"; String s3= new String("abcd"); String s4= s3; String s5= s3; System.out.println(s1==s2); System.out.println(s2==s3); System.out.println(s4==s5); }
|
输出;