Advertisement
satishfrontenddev5

Untitled

Feb 18th, 2024 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let array = [0,1,2,3,4];
  2. array.__proto__ = null;
  3. array.pop();
  4. console.log(array.pop());
  5.  
  6. What is the output above snippet in javacsirpt?
  7.  
  8.     A) 3.
  9.     B) 4.
  10.     C) Error.
  11.     D) undefined.
  12.  
  13. The correct answer is:
  14.  
  15. C) Error.
  16.  
  17. Explanation:
  18. - The code snippet attempts to set the `__proto__` property of the array to null, effectively removing the array's prototype linkage.
  19. - After setting `array.__proto__ = null;`, the array loses its connection to the Array prototype methods.
  20. - 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.
  21. - Therefore, attempting to call `array.pop()` after removing the prototype linkage results in a runtime error.
  22.  
  23.  
  24.  
  25. 2. var index = 0;
  26. while(index < 5){
  27. setTimeout(()=>{
  28.     console.log(index);
  29. },1);
  30. index++;
  31. }
  32.  
  33. What is the output above snippet?
  34.  
  35.  
  36.     A) 0 0 0 0 0.
  37.     B) 0 1 2 3 4.
  38.     C) 4 4 4 4 4.
  39.     D) 5 5 5 5 5.
  40.  
  41. The correct answer is:
  42.  
  43. D) 5 5 5 5 5.
  44.  
  45. Explanation:
  46. - This code snippet creates a `setTimeout` function inside a while loop.
  47. - 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).
  48. - 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.
  49. - Therefore, when the callback function executes, it will log the current value of `index`, which is 5, for all iterations of the loop.
  50. - As a result, the output will be `5 5 5 5 5`.
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57. 3.
  58.  
  59. function example(num){
  60. {
  61. let value = num;
  62. }
  63. console.log(value);
  64. var value = 30;
  65. }
  66. example(10);
  67. What is the output above snippet?
  68.  
  69.     A) 10.
  70.     B) Error, “value is not defined”
  71.     C) undefined.
  72.     D) 30.
  73.  
  74.  
  75. The correct answer is:
  76.  
  77. B) Error, “value is not defined”
  78.  
  79. Explanation:
  80. - 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.
  81. - 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.
  82. - Hence, the output will be an error stating "value is not defined."
  83.  
  84.  
  85. 4.
  86. let x = 10;
  87. function exampleG(){
  88. console.log(x+x);
  89. }
  90.  
  91.  
  92. function example(){
  93. var x = 20;
  94. exampleG();
  95. return function(){
  96.     console.log(x+x);
  97. }
  98. }
  99. example()();
  100.  
  101.  
  102. What is the output above snippet?
  103.  
  104. A)   20 20.
  105. B)  40 40.
  106. C) 40 20.
  107. D) 20 40.
  108.  
  109. The correct answer is:
  110.  
  111. C) 40 20.
  112.  
  113. Explanation:
  114. - Inside the `example` function, a local variable `x` with a value of 20 is declared using `var`.
  115. - When `exampleG()` is called from within `example`, it accesses the global variable `x`, which has a value of 10.
  116. - So, `console.log(x+x)` inside `exampleG()` outputs `10 + 10 = 20`.
  117. - 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.
  118.  
  119.  
  120.  
  121. 5.
  122.  
  123. function getName() {
  124.   if (true) {
  125.     let name = 'Raghul';
  126.     if (false) {
  127.       let name = 'Gokul';
  128.       if (true) {
  129.         console.log(name);
  130.       }
  131.       name = undefined;
  132.     }// false end bracket
  133.     console.log(name);
  134.     name = null;
  135.   }// true end bracket
  136. }
  137.  
  138. getName();
  139.  
  140. What will be logged to the console after executing the getName() function ?
  141.  
  142.     A) Raghul
  143.     B) null
  144.     C) undefined
  145.     D) Gokul
  146.  
  147.  
  148. The correct answer is:
  149.  
  150. ```
  151. A) Raghul
  152. ```
  153.  
  154. Explanation:
  155. - Inside the `getName` function, the first `if` block is always true, so it executes.
  156. - Within this block, a variable `name` is declared and assigned the value `'Raghul'`.
  157. - Then, the second `if` block is false, so it does not execute.
  158. - The `console.log(name)` statement within the first `if` block logs the value of the `name` variable, which is `'Raghul'`.
  159. - After that, `name` is assigned `null` before the function returns. However, this assignment does not affect the logged value.
  160.  
  161.  
  162.  
  163. 6.
  164.  
  165. **Question:**
  166.  
  167. Consider the following JavaScript code snippet:
  168.  
  169. ```javascript
  170. function addition(a) {
  171.   return (b) => {
  172.     return b + a;
  173.   };
  174. }
  175.  
  176. const a = addition(2);
  177. const b = addition(3);
  178. console.log(a(7));
  179. console.log(b(9));
  180. ```
  181.  
  182. What will be the output of the above code snippet?
  183.  
  184. A) Throw an error  
  185. B) 7, 9  
  186. C) 8, 9  
  187. D) 9, 12  
  188.  
  189. **Answer:**  
  190. D) 9, 12
  191.  
  192. 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`.
  193.  
  194. - `const a = addition(2);` creates a closure where `a` is 2, so `a` becomes a function that adds 2 to its argument.
  195. - `const b = addition(3);` creates another closure where `a` is 3, so `b` becomes a function that adds 3 to its argument.
  196.  
  197. When `a(7)` is called, it adds 2 to 7, resulting in 9.  
  198. When `b(9)` is called, it adds 3 to 9, resulting in 12.
  199.  
  200. So, the output will be:
  201.  
  202. D) 9, 12
  203.  
  204. 7.
  205. Calculate number of days between 2020/08/01 and 2020/01/01 in javascript?
  206.  
  207. // Define the two dates
  208. const date1 = new Date('2020/08/01');
  209. const date2 = new Date('2020/01/01');
  210.  
  211. // Calculate the difference in milliseconds
  212. const differenceMs = date1 - date2;
  213.  
  214. // Convert milliseconds to days
  215. const daysDifference = Math.floor(differenceMs / (1000 * 60 * 60 * 24));
  216.  
  217. console.log('Number of days between the two dates:', daysDifference);
  218.  
  219.  
  220.  
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement