


Ele.me's written test questions seem simple, but it stumps a lot of people
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;
for
loopdo...while
Loopbreak
Exit
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.
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.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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You must know Spring, so let’s talk about the order of all notifications of Aop. How does Spring Boot or Spring Boot 2 affect the execution order of aop? Tell us about the pitfalls you encountered in AOP?

OOM means that there is a vulnerability in the program, which may be caused by the code or JVM parameter configuration. This article talks to readers about how to troubleshoot when a Java process triggers OOM.

The extra chapter of the Java concurrent programming series, C A S (Compare and swap), is still in an easy-to-understand style with pictures and texts, allowing readers to have a crazy conversation with the interviewer.

Last week, a friend in the group went for an interview with Ping An Insurance. The result was a bit regretful, which is quite a pity, but I hope you don’t get discouraged. As you said, basically all the questions encountered in the interview can be solved by memorizing the interview questions. It’s solved, so please work hard!

Don’t underestimate the written examination questions of many companies. There are pitfalls and you can fall into them accidentally. When you encounter this kind of written test question about cycles, I suggest you think calmly and take it step by step.

This article will take a look at 5 interview questions about the Java String class. I have personally experienced several of these five questions during the interview process. This article will help you understand why the answers to these questions are like this.

This article has a total of more than 30,000 words, covering Linux overview, disk, directory, file, security, syntax level, practical combat, file management commands, document editing commands, disk management commands, network communication commands, system management commands, backup compression commands, etc. Dismantling Linux knowledge points.
