Multiple Choice Question for Grand JS Quiz



(Chap # 31 - Getting the current date and time )

(1) What does the `Date()` constructor in JavaScript return?

A) The current date and time
B) The current date
C) The current time
D) A specific date and time set by the user

Answer: A) The current date and time

Explanation: `Date()` returns the current date and time according to the system's time zone.



(2) Which method is used to get the current year from a Date object?

A) `getYear()`
B) `getFullYear()`
C) `getCurrentYear()`
D) `getDate()`

Answer: B) `getFullYear()`

Explanation: The method `getFullYear()` returns the current year of the Date object.



(3) What does the `Date.now()` method return?

A) The current date
B) The current date and time
C) The number of milliseconds since January 1, 1970
D) The current month

Answer: C) The number of milliseconds since January 1, 1970

Explanation: `Date.now()` returns the number of milliseconds that have passed since January 1, 1970, 00:00:00 UTC.



(4) Which of the following methods can be used to get the current day of the week?

A) `getDay()`
B) `getWeekDay()`
C) `getCurrentDay()`
D) `getDayOfWeek()`

Answer: A) `getDay()`

Explanation: `getDay()` returns the day of the week as a number (0 for Sunday, 1 for Monday, etc.).



(5) How can you get the current month in a Date object?

A) `getMonth()`
B) `getCurrentMonth()`
C) `getMonthNumber()`
D) `getFullMonth()`

Answer: A) `getMonth()`

Explanation: `getMonth()` returns the month (from 0 to 11), where 0 represents January and 11 represents December.



(6) What will be the output of `new Date().getTime()`?

A) The current date and time
B) The current year
C) The number of milliseconds since January 1, 1970
D) The number of seconds since January 1, 1970

Answer: C) The number of milliseconds since January 1, 1970

Explanation: `getTime()` returns the number of milliseconds since the Unix epoch (January 1, 1970).



(7) Which of the following methods can be used to get the current day of the month?

A) `getDate()`
B) `getDay()`
C) `getCurrentDate()`
D) `getMonth()`

Answer: A) `getDate()`

Explanation: `getDate()` returns the day of the month (1–31) for the specified date.



(8) How can you create a Date object representing a specific date?

A) `new Date("2023-05-15")`
B) `Date.create("2023-05-15")`
C) `new Date(2023, 5, 15)`
D) Both A and C

Answer: D) Both A and C

Explanation: Both `new Date("2023-05-15")` and `new Date(2023, 5, 15)` create a Date object for May 15, 2023. Note that the month is zero-indexed in the second form (0 for January, 5 for June).



(9) Which of the following will display the current date and time in ISO format?

A) `new Date().toLocaleString()`
B) `new Date().toISOString()`
C) `new Date().toString()`
D) `new Date().toFixed()`

Answer: B) `new Date().toISOString()`

Explanation: `toISOString()` converts the Date object to a string in ISO format (e.g., "2023-05-15T10:30:00.000Z").



(10) What does `new Date().getMilliseconds()` return?

A) The milliseconds of the current time
B) The number of seconds since the Unix epoch
C) The current year
D) The current date and time

Answer: A) The milliseconds of the current time

Explanation: `getMilliseconds()` returns the milliseconds portion of the time (0–999) for the current Date object.



(Chap # 32 - Extracting parts of the date and time)

(1) How can you extract the current hour from a Date object?

A) `getHour()`
B) `getHours()`
C) `getCurrentHour()`
D) `getTime()`

Answer: B) `getHours()`

Explanation: `getHours()` returns the hour (0–23) of the specified Date object.



(2) Which method is used to get the minutes of a Date object?

A) `getMinutes()`
B) `getMinutesValue()`
C) `getMinute()`
D) `getTimeMinutes()`

Answer: A) `getMinutes()`

Explanation: `getMinutes()` returns the minutes (0–59) of the specified Date object.



(3) What will `new Date().getSeconds()` return?

A) The current seconds
B) The current minutes
C) The current year
D) The current date

Answer: A) The current seconds

Explanation: `getSeconds()` returns the seconds (0–59) of the specified Date object.



(4) Which method is used to get the day of the month from a Date object?

A) `getDay()`
B) `getDate()`
C) `getDayOfMonth()`
D) `getCurrentDay()`

Answer: B) `getDate()`

Explanation: `getDate()` returns the day of the month (1–31) of the specified Date object.



(5) How can you extract the current month from a Date object?

A) `getMonth()`
B) `getMonthValue()`
C) `getCurrentMonth()`
D) `getMonthNumber()`

