MCQs on Functions and Scope | JavaScript

Understanding functions and scope is crucial for effective JavaScript programming. This set of 30 multiple-choice questions (MCQs) covers key concepts, from declaring and invoking functions to understanding various scopes in JavaScript. Test your knowledge and ensure you’re ready to work with parameters, return values, arrow functions, and scope types.

Declaring and Invoking Functions

  1. Which keyword is used to declare a function in JavaScript?
    • A) func
    • B) declare
    • C) function
    • D) def
  2. What is the correct syntax for invoking a function named myFunction?
    • A) myFunction{}
    • B) myFunction()
    • C) invoke myFunction
    • D) call myFunction
  3. What will the following code output?




  1. function greet() { console.log("Hello, World!"); } greet();
    • A) Hello, World!
    • B) greet
    • C) undefined
    • D) No output
  2. Which of the following options is NOT a valid function name in JavaScript?
    • A) _myFunction
    • B) my-function
    • C) myFunction
    • D) $myFunction
  3. In JavaScript, where can a function be called from if it is declared using function syntax?
    • A) Only below its declaration
    • B) Only within the same block
    • C) Anywhere in the code
    • D) Only above its declaration

Parameters and Return Values

  1. How are parameters defined in a function?
    • A) Inside brackets []
    • B) Inside parentheses ()
    • C) Inside curly braces {}
    • D) Outside of function body
  2. What will the following function return?




  1. function add(a, b) { return a + b; } console.log(add(5, 3));
    • A) 53
    • B) 15
    • C) 8
    • D) undefined
  2. What is the purpose of the return statement in a function?
    • A) To end a function
    • B) To skip to the next iteration
    • C) To send a value back to the calling location
    • D) To execute the function again
  3. If a function does not contain a return statement, what does it return by default?
    • A) null
    • B) 0
    • C) undefined
    • D) An empty string
  4. What is the correct syntax to pass parameters to a function?
    • A) function myFunc(param1, param2)
    • B) myFunc{param1, param2}
    • C) function myFunc[param1, param2]
    • D) function (param1, param2)

Function Expressions and Arrow Functions

  1. How is a function expression assigned to a variable?
    • A) let myFunc = function() {}
    • B) let function = myFunc {}
    • C) function myFunc() = {}
    • D) let function myFunc() {}
  2. Which of the following is the correct syntax for an arrow function?
    • A) let sum = (a, b) => { return a + b; }
    • B) let sum = (a, b) -> { return a + b; }
    • C) let sum = (a, b) { return a + b; }
    • D) let sum = (a, b) { return a + b; }
  3. Arrow functions were introduced in which version of ECMAScript?
    • A) ES5
    • B) ES6
    • C) ES7
    • D) ES8
  4. What does an arrow function with a single line of code automatically do?
    • A) Logs the value
    • B) Throws an error
    • C) Returns the value
    • D) Executes without returning
  5. Which of the following is true about arrow functions in JavaScript?
    • A) They can be used with this keyword like regular functions.
    • B) They do not have their own this context.
    • C) They cannot take parameters.
    • D) They must always return a value.

Scope: Block Scope, Function Scope, Global Scope

  1. Variables declared with var have which type of scope?
    • A) Block scope
    • B) Function scope
    • C) Module scope
    • D) Lexical scope
  2. Which of the following keywords enforces block scope?
    • A) const
    • B) var
    • C) static
    • D) function
  3. A variable declared outside any function or block is in which scope?
    • A) Block scope
    • B) Function scope
    • C) Global scope
    • D) Local scope
  4. Which of the following can alter the value of a let variable declared inside a function from outside that function?
    • A) Another function
    • B) Global context
    • C) No access possible
    • D) const declaration
  5. When a variable is defined with const, which of the following is true?
    • A) It can be re-assigned
    • B) It cannot be reassigned
    • C) It cannot be initialized
    • D) It can be reassigned if inside a loop

Miscellaneous Questions on Functions and Scope

  1. What is the default scope for a function in JavaScript?
    • A) Block scope
    • B) Function scope
    • C) Module scope
    • D) Global scope
  2. In which of the following is a let variable not accessible?
    • A) Within the block it was defined
    • B) Outside the block it was defined
    • C) Inside a nested function
    • D) Inside the global scope
  3. Which function declaration method allows hoisting?
    • A) Function expressions
    • B) Arrow functions
    • C) Named function declarations
    • D) Variable declarations
  4. Which of the following statements correctly explains “lexical scope”?
    • A) Variables are defined based on where they are used
    • B) Variables are defined based on where they are written in the code
    • C) Variables are accessible only inside functions
    • D) Only global variables are accessible
  5. What will happen if you try to access a variable defined with let outside its block?
    • A) It will log undefined
    • B) It will throw a reference error
    • C) It will return null
    • D) It will automatically assign a new value
  6. What happens when you redeclare a variable with var in the same scope?
    • A) Error is thrown
    • B) Redeclaration is ignored
    • C) The variable is redefined
    • D) Variable is hoisted again
  7. How can you create a global variable within a function?
    • A) Declare with const
    • B) Declare without var, let, or const
    • C) Declare with let
    • D) Use an arrow function
  8. What happens if two variables in different scopes have the same name?
    • A) Both are accessible with no issues
    • B) The innermost variable takes precedence
    • C) The outer variable takes precedence
    • D) Code throws an error
  9. Which statement is true for closures in JavaScript?
    • A) They cannot access variables from outer functions
    • B) They remember variables from their parent function’s scope
    • C) They don’t work with arrow functions
    • D) They prevent variable mutation
  10. Which of the following best describes a pure function?
    • A) A function that always produces the same output for the same inputs
    • B) A function that changes global variables
    • C) A function that requires no parameters
    • D) A function with side effects

Answer Key

QnoAnswer
1C) function
2B) myFunction()
3A) Hello, World!
4B) my-function
5C) Anywhere in the code
6B) Inside parentheses ()
7C) 8
8C) To send a value back to the calling location
9C) undefined
10A) function myFunc(param1, param2)
11A) let myFunc = function() {}
12A) let sum = (a, b) => { return a + b; }
13B) ES6
14C) Returns the value
15B) They do not have their own this context
16B) Function scope
17A) const
18C) Global scope
19C) No access possible
20B) It cannot be reassigned
21B) Function scope
22B) Outside the block it was defined
23C) Named function declarations
24B) Variables are defined based on where they are written in the code
25B) It will throw a reference error
26C) The variable is redefined
27B) Declare without var, let, or const
28B) The innermost variable takes precedence
29B) They remember variables from their parent function’s scope
30A) A function that always produces the same output for the same inputs

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top