This a check question in my Numerical Analysis course.
s=0.0;
N=1000000;
for n=1:N
x=rand(1);
y=rand(1);
s=s+x^2+y^2;
end
display(s/N)
Question: The MATLAB code above will generate an output that is usually closest to one value, which one? The answers to choose from are $0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8$ or $0.9$.
The correct answer is $0.7$.
I think that the code generates $s=x_1 ^2+y_1 ^2+ x_2^2+y_2^2+\ldots+x_N^2+y_N^2$ and then computes $\frac{s}N$ but I don't understand how to draw the conclusion that it will be $0.7$.
(This is a question from an old exam where only paper and pen is allowed.)
$X_i \sim \operatorname{Uni}(0,1)$, hence
$$\operatorname{Var}[X_i^2]=\frac1{12}$$
$$\mathbb{E}[X_i^2]=\operatorname{Var}[X_i^2]+\mathbb{E}(X_i)^2=\frac1{12}+\frac14=\frac13$$
I will leave $\mathbb{E}[X_i^2]+\mathbb{E}[Y_i^2]$ as an exercise.
Edit: As pointed out by Did, we could have just compute the second moment directly.
$$\mathbb{E}[X_i^2] =\int_0^1 x^2 \, dx= \frac13 $$