Answer: A) `getMonth()`

Explanation: `getMonth()` returns the month (0–11) of the specified Date object, where 0 is January and 11 is December.



(6) Which method retrieves the full year from a Date object?

A) `getYear()`
B) `getFullYear()`
C) `getCurrentYear()`
D) `getYearValue()`

Answer: B) `getFullYear()`

Explanation: `getFullYear()` returns the four-digit year (e.g., 2023) of the specified Date object.



(7) What is the purpose of the `getMilliseconds()` method in a Date object?

A) It returns the current milliseconds.
B) It returns the current seconds.
C) It returns the current year.
D) It returns the current time in milliseconds.

Answer: A) It returns the current milliseconds.

Explanation: `getMilliseconds()` returns the milliseconds (0–999) from the current Date object.



(8) How can you extract the current day of the week from a Date object?

A) `getDay()`
B) `getCurrentDay()`
C) `getWeekday()`
D) `getDayOfWeek()`

Answer: A) `getDay()`

Explanation: `getDay()` returns the day of the week (0–6), where 0 is Sunday and 6 is Saturday.



(9) What does `getUTCDay()` return?

A) The day of the week in the local time zone.
B) The day of the week in UTC.
C) The current date.
D) The current year in UTC.

Answer: B) The day of the week in UTC.

Explanation: `getUTCDay()` returns the day of the week (0–6) in Coordinated Universal Time (UTC), where 0 is Sunday and 6 is Saturday.



(10) How do you extract the current time (in hours, minutes, and seconds) from a Date object?

A) `getTime()`
B) `getDate()`
C) `getCurrentTime()`
D) `getHours(), getMinutes(), getSeconds()`

Answer: D) `getHours(), getMinutes(), getSeconds()`

Explanation: You can use `getHours()`, `getMinutes()`, and `getSeconds()` to extract the current time from a Date object.



(Chap # 33 - Specifying the date and time )

(1) How can you create a new Date object representing a specific date in JavaScript?

A) `new Date(2023, 5, 25)`
B) `new Date('2023-06-25')`
C) `new Date('June 25, 2023')`
D) All of the above

Answer: D) All of the above

Explanation: JavaScript's Date object can accept various formats to specify a date, such as `new Date(2023, 5, 25)`, `new Date('2023-06-25')`, and `new Date('June 25, 2023')`.



(2) Which method can you use to specify the year, month, and day of a Date object?

A) `setFullYear()`
B) `setMonth()`
C) `setDate()`
D) `setYear()`

Answer: A) `setFullYear()`

Explanation: `setFullYear()` sets the year of a Date object. You can use `setMonth()` and `setDate()` to set the month and day respectively.



(3) Which of the following will create a Date object representing July 1, 2025?

A) `new Date(2025, 7, 1)`
B) `new Date('2025-07-01')`
C) `new Date('July 1, 2025')`
D) All of the above

Answer: D) All of the above

Explanation: All three formats can be used to create a Date object representing July 1, 2025. Remember that months are zero-indexed, so July is month 6 in `new Date(2025, 6, 1)`.



(4) How can you set the time of a Date object to 14:30:00?

A) `setHours(14, 30, 0)`
B) `setTime(14, 30, 0)`
C) `setHours(14, 30)`
D) `setTime(14:30)`

Answer: A) `setHours(14, 30, 0)`

Explanation: `setHours()` sets the hours, minutes, and seconds of a Date object. It accepts parameters for hour, minute, and second.



(5) Which method allows you to set the full year, including century?

A) `setYear()`
B) `setFullYear()`
C) `setCentury()`
D) `setDate()`

Answer: B) `setFullYear()`

Explanation: `setFullYear()` allows you to set the full year, including the century (e.g., 2025).



(6) How would you create a Date object that represents the current date and time?

A) `new Date()`
B) `new Date('now')`
C) `Date.now()`
D) `Date()`

Answer: A) `new Date()`

Explanation: `new Date()` creates a Date object representing the current date and time.



(7) Which of the following will set the month to December in a Date object?

A) `setMonth(11)`
B) `setMonth(12)`
C) `setMonth('December')`
D) `setMonth(10)`

Answer: A) `setMonth(11)`

Explanation: `setMonth()` sets the month. December corresponds to index 11 since months are zero-indexed (0 for January, 11 for December).



(8) What does the `setSeconds()` method do in a Date object?

A) Sets the seconds of a Date object
B) Sets the hours of a Date object
C) Sets the minutes of a Date object
D) Sets the day of the month

