This section includes 7 InterviewSolutions, each offering curated multiple-choice questions to sharpen your Current Affairs knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
In JavaScript, how do you turn an Object into an Array []? |
|
Answer» let obj = { id: "1", name: "user22", age: "26", work: "programmer" }; //Method 1: Convert the keys to Array using - Object.keys() console.log(Object.keys(obj)); // ["id", "name", "age", "work"] // Method 2 Converts the Values to Array using - Object.values() console.log(Object.values(obj)); // ["1", "user22r", "26", "programmer"] // Method 3 Converts both keys and values using - Object.entries() console.log(Object.entries(obj)); //[["id", "1"],["name", "user22"],["age", "26"],["work", “programmer"]] |
|
| 2. |
Write the code to find the vowels |
|
Answer» const findVowels = str => { let count = 0 const vowels = ['a', 'e', 'i', 'o', 'u'] for(let char of str.toLowerCase()) { if(vowels.includes(char)) { count++ } } return count } |
|
| 3. |
Write the code given If two strings are anagrams of one another, then return true. |
|
Answer» var firstWord = "Deepak"; var secondWord = "Aman"; isAnagram(wordOne, wordTwo); // true function isAnagram(one, two) { //Change both words to lowercase for case insensitivity.. var a = one.toLowerCase(); var b = two.toLowerCase(); // Sort the strings, then combine the array to a string. Examine the outcomes. a = a.split("").sort().join(""); b = b.split("").sort().join(""); return a === b; } |
|
| 4. |
Write the code for dynamically inserting new components. |
|
Answer» <html> <head> <title>inserting new components dynamically</title> <script type="text/javascript"> function addNode () { var newP = document. createElement("p"); var textNode = document.createTextNode(" This is other node"); newP.appendChild(textNode); document.getElementById("parent1").appendChild(newP); } </script> </head> <body> <p id="parent1">firstP<p> </body> </html> |
|
| 5. |
Implement a function that returns an updated array with r right rotations on an array of integers a . |
|
Answer» Example: Given the following array: [2,3,4,5,7] return [4,5,7,2,3] Answer: function rotateRight(arr,rotations){if(rotations == 0) return arr; for(let i = 0; i < rotations;i++){ let element = arr.pop(); arr.unshift(element); } return arr; } rotateRight([2, 3, 4, 5, 7], 3); // Return [4,5,7,2,3] rotateRight([44, 1, 22, 111], 5); // Returns [111,44,1,22] |
|
| 6. |
Write a function that performs binary search on a sorted array. |
|
Answer» function binarySearch(arr,value,startPos,endPos){ if(startPos > endPos) return -1; let middleIndex = Math.floor(startPos+endPos)/2; if(arr[middleIndex] === value) return middleIndex; elsif(arr[middleIndex > value]){ return binarySearch(arr,value,startPos,middleIndex-1); } else{ return binarySearch(arr,value,middleIndex+1,endPos); } } |
|
| 7. |
Guess the output of the following code: |
|
Answer» var x = 23; (function(){ var x = 43; (function random(){ x++; console.log(x); var x = 21; })(); })(); Answer: Output is NaN. var x; // x is hoisted x++; // x is not a number since it is not initialized yet console.log(x); // Outputs NaN x = 21; // Initialization of x } |
|
| 8. |
Guess the outputs of the following code: |
|
Answer» // Code 1: let x= {}, y = {name:"Ronny"},z = {name:"John"}; x[y] = {name:"Vivek"}; x[z] = {name:"Akki"}; console.log(x[y]); // Code 2: function runFunc(){ console.log("1" + 1); console.log("A" - 1); console.log(2 + "-2" + "2"); console.log("Hello" - "World" + 78); console.log("Hello"+ "78"); } runFunc(); // Code 3: let a = 0; let b = false; console.log((a == b)); console.log((a === b)); Answers: Nan 2-22 NaN Hello78 Code 3 - Output in the following order due to equality coercion: truefalse |
|
| 9. |
Guess the outputs of the following codes: |
|
Answer» // Code 1: function func1(){ setTimeout(()=>{ console.log(x); console.log(y); },3000); var x = 2; let y = 12; } func1(); // Code 2: function func2(){ for(var i = 0; i < 3; i++){ setTimeout(()=> console.log(i),2000); } } func2(); // Code 3: (function(){ setTimeout(()=> console.log(1),2000); console.log(2); setTimeout(()=> console.log(3),0); console.log(4); })(); Answers:
4 3 1 // After two seconds Even though the second timeout function has a waiting time of zero seconds, the javascript engine always evaluates the setTimeout function using the Web API, and therefore, the complete function executes before the setTimeout function can execute. |
|