Java daily practice (4)

Java daily practice (4)

radio part


1. The following are associated with the queue structure ()

A Recursive call of function

B Reference of array elements

C Execution of multiple loops

D First-come-first-served job scheduling


The characteristics of the queue: First in first out, so the answer is very obvious D

2. The modifier of the interface implemented by the class cannot be ()

A void

B public

C abstract

D final


Answer: Here our class is modified by final and cannot be inherited, and our interface is originally used to be implemented, and then rewrite the methods inside. At this time, if it is modified, it cannot be rewritten, so it cannot be used

here final decorates our interface


3. Which of the following statements is correct ( ).

A Algorithm is a program

B When designing an algorithm, you only need to consider the design of the data structure

C When designing an algorithm, you only need to consider the reliability of the result

D The above three statements are not correct


Answer: D Obviously ABC is wrong.


4. The following description of the abstract keyword is wrong ()


A abstract keyword can modify a class or method

B A method in a final class cannot be abstract, because a final class cannot have subclasses

C An abstract class cannot be instantiated

D A subclass of an abstract class must implement all abstract methods of its superclass


Answer: A: correct

insert image description here


B: A class modified by final cannot be inherited, and a method modified by abstract must be rewritten, so there is a contradiction here. Because it cannot be inherited, there is no subclass, and no subclass, it cannot Rewrite so B is correct

C: Correct abstract classes cannot be instantiated, and interfaces are also

D: wrong, when the subclass B of abstract class A is also an abstract method, the abstract method can not be rewritten, but if in If there is an ordinary class that inherits B, you need to rewrite the abstract method of A, and the abstract method of B,

insert image description here


5. The three basic structures of structured programs are ( )

A recursion, iteration and backtracking

B process, function and subroutine

C sequence, selection and loop

D call, return and selection


Answer: C

structured programming (structured programming) is the basic principle of detailed design based on module function and process design (process-oriented). Structured programming is a subset of procedural programming that uses a logical structure for written programs to make understanding and modification more efficient and easier.


6. For the class declared by abstract, the following statement is correct:

A can be instantiated

B cannot be inherited

C subclass is abstract

D can only be inherited

E can be inherited by abstract class


Answer:

A: The class declared by abstract is an abstract class and cannot be instantiated

B: Abstract classes need to be inherited in most cases, but not necessarily.

C: Subclasses can be abstracted , at this time, the subclass does not need to override the abstract method

D of the parent class: at this time, our abstract class B is combined

insert image description here

E : correct

The exact value of 7.1GB is ( )

A 1024×1024 Bytes

B 1024 KB

C 1024 MB

D 1000×1000 KB


bit -> byte -> kb -> mb -> gb -> tb -> pb

1byte == 8bit

1kb = 1024 byte

1mb = 1024 kb

1gb = 1024 mb

so the answer is C

Indefinite multiple choice questions


1. Among the following options, which ones are legal method definitions in the interface? ()


A public void main(String [] args);

B private int getSum();

C boolean setFlag(Boolean [] test);

D public float get(int x);


Figure 1:

insert image description here


Figure II:

insert image description here


So the answer is: ACD ,

B: All methods in the interface are modified by public by default, and here private will cause compilation errors, and methods modified by private cannot be rewritten.

Our interface implementation method (abstract method by default) is originally used to rewrite.

Abstract methods are public abstract by default.


2. Which of the following classes implement or inherit the Collection interface?

A HashMap

B ArrayList

C Vector

D Iterator


We can know who has implemented our Collection interface by looking at the picture below. So the answer is BC

insert image description here

multiple choice


1. For jdk1.8, the following are reserved for java syntax and cannot be used as class names and method names


A default

B int

C implements

D throws


The above all belong to our java keywords, so they cannot be defined as our class names and method names.

So the answer: ABCD

programming questions

Topic 1: Counting Candy

insert image description here

This question is very simple, we only need her to calculate A - B, and B - C and A + B, and B + C,

A = (A - B + A + B ) / 2 , B = (B - C + B + C) / 2

Let's look directly at the code:

import java.util.Scanner;



public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        // a = A - B
        int a = sc.nextInt();
        // b = B - C
        int b = sc.nextInt();
        // c = A + B
        int c = sc.nextInt();
        // d = B + C
        int d = sc.nextInt();

        int A = (a + c) / 2;
        int B = (b + d) / 2;

        int C = d - B;

//        验证一下 我们的A , B 是否能还原 
        if (2 * A + 2 * B != a + b + c + d ) {
    
    
            System.out.println("No");
        } else {
    
    
            // 防止出现算出来有负数的情况 
            if(A < 0 || B < 0 || C < 0){
    
    
                System.out.println("No");
            }else {
    
    
                System.out.print(A + " " + B + " " + C + " ");
            }

        }

    }
}

Topic 2: [ Base Conversion_Niuke Topic_Niuke.com (nowcoder.com) ](https://www.cctalk.com/v/16466262153331?sid=1646298415770519&xh_fshareuid=124017910)


Figure 1:

insert image description here


code:

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    
    
    Scanner sc = new Scanner(System.in);
    //  中共 16 进制 使用一个字符串保留 进制数
    String str = "0123456789ABCDEF";

    // 构造数据
    int m = sc.nextInt();
    int n = sc.nextInt();
    if (m == 0) {
    
    
        System.out.println("0");
        return;
    }
    // 此时判断 m 是否为整数
    boolean flag = false;
    if (m < 0) {
    
    
        flag = true;
        // 将m 改成正数后面好操作
        m = -m;
    }
    // 使用 StringBuilder 好拼接我们的字符串
    StringBuilder ret = new StringBuilder();
    while (m != 0) {
    
    
        ret.append(str.charAt(m % n));
        m /= n;
    }
    if (flag) {
    
    
        ret.append("-");
    }
    // 逆置我们的字符串
    ret.reverse();
    System.out.println(ret.toString());
}

Guess you like

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