Which of the following creates a String named constant called partNo, whose value is AB45?

”const
partNo = “AB45″;” option2=”const char[5] partNo = ‘AB45’;” option3=”const char[5] partNo = “AB45″;” option4=”const char partNo[5] = “AB45″; E. None of the above” correct=”option4″]

The correct answer is: D. const char partNo[5] = “AB45”;

A string literal is a sequence of characters enclosed in double quotes. It is a compile-time constant, which means that its value is determined at compile time and cannot be changed at runtime.

A string constant is a variable that stores a string literal. It is also a compile-time constant, which means that its value is determined at compile time and cannot be changed at runtime.

The syntax for declaring a string constant is:

const char arrayName[arraySize] = "stringLiteral";

In this syntax, arrayName is the name of the string constant, arraySize is the number of characters in the string literal, and stringLiteral is the string literal itself.

In the case of the question, the string literal is "AB45". The number of characters in the string literal is 5. Therefore, the correct declaration for the string constant is:

const char partNo[5] = "AB45";

The other options are incorrect because they either do not declare a string constant or they declare a string constant with the wrong number of characters.

Option A: const char[4] partNo = "AB45" declares a string constant with a length of 4, but the string literal has a length of 5.

Option B: const char[5] partNo = 'AB45' declares a string constant with a length of 5, but the string literal is a single character, which is not valid.

Option C: const char[5] partNo = "AB45" is the same as Option A.

Option E: None of the above is the correct answer because none of the other options are correct.

Exit mobile version