Answer: A) The current date and time
Explanation: `Date()` returns the current date and time according to the system's time zone.
Answer: B) `getFullYear()`
Explanation: The method `getFullYear()` returns the current year of the Date object.
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.
Answer: A) `getDay()`
Explanation: `getDay()` returns the day of the week as a number (0 for Sunday, 1 for Monday, etc.).
Answer: A) `getMonth()`
Explanation: `getMonth()` returns the month (from 0 to 11), where 0 represents January and 11 represents December.
Answer: C) The number of milliseconds since January 1, 1970
Explanation: `getTime()` returns the number of milliseconds since the Unix epoch (January 1, 1970).
Answer: A) `getDate()`
Explanation: `getDate()` returns the day of the month (1–31) for the specified date.
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).
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").
Answer: A) The milliseconds of the current time
Explanation: `getMilliseconds()` returns the milliseconds portion of the time (0–999) for the current Date object.
Answer: B) `getHours()`
Explanation: `getHours()` returns the hour (0–23) of the specified Date object.
Answer: A) `getMinutes()`
Explanation: `getMinutes()` returns the minutes (0–59) of the specified Date object.
Answer: A) The current seconds
Explanation: `getSeconds()` returns the seconds (0–59) of the specified Date object.
Answer: B) `getDate()`
Explanation: `getDate()` returns the day of the month (1–31) of the specified Date object.
Answer: A) `getMonth()`
Explanation: `getMonth()` returns the month (0–11) of the specified Date object, where 0 is January and 11 is December.
Answer: B) `getFullYear()`
Explanation: `getFullYear()` returns the four-digit year (e.g., 2023) of the specified Date object.
Answer: A) It returns the current milliseconds.
Explanation: `getMilliseconds()` returns the milliseconds (0–999) from the current Date object.
Answer: A) `getDay()`
Explanation: `getDay()` returns the day of the week (0–6), where 0 is Sunday and 6 is Saturday.
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.
Answer: D) `getHours(), getMinutes(), getSeconds()`
Explanation: You can use `getHours()`, `getMinutes()`, and `getSeconds()` to extract the current time from a Date object.
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')`.
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.
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)`.
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.
Answer: B) `setFullYear()`
Explanation: `setFullYear()` allows you to set the full year, including the century (e.g., 2025).
Answer: A) `new Date()`
Explanation: `new Date()` creates a Date object representing the current date and time.
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).
Answer: A) Sets the seconds of a Date object
Explanation: `setSeconds()` sets the seconds (0–59) of a Date object.
Answer: D) All of the above
Explanation: All three formats will create a Date object representing January 1, 2023.
Answer: A) `new Date().toUTCString()`
Explanation: `toUTCString()` converts a Date object to a string, using UTC time zone.
Answer: B) `setFullYear()`
Explanation: `setFullYear()` is used to change the year of a Date object.
Answer: A) `setDate()`
Explanation: `setDate()` allows you to set the day of the month (1-31) for a Date object.
Answer: A) `setMinutes()`
Explanation: `setMinutes()` allows you to set the minutes (0–59) of a Date object.
Answer: A) `setMonth(11)`
Explanation: Months are zero-indexed in JavaScript, so December corresponds to index 11.
Answer: A) `setHours()`
Explanation: `setHours()` is used to set the hours, minutes, and seconds of a Date object.
Answer: A) `setSeconds(45)`
Explanation: `setSeconds()` sets the seconds of a Date object (0–59).
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.
Answer: B) `setTime()`
Explanation: `setTime()` can be used to set the Date object to a specific time value (milliseconds since January 1, 1970).
Answer: B) `setFullYear(2027)`
Explanation: `setFullYear()` is used to set the full year, including the century.
Answer: A) `setHours(14, 30)`
Explanation: `setHours()` accepts parameters for hour and minute, where 14 corresponds to 2:00 PM in 24-hour format.
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.
Answer: B) `function myFunction() { }`
Explanation: A function is defined using the `function` keyword, followed by its name and a pair of parentheses.
Answer: C) `myFunction()`
Explanation: Functions are called by using their name followed by parentheses: `myFunction()`.
Answer: A) `return value;`
Explanation: The `return` statement is used to return a value from a function.
Answer: A) `functionName(parameter1, parameter2)`
Explanation: Functions with parameters are called by passing the arguments inside parentheses, separated by commas.
Answer: A) `var myFunc = function() { }`
Explanation: A function expression is created by assigning an anonymous function to a variable.
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.
Answer: C) `function`
Explanation: The `function` keyword is used to define a function in JavaScript.
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`.
Answer: B) It will return `undefined`
Explanation: If a function does not have a `return` statement, it implicitly returns `undefined`.
Answer: B) Local
Explanation: Variables declared inside a function are local to that function and cannot be accessed outside it.
Answer: B) The function will return `undefined`
Explanation: If a function does not explicitly return a value, it will implicitly return `undefined`.
Answer: C) Functions can be called recursively
Explanation: Functions in JavaScript can call themselves, a process known as recursion, to solve problems.
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.
Answer: D) The function will not be accessible
Explanation: Function expressions are not hoisted, meaning the function is not accessible before its definition.
function add(x, y) {
return x + y;
}
console.log(add(5, 3));
Answer: C) 8
Explanation: The function `add()` adds 5 and 3, returning 8, which is logged to the console.
function multiply(a, b) {
return a * b;
}
console.log(multiply(4, '5'));
Answer: B) 20
Explanation: JavaScript coerces the string '5' into a number, so 4 * 5 equals 20.
function greet(name) {
return 'Hello, ' + name;
}
console.log(greet('Alice'));
Answer: A) "Hello, Alice"
Explanation: The function `greet()` concatenates 'Hello, ' with the string 'Alice', returning "Hello, Alice".
function sum(arr) {
return arr.reduce((acc, val) => acc + val, 0);
}
console.log(sum([1, 2, 3, 4]));
Answer: A) 10
Explanation: The `sum()` function adds all elements in the array, so 1 + 2 + 3 + 4 equals 10.
function calculateArea(radius) {
return Math.PI * radius * radius;
}
console.log(calculateArea(3));
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.
function square(x) {
return x * x;
}
console.log(square(4));
Answer: B) 16
Explanation: The function `square()` calculates the square of 4, which is 16.
function isEven(num) {
return num % 2 === 0;
}
console.log(isEven(7));
Answer: B) false
Explanation: Since 7 is not divisible by 2, the function returns false.
function greet(name = "Guest") {
return 'Hello, ' + name;
}
console.log(greet());
Answer: A) "Hello, Guest"
Explanation: Since no argument is passed, the default value "Guest" is used.
function multiply(a, b = 2) {
return a * b;
}
console.log(multiply(3));
Answer: B) 6
Explanation: Since `b` is not passed, it defaults to 2, so 3 * 2 equals 6.
function subtract(a, b) {
return a - b;
}
console.log(subtract(5, 3));
Answer: B) 2
Explanation: The function subtracts 3 from 5, returning the result 2.
function foo(a, b) {
arguments[1] = 5;
return a + b;
}
console.log(foo(2, 3));
Answer: A) 7
Explanation: The `arguments` object modifies `b` (which is 3) to 5, so the function returns 2 + 5 = 7.
function test(x, x) {
return x;
}
console.log(test(5, 10));
Answer: B) 10
Explanation: JavaScript does not allow duplicate parameter names. The second `x` overwrites the first one, so the function returns 10.
function add(a, b = a) {
return a + b;
}
console.log(add(4));
Answer: B) 8
Explanation: Since `b` is not passed, it defaults to `a`, so the function returns 4 + 4 = 8.
function bar() {
console.log(arguments.length);
}
bar(1, 2, 3);
Answer: C) 3
Explanation: The `arguments.length` property returns the number of arguments passed to the function, which in this case is 3.
function sum(a, b) {
return a + b;
}
var add = sum;
console.log(add(5, 3));
Answer: C) 8
Explanation: The function `sum` is assigned to the variable `add`, and calling `add(5, 3)` returns the sum 5 + 3 = 8.
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.
Answer: B) Hello, Ali!
Explanation: The string "Ali" is passed as an argument to the parameter `name`, which is then used in the alert.
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.
Answer: A) 8
Explanation: The numbers 5 and 3 are passed to the parameters `a` and `b`, and their sum is alerted.
Answer: C) Extra parameters become undefined
Explanation: In JavaScript, if fewer arguments are passed, the remaining parameters are automatically set to `undefined`.
Answer: D) NaN
Explanation: Only one argument is passed, so y becomes undefined. 4 * undefined results in NaN (Not a Number).
Answer: B) Welcome, Sara
Explanation: The order of arguments matters. "Welcome" is passed to `message`, and "Sara" to `name`, forming "Welcome, Sara".
Answer: D) 234 (as a string)
Explanation: JavaScript evaluates left to right. 2 + "3" becomes "23" (string), and "23" + 4 becomes "234".
Answer: C) Unlimited
Explanation: JavaScript functions can accept any number of parameters, separated by commas.
Answer: A) Nothing, it works but shows “Hi undefined”
Explanation: Since no argument is passed, `name` is `undefined`, so the alert shows “Hi undefined”.
Answer: C) return
Explanation: The `return` keyword is used in JavaScript to pass a value back from a function to where it was called.
Answer: B) 5
Explanation: The function adds 2 and 3 and returns the result, which is then stored in `result` and alerted.
Answer: C) It returns undefined
Explanation: A return without a value results in `undefined` being returned.
Answer: B) Hello
Explanation: The function returns the string "Hello", and `alert(greet())` displays it.
Answer: C) undefined
Explanation: A `return` statement with no value returns `undefined` in JavaScript.
Answer: A) Yes
Explanation: Functions can return the result of any valid JavaScript expression.
Answer: A) 12
Explanation: 6 is passed to the function, which returns 6 * 2 = 12.
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.
Answer: B) 9
Explanation: 3 * 3 equals 9, which is returned and logged to the console.
Answer: B) The value is lost
Explanation: If the return value isn’t assigned or used, it is simply discarded after being returned.
Answer: C) A variable declared inside a function
Explanation: Local variables are declared inside a function and can only be accessed within that function.
Answer: C) A variable declared outside any function
Explanation: Global variables are accessible anywhere in the code after their declaration.
Answer: B) Sara
Explanation: The local variable `name` inside the function shadows the global one.
Answer: A) 30
Explanation: The function accesses the global variable `age` since no local variable of that name exists.
Answer: B) Local variable
Explanation: Local variables override global variables with the same name inside the function.
Answer: B) Only the function it's declared in
Explanation: Local variables can only be accessed within the function where they were defined.
Answer: C) It causes an error
Explanation: Trying to access a local variable outside its function leads to a reference error.
Answer: B) Hello
Explanation: The global variable `message` is modified inside the function.
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.
Answer: B) 5
Explanation: The function creates a local variable `x`, so the global `x` remains unchanged.
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.
Answer: C) switch
Explanation: The switch statement begins with the `switch` keyword, followed by an expression in parentheses.
Answer: A) Start of the week
Explanation: The expression matches "Monday", so that case is executed.
Answer: C) switch (value) {}
Explanation: The correct syntax uses the keyword `switch`, followed by parentheses and curly braces for the block.
Answer: B) It compares values within a switch
Explanation: Each `case` in a switch block represents a potential match for the expression being evaluated.
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.
Answer: B) Two
Explanation: The value `2` matches the second case, so "Two" is displayed.
Answer: C) To handle unmatched cases
Explanation: The `default` case runs if none of the case values match the switch expression.
Answer: C) Yes, both switch and case can use strings
Explanation: JavaScript allows both numbers and strings in switch expressions and case values.
Answer: D) Enjoy your day!
Explanation: Since no case matches "happy", the default case is executed.
Answer: B) To exit the switch block
Explanation: The `break` statement stops execution inside the switch block once a matching case has been executed.
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).
Answer: C) The default case
Explanation: The default case is executed when no matching case is found.
Answer: B) Pizza party!
Explanation: The case for "pizza" matches, so "Pizza party!" is displayed.
Answer: B) No
Explanation: The `default` case is optional but recommended to handle unmatched values.
Answer: C) Unknown color
Explanation: No case matches "blue", so only the default case is executed.
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.
Answer: C) Average
Explanation: The grade matches case "C", so "Average" is shown.
Answer: B) Only one
Explanation: Only one `default` case is allowed, and it should be unique in the switch block.
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.