1.下列输出结果
public class Test {
public static void main(String[] args) {
Test t=new t();
System.out.println(t.a(1));
}
public long a(long a){
System.out.println("a");
return ++a;
}
}
class t extends Test{
public long a(int a){
System.out.println("b");
return (long)++a;
}
}
答案:a 2(这两个不是重写,重写后用子类的,不是重写用父类的)
2.下列代码输出结果
public class Test2 {
int i=0;
public void a(){
++i;
}
public void b(){
++i;
}
public static void main(String[] args) {
Test2 t=new Test2();
System.out.println(t.i);
t.a();
System.out.println(t.i);
t.b();
System.out.println(t.i);
}
}
答案:0 1 2
3.下列代码运行结果
public class Test2 {
public void a(final int b){
b=3;
}
public static void main(String[] args) {
Test2 t=new Test2();
t.a(2);
}
}
答案:编译错误(final传入参数的时候会赋值,不可以在修改,final只能被初始化一次)
4.下列代码运行结果
public class Test {
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.a(1));
}
public long a(long a){
System.out.println("a");
return ++a;
}
public long a(double a){
System.out.println("b");
double b=a+1;
return (long)b;
}
}
class t extends Test{
public long a(int a){
return (long)++a;
}
}
答案:a 2
5.下列代码运行结果
public class Test {
final int a=1;
public static void main(String[] args) {
Test t=new t();
System.out.println(t.a);
}
}
答案:编译错误,(final的实例变量必须初始化,而且不能改变)
6.下列两个方法是重载吗?
private int a(int a){
System.out.println("a");
return ++a;
}
private short a(short a){
System.out.println("b");
return ++a;
}
答案:是
7.下列两个方法能够重写吗?
private int a(int a){
System.out.println("a");
return ++a;
}
private int a(int a){
System.out.println("b");
return ++a;
}
8.下列代码运行结果
public class Test {
public static void main(String[] args) {
Test t=new t();
System.out.println(t.a(1));
}
private int a(int a){
System.out.println("a");
return ++a;
}
}
class t extends Test{
private int a(int a){
System.out.println("b");
return ++a;
}
}
答案:a 2 (private 不能够重写)
9.下列语句会有编译错误吗?
public abstract class Test {
private int c;
}
答案:不会,但是c没有用处,抽象类需要被继承
10.下列运行结果
public abstract class Test {
public int age(){
return 1;
}
}
11.下列运行结果
public abstract class Test {
int a=33333;
static int b=777;
final int c=4444;
int d=555;
String s;
public int age(){
return 1;
}
public int age2(){
return 1;
}
public abstract String s(int a,String s);
}
public class Test2 extends Test{
int d=888;
public static void main(String[] args) {
Test2 t=new Test2();
System.out.println("age:"+t.age());
System.out.println("age2:"+t.age2());
System.out.println("a:"+t.a);
System.out.println("c:"+t.c);
System.out.println("d:"+t.d);
System.out.println("b:"+b);
System.out.println(t.s(5, "tom"));
}
public int age2(){
return 2;
}
@Override
public String s(int a, String s) {
// TODO Auto-generated method stub
return s+":"+a;
}
}
答案:(Test2中有的输出Test2中的,没有输出Test中的)
age:1
age2:2
a:33333
c:4444
d:888
b:777
tom:5
12.下列语句是否正确
public interface Test {
int a;
public String s(int a,String s);
}
答案:不正确(编译错误,接口中的变量默认为public static final类型,必须初始化,应为int a=9;在这里等价于public static final int a=9;)
13.下列语句输出结果
public interface Test {
int a=3;
}
public class Test2 implements Test{
public static void main(String[] args) {
Test2 t=new Test2();
System.out.println("a:"+t.a);
}
}
答案:3
14.下列语句输出结果
public interface Test {
int a=3;
}
public class Test2 implements Test{
public static void main(String[] args) {
Test2 t=new Test2();
t.a=5;
System.out.println("a:"+t.a);
}
}
答案:编译错误(a默认类型为public static final 是不可以修改的)
15.下列代码运行结果
public static void main(String[] args) {
Map<String,Integer> m=new HashMap<>();
m.put("1", 2);
m.put("", 3);
System.out.println(m);
}
答案:{ =3,1=2}
16.下列代码运行结果
public static void main(String[] args) {
Map<String,Integer> m=new HashMap<>();
m.put("1", 2);
m.put("1",5);
m.put("", 3);
m.put("", 4);
System.out.println(m);
}
答案:{ =4,1=5} (map的key唯一,当有相同的key插入时,原始数据将被覆盖)
17.下列代码运行结果
pub