Understanding Syntax
In programming, what does the term 'syntax' refer to when writing code that a compiler or interpreter must read and validate?
- The formal set of rules that defines the structure and arrangement of code so it can be parsed
- The meaning and intended effects of the code at runtime, known as semantics
- Personal style conventions for spacing and line wrapping that do not affect execution
- The sequence of CPU micro-operations that make a program run
- Sintax is a term for spell-checking comments only
Syntax vs. Runtime Errors
Which statement best explains the difference between a syntax error like print(2 + ) and a runtime error like 10/0?
- A syntax error violates the formal structure and prevents the program from being parsed or compiled, while a runtime error occurs after successful parsing when the program executes
- Both errors occur only after the program finishes running
- A runtime error prevents parsing, while a syntax error appears only at runtime
- Both errors are identical and depend only on editor color themes
- A syntax error always calculates the wrong numeric result but still runs
Matching Parentheses
If a language requires matching parentheses, what is the syntax error in the statement print(3 + 2?
- The closing parenthesis is missing after the 2
- The plus sign must be written as the word plus
- Numbers cannot appear inside parentheses
- The word print must be uppercase to allow math
- Prenthesis order is irrelevant, so there is no error
Reserved Keywords
Why does the line int if = 5; cause a syntax error in many languages that use reserved keywords?
- Because 'if' is a reserved keyword and cannot be used as a variable name
- Because integers cannot be assigned numeric values
- Because the number 5 must be written as 'five'
- Because variable names must start with a digit
- Because the statement must start with a parenthesis
String Literal Delimiters
Given the line msg = 'Hello, world, which syntax rule is violated in many languages that use quotes for strings?
- A closing quote is missing, so the string literal is not properly terminated
- A semicolon is missing, and strings may not contain commas
- Strings must start with a double quote, not a single quote
- Variables cannot be named msg in any language
- The comma must be escaped with a backslash in all languages
Statement Terminators
In a language where every statement must end with a semicolon, which example follows the syntax rule?
- x = 10;
- x = 10
- x = 10)
- 'x = 10;'
- x = 10;)
Indentation as Syntax
In a language where indentation defines code blocks, what can happen if a function mixes tabs and spaces inconsistently in a nested if statement?
- The code may produce a syntax error or create unintended block structure
- Only the color of the code editor changes, not behavior
- Indentation is always ignored by compilers and interpreters
- Inconsistent indentation automatically optimizes performance
- Mixed indentation is required to declare variables
Who Checks Syntax
Which component typically checks whether program text obeys the language's syntax rules before execution?
- The parser analyzes the token sequence to verify it matches the grammar
- The linker enforces syntax by combining finished programs at runtime
- The garbage collector checks heap memory to ensure tokens are matched
- The debugger executes the source code to produce parse trees
- The tokenizer executes instructions to enforce control flow
Balanced Braces
In a brace-delimited language, what must accompany an opening brace in code like if (ok) { do(); to satisfy syntax rules?
- A matching closing brace must appear later to end the block
- A matching closing parenthesis must appear after the semicolon
- A colon must follow the opening brace immediately
- An end keyword must follow the opening brace on the same line
- No other symbol is required; braces are optional noise
Valid Identifier Forms
Which identifier is syntactically valid in many languages that require identifiers to start with a letter or underscore and contain only letters, digits, or underscores?
- first_name
- 2ndPlace
- first-name
- first name
- first.name