Answer: A) Sets the seconds of a Date object

Explanation: `setSeconds()` sets the seconds (0–59) of a Date object.



(9) How can you create a Date object representing January 1, 2023?

A) `new Date('2023-01-01')`
B) `new Date(2023, 0, 1)`
C) `new Date('Jan 1, 2023')`
D) All of the above

Answer: D) All of the above

Explanation: All three formats will create a Date object representing January 1, 2023.



(10) How can you specify the current date and time in UTC?

A) `new Date().toUTCString()`
B) `new Date().getUTCDate()`
C) `new Date().getUTCFullYear()`
D) `new Date().toString()`

Answer: A) `new Date().toUTCString()`

Explanation: `toUTCString()` converts a Date object to a string, using UTC time zone.



(Chap # 34 - Changing elements of date and time)

(1) How can you change the year of a Date object?

A) `setYear()`
B) `setFullYear()`
C) `setDate()`
D) `setMonth()`

Answer: B) `setFullYear()`

Explanation: `setFullYear()` is used to change the year of a Date object.



(2) Which method would you use to change the day of the month in a Date object?

A) `setDate()`
B) `setDay()`
C) `setMonth()`
D) `setFullYear()`

Answer: A) `setDate()`

Explanation: `setDate()` allows you to set the day of the month (1-31) for a Date object.



(3) What method is used to set the minutes of a Date object?

A) `setMinutes()`
B) `setTime()`
C) `setHours()`
D) `setDate()`

Answer: A) `setMinutes()`

Explanation: `setMinutes()` allows you to set the minutes (0–59) of a Date object.



(4) How would you change the month of a Date object to December?

A) `setMonth(11)`
B) `setMonth(12)`
C) `setMonth('December')`
D) `setMonth('Dec')`

Answer: A) `setMonth(11)`

Explanation: Months are zero-indexed in JavaScript, so December corresponds to index 11.



(5) Which method would you use to update the time (hours, minutes, seconds) of a Date object?

A) `setHours()`
B) `setMinutes()`
C) `setSeconds()`
D) `setTime()`

Answer: A) `setHours()`

Explanation: `setHours()` is used to set the hours, minutes, and seconds of a Date object.



(6) How can you set the seconds of a Date object to 45?

A) `setSeconds(45)`
B) `setMilliseconds(45)`
C) `setTime(45)`
D) `setDate(45)`

Answer: A) `setSeconds(45)`

Explanation: `setSeconds()` sets the seconds of a Date object (0–59).



(7) If the current date is `2025-06-15`, what will the code `myDate.setDate(10)` do?

A) It will set the day to June 10, 2025
B) It will set the month to June 10, 2025
C) It will set the year to June 10, 2025
D) It will cause an error

Answer: A) It will set the day to June 10, 2025

Explanation: `setDate(10)` will set the day of the month to 10, without changing the month or year.



(8) Which method can be used to set both the date and time (hours, minutes, and seconds) in a Date object?

A) `setDateTime()`
B) `setTime()`
C) `setFullDate()`
D) `setHours()`

Answer: B) `setTime()`

Explanation: `setTime()` can be used to set the Date object to a specific time value (milliseconds since January 1, 1970).



(9) How would you change the year of a Date object to 2027?

A) `setYear(2027)`
B) `setFullYear(2027)`
C) `setMonth(2027)`
D) `setDate(2027)`

Answer: B) `setFullYear(2027)`

Explanation: `setFullYear()` is used to set the full year, including the century.



(10) How can you set a specific time, such as 2:30 PM, on a Date object?

A) `setHours(14, 30)`
B) `setTime(14, 30)`
C) `setMinutes(14, 30)`
D) `setHours(2, 30)`

Answer: A) `setHours(14, 30)`

Explanation: `setHours()` accepts parameters for hour and minute, where 14 corresponds to 2:00 PM in 24-hour format.



(Chap # 35 - Functions )

(1) What is the purpose of a function in JavaScript?

A) To store data
B) To perform a specific task or operation
C) To loop through arrays
D) To define variables

Answer: B) To perform a specific task or operation

Explanation: Functions are used to group code that performs a specific task, which can be executed when called.



(2) How do you define a function in JavaScript?

A) `function myFunction { }`
B) `function myFunction() { }`
C) `function: myFunction() { }`
D) `myFunction() { }`

Answer: B) `function myFunction() { }`

Explanation: A function is defined using the `function` keyword, followed by its name and a pair of parentheses.



