Five Questions of Daily Java Written Exam-2020-9-16

Five Questions of Daily Java Written Exam-2020-9-16

/ ... / can be nested // comments can also be nested / ... / comment.

​ Correct answer: B Your answer: B (correct)

正确
错误

The inner class (also called the member inner class) can have 4 kinds of access rights. ()

​ Correct answer: A Your answer: B (wrong)

正确
错误

Analysis:

You can understand the inner class as a member of the class. Members have 4 access rights, and so does the inner class! They are private, protected, public, and default access permissions

  • Static members cannot be defined in inner classes

  • The inner class can directly access the member variables in the outer class,

  • The inner class can be defined outside the method of the outer class, can also be defined in the method body of the outer class

    • The access type of the inner class defined outside the method body can be four types: public, protecte, default, private, etc.-When creating an instance object of the inner class, you must first create an instance object of the outer class, and then use this outer class Instance object to create an instance object of the inner class
    • There can be no access type modifier in front of the inner class defined in the method, just like the local variable defined in the method, but the final or abstract modifier can be used in front of the inner class. This kind of inner class is invisible to other classes, and other classes cannot refer to this kind of inner class, but the instance objects created by this kind of inner class can be passed to other classes for access.
  • The static keyword can be added in front of the internal class defined outside the method to become a Static Nested Class, which no longer has the characteristics of an internal class, so in a narrow sense, it is not an internal class

What is the result of the following code execution ()?

public class Demo {
    
    
    class Super {
    
    
        int flag = 1;

        Super() {
    
    
            test();
        }

        void test() {
    
    
            System.out.println("Super.test() flag=" + flag);
        }
    }

    class Sub extends Super {
    
    
        Sub(int i) {
    
    
            flag = i;
            System.out.println("Sub.Sub()flag=" + flag);
        }

        void test() {
    
    
            System.out.println("Sub.test()flag=" + flag);
        }
    }

    public static void main(String[] args) {
    
    
        new Demo().new Sub(5);
    }
}

​ Correct answer: A Your answer: C (wrong)

Sub.test() flag=1
Sub.Sub() flag=5

Sub.Sub() flag=5
Sub.test() flag=5

Sub.test() flag=0
Sub.Sub() flag=5

Super.test() flag=1
Sub.Sub() flag=5

Analysis:

The order of code execution in inheritance is:

​ 1. The parent class static object, the parent class static code block

​ 2. Subclass static object, subclass static code block

​ 3. The parent class is non-static object, the parent class is non-static code block

​ 4. Parent class constructor

​ 5. Subclass non-static object, subclass non-static code block

​ 6. Subclass constructor

For this question: when only thinking about new Sub(5), the parent class initializes int flag = 1, and then executes the super class constructor Super (), the test () method executed in the super class constructor, factor The class rewrites the test() method, so the test() method in the parent class constructor actually executes the test() method of the subclass, so the output is Sub.test() flag=1, and then the subclass is executed The constructor Sub(5) assigns flag to 5, so the output result Sub.Sub() flag=5. Finally, A was chosen.

Starting from the previous question, I thought of a similar question I wrote last time:

package com.lbl.bstTest;

public class Base {
    
    
    private String baseName = "base";

    public Base() {
    
    
        callName();
    }

    public void callName() {
    
    
        System.out.println(baseName);
    }

    static class Sub extends Base {
    
    
        private String baseName = "sub";

        public void callName() {
    
    
            System.out.println(baseName);
        }
    }

    public static void main(String[] args) {
    
    
        Base b = new Sub();
    }
}

operation result:

Insert picture description here

Why is it called?

It seems to be similar to the above question. After my research, I found that the problem lies in private

//下面一题定义baseName是private私有的,所以子类无法继承,所以得到结果是null
private String baseName = "base";
//上面定义是的default类型,所以子类可以使用,所以结果是1
 int flag = 1;

Which of the following options is the correct calculation of the cosine of 42 degrees (angle)?

​ Correct answer: C Your answer: D (wrong)

double d=Math.cos(42)
double d=Math.cosine(42)
double d=Math.cos(Math.toRadians(42))
double d=Math.cos(Math.toDegrees(42))

Analysis:

Calculate the cosine value using the cos() method of the Math class

Because the unit of the parameter in Math.cos is radians

  • toRadians() is to convert angles to radians
  • toDegrees() converts radians to angles

The CMS garbage collector does not involve user threads in those stages

​ Correct answer: AC Your answer: A (wrong)

初始标记
并发标记
重新标记
并发清理

Analysis:

**User-level threads** refers to threads that are implemented in user programs without kernel support. They do not depend on the core of the operating system. Application processes use the thread library to provide functions for creating, synchronizing, scheduling and managing threads To control the user thread.

The GC process of CMS has 6 stages (4 concurrent, 2 suspends other applications)

1. STW initial mark

2. Concurrent marking

3. Concurrent precleaning (Concurrent precleaning)

4. Final remark (STW remark)

5. Concurrent sweeping

6. Concurrent reset

When marking for the first time and re-marking, we are required to suspend other applications, then user threads will not participate in these two stages

Guess you like

Origin blog.csdn.net/qq_37924905/article/details/108624368