This article is a rant about the questions that are posted on the Linked In Java Developers Community. I see many times questions that are not well formulated, and the answers are not well thought through.

There is some good in it. Being brave to go ahead and publish may be a good thing, but there is also some bad in it. I face candidates during Java technical interviews many times giving "typical" wrong answers to some questions. The source of that is the spread of these false and half, easy to misunderstand information. One forum for them is LinkedIn Java Developers Community. Presumably, there are other platforms.

What makes me really sad is that this community is moderated. Still, questions of questionable quality are published.

My aim with this article is to raise awareness in the vain hope that this will change.

1. Examples

In this section, I will display and discuss two examples. After that I will write a few words of conclusion.

1.1. Example 1

The very first question that I faced recently is the following:

Which one of the following loop cannot be used to change the value of any element in an array variable

  • The While Loop

  • The ForEach Loop

  • The For Loop

Note
A simple grammar checker could also help a bit, but that is not an issue worth mentioning.
linked in question example 1

I can understand the intention of the question. The author really wanted to draw attention to the fact that a simple for-each loop cannot be used to modify the array elements it is looping through in a naive way.

1.     void testForEachLoop0() {
2.         int[] array = {0, 0, 0, 0, 0, 0};
3.         for (int a : array) {
4.             a = 55; // does nothing
5.         }
6.         Assertions.assertArrayEquals(new int[]{0, 0, 0, 0, 0, 0}, array);
7.     }

As clearly demonstrated by the above example, when we loop over an array using a for-each loop, the loop variable is NOT the array element. It is the value of the array element. In the case of a primitive value, this is a copy of the value. In the case of an object array, this is a copy of the reference to the object. Which, indeed, in some sense is the copy of the value but not the copy of the object, rather the reference.

What we can do is to modify the object that the reference points to. I can even say that if the value is an object (reference), then we can modify the object, hence the value. For example,

 1.     void testForEachLoop1() {
 2.         class X {
 3.             int value;
 4.
 5.             X(int value) {
 6.                 this.value = value;
 7.             }
 8.
 9.             @Override
10.             public boolean equals(Object o) {
11.                 return ((X) o).value == value;
12.             }
13.         }
14.         final var array = new X[]{new X(0), new X(0), new X(0)};
15.         for (final var a : array) {
16.             a.value = 55;
17.         }
18.         Assertions.assertArrayEquals(new X[]{new X(55), new X(55), new X(55)}, array);
19.     }

will modify the field value in the object array.

Note
Just for the shake of brevity I created an overly simplistic equals method. Don’t do that in production code. Don’t do that even in test code. Creating a equals() method without a matching hashCode() is a deadly sin.

You can say that blaming the author on the language and the precision is nitpicking. Do not forget, however, that these questions are targeting Java juniors, who can be confused by the imprecise language very easily.

And as a bonus, here is another example that modifies the original array. If you look at it, you may realize that this is the same as the previous example, just overcomplicated.

1.     void testForEachLoop() {
2.         int[] array = {0, 0, 0, 0, 0, 0};
3.         for (int[] j : IntStream.range(0, array.length).mapToObj(i -> array).toArray(int[][]::new)) {
4.             j[j[0]] = j[0];
5.             j[0]++;
6.         }
7.         Assertions.assertArrayEquals(new int[]{6, 1, 2, 3, 4, 5}, array);
8.     }

1.2. Example 2

The next question is the following:

What is the access modifier for an interface in Java?

  • public

  • private

  • protected

  • No modifier

At least in this question, there is no grammar error.

linked in question example 2

Even if I do not know too much about Java, the question is fishy. A modifier is something that modifies something. Java is a well-designed language. There is some clutter in it. It could cut some things shorter, like writing public static void in front of main and decorating it with a whole class surrounding it. However, if something is not needed, it is not there. There is nothing like mandatory modifiers that you HAVE TO write. There is no "general and universal" access modifier for an interface.

Some answers pointed out that an interface can be public, private, protected or package-private. Not any of the interfaces though, because only nested interfaces can be private or protected, and only inside a class.

What the author wanted to ask, I assume, was the default visibility of the members in an interface. That is public. From the votes, as depicted below, it seems that three quarters of the people answered the question according to the intention of the author.

linked in question example 3

However, a significant number of people said that there is no need for a modifier, which is also true. By default, the members are public, and for a long time in the history of Java that was the only option.

2. Conclusion

Asking questions is a good thing. Usually, people ask questions because they want to learn. StackOverflow is an excellent platform for that.

These questions, however, more like exams. Professors ask such questions. It is a profession.

To ask an "exam" question, you have to be an expert in the field. But that is not enough. You also have to be precise and know what and how to ask. You have to understand what the readers will learn from your question.

I feel that many of these authors evaluate this latter assuming that all the readers will read the questions and also the answers.

First of all: there is no answer. People vote and hardly ever go back when the voting finishes. The answer is simply the result, but no explanation for the details. The explanation is in the comments. Many times there are some good comments but also many misleading.

It reminds me of a Peter Bruegel painting from 1568, "The Blind Leading the Blind".

2560px %D0%9F%D1%80%D0%B8%D1%82%D1%87%D0%B0 %D0%BE %D1%81%D0%BB%D0%B5%D0%BF%D1%8B%D1%85

If you want to ask a question: do it. However, do it right. Before publishing it for thousands of people, ask a few friends to read it. If more than one misunderstands it, then you have to rephrase it. If some of them give a wrong answer and then seeing the right one, they start to argue about the question and the correctness: the question is wrong. You may believe that they are wrong and not the question, but you cannot change your audience. You can change your question. Your question can hypothetically be correct, but wrong for the given audience.


Comments

Please leave your comments using Disqus, or just press one of the happy faces. If for any reason you do not want to leave a comment here, you can still create a Github ticket.