















































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Designed to assess mastery of Esprima, a high-performance JavaScript parser. The exam covers AST structures, tokenization, syntax error identification, parser configuration, integration with analysis tools, and compatibility with ESTree standards. Candidates must parse JavaScript code, analyze AST nodes, design transformation workflows, and integrate Esprima into static analysis and tooling pipelines.
Typology: Exams
1 / 87
This page cannot be seen from the preview
Don't miss anything!
















































































Question 1. Which Esprima method should be used to parse an ES6 module source string? A) esprima.parseScript B) esprima.parseModule C) esprima.parse D) esprima.tokenize Answer: B Explanation: parseModule tells Esprima to treat the input as an ES6 module, enabling import/export syntax handling. Question 2. In a Node.js environment, which syntax correctly loads Esprima using CommonJS? A) import esprima from 'esprima'; B) require('esprima'); C) load('esprima'); D) esprima = include('esprima'); Answer: B Explanation: CommonJS modules are loaded with require; the other options are for ES modules or non‑existent functions. Question 3. When configuring Esprima for AMD (RequireJS), which call registers the library? A) define(['esprima'], function (esprima) {}); B) require(['esprima'], function (esprima) {}); C) import('esprima').then(...) D) module.exports = esprima; Answer: A
Explanation: AMD uses define to declare a module and its dependencies; require is used to load modules after definition. Question 4. Which of the following input types can Esprima directly parse without preprocessing? A) A Buffer object containing UTF‑8 bytes B) A file stream opened with fs.createReadStream C) A plain JavaScript string D) A compiled WebAssembly module Answer: C Explanation: Esprima expects a source code string; buffers or streams must be converted to a string first. Question 5. Which Esprima option enables parsing of code that contains syntax errors without throwing an exception? A) range: true B) tolerant: true C) loc: true D) comment: true Answer: B Explanation: The tolerant flag makes Esprima collect errors and continue parsing, rather than aborting on the first error. Question 6. What does the range property of a token represent? A) The start and end line numbers B) The character offset of the token in the source string C) The column numbers of the token
D) Identifier Answer: B Explanation: VariableDeclaration is the top‑level node that contains one or more VariableDeclarator children. Question 10. Which property of the Program node indicates whether the source was parsed as a script or a module? A) type B) sourceType C) body D) range Answer: B Explanation: sourceType is set to "script" or "module" depending on the parsing mode used. Question 11. In an AST, how are function declarations distinguished from function expressions? A) FunctionDeclaration vs. FunctionExpression node types B) Presence of a name property C) The kind property set to "declaration" or "expression" D) They are both Function nodes with a declaration flag Answer: A Explanation: Esprima uses separate node types: FunctionDeclaration for named top‑level functions and FunctionExpression for anonymous or nested functions. Question 12. Which AST node type is used for a class declaration? A) ClassExpression
B) ClassDeclaration C) ObjectExpression D) Identifier Answer: B Explanation: ClassDeclaration represents a top‑level class definition; ClassExpression is used for class literals. Question 13. Which node type represents the literal value null in an Esprima AST? A) Literal with value: null B) NullLiteral C) Identifier named null D) Keyword with value: null Answer: A Explanation: All primitive literals, including null, are represented by a Literal node with the appropriate value. Question 14. Which of the following is NOT a valid BinaryExpression operator in Esprima? A) + B) === C) ?? (nullish coalescing) D) => (arrow) Answer: D Explanation: => is not a binary operator; it is part of an ArrowFunctionExpression. The other three are binary operators. Question 15. In an UpdateExpression node, what does the prefix property indicate? A) Whether the operator appears before the operand (++i)
Question 18. Which flag must be set to include the full token array inside the generated AST? A) range B) loc C) tokens D) tolerant Answer: C Explanation: Setting tokens: true attaches the token list to the top‑level AST node. Question 19. When the comment flag is enabled, where are comment nodes attached in the AST? A) As children of the nearest statement node B) In a top‑level comments array property of the Program node C) Inside the leadingComments property of each identifier D) They replace whitespace tokens in the token stream Answer: B Explanation: Esprima adds a comments array to the Program node containing comment nodes with location data. Question 20. Which configuration option enables parsing of JSX syntax? A) jsx: true B) allowJSX: true C) sourceType: "module" D) tolerant: true Answer: A Explanation: Setting jsx: true tells Esprima to accept React‑style JSX constructs.
Question 21. What is the purpose of the delegate option in Esprima’s parsing API? A) To replace the default tokenization algorithm B) To receive a callback for each node as it is created C) To delegate error handling to a custom function D) To delegate source map generation Answer: B Explanation: The delegate callback is invoked with each newly constructed AST node, allowing custom processing. Question 22. Which of the following statements about Esprima’s tolerant mode is FALSE? A) It collects syntax errors in an errors array on the AST B) It stops parsing after the first error even if tolerant is true C) It attempts to produce a best‑effort AST despite errors D) It can be useful for IDEs that need partial parsing Answer: B Explanation: In tolerant mode, Esprima does not stop after the first error; it continues and records errors. Question 23. Which property on a Literal node holds the raw source text of the literal? A) value B) raw C) source D) text Answer: B Explanation: raw contains the exact characters from the source code (e.g., "42"), while value is the parsed JavaScript value.
Question 27. What does the loc option add to each AST node? A) A range array of character offsets B) start and end objects with line and column numbers C) A source string containing the original code snippet D) A tokenIndex linking to the token array Answer: B Explanation: loc: true adds loc.start and loc.end with line and column information. Question 28. Which of the following is a correct way to traverse an Esprima AST using Estraverse? A) estraverse.traverse(ast, { enter: function (node) {} }); B) estraverse.walk(ast, node => {}); C) estraverse.map(ast, fn); D) estraverse.iterate(ast); Answer: A Explanation: estraverse.traverse accepts an object with enter and/or leave callbacks to visit nodes. Question 29. To generate source code from a modified AST, which library is typically used? A) escodegen B) esprima-generator C) babel-core D) uglify-js Answer: A Explanation: escodegen takes an ESTree‑compatible AST and produces JavaScript source code.
Question 30. Which Esprima configuration is most appropriate for a linting tool that needs token and comment information? A) { tolerant: true, range: true } B) { tokens: true, comment: true, loc: true } C) { jsx: true, sourceType: "module" } D) { range: false, comment: false } Answer: B Explanation: Enabling tokens, comment, and loc provides the detailed source mapping required by linters. Question 31. How does Esprima represent the identifier myVar in the AST? A) As a Literal node with value: "myVar" B) As an Identifier node with name: "myVar" C) As a VariableDeclarator node directly D) As a Property node in the global scope Answer: B Explanation: Identifiers are represented by Identifier nodes containing a name property. Question 32. Which node type would Esprima use for the expression a && b? A) LogicalExpression B) BinaryExpression C) ConditionalExpression D) ExpressionStatement Answer: A Explanation: Logical operators (&&, ||) are modeled with LogicalExpression nodes. Question 33. In a MemberExpression, what does the computed property indicate?
Question 36. Which flag must be set to obtain the exact character offsets (range) for each node? A) range: true B) loc: true C) tokens: true D) tolerant: true Answer: A Explanation: range: true adds a [start, end] array to every node. Question 37. In Esprima’s AST, which node type represents a throw statement? A) ThrowStatement B) ErrorStatement C) ExceptionStatement D) RaiseStatement Answer: A Explanation: ThrowStatement is the ESTree node for throw expressions. Question 38. Which of the following best describes the purpose of the escope library? A) To generate code from an AST B) To perform lexical scope analysis on an ESTree AST C) To minify JavaScript source code D) To parse JSX syntax Answer: B Explanation: escope traverses an ESTree AST and builds scope information (variables, references, etc.).
Question 39. If you need to detect undeclared variables in a code base, which combination of tools is most effective? A) Esprima + Estraverse + custom analysis of Identifier nodes B) Esprima + Escope to get the scope chain C) Esprima with tolerant: true only D) Esprima with comment: true only Answer: B Explanation: Escope provides precise variable declaration and reference tracking, simplifying undeclared‑variable detection. Question 40. Which node type is used for a class method definition inside a class body? A) MethodDefinition B) FunctionExpression C) Property D) ClassMethod Answer: A Explanation: In ESTree, class methods are represented by MethodDefinition nodes within a ClassBody. Question 41. When Esprima parses a template literal `Hello ${name}`, how is the expression ${name} represented? A) As a TemplateLiteral node with expressions array containing an Identifier B) As a BinaryExpression concatenating strings C) As a Literal node with raw value "Hello ${name}" D) As a TaggedTemplateExpression Answer: A
Answer: A Explanation: The left side is an ArrayPattern node, which is a kind of pattern used in assignments and declarations. Question 45. What does the allowReturnOutsideFunction option (if present) control? A) Whether return statements are permitted at the top level of a script B) Whether return can be used inside arrow functions C) Whether return statements generate a syntax error in modules D) This option does not exist in Esprima Answer: D Explanation: Esprima does not provide an allowReturnOutsideFunction flag; such behavior is fixed by the ECMAScript grammar. Question 46. Which of the following statements about the tokens array attached to the AST is TRUE? A) Tokens are sorted by their range values in descending order B) Each token includes a type property matching the token type enumeration C) The token array includes whitespace tokens by default D) Tokens are only generated when the comment flag is also true Answer: B Explanation: Each token object has a type field (e.g., "Identifier"). Tokens are ordered as they appear, whitespace is omitted, and tokens are independent of comment. Question 47. In Esprima, which node type corresponds to a new expression such as new Date()? A) CallExpression with a new flag B) NewExpression
C) ConstructorExpression D) ObjectCreationExpression Answer: B Explanation: NewExpression is the ESTree node representing object instantiation with new. Question 48. Which property of a FunctionExpression node indicates whether it is an async function? A) async: true B) generator: true C) type: "AsyncFunctionExpression" D) expression: true Answer: A Explanation: The boolean async property flags async functions; generators use the generator flag. Question 49. When Esprima parses the code a?.b.c, which node type is used for the optional chaining operator? A) ChainExpression wrapping a MemberExpression B) OptionalMemberExpression C) LogicalExpression with operator ?. D) ConditionalExpression Answer: A Explanation: Optional chaining is represented by a ChainExpression node whose expression is a MemberExpression with optional: true. Question 50. Which of the following is a correct way to enable both tolerant mode and JSX parsing simultaneously?
B) ArrowFunctionExpression node type C) FunctionDeclaration with type: "arrow" D) LambdaExpression node type Answer: B Explanation: Arrow functions have their own node type ArrowFunctionExpression. Question 54. Which of the following is true about the loc property when both range and loc are enabled? A) loc values are calculated from range automatically B) loc and range are independent but both provided C) Enabling loc disables range D) range is omitted when loc is true Answer: B Explanation: Both properties are added independently; one does not suppress the other. Question 55. Which node type represents a switch statement? A) SwitchStatement B) CaseStatement C) SelectStatement D) ConditionalStatement Answer: A Explanation: SwitchStatement is the ESTree node for switch constructs, containing cases (array of SwitchCase). Question 56. When parsing the code /* comment */ var x = 1; // end, which token types appear for the comments if comment: true is set? A) Line and Block token types
B) CommentLine and CommentBlock token types C) Punctuator token types D) Comments are not represented as tokens, only as AST comment nodes Answer: D Explanation: With comment: true, comments are added to the comments array, not to the token list. Question 57. Which ESTree node property holds the list of parameters for a function? A) args B) params C) arguments D) variables Answer: B Explanation: The params array contains pattern nodes representing each parameter. Question 58. In Esprima, what does the strict flag indicate when parsing code? A) That the parser should enforce strict mode semantics B) That the source contains a "use strict" directive C) That the generated AST will be immutable D) Esprima does not have a strict flag; strict mode is detected automatically Answer: D Explanation: Esprima automatically detects "use strict" directives; there is no explicit strict option. Question 59. Which of the following node types can appear as the test property of an IfStatement? A) ExpressionStatement