$f$ continuous iff $f(K)$ is compact for all compact $K$. In topology, I have seen that $f$ is continuous if and only if $f^{-1}(U)$ is open when $U$ is open. But does it hold that $f$ continuous iff $f(K)$ is compact when $K$ is compact ? I know that if $f$ is continuous, then $f(K)$ is compact when $K$ is compact. But does the converse hold ? I'm talking especially for a function $f:\mathbb R\longrightarrow \mathbb R$, but a general answer would be good too.
 A: No, the converse is not true. Consider the function $f: \mathbb{R}\rightarrow \mathbb{R}$ defined by $f(x)=1$ if $x \in \mathbb{Q}$ and $f(x)=0$ otherwise. Then $f$ is nowhere continuous, but the image of each compact subset $K$ of $\mathbb{R}$ is compact as finite sets are compact.
A: The answer is no in general. However, in some 'nice' cases the answer is positive. See this question on MO.
A: There is a MathOverflow question which has been closed as off-topic (because it's regarded as a question belonging to math.SE), but which has the following answer by Lajos Soukup:

There are highly non-trivial positive results. 
Let us call a function $f$ from a space $X$ into a space $Y$
  preserving if the image of every compact subspace of $X$ is compact in $Y$ and the image of every connected subspace of $X$ is
  connected in $Y$. 
McMillan  [On continuity conditions for functions, Pacific J. Math. 32
  (1970) 479-494] proved the following result:
Theorem: If $X$ is Hausdorff, locally connected and Frechét, $Y$ is Hausdorff (e.g. if $X=Y=\mathbb R$), then any preserving function
  $f:X\to Y$ is continuous.
For further  results see Gerlits, Juhasz, Soukup, Szentmiklossy:
  Characterizing continuity by preserving compactness and connectedness,
  Top. Appl, 138 (2004), 21-44
Our main result is the following:
Theorem:  If $X$ is any product of connected linearly ordered spaces (e.g., if $X = \mathbb R^\kappa$ ) and $f:X \to Y$ is a
  preserving function into a regular space $Y$, then $f$ is continuous.

A: No.
Consider $f: \mathbb{R}\to \mathbb{R}$ given by $f(x)=0$ if $x<0$ and $f(x)=1$ if $x\ge 0$.
Then, for every $X \subseteq \mathbb R$, we have that $f(X)$ is a finite set and so compact. But $f$ is not continuous.
