This depends on context.
The distinction has largely been academic for some years, but recent political developments have brought it into the public eye - and the difference between the two terms is different under different academic theories.
Broadly, most are based on the idea that sex is biological or anatomical, while gender is social or psychological.
However, it is still normal for many people to use the terms interchangeably, so if you were ever to ask them as separate questions, you'd need to make sure you explained them.
Also, asking for both, by whatever definition, is asking extremely personal questions.
You'd better have a good reason, and expect some people to be unhappy.
A hospital setting is one of the cases where you may have a need to know both, because you want to address people according to their gender identity and so on, but you also need to know for health/medical reasons what their anatomy is, and if it used to be different.
It just has to be done really carefully, with sensitivity.
ell.stackexchange.com/users/89137/SamBC
The two words are often used interchangeably by many speakers, so the distinction that "gender" is subjective and psychological and "sex" is objective and biological won't always apply.
ell.stackexchange.com/users/10264/Tᴚoɯɐuo
The distinction between sex and gender differentiates a person's biological sex (the anatomy of an individual's reproductive system, and secondary sexual characteristics ) from that person's gender, which can refer to social roles
Sex is what "Parts" and gender is how you feel/what you want to be.
ell.stackexchange.com/users/91047/Jeef
In a hospital setting and other administrative-type settings, the terms can be the same to mean male or female.
Generally speaking, the technical term is M or F, for male or female.
However, there is no reason one cannot say the term gender to refer to the same thing.
But it would not appear on a hospital form.
However, in terms of what is called gender identity , gender refers to male, female and LBGTQ.
[at least in the U.S. and the UK].
Please see the highlighted paragraph below.
definition from Planned Parenthood of gender identity
However, both would not be asked of a person in formal settings.
Usually, on forms, one sees sex and one ticks a box: M or F. "Gender—or the different characteristics that begin to define a person as masculine or feminine—consists of several categories apart from the traditional binary ends of the male/female spectrum.
It’s possible for someone to identify as transgender, for instance—with a gender identity that does not correspond to their biological sex—or cisgender, with a gender identity that does match up with their biological sex.
Others, whose gender identity feels neither masculine nor feminine, may identify as non-binary.
Though the words are sometimes used interchangeably, gender is different than sex; while sex refers to certain genetic traits assigned at birth, gender is understood by many researchers to be influenced by a range of societal, environmental, and genetic factors.
" gender as identity
ell.stackexchange.com/users/33113/Lambie
This question may have several answers based on opinion.
Many such questions are eventually closed, but I think this may bee worth at least a discussion.
Until a few decade ago, the word sex was used to distinguish males from females, and had very little other connotations.
It was used to describe either the trait (being a man or a woman) or the group (all men of all women).
For example, Arthur Conan Doyle uses "the whole of her sex" to describe all women.
Gender , on the other hand, was mostly a term used in grammar of many languages that have a distinction between male and female (and in some cases neutral) for all nouns, not just for people or animals (where it "makes sense").
In these languages every noun has a gender (not sex!).
English has little notion of gender - the words he and she are used primarily according to the biological sex of a person, and the word it is used for almost every noun that does not refer to a person.
So the terms "sex" and "gender" were related but not identical.
More recently the word "sex" got another meaning, that of sexual intercourse (nowadays this is probably the predominant use of this word).
As such it acquired an offensive connotation, and some people feel uncomfortable using it.
The word "gender" caught instead as a "clean language" term for the biological trait.
This made "sex" and "gender" almost synonyms.
Even more recently, with the increased legitimacy and influence of the LGBT movement, there is an increased awareness that there are some people for whom the social or psychological identity does not match the biological and genetic trait of being a male or a female.
Such people are identified (sometimes self-identified) as genderqueer , and their social identity is sometimes expressed by the linguistic gender they assume - for example, a biologically male person may prefer to be addressed as a "lady", "she", "sister", etc.
The awareness to the existence of genderqueers makes it important in some cases to inquire separately for a person's sex (biological trait) and for their gender (sociological identity).
However, this is often a touchy point - many people object to the idea of genderqueer identity and would be angry of having separate questions.
I would assume that in a medical questionnaire this separation makes sense, especially in a society that is sensitive to LGBT rights.
For example, a patient may have an external appearance of a female and be addressed as a woman, while having male organs; the medical team needs to be aware of this situation.
ell.stackexchange.com/users/27840/laugh salutes Monica C
What is the difference in asking a person (1) What is your 'Gender'?and (2) What is your 'Sex'?  Can there be any scenario where both these questions can be asked to a person at the same time? I am particularly interested in medical contexts, specifically hospitals.
What is the difference between 'Gender' and 'Sex'?
ell.stackexchange.com
<word-difference><terminology>
ell.stackexchange.com/questions/199998
ell.stackexchange.com/users/91095/Optimight
 The two words are often used interchangeably by many speakers, so the distinction that "gender" is subjective and psychological and "sex" is objective and biological won't always apply.  A hospital setting is one of the cases where you may have a need to know both, because you want to address people according to their gender identity and so on, but you also need to know for health/medical reasons what their anatomy is, and if it used to be different. However, in administrative-type settings, the terms  can  be the same to mean male or female. The terms are generally interchangeable though sex is associated more with biology and gender is associated more with social roles. 
The terms are generally interchangeable. Sex is associated more with biology and gender is associated more with social roles. 