Home  >  Article  >  Java  >  Ele.me's written test questions seem simple, but it stumps a lot of people

Ele.me's written test questions seem simple, but it stumps a lot of people

Java后端技术全栈
Java后端技术全栈forward
2023-08-24 15:29:412042browse


Please read the question

Please write down what this code outputs?

/**
 * @author 面试专栏
 * @date 2021/7/11 13:57
 */
public class Test1 {
    public static void main(String[] args) {
        //输出什么?
        test();
    }

    public static void test() {
        for (int i = 4; i > 0; i--) {
            int j = 0;
            do {
                j++;
                if (j == 2) {
                    break;
                }
            } while (j <= i);
            System.out.print(j);
        }
    }
}

Seeing this question, many people will think it is too simple. In fact, this question examines three knowledge points;

  • forloop
  • do...whileLoop
  • breakExit

Don’t underestimate me, too I have asked many people, and the answers are all strange. Four answers appear most frequently:

  • 第一个:4321
  • 第二个:1232
  • 第三个:2211
  • 第四个:2222

你觉得是哪个?还是觉得都不是?

注意点

注意点1

do...while循环是while循环的变种,在检查条件是否为真之前,该循环其实已经执行过一次了,然后在检查条件是否为真,如果为true,就重复执行这个循环。注意:只要循环体中执行break后也会结束循环。

注意点2

for(表达式1;表达式2;表达式3){
//循环体
}

这个循环体执行步骤如下:

  • The first step: execute initialization: expression 1 (will only be executed once)
  • The second step: execute expression 2, if expression 2 If the result is false, the loop ends, otherwise the loop body is executed, and then expression 3 is executed.
  • Step 3: Loop step 2, when the result of expression 2 is false , exit the loop, or encounter the return, break keywords.

Ontology analysis

  • ##When entering the for loop body for the first time, i=4, and then included in the do...while loop body. At this time, j=0, and then this loop continues to execute j until j=2 or j>i exits the loop body. Obviously, j=2 at this time, also That is, the output at this time is 2.

  • The next time the for loop, i=3, the output is still 2,

  • The next for loop, i=2, the same output is still 2,

  • The next for loop, i=1, the same output The result is 2. At this time, the i-- operation of the foe loop is executed, i=0, and the condition of i>0 is not met. At this time, the for loop exits.

So, the final output result is:

2222.

Postscript

Don’t underestimate the written test questions of many companies. There are pitfalls. If you are not careful, you will be wrong. fell in. When you encounter this kind of written test question about cycles, I suggest you think calmly and take it step by step.


In the written test, the more seemingly simple the question is about coding, the less we should take it lightly.

The above is the detailed content of Ele.me's written test questions seem simple, but it stumps a lot of people. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:Java后端技术全栈. If there is any infringement, please contact admin@php.cn delete