Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let array = [0,1,2,3,4];
- array.__proto__ = null;
- array.pop();
- console.log(array.pop());
- What is the output above snippet in javacsirpt?
- A) 3.
- B) 4.
- C) Error.
- D) undefined.
- The correct answer is:
- C) Error.
- Explanation:
- - The code snippet attempts to set the `__proto__` property of the array to null, effectively removing the array's prototype linkage.
- - After setting `array.__proto__ = null;`, the array loses its connection to the Array prototype methods.
- - When `array.pop()` is called, it results in an error because the `pop()` method is not accessible due to the removal of the prototype linkage.
- - Therefore, attempting to call `array.pop()` after removing the prototype linkage results in a runtime error.
- 2. var index = 0;
- while(index < 5){
- setTimeout(()=>{
- console.log(index);
- },1);
- index++;
- }
- What is the output above snippet?
- A) 0 0 0 0 0.
- B) 0 1 2 3 4.
- C) 4 4 4 4 4.
- D) 5 5 5 5 5.
- The correct answer is:
- D) 5 5 5 5 5.
- Explanation:
- - This code snippet creates a `setTimeout` function inside a while loop.
- - The `setTimeout` function schedules the execution of the provided function (which logs the value of `index`) after a specified delay (1 millisecond in this case).
- - However, the `setTimeout` function does not block the execution of the loop. So, by the time the callback function is executed, the value of `index` will have already reached 5.
- - Therefore, when the callback function executes, it will log the current value of `index`, which is 5, for all iterations of the loop.
- - As a result, the output will be `5 5 5 5 5`.
- 3.
- function example(num){
- {
- let value = num;
- }
- console.log(value);
- var value = 30;
- }
- example(10);
- What is the output above snippet?
- A) 10.
- B) Error, “value is not defined”
- C) undefined.
- D) 30.
- The correct answer is:
- B) Error, “value is not defined”
- Explanation:
- - The `value` variable is declared using `let` inside the `example` function, which means it has block scope and is only accessible within the block where it's declared.
- - Therefore, when `console.log(value)` is called outside the block where `value` is declared, it will result in an error because `value` is not defined in that scope.
- - Hence, the output will be an error stating "value is not defined."
- 4.
- let x = 10;
- function exampleG(){
- console.log(x+x);
- }
- function example(){
- var x = 20;
- exampleG();
- return function(){
- console.log(x+x);
- }
- }
- example()();
- What is the output above snippet?
- A) 20 20.
- B) 40 40.
- C) 40 20.
- D) 20 40.
- The correct answer is:
- C) 40 20.
- Explanation:
- - Inside the `example` function, a local variable `x` with a value of 20 is declared using `var`.
- - When `exampleG()` is called from within `example`, it accesses the global variable `x`, which has a value of 10.
- - So, `console.log(x+x)` inside `exampleG()` outputs `10 + 10 = 20`.
- - Then, the returned function from `example()` is immediately invoked, and it logs the value of the local variable `x` (which is 20) twice, resulting in `20 + 20 = 40` for the second output.
- 5.
- function getName() {
- if (true) {
- let name = 'Raghul';
- if (false) {
- let name = 'Gokul';
- if (true) {
- console.log(name);
- }
- name = undefined;
- }// false end bracket
- console.log(name);
- name = null;
- }// true end bracket
- }
- getName();
- What will be logged to the console after executing the getName() function ?
- A) Raghul
- B) null
- C) undefined
- D) Gokul
- The correct answer is:
- ```
- A) Raghul
- ```
- Explanation:
- - Inside the `getName` function, the first `if` block is always true, so it executes.
- - Within this block, a variable `name` is declared and assigned the value `'Raghul'`.
- - Then, the second `if` block is false, so it does not execute.
- - The `console.log(name)` statement within the first `if` block logs the value of the `name` variable, which is `'Raghul'`.
- - After that, `name` is assigned `null` before the function returns. However, this assignment does not affect the logged value.
- 6.
- **Question:**
- Consider the following JavaScript code snippet:
- ```javascript
- function addition(a) {
- return (b) => {
- return b + a;
- };
- }
- const a = addition(2);
- const b = addition(3);
- console.log(a(7));
- console.log(b(9));
- ```
- What will be the output of the above code snippet?
- A) Throw an error
- B) 7, 9
- C) 8, 9
- D) 9, 12
- **Answer:**
- D) 9, 12
- This JavaScript code defines a function `addition` that takes a parameter `a` and returns a function that takes another parameter `b` and adds `a` to `b`.
- - `const a = addition(2);` creates a closure where `a` is 2, so `a` becomes a function that adds 2 to its argument.
- - `const b = addition(3);` creates another closure where `a` is 3, so `b` becomes a function that adds 3 to its argument.
- When `a(7)` is called, it adds 2 to 7, resulting in 9.
- When `b(9)` is called, it adds 3 to 9, resulting in 12.
- So, the output will be:
- D) 9, 12
- 7.
- Calculate number of days between 2020/08/01 and 2020/01/01 in javascript?
- // Define the two dates
- const date1 = new Date('2020/08/01');
- const date2 = new Date('2020/01/01');
- // Calculate the difference in milliseconds
- const differenceMs = date1 - date2;
- // Convert milliseconds to days
- const daysDifference = Math.floor(differenceMs / (1000 * 60 * 60 * 24));
- console.log('Number of days between the two dates:', daysDifference);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement