Give examples of sets and functions with the following properties:
an open set $D_1 \subseteq \mathbb{R}$ and a continous function $f:D_1\rightarrow \mathbb{R}$ such that $f$ has both a maximum and a minimum on $D_1$.
My attempt
$D_1=(-\pi,\pi)$
$f(x)=\sin(x)$
Therefore, $f$ has a maximum at $x=\frac{\pi}{2}$ and a minimum at $x=-\frac{\pi}{2}$
This is correct, isn't it? And are there simpler examples?
a compact set $D_2\subseteq \mathbb{R}$ and two functions $g+h:\mathbb{D_2}\rightarrow \mathbb{R}$ such that neither $g$ or $h$ have a maximum or minimum on $D_2$ but the sum $g+h$ does;
My attempt
I have no idea how to begin. I know exactly what is required and the meaning behind the definitions but I just am not able to come up with the functions $g$ and $h$ with the required properties.
Please help.
a set $D_3 \subseteq \mathbb{R}$ and a continous function $f:\mathbb{D_2}\rightarrow \mathbb{R}$ such that $f$ attains a maximum but not a minimum on $D_2$.
My attempt
$D_3=[-1,1]$
$f(x)=\left\{ \begin{align} &x, & \text{ if } x\ne -1 \\ & 0, & \text{ if } x=-1 \end{align}\right.$
Therefore, $f$ has a maximum at $x=1$ no minimum.
The above is correct, isn't it?
a set $D_4 \subseteq \mathbb{R}$ which is not compact and a function $f:D_4\rightarrow \mathbb{R}$ which is not continuous, but which attains both a maximum and a minimum on $D_4$
My attempt
$D_4=\mathbb{R}$, since $\mathbb{R}$ is not bounded, hence not compact.
$f(x)=\left\{ \begin{align} &1, & \text{ if } x \text{ is rational} \\ & 0, & \text{ otherwise } \end{align}\right.$
Therefore, the maximum of $f$ is 1 and its minimum is $0$ and $f$ is clearly not continous.
This is correct isn't it?
Lastly, in general, what is the best way to approach this kind of questions and are there useful facts, properties, theorems to keep it mind that can aid in answering such questions reasonably quickly? It took me almost an hour to attempt this and this is not acceptable under examination conditions.
Your answer is correct. I think it's simple enough.
Consider the function $$ g\colon [-1,1] \to \mathbb R, \quad x \mapsto \begin{cases} \frac 1 x & \text{for $x \neq 0$} \\ 0 & \text{for $x = 0$}\end{cases} $$ and $h = -g$. Then $g(x) \to -\infty$ as $x \uparrow 0$ and $g(x) \to \infty$ as $x \downarrow 0$, so there are no extreme values of $g$. The same holds for $h$ with reversed signs. Of course $g + h = 0$.
Your answer is incorrect because your function is discontinuous at $x = -1$. An example would be $$f\colon (-\infty, 0] \to \mathbb R, \quad x \mapsto e^x.$$ The maximum is $e^0 = 1$, the infimum of $f$ is $0$ but not attained as $e^x > 0$ for all $x \in \mathbb R$.
Your answer is correct. A simpler example would be $$f\colon \mathbb R\to \mathbb R, \quad x \mapsto \begin{cases} -1 & \text{for $x < 0$} \\ +1 & \text{for $x \geq 0$} \end{cases}.$$
There is no universal way to come up with examples / counterexamples, but you should try to think of all general results you know which may be helpful to rule out what can or cannot be an example. Often examples / counterexample questions in exams are really about checking the necessity of a theorem's preliminaries.
You probably have learned about the following theorem:
Theorem. Let $D \subset \mathbb R$ be a compact set and $f\colon D \to \mathbb R$, then $f$ has and attains its maximum and minimum.
With this theorem you can immediately tell that the set $D$ in question 3 cannot be compact and that the functions in question 2 cannot be continuous.