Java daily practice(7)

Java daily practice (7)

radio


1. JAVA belongs to ( ).

A operating system

B Office software

C database system

D computer language


Answer: Java is a language that can be recognized by computing, so the answer is D


2. In the class declaration, the keyword for declaring an abstract class is ( )


A public

B abstract

C final

D class


Answer: This question is also very simple. The keyword for declaring an abstract class is abstract, so the answer B,

A: public modified access qualifier means public

C: final means constant, the meaning of constant, and modified class means that the real class cannot be inherited , the modified method means that the real method cannot be rewritten, and modified a reference, the instance pointed to by the reference cannot be changed, but the attributes of the instance can be changed. D

: class The keyword for defining a class.


3. When using interface to declare an interface, only the () modifier can be used to modify the interface


A private

B protected

C private protected

D public


Answer: Only public can be used to modify the interface here, so the answer is D


4. Math.round(11.5) is equal to ()


A 11

B 11.5

C 12

D 12.5


Answer: round is a mathematical function that means rounding, the result here will be output as an int or long, here 5 is entered, so the result is 12, choose C


5. The following description of inheritance is wrong ()


A Inheritance in Java allows a subclass to inherit multiple parent classes

B The parent class is more general, and the subclass is more specific

C Inheritance in Java is transitive

D When the subclass is instantiated, the structure in the parent class will be called recursively method


Answer:

A: Error, a subclass is not allowed to inherit multiple parent classes in java

Demonstration: C tries to inherit A and B classes

insert image description here


B: Correct, the parent class represents the common characteristics of this type (extraction of the commonality of multiple classes), and the subclass can add its own unique attributes to its own class.


C : Correct,

B inherits A, and C inherits B. At this time, C can also have the attributes of A. Here is the transitivity in java inheritance.

insert image description here


D: Correct, when we learned inheritance, we focused on saying that when we create a new subclass object, we will first call the subclass constructor, and when calling the subclass constructor, we will first display the help parent class Construct (through super(), note: we did not write it does not mean there is no, the compiler will provide a construction method without parameters by default), the parent class construction is completed, it will be the turn of the subclass itself. 6. In java

, a class()


A can inherit multiple classes

B can implement multiple interfaces

C can only have one subclass in a program

D can only implement one interface


Answer:

A: As mentioned above, so it is wrong,

B: Correct, our class can implement multiple interfaces, through the keyword implements, the interfaces are separated by commas. C:

Wrong, a class can derive multiple sub-interfaces kind.

Both B and C here are subclasses of A


D : Error, our class can implement more than one interface.


7. The description of the following program code is correct

insert image description here


A Line 5 cannot be compiled because a private static variable is referenced

Line 10 cannot be compiled because x is a private static variable

C The program is compiled and the output result is: x=103

D The program is compiled and the output result is: x=102


Answer: Here, x is statically modified and placed in the method area. There is only one copy. No matter how many instances we call new, we call this x is the same copy, so new hs1.x++, x becomes 101, and hs2.x++ becomes 102, and later changed to 103 by hs1.x++, and finally we call through the class name, let x--, so finally x = 102,

note: We generally recommend using the class directly for static member variables or methods modified by static Name to call, you can see that the direct warning on the compiler to call through the instance can also prove it.


Run the result:

insert image description here


8. The following _____ are not methods of the Object class


A clone()

B finalize()

C toString()

D hasNext()


Answer: If you are not familiar with the source code of Object, it is difficult to choose this question. If you observe the method of Object below, you will find that only D is missing.

insert image description here

Here hashNext() is a method belonging Iteratorto .

insert image description here


9. Which of the following is not a java class access control keyword


A public

B this

C private

D protected


Answer: This question is very simple B, our access right modifiers are public, protected, default 包访问权限限定符(一般什么都不加) ,这里是为了与其他对标,才写了个 default , private


This current, main usage, 1. Call the method or variable in the current class, 2. Call the constructor of the current class.


10. The character code set used by java language is


A ASCII

B BCD

C DCB

D Unicode


A: ASCII mainly encodes special characters 0-9, uppercase and lowercase letters

B : BCD encodes numbers

C : DCB no discussion

D: Unicode, Unicode is used in java.


Simply understand, remember that java uses Unicode for encoding.

Unicode, also known as Unicode, Universal Code, and Unicode, is a coding scheme developed by international organizations to accommodate all characters in the world, including character sets, coding schemes etc., it sets a unified and unique binary code for each character in each language to meet cross-language and cross-platform requirements.

programming questions


Topic 1: Fibonacci Sequence

insert image description here


Attach the code:

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();

        int a = 0;
        int b = 1;
        int c = a + b;
        while(c < n){
    
    
            // 求出第一个大于 n 的斐波那契数
            a = b;
            b = c;
            c = a + b;
        }
        // 此时 b 为 第一次大于 n 的上一个斐波那契数

        int result = n - b < c - n ? n - b : c - n;

        System.out.println(result);
    }
}


Topic 2:

insert image description here


code show as below:

import java.util.*;

public class Parenthesis {
    
    
    public boolean chkParenthesis(String A, int n) {
    
    
        // write code here

        // 创建我们的栈用于保存 ( 括号
        Stack<Character> stack = new Stack<>();

        for (int i = 0; i < A.length(); i++) {
    
    
            char c = A.charAt(i);

            if (c == '(') {
    
    
                //  ( 直接入栈
                stack.push(c);
            } else if (c == ')') {
    
    
                // 此时 为 ) ,判断 栈是否为空 ,
                // 为空 就是 ))) 这种情况 返回 false
                if (stack.isEmpty()) {
    
    
                    return false;
                }
                // 此时不为空 , 出栈
                stack.pop();
            } else {
    
    
                // 字母部分
                return false;
            }
        }
        
        // 走到这里 判断一下栈是否为空 ,防止出现 (((( ))) 这种情况
        return stack.isEmpty() ? true : false;

    }
}

Guess you like

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