(3) What is the syntax to call a function in JavaScript?

A) `call myFunction()`
B) `myFunction{}`
C) `myFunction()`
D) `call() myFunction`

Answer: C) `myFunction()`

Explanation: Functions are called by using their name followed by parentheses: `myFunction()`.



(4) How can a function return a value in JavaScript?

A) `return value;`
B) `function(value)`
C) `output value;`
D) `function return(value)`

Answer: A) `return value;`

Explanation: The `return` statement is used to return a value from a function.



(5) Which of the following is the correct way to call a function with parameters in JavaScript?

A) `functionName(parameter1, parameter2)`
B) `functionName(parameter1 + parameter2)`
C) `parameter1, parameter2.functionName()`
D) `parameter1, parameter2: functionName()`

Answer: A) `functionName(parameter1, parameter2)`

Explanation: Functions with parameters are called by passing the arguments inside parentheses, separated by commas.



(6) How do you define a function expression in JavaScript?

A) `var myFunc = function() { }`
B) `function myFunc = () { }`
C) `function myFunc() { return; }`
D) `myFunc = function() { }`

Answer: A) `var myFunc = function() { }`

Explanation: A function expression is created by assigning an anonymous function to a variable.



(7) What is a callback function in JavaScript?

A) A function that is called when an event occurs
B) A function that is called after another function completes
C) A function that returns data
D) A function that calls itself

Answer: B) A function that is called after another function completes

Explanation: A callback function is passed as an argument to another function and is executed after that function finishes.



(8) Which keyword is used to define a function in JavaScript?

A) `def`
B) `func`
C) `function`
D) `define`

Answer: C) `function`

Explanation: The `function` keyword is used to define a function in JavaScript.



(9) How would you define a function that takes a number and returns its square?

A) `function square(num) { return num * 2; }`
B) `function square(num) { return num * num; }`
C) `function square(num) { return num + num; }`
D) `function square(num) { return num - num; }`

Answer: B) `function square(num) { return num * num; }`

Explanation: The correct way to calculate the square of a number is to multiply it by itself: `num * num`.



(10) What does a function without a `return` statement do?

A) It will not execute
B) It will return `undefined`
C) It will throw an error
D) It will return `null`

Answer: B) It will return `undefined`

Explanation: If a function does not have a `return` statement, it implicitly returns `undefined`.



(11) What is the scope of a variable declared inside a function?

A) Global
B) Local
C) Both global and local
D) It depends on the function type

Answer: B) Local

Explanation: Variables declared inside a function are local to that function and cannot be accessed outside it.



(12) What happens if a function does not have a return statement?

A) The function will throw an error
B) The function will return `undefined`
C) The function will return `null`
D) The function will stop executing

Answer: B) The function will return `undefined`

Explanation: If a function does not explicitly return a value, it will implicitly return `undefined`.



(13) Which of the following is a feature of JavaScript functions?

A) Functions can only return a single value
B) Functions cannot accept parameters
C) Functions can be called recursively
D) Functions cannot call other functions

Answer: C) Functions can be called recursively

Explanation: Functions in JavaScript can call themselves, a process known as recursion, to solve problems.



(14) What is a function expression in JavaScript?

A) A function that is declared as a variable
B) A function that cannot return values
C) A function that is invoked immediately
D) A function that is called by another function

Answer: A) A function that is declared as a variable

Explanation: A function expression involves assigning a function to a variable, which can then be called using that variable.



(15) What is the result of calling a function before it is defined using a function expression?

A) The function will throw an error
B) The function will execute successfully
C) The function will return `undefined`
D) The function will not be accessible

Answer: D) The function will not be accessible

Explanation: Function expressions are not hoisted, meaning the function is not accessible before its definition.



(16) What will the following code output?

function add(x, y) {
   return x + y;
}
console.log(add(5, 3));


A) 5
B) 3
C) 8
D) NaN

Answer: C) 8

Explanation: The function `add()` adds 5 and 3, returning 8, which is logged to the console.



(17) What will this code return?

function multiply(a, b) {
   return a * b;
}
console.log(multiply(4, '5'));


A) 45
B) 20
C) NaN
D) Error

Answer: B) 20

Explanation: JavaScript coerces the string '5' into a number, so 4 * 5 equals 20.



(18) What will the following code return?

function greet(name) {
   return 'Hello, ' + name;
}
console.log(greet('Alice'));


A) "Hello, Alice"
B) "Alice"
C) "Hello, " + Alice
D) undefined

Answer: A) "Hello, Alice"

