1、byte b= 255 ; 报错么?
报错,byte的范围是 -128~127
另外int的范围是-2^31 ~ 2^31-1
2、int $x; 和int #x; 会报错么?
#x会报错,java命名规范:
$ 、字母、下划线开头都行,后面的可以是数字、字母、下划线.
3、以下程序不会不会报错?为什么?
public class B5Mtest {
static int k ;
public static void main(String[] args) {
System.out.println(k);
}
}
4、子类能否缩小父类的访问权限?能不能放大?
不能缩小,能够放大。
public class B5MC1 {
protected void printHello(){
System.out.println("Hello class1");
}
}
public class B5MC2 extends B5MC1{
public void printHello(){
System.out.println("Hello class1");
}
}
另外附上访问权限:
包外 子类 包内 类内
public yes yes yes yes
protected no yes yes yes
default no no yes yes
private no no no yes
5、static 方法能不能被重载?
能够被重载,也能够被覆盖。
6、以下输出是什么?
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();
}
}
7、运行时异常和检查式异常有哪些?
运行时异常:(都继承了RuntimeException)
ClassCastException
ConcurrentModificationException
IndexOutOfBoundsException
NullPointerException
IOException
SQLException
8、下面程序输出是什么?
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);
}
true
false
false
true
true
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);
}
true
false
true