Table of Contents
注意点 " >注意点
注意点2
Home Java JavaInterview questions 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

Aug 24, 2023 pm 03:29 PM
java interview questions


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);
        }
    }
}
Copy after login

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){
//循环体
}
Copy after login

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

  • 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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Interviewer: Spring Aop common annotations and execution sequence Interviewer: Spring Aop common annotations and execution sequence Aug 15, 2023 pm 04:32 PM

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?

Interview with a certain group: If you encounter OOM online, how should you troubleshoot it? How to solve? What options? Interview with a certain group: If you encounter OOM online, how should you troubleshoot it? How to solve? What options? Aug 23, 2023 pm 02:34 PM

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.

Novices can also compete with BAT interviewers: CAS Novices can also compete with BAT interviewers: CAS Aug 24, 2023 pm 03:09 PM

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, I had an interview with XX Insurance and it was cool! ! ! Last week, I had an interview with XX Insurance and it was cool! ! ! Aug 25, 2023 pm 03:44 PM

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!

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 Aug 24, 2023 pm 03:29 PM

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.

5 String interview questions, less than 10% of people can answer them all correctly! (with answer) 5 String interview questions, less than 10% of people can answer them all correctly! (with answer) Aug 23, 2023 pm 02:49 PM

​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.

It is recommended to collect 100 Linux interview questions with answers It is recommended to collect 100 Linux interview questions with answers Aug 23, 2023 pm 02:37 PM

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.

Meituan, see if you can answer it? Meituan, see if you can answer it? Aug 24, 2023 pm 03:51 PM

Meituan, see if you can answer it?

See all articles