Let $E$ be nonmeasurable subset of [0,1] and define $f:[0,1]$ as: $$f(x)=\begin{cases}-\frac1{2},\ \ x\in E\\0,\ \ \text{otherwise}\end{cases}$$
Then, I feel that both $f,|f|$ are clearly nonmeasurable, because preimage of the singleton sets $\{0\}\ \ ,\{\frac1{2}\}, \ \ \{\frac1{2}\}$ are non measurable. Am I right? I have two books which say that both $f,|f|$ are measurable in the answers section. I am confused. Any hints. Thanks beforehand.
We will prove the following useful and more general result, which provides an answer to your question:
(In all the following, $\mathbb{R}$ is considered to be equiped with the usual Lebesgue measure).
Proof:
($\Rightarrow$) Let $E$ be a measurable subset of $X$. Let also $a\in\mathbb{R}$ and let $A=\chi_E^{-1}((-\infty,a])$. Consider the following cases:
So, in any case, $A$ is a measurable set, and, since $a$ was arbitrary, $\chi_E$ is measurable.
($\Leftarrow$) Let $\chi_E$ be measurable. Then, since $\{1\}$ is (Lebesgue) measurable, it is evident that $E=\chi_E^{-1}(\{1\})$ is measurable.
Notes:
Taking into consideration all the above, your claim is right.