Given two functions: $$ \begin{align} f(x) &= 1 - |x-1|\\ g(x) &= \begin{cases} x,\ \text {if $x\in \Bbb Q$}\\ 2-x,\ \text {if $x\in \Bbb R\setminus\Bbb Q$} \end{cases} \end{align} $$ Study continuity of the following compositions: $$ \begin{align*} (f\circ g)(x)\tag 1\\ (g\circ f)(x)\tag 2 \end{align*} $$
$\Box$ I've started with case $(1)$. Both $f(x)$ and $g(x)$ are well defined in $\Bbb R$. So we might consider their composition: $$ (f\circ g)(x) = \begin{cases} 1-|x-1|,\ \text{if $x\in\Bbb Q$}\\ 1-|2-x-1|,\ \text{if $x\in\Bbb R\setminus\Bbb Q$} \end{cases} $$ Or simply: $$ (f\circ g)(x) = 1-|x-1|,\ \forall x\in \Bbb R $$
By composition of continuous functions $1-|x-1|$ is continuous.
Conclusion: $(f\circ g)(x)$ is continuous in $\Bbb R$ $\blacksquare$.
$\Box$ Consider case $(2)$. Again both function are well defined in $\Bbb R$, so we migh consider their composition: $$ (g\circ f)(x) = \begin{cases} 1-|x-1|,\ \text{if $x\in\Bbb Q$}\\ 2 - (1-|x-1|),\ \text{if $x\in\Bbb R\setminus\Bbb Q$} \end{cases} $$
Or simplifying: $$ (g\circ f)(x) = \begin{cases} 1 - |x-1|,\ \text{if $x\in\Bbb Q$}\\ 1 + |x-1|),\ \text{if $x\in\Bbb R\setminus\Bbb Q$} \end{cases} $$
1) It feels natural to consider a point $x_0 = 1$ since it will render the absolute value to $0$, by which: $$ (g\circ f)(x_0) = 1 $$
Take any sequence $(x_n)_{n\in\Bbb N}$ of rational or irrational numbers such that: $$ \lim_{n\to\infty}x_n = 1 $$
For a sequence of rationals: $$ \lim_{n\to\infty}(g\circ f)(x_n) = \lim_{n\to\infty}(1-|x_n - 1|) = 1 = (g\circ f)(1) $$
The same is true for any sequence of irrational numbers.
2) Now consider some point $x_0 \ne 1$. Suppose $x_0$ is irrational. Take any sequence $(x_n)_{n\in\Bbb N}$ such that $\forall n\in\Bbb N: x_n \in \Bbb Q$ and: $$ \lim_{n\to\infty}x_n = x_0 $$
For any sequence $(x_n)_{n\in\Bbb N}$ and $\forall n\in\Bbb N: x_n \in\Bbb Q$: $$ \begin{align} \lim_{n\to\infty}(g\circ f)(x_n) &= \lim_{n\to\infty}(1 - |x_n - 1|) \\ &= 1 - |x_0 - 1| < 1 \end{align} $$
But since $x_0$ is irrational: $$ (g\circ f)(x_0) = 1 + |x_0 - 1| > 1 $$
Which means: $$ \lim_{n\to\infty} (g\circ f)(x_n) \ne (g\circ f)(x_0) $$
3) Suppose $x_0$ is rational and $x_0 \ne 1$, take any sequence $(x_n)_{n\in\Bbb N}$ and $\forall n\in \Bbb N: x_n \in\Bbb R\setminus \Bbb Q$: $$ \lim_{n\to\infty}x_n = x_0 $$ Since $x_0$ is rational we have: $$ (g\circ f)(x_0) = 1 - |x_0 - 1| < 1 $$ But: $$ \begin{align} \lim_{n\to\infty}(g\circ f)(x_n) &= \lim_{n\to\infty}(1 + |x_n - 1|) \\ &= 1 + |x_0 - 1| > 1 \end{align} $$ So: $$ \lim_{n\to\infty}(g\circ f)(x_n) \ne (g\circ f)(x_0) $$ And that completes the proof.
Conclusion: $(g\circ f)(x)$ in continuous at $x = 1$ and $(g\circ f)(x)$ is discontinuous $\forall x \in \Bbb R\setminus \{1\}$ $\blacksquare$.
I'm still a bit lost while studying such bizarre functions and not fully sure in the correctness of my reasoning behind the proof. I would like to ask for verification of the above.
Thank you!
A quicker simpler way to note that $h(x) = g(x)$ (where $g$ is continuous everywher) if $x$ is rational but $h(x) = f(x) $(where $f$ is continuous everywhere) if $x$ is irrational, is continuous at $x = a$ if and only if $f(a) = g(a)$ is to consider the following.
As $f,g$ are continuous for any $\epsilon > 0$ there are $\delta_1$ and $\delta_2$ so that $|x-a|< \delta_1$ then $|f(x) -f(a)| < \epsilon$ and if $|x-a|< \delta_2$ then $|g(x) -g(a)|< \epsilon$.
If $f(a) = g(a)$ then then if $|x - a| < \min(\delta_1, \delta_2)$ then if $x$ is rational then $|h(x) - h(a)| = |f(x) -f(a)| < \epsilon$. If $x$ is irrational then $|h(x) -h(a)| =|g(x) - g(a)| < \epsilon$ and either way $|h(x) - h(a)| < \epsilon$. So $h$ is continuous at $a$.
But if $f(a)\ne g(a)$ then $|f(a)-g(a)| = c > 0$.
Lets assume $h$ is continuous at $a$. That would mean for the same $\epsilon, \delta_1, \delta_2$ above we would have a $\delta_3$ so that $|x-a|<\delta_3 \implies |h(x) - h(a)| < \epsilon$
Now for any interval, there exist a rational $x$ and an irrational $y$ so that $|x-a|<\min(\delta_1, \delta_2,\delta_3)$ and $|y-a| < \min(\delta_1, \delta_2,\delta_3)$.
But $c = |f(a) -g(a)| =$
$|[f(a)-f(x)] + [f(x) - g(y)] +[g(y)-g(a)]| =$
$|[f(a)-f(x)] + [h(x) - h(y)] +[g(y)-g(a)]| \le $
$|f(a)-f(x)| + |h(x)-h(y)| + |g(y) - g(a)| <$
$\epsilon + |[h(x) -h(a)]+[h(a)-h(y)] + \epsilon \le$
$2\epsilon + |h(x)-h(a)| + |h(y)-h(a)|< $
$2\epsilon + \epsilon+\epsilon = 4\epsilon$.
So $\epsilon > \frac c4 > 0$. Which contradicts that $\epsilon$ can be arbitrarily small.
So $h$ is not continuous at $a$ if $f(a)\ne g(a)$.
......
You correctly figured that $f\circ g(x) = 1-|x-1|$ which is continuous by, as you say, composition of continuous functions.
And you correctly figured that $(g\circ f)(x) = \begin{cases} 1 - |x-1|,\ \text{if $x\in\Bbb Q$}\\ 1 + |x-1|),\ \text{if $x\in\Bbb R\setminus\Bbb Q$} \end{cases}$
So this is continuous at points $a$ where $1-|x-1| = 1 + |x-1|$ and discontinuous everywhere else.
Now $1-|x-1| = 1 + |x-1|\iff -|x-1|=|x-1|\iff x-1 = 0 \iff x = 1$.
So $g\circ f$ is continuous at $1$ and discontinuous everywhere else.