Syntax Sense: Easy Quiz on Code Rules and Structure Quiz

  1. Understanding Syntax

    In programming, what does the term 'syntax' refer to when writing code that a compiler or interpreter must read and validate?

    1. The formal set of rules that defines the structure and arrangement of code so it can be parsed
    2. The meaning and intended effects of the code at runtime, known as semantics
    3. Personal style conventions for spacing and line wrapping that do not affect execution
    4. The sequence of CPU micro-operations that make a program run
    5. Sintax is a term for spell-checking comments only
  2. Syntax vs. Runtime Errors

    Which statement best explains the difference between a syntax error like print(2 + ) and a runtime error like 10/0?

    1. 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
    2. Both errors occur only after the program finishes running
    3. A runtime error prevents parsing, while a syntax error appears only at runtime
    4. Both errors are identical and depend only on editor color themes
    5. A syntax error always calculates the wrong numeric result but still runs
  3. Matching Parentheses

    If a language requires matching parentheses, what is the syntax error in the statement print(3 + 2?

    1. The closing parenthesis is missing after the 2
    2. The plus sign must be written as the word plus
    3. Numbers cannot appear inside parentheses
    4. The word print must be uppercase to allow math
    5. Prenthesis order is irrelevant, so there is no error
  4. Reserved Keywords

    Why does the line int if = 5; cause a syntax error in many languages that use reserved keywords?

    1. Because 'if' is a reserved keyword and cannot be used as a variable name
    2. Because integers cannot be assigned numeric values
    3. Because the number 5 must be written as 'five'
    4. Because variable names must start with a digit
    5. Because the statement must start with a parenthesis
  5. String Literal Delimiters

    Given the line msg = 'Hello, world, which syntax rule is violated in many languages that use quotes for strings?

    1. A closing quote is missing, so the string literal is not properly terminated
    2. A semicolon is missing, and strings may not contain commas
    3. Strings must start with a double quote, not a single quote
    4. Variables cannot be named msg in any language
    5. The comma must be escaped with a backslash in all languages
  6. Statement Terminators

    In a language where every statement must end with a semicolon, which example follows the syntax rule?

    1. x = 10;
    2. x = 10
    3. x = 10)
    4. 'x = 10;'
    5. x = 10;)
  7. 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?

    1. The code may produce a syntax error or create unintended block structure
    2. Only the color of the code editor changes, not behavior
    3. Indentation is always ignored by compilers and interpreters
    4. Inconsistent indentation automatically optimizes performance
    5. Mixed indentation is required to declare variables
  8. Who Checks Syntax

    Which component typically checks whether program text obeys the language's syntax rules before execution?

    1. The parser analyzes the token sequence to verify it matches the grammar
    2. The linker enforces syntax by combining finished programs at runtime
    3. The garbage collector checks heap memory to ensure tokens are matched
    4. The debugger executes the source code to produce parse trees
    5. The tokenizer executes instructions to enforce control flow
  9. Balanced Braces

    In a brace-delimited language, what must accompany an opening brace in code like if (ok) { do(); to satisfy syntax rules?

    1. A matching closing brace must appear later to end the block
    2. A matching closing parenthesis must appear after the semicolon
    3. A colon must follow the opening brace immediately
    4. An end keyword must follow the opening brace on the same line
    5. No other symbol is required; braces are optional noise
  10. 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?

    1. first_name
    2. 2ndPlace
    3. first-name
    4. first name
    5. first.name