Valid Variable Name in Programming

Which of the following is a valid variable name?

1. three com

2. 3com

3. 3 com

4. ampersand&

Answer:

The correct answer is Option (1) three_com

There are a few rules you need to keep in mind while naming any variable, which are as follows:

1. The first letter of any variable name should be either a letter of an alphabet or an underscore. For example, helloWorld or _helloWorld.

2. No special characters (except underscore obviously) or spaces in between the name are allowed. For example, hello&world or hello world are not valid names.

3. The first letter of the variable name should not be a number.

Now look at all the options given:

Option-1 (three_com): The name starts with the letter t of an alphabet and there are no spaces or special characters in it except an underscore (which is acceptable). Therefore, this option is correct.

Option-2 (3com): The name starts with a number. Not allowed.

Option-3 (3_com): The name starts with a number. Not allowed.

Option-4 (ampersand&): The name has a special character & in it, which is not allowed as per the aforementioned rules.

Hence, the correct answer is option (1) three_com.

← Printing lines with an even number of characters from stdin Connect a screen to another the right sequence →