java daily practice(6)

java daily practice(6)

radio


1. Regarding abstract classes and final classes, which of the following statements is wrong?

A Abstract classes can be inherited, final classes can only be instantiated.

B Both abstract class and final class can be declared and used

C There can be no abstract method in the abstract class, and there can be no final method in the final class

D When the abstract class and the final class are inherited, the method can be overridden by subclasses

Supplement: The final class is also called a sealed class (a class modified by final)


A: Correct, the abstract class is originally used for inheritance, and the final class is a class modified by final, which cannot be inherited, so it can only be instantiated.


B : correct

insert image description here


Here A and B are declared to use


C: Looking at the diagram of B, the final class has no methods modified by final, and the abstract class has no abstract methods, so C is correct.


D : Error, the final class is modified by final and cannot be inherited.


2. The known expression int m[]={0, 1, 2, 3, 4, 5, 6}; which of the following expressions is equal to the maximum value of the array subscalar?


A m. length()

B m. length-1

C m. length()+1

D m. length+1


Answer: Obviously choose B, Note: The length of the array is not () it is not a method.


3. Which of the following is correct for the scope of access modifiers in descending order?


A private>default>protected>public

B public>default>protected>private

C private>protected>default>public

D public>protected>default>private


Answer: public > protected > default > private so the answer is D


Use the following picture to deepen the impression of access modifier qualifiers

insert image description here


4. There are six elements 6, 5, 4, 3, 2, 1 that are pushed onto the stack in sequence, which one of the following is not a legal stack sequence? ()


A 5 4 3 6 2 1

B 4 5 3 1 2 6

C 3 4 6 5 2 1

D 2 3 4 1 5 6


The characteristics of the stack: first in, last out

We can quickly get the answer according to the characteristics of the stack

insert image description here


According to the above method, when we try C, 5 will appear on top of 6. When we want to get 6, we need to get 5 first to get 6, so C is wrong

So the answer to this question is C


5. Read the following programs and choose which one is the correct output

insert image description here


A static A I’m A class static B I’m B class

B I’m A class I’m B class static A static B

C static A static B I’m A class I’m B class

D I’m A class static A I’m B class static B


Answer: This question mainly examines code blocks, review articles: (56 messages) Inheritance_


Summary about code blocks:

1. The static code block of the parent class is executed prior to the static code block of the child class, and it is the earliest execution

2. The parent class instance code block and the parent class construction method are executed immediately

3. The instance code block of the subclass and the construction method of the subclass are executed immediately

4. When the subclass object is instantiated for the second time, the static code blocks of the parent class and the subclass will not be executed again


Analysis: First print the static code block of the parent class, and then print the static code block of the subclass. Because there is only a construction method in the title, the execution method is echoed. When the subclass is constructed, it will first help the parent class to construct, so the parent class is executed first The construction method of the subclass is executing the construction method of the subclass, so the answer: C


implement:

insert image description here


6. The output result of executing the following code is ( )


insert image description here


A 10

B 20

C 30

D 40


Answer: This question mainly examines whether finally in the exception is caught or not, it will be executed. We pass in a 10 to the test method, and then b+=10 makes b equal to 20, and returns 20, but because the code in finally does not matter whether it is If there is an exception, it will be executed, then b += 10 becomes 30 again, and finally returns 30 through return b, replacing the original one.


7. What is the output of the following code?

insert image description here

A true

B false

C null

D empty string


Parse:

insert image description here


So the answer is obviously B

Indefinite multiple choice questions

1. Which of the following Class declarations in Java are wrong?

A public abstract final class Test {
    
     abstract void method(); }

B public abstract class Test {
    
     abstract final void method(); }

C public abstract class Test {
    
     abstract void method() {
    
     } }

D public class Test {
    
     final void method() {
    
     } }


A : abstract defines an abstract class or abstract method. Since it is an abstract class, it is used to be inherited, and a class modified by the keyword final cannot be inherited, and a method modified by final cannot be rewritten. So abstract and final cannot appear together. So A is wrong

B: Same as above

C: The abstract method is originally rewritten, so no specific implementation is required, so it cannot be included here {}So C is wrong

insert image description here


D: correct

2. In the following description about the construction method, the wrong one is ()


A The java language stipulates that the constructor name must be the same as the class name

B The java language stipulates that the constructor has no return value, but different void declarations

C The java language stipulates that the constructor cannot be overloaded

D The java language stipulates that the constructor can only be called automatically through new


A : Correct

B : Correct

C : Incorrect, a class can have multiple construction methods, and the construction method can have no parameters, one parameter, etc. At this time, the rules of overloading are satisfied, so it can constitute overloading Overloading

: The method name is the same, the return type is not required, and the parameter type and number of parameters are different.


D : Error, our this or super can call the constructor, this calls the constructor of the current class, and super calls the constructor of the parent class


3. Which of the following assignment statements are correct? ()


A long test=012

B float f=-412

C int other =(int)true

D double d=0x12345678

E byte b=128


A: test is assigned an octal 012, 0 * 2 ^ 0 + 1 * 2 ^ 1 + 2 * 2 ^ 2, so test is actually assigned a value of 10.

B: f is assigned a value of -412, which is also possible.

C: Wrong, boolean type, cannot be forced into a numerical type.

D: At this time, d is equivalent to being assigned a hexadecimal value, which can be written like this

E: The value range of byte is -128 to 127, and the value assigned at this time exceeds the maximum range of 127.


So the answer ABD

programming questions

Topic 1: No Two


Figure 1:

insert image description here


Figure II:

insert image description here


After the analysis, the code is very easy to write. Here we create an array with a default value of 0, and then set it to 1 for places that cannot be placed. Define a count counter to count the places where we can put cakes, and then set the places where cakes cannot be placed to 1 is enough. Finally print our count.

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        // 注意 题目是 可能是有点一点问题, 先给了 行
        // 所以 那 H 接收 ,然偶 那 W 接收列 
        // 
        int H = in.nextInt();
        int W = in.nextInt();
        
        int[][] arr = new int[H][W];
        int count = 0;
        for(int x = 0;x<H;x++){
    
    
            // 先遍历行
            for(int y = 0 ; y <W; y++){
    
    
                // 在遍历列
                if(arr[x][y] == 0){
    
    
                    // 此时 可以存放蛋糕
                    count++;
                    // 此时 将 不能放的置为 1
                    if(x+2 < H){
    
    
                        arr[x+2][y] = 1;
                    }
                    if(y + 2 < W){
    
    
                        arr[x][y+2] = 1;
                    }
                }
            }
        }
        System.out.print(count);
    }
}

Topic 2: Convert a string to an integer_Niuke.com (nowcoder.com)

insert image description here


Because it is relatively simple to directly give the code:

import java.util.*;
public class Solution {
    
    
    public int StrToInt(String str) {
    
    
        int a = 0;
        boolean flag = false;
        // 遍历我们的字符串 
        for(int i = 0 ; i < str.length(); i++){
    
    
            char c = str.charAt(i);
            if(c == '+' && i == 0){
    
    
                continue;
            }
            if(c == '-' && i == 0){
    
    
                flag = true;
                continue;
            }
            // 此时 不是 + - ,普通的数字部分
            if(c <= '0' || c > '9'){
    
    
                // 此时 是字母所以直接返回 0,
                // 题目说 为0 同样需要返回 0
                return 0;
            }
            // 将字符串 转为 数字
            a  = a * 10 + (c - '0');           
        }
        return flag ? -a : a;
    }
}

Guess you like

Origin blog.csdn.net/mu_tong_/article/details/128121946