JavaScript Fundamentals: Scoping, Template Literals, and Arrow Functions Quiz
- Variable Hoisting and Scoping (
var
, let
, const
) - Template Literals (using
${}
for string interpolation) - Arrow Functions and the
this
Keyword
This quiz is designed to help you understand JavaScript variables, template literals, and arrow functions through real-world scenarios!
Question 1. What will the following code output? console.log(x); var x = 10;
-
A.
10
-
B.
undefined
-
C.
ReferenceError
-
D.
null
Explanation: Variables declared with var are hoisted and initialized to undefined
Question 2. What happens when you declare a variable with let and access it before initialization?
Explanation: Variables declared with let are hoisted but not initialized, resulting in a ReferenceError if accessed before initialization
Question 3. Which of the following is a correct use of template literals?
-
A.
My age is + age
-
B.
My age is ${}
-
C.
My age is ${age}
-
D.
"My age is " + age
Explanation: Template literals use backticks and ${} to interpolate variables
Question 4. What will the following code output? const name = "Alice"; console.log(`Hello, ${name}!`);
-
A.
Hello, Alice!
-
B.
Hello, ${name}!
-
C.
undefined
-
D.
Error
Explanation: The variable name is interpolated properly using template literals
Question 5. What is the behavior of this inside an arrow function?
Explanation: Arrow functions inherit this from their enclosing scope
Question 6. What will the following code output? const obj = { name: "Bob", greet: () => console.log(`Hello, ${this.name}`) }; obj.greet();
-
A.
Hello, Bob
-
B.
Error
-
C.
Hello, undefined
-
D.
Hello, null
Explanation: Arrow functions do not bind their own this, so it refers to the global scope where name is undefined
Question 7. What will the following code do? try { console.log(5 / 0); } catch (err) { console.log("Error"); }
-
A.
Output Error
-
B.
Throws a runtime error
-
C.
Output Infinity
-
D.
Error in syntax
Explanation: Division by zero in JavaScript does not throw an error; it returns Infinity
Question 8. Which block is always executed in a try-catch-finally statement?
-
A.
try
-
B.
catch
-
C.
finally
-
D.
None of the above
Explanation: The finally block is always executed, regardless of whether an error occurs or not
Question 9. What will the following code output? let x = 10; { let x = 20; console.log(x); } console.log(x);
-
A.
20, 20
-
B.
20, 10
-
C.
10, 20
-
D.
Error
Explanation: The inner x is block-scoped due to let, and does not affect the outer x
Question 10. What will happen if you reassign a const variable? const age = 25; age = 30; console.log(age);
-
A.
Outputs 30
-
B.
Outputs 25
-
C.
Throws a TypeError
-
D.
Outputs undefined
Explanation: onst variables cannot be reassigned once initialized
Share Quiz Result