Explanation: The function `greet()` concatenates 'Hello, ' with the string 'Alice', returning "Hello, Alice".



(19) What will the following code output?

function sum(arr) {
   return arr.reduce((acc, val) => acc + val, 0);
}
console.log(sum([1, 2, 3, 4]));


A) 10
B) 6
C) [1, 2, 3, 4]
D) Error

Answer: A) 10

Explanation: The `sum()` function adds all elements in the array, so 1 + 2 + 3 + 4 equals 10.



(20) What will this code return?

function calculateArea(radius) {
   return Math.PI * radius * radius;
}
console.log(calculateArea(3));


A) 28.27
B) 9.42
C) 6.28
D) 18.85

Answer: A) 28.27

Explanation: The function calculates the area of a circle using the formula π * r². For r = 3, it returns approximately 28.27.



(21) What will this code output?

function square(x) {
   return x * x;
}
console.log(square(4));


A) 8
B) 16
C) 4
D) 12

Answer: B) 16

Explanation: The function `square()` calculates the square of 4, which is 16.



(22) What will the following code return?

function isEven(num) {
   return num % 2 === 0;
}
console.log(isEven(7));


A) true
B) false
C) undefined
D) Error

Answer: B) false

Explanation: Since 7 is not divisible by 2, the function returns false.



(23) What will the following code output?

function greet(name = "Guest") {
   return 'Hello, ' + name;
}
console.log(greet());


A) "Hello, Guest"
B) "Hello, undefined"
C) "Hello, null"
D) Error

Answer: A) "Hello, Guest"

Explanation: Since no argument is passed, the default value "Guest" is used.



(24) What will this code return?

function multiply(a, b = 2) {
   return a * b;
}
console.log(multiply(3));


A) 3
B) 6
C) 1
D) Error

Answer: B) 6

Explanation: Since `b` is not passed, it defaults to 2, so 3 * 2 equals 6.



(25) What will this code return?

function subtract(a, b) {
   return a - b;
}
console.log(subtract(5, 3));


A) 8
B) 2
C) -2
D) 0

Answer: B) 2

Explanation: The function subtracts 3 from 5, returning the result 2.



(26) What will be the output of this code?

function foo(a, b) {
   arguments[1] = 5;
   return a + b;
}
console.log(foo(2, 3));


A) 7
B) 5
C) NaN
D) Error

Answer: A) 7

Explanation: The `arguments` object modifies `b` (which is 3) to 5, so the function returns 2 + 5 = 7.



(27) What will be the output of this code?

function test(x, x) {
   return x;
}
console.log(test(5, 10));


A) 5
B) 10
C) undefined
D) Error

Answer: B) 10

Explanation: JavaScript does not allow duplicate parameter names. The second `x` overwrites the first one, so the function returns 10.



(28) What will the following code output?

function add(a, b = a) {
   return a + b;
}
console.log(add(4));


A) 4
B) 8
C) NaN
D) Error

Answer: B) 8

Explanation: Since `b` is not passed, it defaults to `a`, so the function returns 4 + 4 = 8.



(29) What will be the output of this code?

function bar() {
   console.log(arguments.length);
}
bar(1, 2, 3);


A) 1
B) 2
C) 3
D) Error

Answer: C) 3

Explanation: The `arguments.length` property returns the number of arguments passed to the function, which in this case is 3.



(30) What will this code output?

function sum(a, b) {
   return a + b;
}
var add = sum;
console.log(add(5, 3));


A) 5
B) 3
C) 8
D) Error

Answer: C) 8

Explanation: The function `sum` is assigned to the variable `add`, and calling `add(5, 3)` returns the sum 5 + 3 = 8.



(Chap # 36 - Functions: passing them data)

(1) What does it mean to pass data to a function?

A) Creating a new variable inside the function
B) Sending values into the function for use
C) Returning a value from the function
D) Declaring a loop inside a function

Answer: B) Sending values into the function for use

Explanation: Passing data means providing values (arguments) to a function's parameters so it can operate on them.



(2) What will be displayed by this code?

function greet(name) {
  alert("Hello, " + name + "!");
}
greet("Ali");


A) Hello, name!
B) Hello, Ali!
C) name
D) Ali

Answer: B) Hello, Ali!

Explanation: The string "Ali" is passed as an argument to the parameter `name`, which is then used in the alert.



(3) What are parameters in a function?

A) The return values
B) The variables used for loops
C) The placeholders used to receive values
D) The data types used in the function

Answer: C) The placeholders used to receive values

Explanation: Parameters are variables listed in the function definition that receive values when the function is called.



(4) What will this output?

function add(a, b) {
  alert(a + b);
}
add(5, 3);


A) 8
B) 53
C) a + b
D) Error

Answer: A) 8

Explanation: The numbers 5 and 3 are passed to the parameters `a` and `b`, and their sum is alerted.



(5) What happens if you call a function with fewer arguments than parameters?

A) An error is thrown
B) Extra parameters become 0
C) Extra parameters become undefined
D) Extra parameters are skipped

Answer: C) Extra parameters become undefined

Explanation: In JavaScript, if fewer arguments are passed, the remaining parameters are automatically set to `undefined`.



(6) What will this code display?

function multiply(x, y) {
  alert(x * y);
}
multiply(4);


A) 4
B) 0
C) undefined
D) NaN

Answer: D) NaN

Explanation: Only one argument is passed, so y becomes undefined. 4 * undefined results in NaN (Not a Number).



(7) What will be the output?

function greet(name, message) {
  alert(message + ", " + name);
}
greet("Sara", "Welcome");


A) Sara, Welcome
B) Welcome, Sara
C) name, message
D) Welcome Sara

Answer: B) Welcome, Sara

Explanation: The order of arguments matters. "Welcome" is passed to `message`, and "Sara" to `name`, forming "Welcome, Sara".



(8) What will this code display?

function test(a, b, c) {
  alert(a + b + c);
}
test(2, "3", 4);


A) 9
B) 234
C) 77
D) 234 (as a string)

Answer: D) 234 (as a string)

Explanation: JavaScript evaluates left to right. 2 + "3" becomes "23" (string), and "23" + 4 becomes "234".



(9) How many parameters can a function have in JavaScript?

A) Only one
B) Two
C) Unlimited
D) None

Answer: C) Unlimited

Explanation: JavaScript functions can accept any number of parameters, separated by commas.



(10) What is wrong with this function call?

function greet(name) {
  alert("Hi " + name);
}
greet();


A) Nothing, it works but shows “Hi undefined”
B) Syntax error
C) Alert will show “Hi null”
D) greet is not defined

Answer: A) Nothing, it works but shows “Hi undefined”

Explanation: Since no argument is passed, `name` is `undefined`, so the alert shows “Hi undefined”.



(Chap # 37 - Functions:passing data back from them )

(1) What keyword is used to return a value from a function?

A) output
B) give
C) return
D) send

Answer: C) return

Explanation: The `return` keyword is used in JavaScript to pass a value back from a function to where it was called.



(2) What will be the output of the following code?

function add(x, y) {
  return x + y;
}
var result = add(2, 3);
alert(result);


A) 2 + 3
B) 5
C) undefined
D) Error

Answer: B) 5

Explanation: The function adds 2 and 3 and returns the result, which is then stored in `result` and alerted.



(3) What happens if a function has a return statement but no value?

A) It returns 0
B) It causes an error
C) It returns undefined
D) It returns null

Answer: C) It returns undefined

Explanation: A return without a value results in `undefined` being returned.



(4) What will this code output?

function greet() {
  return "Hello";
}
alert(greet());


A) greet
B) Hello
C) undefined
D) function greet()

Answer: B) Hello

Explanation: The function returns the string "Hello", and `alert(greet())` displays it.



(5) What will be returned by this function?

function test() {
  return;
}
var output = test();


A) 0
B) null
C) undefined
D) Error

Answer: C) undefined

Explanation: A `return` statement with no value returns `undefined` in JavaScript.



(6) Can a function return the result of an expression?

A) Yes
B) No
C) Only in strict mode
D) Only if it uses global variables

Answer: A) Yes

Explanation: Functions can return the result of any valid JavaScript expression.



(7) What will this code display?

function double(num) {
  return num * 2;
}
alert(double(6));


A) 12
B) 6
C) NaN
D) undefined

Answer: A) 12

Explanation: 6 is passed to the function, which returns 6 * 2 = 12.



(8) What is the main purpose of returning a value from a function?

A) To alert the user
B) To assign it to a variable or use it in an expression
C) To log information
D) To exit a loop

Answer: B) To assign it to a variable or use it in an expression

Explanation: Returned values are usually stored or used elsewhere in the program.



(9) What will be the output?

function square(x) {
  return x * x;
}
console.log(square(3));


A) 6
B) 9
C) 3
D) Error

Answer: B) 9

Explanation: 3 * 3 equals 9, which is returned and logged to the console.



