If $f(x)\ge 0$ for every rational $x$, show that $f(x)\ge 0$ for all real $x$. Let $f:\mathbb{R}\to\mathbb{R}$ be continuous. If $f(x)\ge 0$ for every rational $x$, show that $f(x)\ge 0$ for all real $x$.
My attempt:
Let $f(r)<0$ for some $r\in\mathbb{R}$.
I will attempt to use the intermediate value theorem, which is true for continuous functions. Let $q\in\mathbb{Q}$ s.t. $q>r$. Consider the interval $[r, q]$ wherein $f(r)<0$ and $f(q)\ge 0$. Let $\displaystyle u=\frac{f(r)}{2}$. Then $f(r)<u<f(q)$. Then there is a $c\in (r, q)$ s.t. $f(c)=u$. We note that $u<0$. So, $c\notin\mathbb{Q}$.
Unfortunately, the above argument does not lead anywhere.
 A: The rational numbers are dense in the real numbers. If you take $x\in\mathbb{R}$ then you can find a sequence $x_n$ of rational numbers that converges to $x$. By continuity of $f$ you know that $f(x_n)\to f(x)$ when $n\to\infty$. But $x_n$ are all rational numbers so for each $n\in\mathbb{N}$ we have $f(x_n)\geq0$. Hence the limit of the sequence (which is $f(x)$) is also non negative. 
A: The main idea here is that if for some irrational $x_0$, $f(x_0) < 0$ then because $f$ is continuous, you can find a small neighbourhood around $x_0$ where $f(x) < 0$, and since $\mathbb{Q}$ is dense in $\mathbb{R}$, this leads to a contradiction.
A: Alternatively, if $f:A\to B$ is a continuous functions from a topological space $A$ to a topological space $B$, then
$$f\big(\bar{S}\big)\subseteq \overline{f(S)}$$
for every subset $S$ of $A$.  Here, $\bar{X}$ is the topological closure of a subset $X$ of a certain topological space.  (The converse also holds: if $f:A\to B$ is such that $f(\bar{S})\subseteq \overline{f(S)}$ for every $S\subseteq A$, then $f$ is continuous.  See this link.)  
Back to the problem, we have $f(\mathbb{Q})\subseteq [0,\infty)$.  Note that $\mathbb{Q}$ is dense in $\mathbb{R}$, i.e., $\bar{\mathbb{Q}}=\mathbb{R}$.  That is,
$$f(\mathbb{R})=f(\bar{\mathbb{Q}})\subseteq \overline{f(\mathbb{Q})}\subseteq \overline{[0,\infty)}=[0,\infty)\,.$$
For the last inclusion, we have used the fact that, if $X$ and $Y$ are subsets of a topological space and $X\subseteq Y$, then $\bar{X}\subseteq \bar{Y}$.
A: This is just a fleshing out of Ashish's answer.
Suppose that there is an $x\in\mathbb{R}$ so that $f(x)=-\epsilon\lt0$.
Since $f$ is continuous, there is a $\delta\gt0$ so that
$$
|y-x|\le\delta\implies|f(y)-f(x)|\le\frac\epsilon2
$$
However, there is a $y\in\mathbb{Q}$ so that $|y-x|\le\delta$, which means $f(y)\le-\frac\epsilon2$.
Contradiction.
