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?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2026 Set 1 · Q27
Time
char *str1 = "Hello; /* Statement S1 */
char *str2 = "Hello;"; /* Statement S2 */
int *str3 = "Hello"; /* Statement S3 */Which of the following options is/are correct?Select all that applyOne or more correct
Solution
Official answer: C) S1 has a lexical error and S3 has a semantic error
Lexical AnalysisLet us analyze the three statements:
Statement S1 (char *str1 = "Hello;):
The string literal is missing the closing double quote ".
Tokenization fails to form a valid string token, which is detected during the lexical analysis phase as an unclosed string literal (a lexical error).
Statement S2 (char *str2 = "Hello;";):
This is a completely valid declaration and initialization of a string literal in C. It contains no error.
Statement S3 (int *str3 = "Hello";):
Syntactically, this is valid (a pointer variable is initialized with an expression).
However, the string literal "Hello" produces a pointer to char (char *), which is being assigned to a pointer to int (int *) without a cast. This represents a type mismatch, which is detected during the semantic analysis phase (a semantic error).
Therefore, S1 has a lexical error and S3 has a semantic error, making (C) the correct option.
Try the question first, then check your work.
Examiner trap
Consider the compiler phase responsible for token formation (unclosed string literals) and type-checking (assigning incompatible pointer types).
This Lexical Analysis previous year question is Q27 of the GATE CSE 2026 Set 1 paper. Find more GATE CSE PYQs on Lexical Analysis in Compiler Design, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Lexical Analysis PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Lexical Analysis has 7 GATE CSE PYQs across 6 papers from 2011 to 2026. This is the only 1-mark MSQ among them, and it is the only one in GATE CSE 2026 Set 1.
How GATE tests Lexical AnalysisIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.