Solution:
The answer is: If n=2k then Bernhard can always guess the word chosen by Anja on the first attempt; in the case n=2k he needs two attempts to guess the word with certainty. We denote the word chosen by Anja at the beginning by u.
Case 1: n=2k. Let 1≤i≤n be arbitrary. Among all (kn) words on the board, exactly (kn−1) agree with u at the i-th position and (k−1n−1) differ from u at the i-th position. A simple computation shows that in the case n=2k we also have (kn−1)=(k−1n−1), that is, by looking at the i-th positions of all the words on the board, Bernhard can deduce the i-th position of u. Since this works for all i=1,…,n, Bernhard can uniquely determine the word u chosen by Anja at the beginning, and thus guesses it on the first attempt in this way.
Case 2: n=2k. We first show that Bernhard cannot uniquely deduce u, that is, he may need more than one attempt. Suppose Anja had chosen the (unique) binary word uˉ that differs from her actual word u in all positions. An arbitrary binary word of length n agrees with u in exactly k=n/2 positions if and only if it agrees with uˉ in exactly k=n/2 positions, that is, in this case Anja would have written exactly the same binary words on the board. This means that Bernhard has no way of distinguishing between u and uˉ, and on his first attempt to guess Anja's word he might be wrong. We now show that Bernhard can always succeed in two attempts. For this it suffices to prove that for every binary word w of length n different from u and uˉ, the set of all binary words of length n that differ from w in exactly k=n/2 positions is not identical to the set of words on the board. To this end we may assume without loss of generality that w differs from u in exactly a positions, where 0<a≤k=n/2 (otherwise consider uˉ instead of u). Then we can change exactly k of the remaining n−a≥k positions and obtain a word w′ that differs from w in exactly k positions, but from u in a+k>k positions, that is, it is not on the board.