# 2019

返回JAVA每日一练

# 2019.07-2019.09

# 06-30

public class Cat extends Animal{
   int age = 0;
   public Cat(int age) {
       this.age =  age;
  }

   public static void main(String[] args) {
       Cat cat = new Cat(3);
       System.out.println(cat.age);
  }
}
class Animal{
   int age = 2;
   public Animal(int age){
       this.age  = age;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

下列说法正确的是?

  • A.输出 2
  • B.输出3
  • C.输出0
  • D.编译错误

参考答案及解析

# 07-01

public class Test {
   private int a = 1;
   int b = 2;
   static int c =3;
   public static void main(String[] args) {
       Test t = new Test();
       //这里插入
  }
}
1
2
3
4
5
6
7
8
9

下列成员变量的调用,正确的是?

  • A.Test.c
  • B.Test.b
  • C.this.c D.t.a

参考答案及解析

# 07-02

public class Test {
   public static void main(String[] args) {
       int a =100,b = 50, c = a---b, d = a---b;
       System.out.print(a + "\t");
       System.out.print(b + "\t");
       System.out.print(c + "\t");
       System.out.print(d + "\t");
  }
}
1
2
3
4
5
6
7
8
9

请问上述代码输出的结果为?

  • A.98 50 50 48
  • B.98 50 50 49
  • C.100 49 48 52
  • D.100 48 48 49

参考答案及解析

# 07-03

public class Test {
    public static void main(String[] args) {
        Integer a = 1;
        Integer b = 1;
        Integer c = 233;
        Integer d = 233;
        System.out.print(a == b);
        System.out.print("\t");
        System.out.print(c == d);
    }
}
1
2
3
4
5
6
7
8
9
10
11

请问上述代码的结果为:

  • A.true、true
  • B.true、false
  • C.false、true
  • D.false、false

参考答案及解析

# 07-04


public class Test {
   public static void main(String[] args) {
       int num1 = 0;
       int num2 = 0;
       int sum = 0;
       for(int i = 0; i<3; i++){
           num1 = num1++;
           num2 = ++num2;
           sum += num1 * num2;
      }
       System.out.println(sum);
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

请问上述代码的结果为:

  • A.0
  • B.3
  • C.5
  • D.14

参考答案及解析

# 07-05

String str1 = new String("hello");
String str2 = new String("hello");
String str3 = "hello";
String str4 = "he" + "llo";
String str5 = "he";
String str6 = "llo";
1
2
3
4
5
6

请问下列输出true的是?

  • A.str1 == str2
  • B.str2 == str3
  • C.str3 == str4
  • D.str4 == (str5 + str6)

参考答案及解析

# 07-06

JVM内存不包含如下哪个部分?

  • A.Heap
  • B.Heap Frame
  • C.PC寄存器
  • D.Stacks

参考答案及解析

# 07-07

String str = new String("xiaodao");这个语句一共会创建几个String object?

  • A.1
  • B.2
  • C.3
  • D.4

参考答案及解析

# 07-08

public class Test {
   static boolean flag;
   public static void main(String[] args) {
       System.out.println(flag);
  }
}
1
2
3
4
5
6

执行上述代码,下列说法正确的是?

  • A.编译错误
  • B.输出true
  • C.输出false
  • D.输出null

参考答案及解析

# 07-09

public class Test {
   public static void main(String[] args) {
       Car car1 = Car.getInstance();
       Car car2 = Car.getInstance();
       System.out.println(car1 == car2);
  }
}

class Car{
   private static  Car car = new Car();

   private Car(){}

   public static Car getInstance(){
       return car;
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

上述代码的结果为:

  • A.true
  • B.false
  • C.编译错误
  • D.运行错误

参考答案及解析

# 07-10

int num = 1;
        List arrayList = new ArrayList();
        List linkedList = new LinkedList();

        long t1 = System.currentTimeMillis();
        for(int i = 0; i < 5000; i++){
            arrayList.add(0,num);
        }
        long t2 = System.currentTimeMillis() - t1;

        t1 = System.currentTimeMillis();
        for(int i = 0; i < 5000; i++){
            linkedList.add(0,num);
        }
        long t3 = System.currentTimeMillis() - t1;

        System.out.print(t2 > t3);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

请问上述代码的输出结果为:

  • A.true
  • B.false
  • C.编译错误
  • D.运行异常

参考答案及解析

# 07-11

private static int num(){
        int num = 3;
        try{
            num = num / 0;
        }catch (Exception e){
            num = 4;
        }finally {
            num = 5;
        }
        return num;
    }
1
2
3
4
5
6
7
8
9
10
11

请问上述函数的返回值是什么?

  • A.编译错误
  • B.3
  • C.4
  • D.5

参考答案及解析

# 07-12

下列方法定义中,正确的是?

  • A.int fun(){char c = 'a'; return (int)c;}
  • B.void fun(){...return true;}
  • C.int fun(){...return true;}
  • D.int fun(int a,b){return a+b;}

参考答案及解析

# 07-13

下列说法正确的是?

  • A.WeakMap继承自HashMap
  • B.HashSet继承自AbstractSet
  • C.LinkedList继承自List
  • D.AbstractSet继承自Set

参考答案及解析

# 07-14

下列json数据,错误的是?

  • A.{"name": {“age”:13}}
  • B.{"name":[13,14,15]}
  • C.{[13,14,15]}
  • D.{"name":{"age":[13,14,15]}}
  • E.{name: xiaodao}

参考答案及解析

# 07-15

数组——概念

public class Test {
   static int arr[] = new int[3];
   public static void main(String[] args) {
       System.out.println(arr[0]);
}
}
1
2
3
4
5
6

下列说法正确的是?

  • A.编译错误
  • B.运行错误
  • C.输出0
  • D.输出null

参考答案及解析

# 07-16

在java7中,不能作为switch()语句的参数的是?

  • A.浮点型
  • B.字符型
  • C.枚举型
  • D.int型

参考答案及解析

# 07-17

public class Test {
       public static void main(String[] args) {
        int i = 0;
        Integer j = 0;
        Integer k = 1000;
        Integer l = 1000;
        int m = 1000;
        int n = 1000;
        System.out.println(i == j);
        System.out.println(j.equals(i));
        System.out.println(k==l);
        System.out.println(k.equals(l));
        System.out.println(m==n);
        System.out.println(m==k);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

上述代码输出为:()

参考答案及解析

# 07-18

public class Test {
   private String a = "a";
   public void method(){
       String b = "b";
       final String c = "c";
  }
}
1
2
3
4
5
6
7

请问上述代码中的变量a、b、c分别在内存的什么储存区存放

  • A.堆区、堆区、堆区
  • B.堆区、栈区、堆区
  • C.堆区、栈区、栈区
  • D.堆区、堆区、栈区

参考答案及解析

# 07-19

类——加载顺序执结果

public class BaseEntity {
    public String justTest = "base";

    public BaseEntity(){
        printJust();
    }

    public void printJust(){
        System.out.println(justTest);
    }
    static class Son extends BaseEntity{
        public String justTest = "son2";

        @Override
        public void printJust(){
            System.out.println(justTest);
        }
    }

    public static void main(String[] args) {
        BaseEntity baseEntity = new Son();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

上述程序运行结果?

  • A.null
  • B.father
  • C.son
  • D.编译错误

参考答案及解析

# 07-20

类——实例、值传递变化

public class TestValue {
    void start(){
        RealValue realValue = new RealValue();
        System.out.println(realValue.litByte);
        RealValue realValue1 = change(realValue);
        System.out.println(realValue.litByte+"  "+realValue1.litByte);
    }

    private RealValue change(RealValue realValue){
        realValue.litByte = 1;
        return realValue;
    }

    public static void main(String[] args) {
        TestValue testValue = new TestValue();
        testValue.start();
    }
}

public class RealValue {
    Byte litByte;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

上述代码输出结果?

  • A.null nulll 1
  • B.null 1 1
  • C.0 0 1
  • D.0 1 1

参考答案及解析

# 06-30解析

back

1.相关知识
当父类中没有无参构造时,子类继承父类的构造方法中需要显示的调用super(parameter)父类的有参构造

2.答案解析
Animal没有无参构造
所以Cat类并没有显示调用父类的有参构造
A.编译错误,错误
B.编译错误,错误,若父类有无参构造则选B,错误
C.编译错误,错误
D.编译错误,正确

# 07-01解析

back

1.相关知识
成员变量的调用根据其修饰符的不同,也会有些许不同
一般的成员变量可以通过实例名.成员变量名调用
静态成员变量可以通过类名.成员变量名调用
this不能再静态方法中使用

2.答案解析
A.静态变量需要是用类名.变量名,正确
B.类名.变量名无法调用普通成员变量,错误
C.main方法中不能使用this,错误
D.实例名.变量名可以在同一个类中调用私有的成员变量,正确

答案选A、D

# 07-02解析

back

1.相关知识
++、--运算符优先级大于+、--在后先运算后自减

2.答案解析
a---b等价于先a--再减去b,a = 100 - b=50 ,c=50,a=99
a---b a=99-50 =49,d=49,a=48
因此最终a=98,b=50,c=50,d=49
输出结果为98 50 50 49

答案选B

# 07-03解析

back

1.相关知识
Integer中有预先定义好的-128~127的值,在此范围内则Integer变量指向的是统一存储空间

2.答案解析
1在预先定义范围,233不再预先定义范围,因此a、b指向同一存储空间,而c、d指向不同存储空间,因此输出true false

答案选B

# 07-04解析

back

1.相关知识
a = a++时,因为是先运算后++,所以a先赋值给a,之后才自增,因此a = a++结果还是为a,但是a = ++a时,因为是先自增后赋值,因此是a = a+1。

2.答案解析
每一次循环num1的值为0
每一次循环num2的值+1
因此num1 * num2始终为0
因此最终sum的结果同样为0

答案选A

# 07-05解析

back

1.相关知识
String类型会指向堆内存中的存储空间,因此当通过==对String类型进行比较时,比较的是相应变量的地址。只要指向的是同一地址则两个String类型==比较返回true,如果指向的是不同地址则返回false。

2.答案解析
A.str1、str2都new了一个新的String变量,因此指向不同地址,false
B.str2开辟新的内存空间,因此与str3不是指向同一地址,false
C.两个字符串通过+拼接,本质在编译时会自动变换为一个字符串,相当于str4 = "hello",str3已经有所定义,因此会与str3指向同一地址,true
D.两个单独的String变量通过+拼接,本质是new一个新的String变量,因此会开辟新的内存空间,因此与str4不是指向同一地址,false

答案选C

# 07-06解析

back

1.相关知识
JVM内存分为五大区域

虚拟机栈VM Stacks(栈帧Frame)、本地方法栈(Native Method Stacks)、方法区(Mehthod Area(Non-Heap))、PC寄存器(Program Counter Register)、堆(Heap)

2.答案解析
A.属于、B.不属于、C.属于、D.属于

答案选B。

# 07-07解析

back

1.相关知识
String类创建时会前往字符串常量池寻找有没有相应的字符串,如果没有则会创建新的String对象,并且将相应的字符串放入到字符串常量池,再通过new再次创建一个新的String对象,指向常量池中的String变量。

2.答案解析
由于之前都没有创建过xiaodao的字符串,因此首先会创建一个指向字符串常量池的String对象,再创建一个String对象指向它,因此一共会创建两个String对象

答案选B。

# 07-08解析

back

1.相关知识
成员静态变量如果不对他们赋值,他们会具备初始值
boolean类型的默认初始值为false

2.答案解析
flag为静态成员变量,因此有默认初始值,直接输出,不会编译错误。
boolean类型的初始值为false

答案选C

# 07-09解析

back

1.相关知识
单例模式是一种常见的软件设计模式,该模式一个类只有一个实例,并且它自行实例化,并且向整个系统提供。

2.答案解析
该单例模式的创建方式为饿汉式,每次类加载时就初始化,但容易浪费内存,作为静态变量它在类加载时就进行了创建而getInstance方法调用的都是同一个实例,因此在两个Car对象进行比较时他们比较的是同一个实例,因此会返回true。

答案选A

# 07-10解析

back

1.相关知识
System.currentTimeMillis()是java中用来获取当前系统时间时间最常用的方法
在执行插入时LinkedList比ArrayList的效率更高

2.答案解析
分析代码可以看出通过t1得出当前系统时间,t2得出执行arrayList完成时的系统时间减去之前t1的系统时间,获取得到执行插入操作花费的时间,同理得到执行LinkedList完成插入操作所花费的时间进行比较。
其间并无编译和代码错误,LinkedList插入效率比ArrayList效率高,因此t2>t3为真,输出true.

答案选A

# 07-11解析

back

5

# 07-12解析

back

1.相关知识
方法名前可以定义返回值,每一个参数都需要定义类型

2.答案解析
A.char类型都有对应的字符编码,因此可以转换为整型,正确
B.方法定义为无返回值不可返回true,错误
C.方法定义为整型返回不可返回true,错误
D.参数类型b需要添加参数类型,错误

答案选A

# 07-13解析

back

  • 1.相关知识

大多数集合由Collection衍生而来,map系列类单独为Map接口衍生来

A.WeakMap不存在于java中,错误
B.HashSet继承自Abstract了类同时实现了Set接口,正确
C.LinkedList实现了List接口,并非继承,错误
D.AbstractSet实现了Set接口,并非继承,错误

答案选B

# 07-14解析

back

  • 1.相关知识

{}表示的是json对象
json对象需要有键值对组成,且属性必须加双引号

答案:CE

# 07-15解析

back

  • 1.相关知识

数组作为静态成员时,初始化之后具有默认值
默认值与数组存储的额基本数据类型默认值一致
如int[]数组则默认值为0

  • 2.答案解析

代码编译正常,运行正常
该静态成员数组元素的默认值为0

答案选C

# 07-16解析

back

  • 1.相关知识

switch语句可根据()中的条件来选择执行相应的语句
switch语句属于选择语句的一种通常用来控制程序流程,执行满足条件的相应语句

  • 2.答案解析

java7中开始支持字符型
枚举型、int型之前就可以使用
浮点型不能作为switch语句的参数类型

答案选A

# 07-17解析

back

  • 1.相关知识

jdk1.5 java添加了自动拆装箱

基本类型和基本封装类型通过“==”比较时,会自动拆装箱后进行比较

  • 2.答案解析

i==j为基本类型与Integer的比较,因此会自动将j拆箱,为值的比较因此相等
j.equals(i)会将i做自动装箱,同样为值的比较,因此值相等

所以答案为:true、true、false、true、true、true

# 07-18解析

back

  • 1.相关知识

成员变量存放在堆区
方法中的局部变量存放在栈区

  • 2.答案解析

a为成员变量,存放在堆区
b、c都是方法中的局部变量,存放在栈区
因此是堆区、栈区、栈区

答案选C

# 07-19解析

back

  • 1.相关知识

当声明父类对象,new子类实例时,会首先创建父类对象之后才会创建子类对象。

  • 2.答案解析

首先创建Father对象,调用Father构造方法
调用printName()方法,子类中有此方法因此会调用子类的该方法
但此时子类属性还未加载
因此打印name时没有实际值,输出null

答案选A

# 07-20解析

back