In C++, some of the following are legal identifiers.

In C++, some of the following are legal identifiers. Which? (Choose 2 right answers) Select one or more: a. Xyz b. xy_z c. 9xyz d. xyz! e. X+yz

The Correct Answer and Explanation is:

Correct answers:

  • a. Xyz
  • b. xy_z

Explanation:

In C++, identifiers are the names used to identify variables, functions, arrays, classes, and other user-defined elements. There are specific rules for forming valid identifiers:

  1. An identifier must begin with a letter (A–Z or a–z) or an underscore (_) character.
  2. Subsequent characters can include letters, digits (0–9), or underscores.
  3. Identifiers cannot begin with a digit.
  4. Identifiers cannot contain symbols such as !, +, @, #, etc.
  5. Keywords reserved by the language cannot be used as identifiers.

Let us evaluate each option based on these rules:

  • a. Xyz
    This is a valid identifier. It starts with a letter and contains only letters. No special characters or digits are used inappropriately.
  • b. xy_z
    This is also valid. It begins with a letter and includes an underscore, which is allowed.
  • c. 9xyz
    Invalid. Identifiers cannot begin with a digit. This breaks the first rule.
  • d. xyz!
    Invalid. The exclamation mark ! is not allowed in identifiers.
  • e. X+yz
    Invalid. The plus sign + is not permitted within an identifier. It is considered an operator, not a legal character for names.

Therefore, only a. Xyz and b. xy_z meet the requirements for legal identifiers in C++. These follow the language’s syntax rules for naming variables or other elements, making them safe and acceptable for use in a C++ program

Scroll to Top