Computer Science - Uncommon Quiz Questions In JavaScript For 2022 PDF
Document Details
Uploaded by VeritableTriumph2591
2022
Joseph Wobus, Dr. Biplab Pal
Tags
Summary
This document is a collection of computer science quiz questions focused on JavaScript and programming concepts. It appears to be study material for college-level or high-school advanced placement courses.
Full Transcript
AP QUESTIONS IN JAVASCRIPT FOR COLLEGE TESTS-QUIZ- INTERVIEWS Computer Science (CS) questions are designed to challenge the high school students pursuing AP computer science to excel. The questions are given to test the fundamentals of Java, JavaScript, Python, and object-oriented...
AP QUESTIONS IN JAVASCRIPT FOR COLLEGE TESTS-QUIZ- INTERVIEWS Computer Science (CS) questions are designed to challenge the high school students pursuing AP computer science to excel. The questions are given to test the fundamentals of Java, JavaScript, Python, and object-oriented programming concepts. This volume is covering: v MAP v Stack v Arrays and Loops v Data Type - Object ACKNOWLEDGEMENT Poccomu is incredibly indebted to a large number of Professors and Academicians who have helped us in this project. You are also welcome to be part of Poccomu if you are passionate about the uncommon question. Email us [email protected] Copyright © 2022 by Pal Consulting All rights reserved. No part of this publication may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the publisher, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the publisher, addressed “Attention: Permissions Coordinator,” at the address below. 4500 Stone crest Drive Ellicott City, MD Email: [email protected] Visit: https://poccomu.com Printed in the United States of America About The Authors Joseph Wobus : Joseph Wobus is a current software engineer in the United States. He graduated from University of Maryland, College Park with a B.S. in Computer Science in 2020( CS@UMCP is among top 20 Computer Science Department in International ranking ) , and has been working full time as a software engineer since then. He began coding since early high and school, and has since learned a multitude of languages such as Java, Python, C++, C, JavaScript, and more. During his last 2 years of college, he worked as a TA for one of the advanced CS courses and worked closely with professors in assigning homework, grading tests, and assisting other students debug code. In his free time, he enjoys playing basketball, playing PC games, and going out with friends. Dr. Biplab Pal: Dr. Biplab Pal has a Ph.D. in EE/ECE and MS/BS in Physics. He is CTO & Co-Founder of leading IoT and Healthcare companies Machinesense and Opteev based out of Baltimore, Maryland. He holds more than 30 US patents in IT/IoT/Sensor System areas. He also serves as an Adjunct Research Professor in Information Science at the University of Maryland, at Baltimore Campus (UMBC). Table of Contents AP Tests/Quiz/Interviews: Vol-01 Answers Explanation DEDICATION Poccomu, in Latin, means uncommon. In this world of automation, artificial intelligence (AI), and changing consumer preferences - the workplace is changing rapidly. Are we preparing our next generation to handle those uncommon situations? Will the new graduates be able to handle the uncertainty? Industry leaders are concerned because the present-education system focuses on standardized testing and rote learning, which is where POCCOMU comes in. Poccomu's methodology of logic-based testing of STEM will help keep pace with the unique challenges at the workplace. The idea of Poccomu is to push students to discover the power of their logical thinking using the fundamental concepts they have learned in STEM coursework when they encounter an uncommon problem. Poccomu is here to push students to think out of the box to simplify and solve critical and distinctive problems. Encouraging a young and motivated brain to think out of the box will help them outperform in standardized tests and enhance their mental ability to solve the most mysterious problem in their professional life. AP Tests/Quiz/Interviews: Vol-01 DATA TYPE – OBJECT MAP ARRAYS AND LOOPS STACK 1. In Python, there exists a data type called a dictionary, which is essentially a collection of key-value pairs enclosed within curly braces, which looks something like this: Student = { “name”: “Ferris Bueller”, “school”: “Glenbrook North High School”, “city”: “Chicago” } JavaScript has similar entities but is slightly different. These are called Objects (with a capital ‘O’) in JavaScript. Which of the following is a correct Object definition in JavaScript? A. var Student = { “name”: “Ferris Bueller”, “school”: “Glenbrook North High School”, “city”: “Chicago” } B. var Student = new Object(); Student.name = ‘Ferris Bueller’; Student.school = ‘Glenbrook North High School’; Student.city = ‘Chicago’; C. var Student = { name: “Ferris Bueller”, school: “Glenbrook North High School”, city: “Chicago” } D. All of the above 2. For any given data in JavaScript (properties of an Object in this case), four attributes define it. Identify the four properties correctly from the given list by selecting the correct combination from the given options. I. Iterable II. Enumerable III. Writable IV. Value V. Readable/Writable VI. Accessible A. II, III, V, and VI. B. I, IV, V, and VI. C. I, III, IV, and V. D. None of the above 3. For a simple JavaScript code that updates the number of times people visit your website, let’s say you use a JavaScript data structure to store this information in the following manner: {key: value}, where the key is the first name of the person and the value is the time at which they visited the website. Which data structure is ideal for this kind of situation? (Hint: There are regularly multiple updates (add/delete) operations being carried out on this data structure. A. A Map is better suited for this. B. An Object is better suited for this. C. Neither Object nor Map is suitable. D. Both Object and Map are suitable. 4. The given set of statements contains a blanked-outt word, which will be replaced with either “Object” or “Map”. Identify the correct sequence of the words so that the statements are correctly completed. I. Insertion order for ____ keys was technically undefined before ECMASript2015. II. The number of items in ____ must be determined manually through iteration. III. The keys of ____ do not necessarily have to be either a String or a Symbol. It can be of other data types as well. IV. A JavaScript ____ is not optimized for frequent additions and/or removals of key-value pairs. V. There are no keys by default in ____. A. Object, Map, Map, Object, Object. B. Map, Object, Object, Map, Map. C. Object, Object, Map, Object, Map. D. Map, Map, Object, Object, Map. 5. The given line of code creates a Date object in JavaScript and obtains the current date and time: var current_Date = new Date(); In order to get the date in a format that is readable, a format that looks like this: “12/7/2020, 2:44:25 PM”, which of the following options is correct? A. console.log(current_Date) B. console.log(current_Date.toLocaleString()) C. console.log(current_Date.toString()) D. console.log(Date(current_Date)) 6. The following code contains a JavaScript Map with some movies and their IMDb popularity. Based on the given code, answer the question that follows. movies = new Map(); movies.set("275", "Taxi Driver"); movies.set("35", "Inception"); movies.set("811", "Looper"); movies.set("429", "Aliens"); movies.set("691", "Independence Day"); movies.set("77", "Mad Max: Fury Road"); In order to rank the movies in ascending order of popularity, which of the following lines of code is correct? A. var mapAsc = new Map([...movies.entries()].sort()); B. var mapAsc = new Map([...movies()].sort()); C. var mapAsc = new Map([...movies.keys()].sort()); D. None of the above. 7. Suppose there is a JavaScript entity that stores high school students’ names and GPA scores. Now, this gets updated continuously when the details of new students are added, and every time this list of students and their scores have to be displayed, it is sorted in ascending order of GPA and then displayed. Which of the following is more suitable for creating such a dataset? A. Map B. Object C. Either A or B D. Neither A nor B 8. Find the output of the given code. employees = new Map(); employees.set("Jake", 99); employees.set("Amy", 201); employees.set("Terry", 110); employees.set("Jake", 122); for(var i=0; i=1. Suppose we push N number of integers to it and then pop N integers from it. The push and pop operations will take X and Y minutes, respectively, and Z seconds elapse between each process. If the average life-span of an element is described as the time taken from the end of the first push operation to the start of the pop operation that removes this element from the stack, what is the life-span of the first element in this stack? (All options are in minutes) A. N(X+Z)-Y B. N(Y+Z)-X C. 3X+2Y+Z D. X+N(Y+Z) 13. Suppose you have to implement a First-In Last-Out (FILO) data structure using only a Queue class. How many implementations of the Queue class will be required for that? A. 2 B. 3 C. 4 D. 0 14. You have been provided with a certain number of stacks in your environment (let’s call this number N). You need to implement two different queues using these stacks. What will be the minimum value of N to do so? (Assume that the system works on minimal memory, leading to slower operations if too much memory is used up) A. 6 B. 4 C. 2 D. 1 15. To implement the most basic form of a queue in JavaScript, we can create a Queue function and then apply Array functions to enter and remove elements (ignoring deletion in case the queue is empty). From the given list, identify only two essential functions for this implementation. I. Array.prototype.unshift() II. Array.prototype.shift() III. Array.prototype.length IV. Array.prototype.push() V. Array.prototype.pop() A. II and IV. B. I and IV. C. II and V. D. III and IV. 16. For the given statements, match their numbers correctly with the data structure ideally required for implementation. I. Conversion of mathematical expressions - prefix, infix and postfix. II. Processor scheduling. III. Asynchronous data transfer between two processes. IV. Reversing a string. A. I-Queue, II-Queue, III-Stack, IV-Queue B. I-Stack, II-Queue, III-Stack, IV-Queue C. I-Stack, II-Stack, III-Stack, IV-Queue D. None of the above 17. We have a system that only knows one algorithm to solve problems, which works for only particular issues. The program adopts a specific approach to this algorithm and checks its efficiency. If it is not efficient, it returns to the previous state and tries another approach. It keeps doing this until it finds a good solution. What kind of data structure is ideal for performing this repeated search for an optimal solution? (Hint: It may need to return multiple times to a previous state, which must be stored somewhere.) A. Stack B. Queue C. Binary Search Tree D. All of the above 18. You have been provided with five integers - 3, 24, 20, 12, and 19. The first three are pushed into a stack, and the last two are pushed into a queue in the sequence they have been given. Now, the delete operations (pop for stack and dequeue for queue) are called alternately, starting with the stack until both data structures are empty. Finally, the deleted items are listed in order of deletion. What will be this sequence? A. 3, 24, 20, 12, 19 B. 20, 19, 24, 12, 3 C. 19, 3, 12, 24, 20 D. None of the above. 19. Which of the following statements is incorrect? A. If a stack supports an additional operation, which allows it to reverse the order of elements in it, much like reversing a string, it can be used to implement a First-In, First-Out type of operation. B. Evaluating a mathematical expression without any embedded function calls requires at least two stacks. C. Evaluating a recursive function is an inherent application of stacks. D. None of the above. 20. A sequence of numbers has been provided to you: 1, 2, 3, 5,8, 13, 21, 34. You may recognize this as the first eight integers in the Fibonacci series. You have also been provided with a stack. You need to insert and remove elements from the stack in such a manner that you obtain this series: 3, 2, 1, 8, 5, 34, 21, 13. This is the JavaScript code for the implementation of the same: 1. const Fibonacci = []; 2. Fibonacci.push(1); 3. Fibonacci.push(2); 4. Fibonacci.push(3); 5. Fibonacci.push(5); 6. Fibonacci.push(8); 7. Fibonacci.push(13); 8. Fibonacci.push(21); 9. Fibonacci.push(34); After which line numbers will we need to perform pop operations in order to rearrange the series according to the questions? A. 5 and 9 B. 3, 5 and 7 C. 4, 6 and 9 D. 3, 4, 7 and 9 21. Consider the following statements: I. Method-1 leads to wastage of memory due to an inability to recycle memory. II. Method-2 requires insertion to be performed at the front and deletion to be performed at the rear. III. Method-2 does not require the memory size to be predefined. Considering the above statements, which of the following options is correct about methods 1 and 2? A. Method-1 is the implementation of queues using stacks. B. Method-2 is the implementation of stacks using queues. C. Method-1 is the implementation of a queue using linked lists. D. None of the above. 22. Look at the JavaScript program given below. What kind of data structure does it implement? class DataStruct { constructor() { this.items = []; } insert(element) { this.items.push(element); } delete_one() { if(this.isEmpty()) return "Underflow"; return this.items.shift(); } delete_two() { if (this.items.length === 0) return "Underflow"; return this.items.pop(); } return_last_one() { if(this.isEmpty()) return "No elements"; return this.items; } isEmpty() { return this.items.length === 0; } return_last_two() { return this.items[this.items.length - 1]; } } A. Double-ended queue B. Circular linked list C. Input-restricted double-ended queue D. None of the above 23. Considering the class DataStruct that has been provided in the previous question, can you answer what the given code does? (Assume that the data structure dStruct1 has already been defined and initialized with values) function my_function(num, dStruct1){ dStruct2 = new DataStruct(); var check = 1; var return_last; while(!dStruct1.isEmpty()){ return_last = dStruct1.return_last_one(); dStruct1.delete_one(); if(return_last === check){ check = check + 1; } else { if(dStruct2.isEmpty()) dStruct2.insert(return_last); else if (!dStruct2.isEmpty() && dStruct2.return_last_two() < return_last) return false; else dStruct2.insert(); } while (!dStruct2.isEmpty() && dStruct2.return_last_two() == check) { dStruct2.delete_two(); check = check + 1; } } if(check-1 === num && dStruct2.isEmpty()) return true; return false; } A. Checks if a given queue can be converted into a stack B. Checks if a queue can be sorted into another queue using a stack C. Checks if a stack can be sorted using a queue D. None of the above 24. Identify the incorrect statement from the given options. A. The difference between stacks and queues lies in both insertions and deletion of elements. B. Double-ended queues offer both stack and queue operations. C. Circular queues must always follow the FIFO order of operations. D. All of the above. 25. Some descriptions of queues have been provided below: I. Type-1: This queue type allows us to insert an item at the first node of the queue when the last node is full, and the first node is free. II. Type-2: In this type of queue, insertion takes place at the rear in the order of arrival of the items, while deletion takes place based on the priority of things. III. Type-3: This type of queue allows insertion and deletion from both ends. Now consider some simple situations provided below: a. You need to set up a system of scheduling processes for an operating system, where some techniques are more important than others, based on which they get a more extensive time slice (that does not guarantee that they will finish executing in a one-time slice). b. You need to set up a system to share a single airstrip for landing and take-off. c. Implementing the forward and back buttons on a browser. Which of the following given combinations is correct? A. (a-I), (b-III), (c-I, II) B. (a-I, II), (b-II), (c-III) C. (a-II, III), (b-I, III), (c-II) D. (a-III), (b-I, II), (c-II, III) 26. What will be the output of the given program? class QueueNode { constructor(data1, data2) { this.data1 = data1; this.data2 = data2; } } class Queue { constructor() { this.items = []; } enqueue(data1, data2) { var qNode = new QueueNode(data1, data2); var check = false; for (var i = 0; i < this.items.length; i++) { if (this.items[i].data1 < qNode.data2) { this.items.splice(i, 0, qNode); check = true; break; } } if (!check) { this.items.push(qNode); } } print() { var str = ""; for (var i = 0; i < this.items.length; i++) str += this.items[i].data1 + " "; return str; } } var queue = new Queue(); queue.enqueue(8, 2); queue.enqueue(5, 1); queue.enqueue(3, 1); queue.enqueue(2, 2); queue.enqueue(6, 3); console.log(queue.print()); A. 6 8 2 5 3 B. 5 3 8 2 6 C. 3 5 8 2 6 D. None of the above 27. For the given program, assume that a Queue class has already been defined, which contains the enqueue() and the dequeue() method definitions. What will be the output of the given program? function my_queue_function(value){ var queue = new Queue(); queue.enqueue("0"); while(value--> 0) { var first_value = queue.get_element_from_front_of_queue(); queue.dequeue(); var second_value = first_value; queue.enqueue(first_value + "1"); queue.enqueue(second_value + "0"); value--; } console.log(first_value); } my_queue_function(20); A. 10100 B. 01011 C. 0101 D. Infinite loop 28. A queue has been implemented in the JavaScript code snippet below. Identify the type of en queue () operation that will be suitable, should you not wish to define each node manually. (The options assume non-empty queues.) class Node{ constructor(data){ this.data = data; this.next = null; this.prev = null; } } let node1 = new Node(29); let node2 = new Node(30); node1.next = node2; let node3 = new Node(31); node2.next = node3; let node4 = new Node(32); node3.next = node4; let node5 = new Node(33); node4.next = node5; node1.prev = node5; A. function enqueue(node1, value){ let node_to_be_inserted = new Node(value); let temp = node1; while (true){ temp = temp.next; if(temp.next === node1) break; } temp.next = node_to_be_inserted; node_to_be_inserted.next = node1; } B. function enqueue(node1, value, type){ let node_to_be_inserted = new Node(value); let temp = node1; if(type === "front"){ while(!temp.next) temp = temp.next; temp.next = node_to_be_inserted; } else if(type === "rear"){ node_to_be_inserted.next = node1; node1.prev = node_to_be_inserted; } } C. Both A and B. D. Neither A nor B. 29. Analyze the algorithmic pseudocode provided below (the items in curly braces are variables): FUNCTION: INSERT_INTO_QUEUE(element) ==> PUSH {element} INTO STACK_1 END_FUNCTION FUNCTION: DELETE_FROM_QUEUE ==> IF(STACK_2 is EMPTY) THEN ==> IF(STACK_1 is EMPTY) THEN ==> DISPLAY QUEUE_EMPTY EXIT_METHOD END_IF_BLOCK ELSE ==> LOOP: WHILE (STACK_1 is NOT_EMPTY) ==> REMOVE {top_most_element_STACK_1} FROM STACK_1 GET {top_most_element_STACK_1} FROM STACK_1 SET {element} TO {top_most_element_STACK_1} PUSH {element} INTO STACK_2 END_LOOP END_ELSE_BLOCK END_IF_BLOCK REMOVE {top_most_element_STACK_2} FROM STACK_2 GET {top_most_element_STACK_2} FROM STACK_2 SET {element} TO {top_most_element_STACK_2} END_FUNCTION Considering the pseudocode given above, if you call the first function 6 times continuously, followed by the second function 3 times, how many times will elements be pushed into and popped from some stack, respectively? A. 9 and 16 B. 12 and 9 C. 6 and 13 D. 16 and 10 30. From the pseudocode given above, suppose you had to get exactly 6 pop and 6 push operations. Then, how would you go about structuring your function calls? A. Call the first function 3 times, followed by the second function 4 times. B. Call the second function 6 times, followed by the first function 2 times. C. Call the second function 2 times, followed by the first function 1 time. Now repeat this process 3 times. D. Call the first function once, followed by the second function once. Now repeat this process 3 times. 31. Which of the given statements are incorrect? A. A for-loop can be written without explicitly setting an increment value. B. Do-while loop is an example of an exit-controlled loop in JavaScript. C. It is possible to run a while-loop with a counter variable without decrementing the variable within the loop's body. D. None of the above. 32. For the flowchart given below, can you identify what kind of loop it represents? A. for B. while C. do while D. None of the above 33. Given below is a program that will reverse an input string and log the output to the console: 1. function reverse(str) { 2. if (!str || str.length > 2 || typeof str !== 'string') { 3. return 'Not valid'; 4. } 5. let tempArray = []; 6. const length = str.length; 7. for (let i = length; i 0) { var first_value = queue.front(); queue.dequeue(); var second_value = first_value; queue.enqueue(first_value + "0"); queue.enqueue(second_value + "1"); value--; } console.log(first_value); } myFunc(20); A. 10100 B. 01010 C. 1010 D. 01011 36. Which of the given statements is correct? A. The “===” operator is not preferred for comparisons with “null”. B. It is not necessary to break the last case in a switch block. C. If multiple cases match a case value in a switch-case block, the last case is selected. D. All of the above. 37. Consider the code given below: function myFunction(i) { if (i > 5) return; console.log(i); return myFunction(i++); } myFunction(1); What will be the output? A. Maximum stack value reached. B. No output (with no errors). C. 1 D. None of the above. 38. How would you fill in the blank in the given question so that the program prints “Hello” and then “World”? if([BLANK]) console.log("Hello"); else console.log("World"); A. console.log("Hello") == 5 B. console.log("Hello") && console.log("World") C. console.log("Hello") || 5 D. Both A and B 39. Given below is a simple program in JavaScript that checks whether an input string contains any integers, and removes those from the string. function replaceInt(str){ const intArr = [0,1,2,3,4,5,6,7,8,9]; for(var i=0;i 0) { while(!myFunction(num)) num++ ; listNums.push(num) ; i--; num++ ; } console.log(listNums); A. [1, 1, 2, 3, 5, 8] B. [1, 7, 10, 13, 19, 23] C. [4, 9, 16, 25, 36, 49] D. None of the above 41. Which of the following statement(s) are true? A. Java constructors are invoked when a class is created, even if there are no objects in that particular class. B. Every class in Java will have a constructor, even if it is not explicitly declared. C. Java constructors do not support method overloading. D. All of the above. 42. Predict the output of the given code: class SampleClass{ protected int SampleClass(int x){ System.out.println("Object created"); } } public class MyClass{ public static void main(String[] args) { SampleClass Object1 = new SampleClass(7); } } A. Object Created B. Error: Can’t access the protected object C. Error: Constructor accepts arguments D. Error: Constructor has a return type 43. Which of the following statements are false about object-oriented concepts in Java? I. Execution time polymorphism is achieved only by protected members of a class in Java. II. An object contains an address and takes up some space in memory. III. A class in Java takes up at least 2 bytes of system memory. IV. Method overloading and method overriding are methods to achieve polymorphism in Java. A. I, II and III B. I and III C. II and IV D. II only 44. For the code given below, what type of Polymorphism is illustrated? (Hint: Check the usage of the display function) class Parent{ void display(){ System.out.println("Parent Class()"); } } class Child extends Parent{ void display(){ System.out.println("Child class()"); } } public class MyClass { public static void main(String[] args){ Parent Parent_obj1 = new Parent(); Parent_obj1.display(); Parent Parent_obj2 = new Child(); Parent_obj2.display(); } } A. Compile-time Polymorphism B. Run-time Polymorphism C. Both A and B D. Multiple Polymorphism 45. For the same code as the previous question (provided below once again), identify the OOPS concepts demonstrated. (Hint: If you solved the last question, you already know part of the answer.) class Parent{ void display(){ System.out.println("Parent Class()"); } } class Child extends Parent{ void display(){ System.out.println("Child class()"); } } public class MyClass{ public static void main(String[] args) { Parent Parent_obj1 = new Parent(); Parent_obj1.display(); Parent Parent_obj2 = new Child(); Parent_obj2.display(); } } A. Inheritance and Polymorphism B. Polymorphism and Abstraction C. Abstraction, Encapsulation and Inheritance D. Encapsulation, Abstraction and Polymorphism 46. Find the output of the given program. private class Example{ int num1 = 30, num2 = 5; Example(int num3){ int num = (num1+num2)/num3; System.out.println("Object created" + " " + num); } } public class MyClass{ public static void main(String[] args){ Example Object1 = new Example(7); } } A. Error: Cannot convert from int to double B. 5 C. No output on screen D. None of the above 47. For the code given below (which is a slightly modified version of the code from the previous question), how many output lines will be generated to the output screen? class Example{ int num1 = 30, num2 = 5; protected Example(int num3){ int num = (num1+num2)/num3; System.out.println("Object created" + " " + num); } } class AnotherExample extends Example{ AnotherExample(int x){ super(x); System.out.println("Object created " + x); } } public class MyClass{ public static void main(String[] args){ Example Object1 = new Example(7); AnotherExample Object2 = new AnotherExample(7); } } A. 5 B. 4 C. 3 D. 2 48. Which of the following statements is true? A. Compile-time polymorphism and dynamic polymorphism are one and the same. B. In the case of inheritance, the child class cannot add its own fields and methods in addition to the parent class fields and methods. C. An abstract class cannot have final methods. D. We can create a fully encapsulated class in Java by making all of the data members of the class private. 49. What will be the output for the given program? (This is again a slightly more modified version of the code provided in a couple of the previous questions) class Example{ int num1 = 30, num2 = 5; protected Example(int num3){ int num = (num1+num2)/num3; System.out.println("Object created" + " " + num); } } class AnotherExample extends Example{ AnotherExample(int x){ super(x); System.out.println("Object created " + x); } } class YetAnotherExample extends AnotherExample{ YetAnotherExample(int y){ System.out.println("Object created " + y); } } public class MyClass{ public static void main(String[] args) { Example Object1 = new Example(7); AnotherExample Object2 = new AnotherExample(7); YetAnotherExample Object3 = new YetAnotherExample(5); } } A. Object created 5 Object created 5 Object created 7 Object created 7 Object created 5 Object created 5 B. Object created 5 Object created 5 Object created 7 Object created 7 Object created 5 C. No output D. Error 50. What will be the output of the given program? class SampleClass{ SampleClass(){ System.out.println("Hello World"); } } class Child extends SampleClass{ Child(){ System.out.println("Hello World Again"); } } public class MyClass{ public static void main(String[] args) { Child Object2 = new Child(); } } A. Hello World Hello World Again B. Hello World Again C. Error: super keyword is missing D. No output to screen Answers 1. Answer: D) All of the above 2. Answer: D) None of the above 3. Answer: C) Neither Object nor Map is suitable. 4. Answer: C) Object, Object, Map, Object, Map 5. Answer: B)console.log(current_Date.toLocaleString()) 6. Answer: D) None of the above. 7. Answer: A) Map 8. Answer: A) true false false 9. Answer: B) The values of the keys are not stored in the Map 10. Answer: C) The code will not give the correct output for either an Object or a Map. 11. Answer: D) None of the above. 12. Answer: B) N(Y+Z)-X 13. Answer: A) 2 14. Answer: C) 2 15. Answer: A) II and IV. 16. Answer: D) None of the above. 17. Answer: A) Stack 18. Answer: D) None of the above. 19. Answer: B) The evaluation of a mathematical expression without any embedded function calls requires at least two stacks. 20. Answer: C) 4,6 and 9 21. Answer: D) None of the above 22. Answer: D) None of the above 23. Answer: B) Checks if a queue can be sorted into another queue using a stack 24. Answer: A) The difference between stacks and queues lies in both insertions as well as the deletion of elements. 25. Answer: B) (a-I, II), (b-II), (c-III) 26. Answer: D) None of the above. 27. Answer: C) 0101 28. Answer: D) Neither A nor B 29. Answer: B) 12 and 9 30. Answer: D) Call the first function once, followed by the second function once. Now repeat this process 3 times. 31. Answer: D) None of the above 32. Answer: C) do-while 33. Answer: A) 2,6,7 34. Answer: B) The for…of loop 35. Answer: C) 1010 36. Answer: B) It is not necessary to break the last case in a switch block. 37. Answer: D) None of the above. 38. Answer: D) Both A and B 39. Answer: A) 0 40. Answer: B) [1, 7, 10, 13, 19, 23] 41. Answer: B) Every class in Java will have a constructor, even if it is not explicitly declared. 42. Answer: D) Error: Constructor has a return type 43. Answer: B) I and III 44. Answer: B) Run-time Polymorphism 45. Answer: A) Inheritance and Polymorphism 46. Answer: D) None of the above 47. Answer: C) 3 48. Answer: D) We can create a fully encapsulated class in Java by making all of the data members of the class private. 49. Answer: D) Error 50. Answer: A) Hello World, Hello World Again Explanation 1. EXPLANATION: All three given methods of the declaration are correct. 2. EXPLANATION: The four attributes are valued, writable (this determines whether the value can be changed), enumerable (determines iteration), and configurable. The list does not contain the property “configurable” anywhere, and hence, none of the options are correct. 3. EXPLANATION: In such a situation, there is a high probability of the name of the person repeating. Neither Objects nor Maps in JavaScript are equipped to handle duplicate keys. 4. EXPLANATION: The keys of an Object were not ordered in earlier versions of the ECMAScript, but they are ordered now. Unlike Maps, the number of items has to be calculated for an Object. In a Map, the size property can be used to determine the number of items. A Map can have non-string/Symbol keys. Map keys can be any primitive data types, functions, and even Objects. A Map performs inherently better in situations where multiple add/remove operations are required, unlike Objects. A Map has no keys by default. On the other hand, An Object has a prototype, so it contains default keys. 5. EXPLANATION: The first option will output the date in the raw format, which looks like “2020-12-07T14:44:25.225Z”. The second option converts the raw format to a more human-comprehensible string. The third and the fourth options are actually the same since the output is already in the form of a string, so the “toString()” method becomes redundant here. The output of the last two options will be “Mon Dec 07, 2020, 14:44:25 GMT+0000 (Coordinated Universal Time)” 6. EXPLANATION: The popularity ratings are strings and not integers, so it is impossible to directly sort them into ascending order. The first option is the only option that is syntactically and logically correct. However, it will give the sorted list as: { '275' => 'Taxi Driver', '35' => 'Inception', '429' => 'Aliens', '691' => 'Independence Day', '77' => 'Mad Max: Fury Road', '811' => 'Looper' } 7. EXPLANATION: Since a Map can be iterated over directly, it is better suited to handle scenarios that involve frequent changes to the dataset and operations like sorting, which require iteration in some form. 8. EXPLANATION: A JavaScript Map cannot contain duplicate keys. The second time the key “Jake” was used was invalid, so it was never part of the Map. The Map only contained the first 3 sets. 9. EXPLANATION: Since this method of definition works for both Map and Object, it causes confusion. If this is done for a Map, the keys are not stored in the Map data structure, causing further queries like Map.prototype.has() to fail. 10. EXPLANATION: The correct way to get the number of elements from an Object is to use Object.keys(object_name).length, and for a Map, it will simply be map_name.size. 11. EXPLANATION: The output sequence has been asked for, which is the sequence obtained after popping values - it is the sequence of elements deleted from the stack. The last element is never popped from the stack, so it will never make it to the output sequence. 12. EXPLANATION: The first push operation needs to be discounted since the lifespan of an element starts after it ends. Therefore X has to be subtracted from the operation. Stacks follow a Last-in, First-out (LIFO) structure of operation. Each pop operation takes Y minutes, followed by a Z-minute gap. Therefore, the first element will undergo N(Y+Z) minutes in the stack before being popped. But since the first push operation has to be discounted, the correct expression will be N(Y+Z)-X. 13. EXPLANATION: A FILO data structure is just a stack (FILO is just another way of saying LIFO), which can be implemented using 2 queues by slightly modifying the enqueue (inserting into a queue) and dequeue (deleting from a queue) operations. 14. EXPLANATION: A single queue can be implemented using two stacks. This is the minimum number. To implement the second queue, we can reuse these stacks, which will conserve system memory by avoiding the creation of unnecessary data structures. 15. EXPLANATION: The queue data structure follows a First-In-First- Out (FIFO) format of operation. The Array.prototype.push() method will insert an element at the end of the array while the Array.prototype.shift() method removes an element from the front of the array, simulating the enqueue and dequeue operations, respectively. This preserves the FIFO operation of queues. 16. EXPLANATION: The correct combination is: I-Stack, II-Queue, III- Queue, IV-Stack. None of the given combinations matches; therefore, the answer is the last option. 17. EXPLANATION: The algorithm described in the question is actually the definition of a backtracking algorithm. Suppose the program manages to find the optimal solution in 5 steps. Then for each consecutive step, it will need to store the previous state if the current step fails. If the current step does fail, it will need to revert to the previous state. We need a Last- In-First-Out data structure or a stack for this kind of operation. It will pop the earlier forms in the reverse order in which they were inserted into the stack. 18. EXPLANATION: The elements in the stack will be 20, 24, and 3 from top to bottom, and in the queue will be 19 and 12 from front to the rear. Popping and deleting alternately will give the sequence as 20, 12, 24, 19, and 3. None of the options match. However, a common mistake is to follow LIFO for both the data structures, which makes the second option look correct, but it's not. 19. EXPLANATION: Evaluating a mathematical expression without any embedded function calls requires only one stack. 20. EXPLANATION: Inserting pop operations after these lines will rearrange the series according to the LIFO operation format of stacks, and we will get the series described in the question. 21. EXPLANATION: Consider the given statements. Method-1 leads to wastage of memory due to an inability to recycle system memory. This means that arrays are involved. The second and third statements prove that method-2 consists of the usage of linked lists for some kind of implementation. None of the statements offer any hints about stacks. The methods given are actually for the array and linked-list-based implementation of stacks and queues, respectively. So, none of the given options are correct. 22. EXPLANATION: The given DataStruct class is actually a combination of two different data structures. It can be used to implement both stack as well as queue. The insert() method doubles as an enqueue() function as well as a push() function, the delete_one() and delete_two() methods work as dequeue() and pop() functions respectively. The return_last_one() method returns the front element of a queue while the return_last_two() method returns the top element of a stack. The isEmpty() function works for both stacks as well as queues. 23. EXPLANATION: In order to correctly solve this question, you need to have figured out how the class DataStruct exactly works, which has been given and explained in the previous question. If you already know, then you can figure out that dStruct1 is a queue while dStruct2 is a stack. The return_last variable takes the value of the element in the front of the queue. The second queue, which is supposed to contain the sorted elements from the first queue, takes inputs either from the given queue or stack - which means that the next expected element will be gotten from either the top of the stack or the front of the queue. This process is simulated for the second queue using the check variable. We check whether we can get this element from the front of the given queue or from the top of the given stack. If we cannot take it from either of them, then the front element of the given queue is popped and pushed into the stack. The stack is then sorted after every push operation. The top element of the stack is compared against the element that needs to be pushed. If the top element is less, it cannot be pushed into the stack. Otherwise, it is pushed into the stack. This is done to ensure that the stack always stays sorted. While the stack returns the expected values, they are popped, and the check variable is incremented by 1, as long as the stack is not empty. If the final expected value is the same as the size of the queue, it means that the queue can be sorted into another queue using a stack. Otherwise, the function returns false. 24. EXPLANATION: Stacks and queues differ only in the deletion and not insertion. The insertion operation is the same for both. Since double- ended queues offer operations from both ends, it is quite possible to implement a stack using a double-ended queue. Circular queues always follow FIFO, unlike double-ended queues. 25. EXPLANATION: The types of queues provided are circular, priority, and doubled-ended, respectively. The first situation involves a time- sharing-based scheduling, which means that processes are circularly provided time slices until they finish execution. However, since certain processes are entitled to larger time slices, this will be done using circular and priority queues. The second scenario involves an airstrip. An aircraft trying to land will have more priority than an aircraft trying to take off - making this ideal for implementing a priority queue. The third situation involves moving through your browsing history using the forward and back buttons, which means the web pages will be inserted and deleted from both ends of the queue, requiring a double-ended queue for implementation. 26. EXPLANATION: If you go through the code, you will understand that this is an implementation of a priority queue. The variables “data1” and “data2” are the value and priority respectively. However, this program will not provide an output based on the required priority. Within the if-block of the for-loop in the enqueue() function, data1 and data2 have been compared against each other, which doesn’t make any sense logically. Only the data2 values should be compared with each other. The output for this code will be: 8 5 3 6 2, which does not follow any order of priority, either increasing or decreasing. Therefore, none of the options are correct. 27. EXPLANATION: The “-->” operator already does the job of decrementing the variable “value” by 1 after each iteration. If the “value- -” statement were not there, the output would be the 1’s complement of the binary form of the input number. The “value--” statement inside the loop body further decrements the variable by 1 in each iteration, which means that the output will be half of the 1’s complement of the given number. If the 1’s complement turns out to be an odd number, then the half of it will be (number-1)/2. 28. EXPLANATION: The first option given is a function to insert elements into a circular queue implemented using linked lists. The second option given is a program to insert elements into a double-ended queue based on the requirement. However, the queue given in the question is neither. In fact, it’s not a queue at all. It may seem like a circular queue, but that’s not the case. The front element should point to the rear element in a circular queue. Therefore, the rear element points to the front element in the given code. 29. EXPLANATION: The given pseudocode is an implementation of a queue using two stacks. The “REMOVE”, “GET” and “SET” operations performed one after the other simply add up to a single pop() operation. According to the given question statement, first 6 elements are inserted into a queue, followed by 3 deletions. 6 push() operations are performed in the insert function, pushing elements into STACK_1. When the delete function is called for the first time, STACK_1 is cleared inside the while loop by calling the pop() operation 6 times, and inserting the removed elements into STACK_2 by calling the push() operation 6 times. Once the program exits the if-block, a last pop() operation is called once the program exits the if-block. Now, STACK_1 is empty, and STACK_2 is not empty. So, in all subsequent delete operations (2 in this case), only 1 pop() operation is called. Thus, the total number of push() operations comes to 6+6=12, and the total number of pop() operations comes to 6+1+2=9. 30. EXPLANATION: The first time the insert function is called, a single push() operation is performed. Then the delete function is called. Inside this, a pop() operation is called on STACK_1, followed by a push() into STACK_2 and then a pop() from STACK_2. Now, this whole operation is repeated 3 times. Therefore, the total number of push operations is 3 (from the insert function) + 3 (from the while loop of the delete function) = 6. The total number of pop() operations are: 3 (from inside the while loop in the delete function) + 3 (the last statement of the delete function) = 6. 31. EXPLANATION: It is possible to write a blank for-loop like this: for( ; ; ;), but it doesn’t have a lot of practicality. It is syntactically correct. An exit-controlled loop in JavaScript works by evaluating the test condition at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. It is possible to run a while loop using this symbol: -->. Instead of using the standard comparison symbols, you can use this symbol, as it decrements the value of the counter variable by 1 after comparing. 32. EXPLANATION: Look at the flowchart carefully. After the process begins, the loop body is executed once before the condition is reached. This happens in a do-while loop. The loop body is executed once, regardless of the condition, which is checked after this initial loop body execution. All subsequent iterations are based on the condition. 33. EXPLANATION: The condition should be str.length2. The code will never run with the given parameters because the if condition will throw a “not valid” response for every string greater than two characters. In the 6th line of code, the correct line should be const length = str.length-1 since array indexing begins from 0 and ends at 1 less than the array's length. Finally, in the 7th line of code, the parameters within the for loop should be: (let i = length; i >= 0; i--). 34. EXPLANATION: If the choice is between a for…in and a for…of the loop, then for this particular scenario, a for…of the loop is a better choice since a for…in the loop may not preserve the original order of elements in an array. The index order is implementation-dependent and may differ if a for…in the loop is used. Ideally, a forEach() method should be used for arrays in JavaScript. 35. EXPLANATION: The “-->” operator already does the job of decrementing the variable “value” by 1 after each iteration. If the “value- -” statement were not there, the output would be the binary form of the input number. The “value--” statement inside the loop body further decrements the variable by 1 in each iteration, which means that the output will be half of the given number. If the number is odd, half of it will be (number-1)/2. 36. EXPLANATION: The “===” operator is also called strict comparison, which compares variable values and types. This operator is always preferred for null comparisons. The last statement in a switch-case block is always automatically exited, and there is no need to break from it explicitly. If multiple cases match a case value, the first case is selected. 37. EXPLANATION: The program will become an infinitely executing loop that will continuously print 1. This is because the method is called repeatedly due to the presence of the post-increment operator. 38. EXPLANATION: The first option logs “Hello” to the world and then checks whether the given statement is equal t0 5, which it is not. When false is returned, it switches to the else block and prints “World”. The “Hello” and the “World” are printed in the if-parameters themselves in the second option. The else statement is never reached. 39. EXPLANATION: The array intArr is an array of integer values, while the input is a string. The answer would be 5, had the “==” comparison operator been used since it compares only values. However, the strict comparison operator, the “===”, compares both values as well as data types. This would result in all integer-string comparisons evaluating to false. 40. EXPLANATION: The given function checks whether the input number is a “happy number” or not. A happy number is a number that eventually reaches 1 when replaced by the sum of the square of each digit. For example, 13 is a happy number, because: 13 -> 12+32=10->12+02=1. The second part of the question, whose output is asked, prints the first 6 happy numbers starting with 1. 41. EXPLANATION: Java constructors are invoked when an object of that class is created, not before. So the first statement is false. Constructors support method overloading in Java, so the third statement is false too. The second statement is true since every class in Java does have an implicit constructor declaration if not declared by the user. 42. EXPLANATION: Constructors are not allowed to have a return type. 43. EXPLANATION: There is no such thing as execution time polymorphism in Java. So the first statement is incorrect. Java classes are logical entities and do not take up any space in memory. Hence the third statement is incorrect. 44. EXPLANATION: The given program is an example of method overriding. Method overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. Method overriding is an example of run-time polymorphism. 45. EXPLANATION: A method with the same signature is required in both parent and child classes to carry out method overriding. Polymorphism is shown when the same function is used for different purposes. This means that the concepts illustrated are inheritance and polymorphism. The presence of a child class shows inheritance. 46. EXPLANATION: The top-level classes can only have public, abstract, and final modifiers, and it is also an option not to define any class modifiers at all. This feature of Java is called default/package accessibility. 47. EXPLANATION: There will be 3 lines of output generated on the screen. The first line will be “Object created 5”, which will be generated when Object1 is created. The next line of output will be another “Object created 5” followed by a “Object created 7”, when Object2 is created. This is because the second class is a child of the first class and when an object for the second class is created, it will make a call to its parent class as well, which is why there are 3 lines of output. If the second class was not a child of the first class, there would be two lines of output. 48. EXPLANATION: The first three options are incorrect. Compile-time polymorphism is called static polymorphism, a child class can add its own fields and methods in addition to the parent class fields and methods, and an abstract class can have final methods. Thus, only the fourth option provides a correct statement. 49. EXPLANATION: There will be an error since the last class before MyClass does not have a super keyword declared within its constructor. Thus, the child class will be unable to make a call to its parent class, and the program will not run. 50. EXPLANATION: In the case of a no-arg constructor, there is no need to explicitly make a call to the parent class constructor using the super keyword. The compiler implicitly adds the super keyword, which then invokes the no-arg constructor of the parent class as the first statement in the constructor of the child class. THE END HTTPS://WWW.POCCOMU.COM/