java daily practice(5)

java daily practice(5)

radio


1. After compiling and running the following program, the result displayed on the screen is ()

insert image description here


A: 0

B : 2

C: 5

D :80


Binary of 5: 0101

right shift >>: the rightmost bit is unnecessary, and the leftmost complement sign bit (positive numbers complement 0, negative numbers supplement 1)


0101>>2, 5 is a positive number with 0 on the left.

The first time: 0010,

the second time: 0001.

At this time, x is 1

and then y is >>>also called unsigned right shift.

Unsigned right shift rule: the rightmost bit is not needed, and the leftmost bit is filled with 0.


At this time, 0001 >>> 2 is 0000 for the first time, and 0000 for the second time,

so the answer is obviously 0

2. What is the result of the following code?


insert image description here


Code A can be compiled and run, and output "AB.AB".

Code B can be compiled and run, and output "AA".

The C code can be compiled and run, and output "AB.B".

The D code can be compiled and run, and output "AB".


Illustration:

insert image description here


So the answer is obvious C


3. In JAVA, assuming that A has a construction method A(int a), then calling this construction method in other construction methods of class A and the statement format should be ()

A this.A(x)

B this(x)

C super(x)

D A(x)


The title gives A a construction method A(int a), indicating that it is a construction method with one parameter. Here we want to call the construction method with one parameter through this, just use this(x) directly. Here we

need Note that this() needs to be in the first line of the constructor.


4. The running result of the following code is ()

这里放在 idea上就非常明显了

public static void main(String[] args){
    
    
    String s;
    System.out.println("s="+s);
}


A The code is programmed successfully and outputs "s="

B The code is compiled successfully and outputs "s=null"

C Since String s is not initialized, the code cannot be compiled.

The D code compiles successfully, but a NullPointException is caught


Answer: The local variable s is not initialized, so there will be an error during compilation here. So choosing C

to put it on the idea is very obvious, and it immediately became popular.

insert image description here


5. In java7, which of the following parameter types cannot be used as switch()?


A int type

B enumeration type

C string

D floating point type

Answer: D, here we can directly memorize it, it cannot be used as the parameter type of switch()float , double , boolean , long


6. A source file with the suffix ".java"


A can only contain one class, the class name must be the same as the file name

B can only contain the same class as the file name and the internal class

C can only have one public class with the same file name, can contain other classes

D can contain any kind


A: Error, one of our .java files can contain many classes, and it does not necessarily need to be the same as the file name, only the public modified class is the same as the file name

insert image description here


B: A and B in the above figure are not internal classes, and they can also be included, so B is also wrong.

C: Correct

D: Wrong, our public modified class can only contain one, so it is wrong here.

7. What is the return value of the following code?


insert image description here


A true B false


Here we mainly examine finallywhether will be executed regardless of whether there is an exception. At the beginning, one is returned true, but the code in finally needs to be executed, so another one is executed return false, and one is finally returned at this timefalse


Answer: B


8. The following () is not a legal identifier?


A STRING

B x3x

C void

D deSf


Answer: Identifiers in java are numbers, letters, underscores, and dollar signs $, where void is a keyword, and you cannot use void to define a variable, method, etc., so choose C


9. Point out that the result of the following program is

insert image description here


A good and abc

B good and gbc

C test ok and abc

D test ok and gbc


Answer: Here is still a reference, which is similar to the above a and b.

insert image description here

Indefinite multiple choice questions

1. Which of the following statements about JAVA exception handling is correct ()


A finally is to ensure that a piece of code will be executed regardless of whether the exception is caught or not.

B throws is used to declare various non-running exceptions that may be thrown by a member method.

C final is used to declare attributes and methods, which represent attributes respectively. Immutable and non-inheritable

D throw is used to explicitly throw an exception

Answer:

A is correct. If you don’t know, you can read related articles about exceptions.

B is correct.

insert image description here


C: final Here is the non-overridable method, the class can inherit, so C is wrong


D: An exception is explicitly thrown here. Note: Just write here.

insert image description here

programming questions


Topic 1: Statistical Palindrome


Figure 1:

insert image description here

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        String str2 = sc.next();
        int n = str1.length();

        int count = 0;
        for (int i = 0; i <= n; i++) {
    
    
            StringBuilder ret = new StringBuilder();
            ret.append(str1.substring(0, i)).append(str2).append(str1.substring(i, n));
            if (sum(ret.toString().toCharArray(), 0, ret.length() - 1)) {
    
    
                count++;
            }
        }
        System.out.println(count);
    }

    public static boolean sum(char[] a, int start, int end) {
    
    
        // 单独写一个方法判断是否为回文
        while (start < end) {
    
    
            if (a[start] != a[end]) {
    
    
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}


Finally, please pay attention, because the interception of our substring(a,b) is left-closed and right-open, so we need to set i = n at the end to complete the interception, which is equivalent to adding the string b at the end.


If you are not familiar with the substring() interception method, you can use the insert() method to directly insert our string into the string and then judge whether it is a palindrome. Note: the insert method is in StringBuilder or StringBuffer,

inside the String class No.


So the code is as follows:

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        String str1 = sc.next();
        String str2 = sc.next();
        int n = str1.length();

        int count = 0;
        for (int i = 0; i <= n; i++) {
    
    
            StringBuilder str = new StringBuilder(str1);
            str.insert(i, str2);
            if (sum(str.toString().toCharArray(), 0, str.length() - 1)) {
    
    
                count++;
            }

        }
        System.out.println(count);
    }

    public static boolean sum(char[] a, int start, int end) {
    
    
        // 单独写一个方法判断是否为回文
        while (start < end) {
    
    
            if (a[start] != a[end]) {
    
    
                return false;
            }
            start++;
            end--;
        }
        return true;
    }
}

Topic 2: Consecutive Maximum Sum

Figure 1:

insert image description here

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[] arr = new int[n];
        for(int i = 0 ; i < n;i++){
    
    
            arr[i] = in.nextInt();
        }

        int count = arr[0];
        
        int max = arr[0];
        for(int i = 1 ;i < n ;i++){
    
    
            // 动态规划 : 状态方程 
            count = Math.max(arr[i], count+arr[i]);
            // 与之前的最大值比较,如果 大就重新赋值给 max 
            max = Math.max(count, max);
        }
        System.out.print(max);
    }
}

Guess you like

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