Day02: summary of written test questions

Posted by phpwannabe25 on Sat, 26 Feb 2022 13:58:35 +0100

catalogue

1, Multiple choice questions

2, Programming problem

1. Sort subsequence

2. Inverted string

1, Multiple choice questions

1.

Resolution:

Both parent class and new subclass objects are allowed

2.

Resolution:

You can see from the source code of String method tolower case

The return value of this method means that if there is in the constant pool, it will return the data in the constant pool

The return value of this method means that if there is no constant in the constant pool, a new string will be put in

Therefore, from this question, there is "Admin" in the original string constant pool, but there is no "Admin". During conversion, a new Admin will be executed. Therefore, the contents of the two strings are the same, but the addresses are different

3.

Resolution:

Because the hello method is a static member method, that is, a class member method, but the new object is an empty reference, but it is also an object that can call a class member method, which cannot be written in actual development

4.

Resolution:

A. Both super () and this() must be on the first line

C.this() and super() cannot appear in the same constructor at the same time, so they must be in the first line

D. In the static environment, this cannot be used because this is a reference to the current object, but the static method does not depend on the object

5.

Resolution:

Because the subclass does not have a parameterless constructor, the subclass will use the constructor of the parent class when creating objects

6.

Resolution:

Static member variables cannot appear in static member methods or ordinary methods. Static member variables belong to classes and do not belong to any method

7.

Resolution:

A.abstract can modify methods and classes, but cannot modify fields (member variables)

B. Abstract methods cannot have a method body, that is, they cannot have curly braces

C. Declaring abstract methods must not have curly braces

8.

Resolution:

A. The construction method cannot be omitted

B. The constructor must have the same name as the class name, but the constructor is a method

C. The constructor must be executed when a new object is created

D. A class can define multiple construction methods, and the construction methods can be overloaded

9.

Resolution:

A. Can achieve

B. Operations on member variables must be performed inside the method

C. If there is no abstract, even if there is, it is also wrong, because there is no overload, and it has the same name as the method method

D. Abstract methods cannot have method bodies (curly braces)

10.

Resolution:

The interface is to be implemented and provided for external use. It must not be private. Moreover, the modifier qualifier of the interface is public by default

2, Programming problem

1. Sort subsequence

Title Content:

Resolution:

1) The first is the array problem, and then subdivided is the problem of splitting the array

2) Secondly, the topic requirement is to know how many monotonically increasing or decreasing subarrays there are in this array

3) According to the traditional judgment of whether an array is increasing or decreasing, it should be to define a pure greater than or less than condition, but for this problem, pure judgment is not enough, because this array is a combination of these small arrays, and composite judgment must be carried out

4) Therefore, the problem-solving idea is obvious. How to make a composite judgment and judge whether it is possible according to the previous cycle? The answer is no, what about those two times? This needs to be analyzed again. What is most similar to the double-layer loop is breadth first search. For reference, we can traverse the entire array through the outer loop, and add constraints to the inner loop. We also know that the array is composed of multiple monotonically increasing or decreasing sub arrays. Therefore, we can give three loop conditions, greater than or equal to and less than, The i of the loop is used to control the loop process of the array

5) See the problem-solving code and notes for specific analysis

Problem solving Code:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        /*
        1.First, define an array with a length of n+1 to receive the data of the topic
        2.Why "n + 1"
        3.In the following multiple loops, there must be an array out of bounds, because i always has to be compared with the element at i+1
          Comparison, therefore, prevents the array from going out of bounds
        4.According to the topic, the minimum number of array elements is 1 and cannot be negative (this is not the most critical)
        5.Most importantly, no matter whether the last element is negative or positive, it will not be accessed because of our outer layer
          The cycle condition only determines i = n - 1;
        */
        int[] array = new int[n + 1];
        for(int i = 0;i < n;i++){
            array[i] = sc.nextInt();
        }
        //Define i variables and subarray counters for the outer loop
        int i = 0;
        int count = 0;
        //Outer loop. The control is also accessed in the array and will not be accessed beyond the boundary
        while(i < n){
            //The first case: if it is increased, it will increase until it is not increased. count + + allows i + + to access the subscript of the next element
            if(array[i] < array[i + 1]){
                //Keep visiting the same kind of increase
                while(i < n && array[i] < array[i + 1]){
                    i++;
                }
                //If the element at i+1 is less than the element at i, count++
                count++;
                i++;
                //For equal elements, they can be classified into one category without affecting the increase or decrease
            }else if(array[i] == array[i + 1]){
                i++;
            }else{
                //The decrease is the same as the increase
                while(i < n && array[i] > array[i + 1]){
                    i++;
                }
                count++;
                i++;
            }
        }
        System.out.println(count);
    }
}

2. Inverted string

Title Content:

Resolution:

1) This is a simple String topic. It uses the api of String to split and receive it in the form of String array

2) (method 1) define the left and right endpoints for element exchange

3) (method 2) define a StringBuilder object sb, cycle in reverse order, and splice sb

Problem solving Code:

Method 1:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] str = s.trim().split(" ");
        int left = 0;
        int right = str.length - 1;
        while(left < right){
            String temp = str[left];
            str[left] = str[right];
            str[right] = temp;
            left++;
            right--;
        }
        String s2 = "";
        for(int i = 0;i < str.length;i++){
            s2 = s2 + str[i];
            s2 = s2 + " ";
        }
        System.out.println(s2.trim());
    }
}

Method 2:

import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        String[] str = s.trim().split(" ");
        StringBuilder sb = new StringBuilder();
        for(int i = str.length - 1;i >= 0;i--){
            sb.append(str[i]);
            sb.append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}

Topics: Java leetcode