(10) If a function returns a value, but it's not stored or used, what happens?

A) The program crashes
B) The value is lost
C) The value is automatically stored
D) It becomes global

Answer: B) The value is lost

Explanation: If the return value isn’t assigned or used, it is simply discarded after being returned.



(Chap # 38 - Functions: local vs. global variables )

(1) What is a local variable in JavaScript?

A) A variable declared outside any function
B) A variable used in the browser
C) A variable declared inside a function
D) A variable shared between multiple files

Answer: C) A variable declared inside a function

Explanation: Local variables are declared inside a function and can only be accessed within that function.



(2) What is a global variable?

A) A variable declared using `let`
B) A variable declared inside a loop
C) A variable declared outside any function
D) A variable that starts with "g_"

Answer: C) A variable declared outside any function

Explanation: Global variables are accessible anywhere in the code after their declaration.



(3) What will this code output?

var name = "Ali";
function greet() {
  var name = "Sara";
  alert(name);
}
greet();


A) Ali
B) Sara
C) undefined
D) Error

Answer: B) Sara

Explanation: The local variable `name` inside the function shadows the global one.



(4) What will this code display?

var age = 30;
function showAge() {
  alert(age);
}
showAge();


A) 30
B) undefined
C) Error
D) 0

Answer: A) 30

Explanation: The function accesses the global variable `age` since no local variable of that name exists.



(5) Which variable has higher priority inside a function?

A) Global variable
B) Local variable
C) Undefined variable
D) Constant variable

Answer: B) Local variable

Explanation: Local variables override global variables with the same name inside the function.



(6) What is the scope of a local variable?

A) Global
B) Only the function it's declared in
C) The whole script
D) The browser window

Answer: B) Only the function it's declared in

Explanation: Local variables can only be accessed within the function where they were defined.



(7) What will happen if you try to access a local variable outside the function?

A) It will work
B) It returns 0
C) It causes an error
D) It returns undefined

Answer: C) It causes an error

Explanation: Trying to access a local variable outside its function leads to a reference error.



(8) What will this code alert?

var message = "Hi";
function test() {
  message = "Hello";
}
test();
alert(message);


A) Hi
B) Hello
C) undefined
D) Error

Answer: B) Hello

Explanation: The global variable `message` is modified inside the function.



(9) How can you prevent a function from accidentally changing a global variable?

A) By using `return`
B) By declaring the variable locally using `var`, `let`, or `const`
C) By using `alert`
D) By wrapping the code in `if` condition

Answer: B) By declaring the variable locally using `var`, `let`, or `const`

Explanation: Declaring a variable locally ensures it doesn't interfere with global variables of the same name.



(10) What will this code display?

var x = 5;
function change() {
  var x = 10;
}
change();
alert(x);


A) 10
B) 5
C) undefined
D) NaN

Answer: B) 5

Explanation: The function creates a local variable `x`, so the global `x` remains unchanged.



(Chap # 39 - switch statements: how to start them)

(1) What is the purpose of a switch statement in JavaScript?

A) To loop through code
B) To handle multiple conditions
C) To declare variables
D) To stop execution

Answer: B) To handle multiple conditions

Explanation: A switch statement evaluates an expression and matches it with multiple possible cases, providing an efficient way to handle various conditions.



(2) What keyword begins a switch block?

A) case
B) break
C) switch
D) default

Answer: C) switch

Explanation: The switch statement begins with the `switch` keyword, followed by an expression in parentheses.



(3) What will the following code output?

var day = "Monday";
switch (day) {
  case "Monday":
    alert("Start of the week");
    break;
  case "Friday":
    alert("Weekend soon");
    break;
}


A) Start of the week
B) Weekend soon
C) undefined
D) Error

Answer: A) Start of the week

Explanation: The expression matches "Monday", so that case is executed.



(4) Which of the following is correct syntax for a switch statement?

A) switch = (value) {}
B) switch (value) []
C) switch (value) {}
D) switch: (value) {}

Answer: C) switch (value) {}

Explanation: The correct syntax uses the keyword `switch`, followed by parentheses and curly braces for the block.



(5) What is the role of the `case` keyword?

A) It ends a switch block
B) It compares values within a switch
C) It defines a condition in an if statement
D) It is used in loops

Answer: B) It compares values within a switch

Explanation: Each `case` in a switch block represents a potential match for the expression being evaluated.



(6) What happens if no `break` is used in a switch case?

A) Only the matched case is executed
B) All remaining cases are skipped
C) All remaining cases after the match are executed
D) The switch block throws an error

