IAE Academy

GATE 2026 CS1 Q.27: solution and answer

GATE 2026 · CS1Q.27 · 1 mark · MSQ

Consider the following C statements:

char *str1 = "Hello;    /* Statement S1 */
char *str2 = "Hello;";  /* Statement S2 */
int *str3 = "Hello";    /* Statement S3 */

Which of the following options is/are correct?

  1. (A)

    S1 and S2 have syntactic errors

  2. (B)

    S2 has a lexical error and S3 has a syntactic error

  3. (C)

    S1 has a lexical error and S3 has a semantic error

  4. (D)

    S1 has a syntactic error and S3 has a semantic error

See the question in the paper

Answer and solution

ANSWER

Option C: S1 has a lexical error and S3 has a semantic error.

Official answer: CSource: Official answer key, GATE 2026 CS1 (IIT Guwahati)

  1. A lexical error is a malformed token, found by the scanner. A syntactic error breaks the grammar. A semantic error is well-formed code that breaks a type or meaning rule.

  2. S1: char *str1 = "Hello; /* Statement S1 */ opens a string literal with " but has no closing " before the end of the line. The scanner cannot form a valid string-literal token, so this is a lexical error. The /* sits inside the unfinished literal, so it does not start a comment.

  3. S2: "Hello;" is a complete string literal, because the ; is an ordinary character inside it. The statement continues with its own ; and then a comment, so it is fully valid. S2 has no error, which rules out A and B.

  4. S3: int *str3 = "Hello"; is a grammatically valid declaration with an initializer, so there is no syntactic error. The literal has type char * after array decay, and it is used to initialize an int *. This incompatible pointer type is a type-checking violation, which is a semantic error.

  5. So S1 has a lexical error and S3 has a semantic error. This is option C. Option D is wrong because S1 fails at the token level, not at the grammar level.

KEY IDEA

Classify each error by the compiler phase that catches it. An unterminated string literal fails in the scanner (lexical). A well-formed declaration that assigns a char pointer to an int pointer fails in type checking (semantic).

WATCH OUT FOR

Calling S1 a syntactic error because the statement looks broken, or reading the ; inside the quotes in S2 as the end of the statement and so marking S2 as an error.

From the GATE 2026 CS1 paper. Every question of this paper · Download the full paper · All 8 Lexical analysis questions, with notes

More Lexical analysis questions