“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

⭐ Welcome to the algorithm entry to the King’s Road column, learn together, progress together ⭐

preface

Hi, I’m the strongest man in the world. – White beard. Starting today I intend to set a flag. That’s 48 days in a row. At least two programming problems per day. Plus multiple choice questions. Still hope everybody supervises!! . I will set up an algorithmic communication group if there are too many people working together. Mainly depends on everybody’s idea.

@TOC

1. Multiple choice questions

1: What does the following code output: Public class SystemUtil{public static Boolean isAdmin(String userId){return userid.tolowerCase ()==”admin”; } public static void main(String[] args){ System.out.println(isAdmin(“Admin”)); }}

A true B false C 1 D Compiler error

The above code is meant to output the result of the isAdmin method. This method passes an Admin argument, note that the A is capitalized. The code executed by the method body calls the toLowerCase () method with the passed Userid. Convert the userID to lowercase. Finally returns the lowercase and the string admin compared with ==.

Here’s a review of some Java object comparisons. String uses == to compare whether two references refer to the same object. To really check whether the referenced values are equal, you need to use equals.

Take a look at the following code:

  public static void main (String[] args) {
        String userId=new String ("Admin");
      
        String str1=new String ("abc");
        String str2=new String ("abc");
        System.out.println (str1==str2);
        System.out.println (userId.toLowerCase ()=="admin");
    }
Copy the code

Can you guess what the final output was? Both are False. Because str1 and str2 have the same string content, but == is comparing their references. So the way we’re going to initialize this is we’re going to new a string. So it’s like creating a new object. The effect on the JVM stack is as follows. So their references are different, print false. And the second one makes the same sense because the userID is also a new String.

Take a look at this code again:

 String str1="abc";
        String str2="abc";
        System.out.println (str1==str2);
Copy the code

And guess what? This time it’s true. Str1 and str2 are not initialized as new, which is equivalent to a reference to an ABC object in the string constant pool. The effect is like the following, but with ABC instead of Hello. B


2: Read the following code. Test.hello (). () p ackage NowCoder; class Test { public static void hello() { System.out.println(“hello”); } } public class MyApplication { public static void main(String[] args) { // TODO Auto-generated method stub Test test=null; test.hello(); }} C: static method C: static method D: static variable C: static method D: static variable C: static variable D: static variable C: static variable D: static variable

Parsing: The use of static methods does not depend on the object, just the type, which is determined at compile time. So it doesn’t matter if you test is null or not.

A


3: What is the result of the following code? class Base { Base() { System.out.print(“Base”); } } public class Alpha extends Base { public static void main( String[] args ) { new Alpha(); // Call the constructor new Base() with no arguments in the parent class; }}

A BaseB BaseBase C compile failure D code run but no output E runtime exception thrown

Either new Base () or new Alpha () prints a Base at the end. The Alpha class inherits from the Base class. So print once when calling a constructor that has no arguments from its parent class. It also prints once when you call a subclass so there are two bases.

B


4: What is the output of the following code? public class Test { public int aMethod(){ static int i = 0; i++; return i; } public static void main(String args[]){ Test test = new Test(); test.aMethod(); int j = test.aMethod(); System.out.println(j); }}

A 0 B 1 C 2 D compile failure parsing: will compile failure, class method class cannot create static variables.

D


Which of the following statements is true? () A abstract modifier modifies fields, methods, and the body of an abstract method of class B. the body of an abstract method of class B must be wrapped in braces {}

Practice is the mother of wisdom. Let’s try them one by one. A: No problem:No problemModify field: No.In fact, it’s easy to see why. The abstract keyword is introduced because of the uncertainty of the superclass method. To prevent programmers from writing a specific keyword for a method or class that they don’t want to specify. Abstract classes also have the following property: Abstract classes cannot be instantiated. A class with abstract methods must be an abstract class, but an abstract class may not have abstract methods. When the parent of a class is an abstract class, we need to implement all the abstract methods in the abstract class. An abstract method cannot have a method body.

B: Abstract methods are designed to keep programmers from implementing them. So you get an error when you try to implement it inside curly braces.

C: In the same way, B is not optional, but non-negotiable. You can’t use curly braces

D: Correct. You can’t write curly braces without an implementation.

D


6. The following statements are true: () A class can define only one constructor. () A class can define only one constructor. () A class can define only one constructor

A: Can the constructor be omitted? The system creates A constructor for you by default when you write nothing

B: It is true that the constructor must have the same name as the class. Otherwise, an error will be reported.Method can have the same name as class, there is no error here.C: Constructor executes when an object is new, and the constructor starts when the object is new.

D: A class can define multiple constructors. Here I define two constructors, one for A and one for B.

7: Which line of code in the options can replace //add code here without generating a compilation error

public abstract class MyClass { public int constInt = 5; //add code here public void method() { } }

A public abstract void method(int a); B consInt=constInt+5; C public int method(); D public abstract void anotherMethod(){}

A: Implement abstract methods in abstract classes without curly braces. Correct, no programming errors. C: Cannot have the same name D: Cannot contain the body of an abstract method with curly braces

Second, programming questions

Title: the horse string | time limit: 1 SEC | memory limit: 32768 k | language restrictions: no inversion string 】 【 will be inverted sentence words, punctuation is not inverted. For example, I like Beijing. After the function changes to Beijing. Like I

I’ve written about this problem before, and here’s a link to my blog: Inverted Strings

Here is the code attached:

import java.util.Scanner;

public class reverseStr {}/** * inverted string * Inverts the words of a sentence, without inverted punctuation. For example, I like Beijing. After the function changes to: Beijing. Like I */
class Main{
    / * * * ideas first whole inverse, then according to the inverse each word. *@param ch
     */
    public  static  void rerverse(char ch[],int start,int end){
          while (start<end){
              chartmp=ch[start]; ch[start]=ch[end]; ch[end]=tmp; start++; end--; }}public static void main (String[] args) {
        Scanner scanner=new Scanner (System.in);
        String str=scanner.nextLine ();
        char ch[]=str.toCharArray ();
        rerverse (ch, 0, str.length ()-1);
        int i=0;
        int len= ch.length;
        while (i<len){
            int j=i;
            while(j<len && ch[j]! =' '){
                j++;
            }
            // A space is encountered, inverse
            // J
            // Only one word avcd is entered.
            // The array is out of bounds
            if(j<len){
                rerverse (ch, i, j-1);
                // Update I after inverting a word. Next time I enter the loop j equals I again. So again and again
                i=j+1;
            }
            else {
                // Only one word
                rerverse (ch, i, j-1);
                i=j;
            }
        }
        String ret=newString (ch); System.out.println (ret); }}Copy the code

Third, the end

Hope you can give a feedback, today is the second day, come on together! .