Answer: C) All remaining cases after the match are executed

Explanation: Without `break`, JavaScript performs fall-through and executes the matched case and all subsequent ones until it encounters a break or the end.



(7) What will be the output?

var num = 2;
switch (num) {
  case 1:
    alert("One");
    break;
  case 2:
    alert("Two");
    break;
  default:
    alert("Other");
}


A) One
B) Two
C) Other
D) undefined

Answer: B) Two

Explanation: The value `2` matches the second case, so "Two" is displayed.



(8) Where is the `default` case used in a switch statement?

A) At the beginning
B) Only inside a loop
C) To handle unmatched cases
D) To break the switch block

Answer: C) To handle unmatched cases

Explanation: The `default` case runs if none of the case values match the switch expression.



(9) Can you use strings in switch expressions?

A) No, only numbers are allowed
B) Yes, but only in case labels
C) Yes, both switch and case can use strings
D) Only characters are allowed

Answer: C) Yes, both switch and case can use strings

Explanation: JavaScript allows both numbers and strings in switch expressions and case values.



(10) What will this code display?

var mood = "happy";
switch (mood) {
  case "sad":
    alert("Cheer up!");
    break;
  default:
    alert("Enjoy your day!");
}


A) Cheer up!
B) sad
C) happy
D) Enjoy your day!

Answer: D) Enjoy your day!

Explanation: Since no case matches "happy", the default case is executed.



(Chap # 40 - switch statements: how to complete them?)

(1) What is the purpose of the `break` statement in a switch block?

A) To skip the current iteration
B) To exit the switch block
C) To continue the next case
D) To restart the program

Answer: B) To exit the switch block

Explanation: The `break` statement stops execution inside the switch block once a matching case has been executed.



(2) What happens if you omit `break` in a case?

A) Only one case runs
B) It throws an error
C) All subsequent cases run until a break or end is found
D) The switch stops automatically

Answer: C) All subsequent cases run until a break or end is found

Explanation: Without `break`, JavaScript continues executing all cases below the matched one (fall-through).



(3) Which case is executed when none of the cases match and a default case is provided?

A) The first case
B) The last case
C) The default case
D) None

Answer: C) The default case

Explanation: The default case is executed when no matching case is found.



(4) What will this code output?

var food = "pizza";
switch (food) {
  case "burger":
    alert("Burger time!");
    break;
  case "pizza":
    alert("Pizza party!");
    break;
  default:
    alert("Something else!");
}


A) Burger time!
B) Pizza party!
C) Something else!
D) undefined

Answer: B) Pizza party!

Explanation: The case for "pizza" matches, so "Pizza party!" is displayed.



(5) Is the `default` case required in every switch statement?

A) Yes
B) No
C) Only when using strings
D) Only when using numbers

Answer: B) No

Explanation: The `default` case is optional but recommended to handle unmatched values.



(6) What will this code display?

var color = "blue";
switch (color) {
  case "red":
    alert("Stop");
  case "green":
    alert("Go");
  default:
    alert("Unknown color");
}


A) Stop
B) Go
C) Unknown color
D) Go, then Unknown color

Answer: C) Unknown color

Explanation: No case matches "blue", so only the default case is executed.



(7) When does `fall-through` occur in a switch statement?

A) When using strings
B) When using numbers
C) When there is no `break` after a case
D) When the default case is first

Answer: C) When there is no `break` after a case

Explanation: Fall-through happens when a matched case does not include `break`, causing the next case(s) to execute.



(8) What is the result of this code?

var grade = "C";
switch (grade) {
  case "A":
    alert("Excellent");
    break;
  case "B":
    alert("Good");
    break;
  case "C":
    alert("Average");
    break;
  default:
    alert("Invalid"); }


A) Excellent
B) Good
C) Average
D) Invalid

Answer: C) Average

Explanation: The grade matches case "C", so "Average" is shown.



(9) How many default cases are allowed in one switch block?

A) None
B) Only one
C) As many as needed
D) One per case

Answer: B) Only one

Explanation: Only one `default` case is allowed, and it should be unique in the switch block.



(10) What is the correct order in a switch statement?

A) switch → default → case → break
B) case → switch → break → default
C) switch → case → break → default
D) switch → break → case → default

Answer: C) switch → case → break → default

Explanation: The switch block starts with `switch`, then has one or more `case`s with optional `break`s, and may end with a `default` case.



⏪ ⏩

Prepared by "Ismail Shah"