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. |
What is a join in SQL? |
|
Answer» JOINS in SQL join ROWS from two or more tables BASED on a shared COLUMN. The different categories of joins are:
SQL Data Structures OOPs Java JavaScript Networking |
|
| 2. |
Explain processes and threads in context of OS. |
|
Answer» A process is a running PROGRAM that serves as the foundation for all computations. The process is not the same as programming code, although it is very similar. In contrast to the programme, which is regarded as a 'passive' entity, a process is a 'active' entity. Hardware state, MEMORY and CPU are among the attributes held by the process. Within a process, a THREAD is a single sequence stream. Threads have the same properties as the process, which is why they're REFERRED to as lightweight processes. Threads are executed one after the other, giving the appearance that they are running in PARALLEL. Each thread has a unique state. Each thread has:
Threads are dependent on each other since they share the code, data and OS resources. |
|
| 3. |
Explain the working of AJAX. |
|
Answer» Ajax (Asynchronous JavaScript and XML) is a set of WEB development approaches that generate asynchronous web applications using various client-side web technologies. Web applications that use Ajax can transmit and get data from a server asynchronously (in the BACKGROUND) without interfering with the existing page's appearance and behaviour. The XMLHttpRequest Object is used by Ajax to communicate with the server. The user submits a request using the User Interface, and a JavaScript call is made to the XMLHttpRequest Object, which then sends an XMLHttp request to the XMLHttpRequest Object. At that point, the server interacts with the database via PHP, servlet, ASP.net, and other technologies. The data is RETRIEVED, and the server transmits it to the XMLHttpRequest Callback method in the form of XML or Jason data. The Data then is presented on the browser using HTML and CSS. |
|
| 4. |
Given an array of strings, you need to remove strings which are anagrams of strings preceding them in the given array. You need to return the remaining strings in sorted order. |
|
Answer» For EXAMPLE, OUTPUT : Explanation : We can simply sort and compare the given two strings to see if they are anagrams or not. We may also use a hashmap to see if a string has appeared or not. Make an auxiliary array to STORE the generated strings, as well as a hashmap to keep track of the string we've FOUND thus far. Then iterate through the array's supplied string, sort the current string, and look it up in the hashmap. If the current text is not found in the hashmap, insert the sorted string arr[i] in the resultant array. After that, sort the array and print each string. Code: void uniqueAnagrams(string arr[], int n){ vector<string> result; //to keep track of already encountered sorted string unordered_set<string> found; for (int i = 0; i < n; i++) { string cur_word = arr[i]; // sorting the current string sort(begin(cur_word), end(cur_word)); // Checking if the current sorted string is present in the data structure already if (found.find(cur_word) == found.end()) { result.push_back(arr[i]); found.insert(cur_word); } } // Sorting the answer vector sort(begin(result), end(result)); // Printing the answer for (int i = 0; i < result.size(); ++i) { cout << result[i] << " "; }}Explanation: In the above code, we created a function uniqueAnagrams, which removes all the duplicate anagrams and prints the sorted array. We take the help of a hashmap to store each visited string in its sorted form. If the current string is not found in the hash map, we push it in the answer vector. At last we sort the answer vector and then display the resultant vector. |
|
| 5. |
Given an array whose elements denote the price of a stock on the ith day, find the maximum profit that you can make by purchasing and selling those stocks. No stock can be bought unless the previously bought stock has been sold. |
|
Answer» For example, Explanation of the above test case: In our approach, we will iterate through each of the prices of the STOCK starting from day 1 and check if its price is greater than the PREVIOUS day’s price. This is because there is no limit on the number of transactions being made. For example, buying a stock on day 1 and selling on day 4 is equivalent to buying a stock on day 1, selling on day 2, buying on day 2, selling on day 3, buying on day 3, selling on day 4. Code: #include<bits/stdc++.h>using namespace STD;//function to calculate the maximum profit attainableint maximiseProfit(int prices[], int size){ int maximumProfit = 0; // stores the maximum profit for(int i = 1; i < size; i ++) { if(prices[i] - prices[i - 1] > 0) maximumProfit += prices[i] - prices[i - 1]; } return maximiseProfit;}int main(){ int prices[] = { 100, 180, 260, 310, 40, 535, 695 }; int size = sizeof(prices) / sizeof(prices[0]); cout << maximiseProfit(prices, size) << endl; return 0;}Explanation: In the above code, the function maximiseProfit calculates the maximum profit attainable on the given SET of stock prices. We iterate over the array and add up any DIFFERENCE in which an element is greater than its preceding element. |
|
| 6. |
You are given a number N. You need to check if it can be written as a sum of k prime numbers. |
|
Answer» For example, Output : EXPLANATION : Input : Output : We use the Goldbach Conjecture for solving this PROBLEM. Goldbach’s conjecture states that every even integer (greater than 2) can be represented as the sum of two primes. When N >= 2k and k = 1: The answer will be True if and only if N is a prime number When N >= 2K and K = 2: If N is an even number the answer will be Yes(Goldbach’s conjecture) If N is odd the answer will be No if N-2 is not a prime number and Yes if N-2 is a prime number. This is because we know odd + odd = even and even + odd = odd. So when N is odd, and K = 2 one number must be 2 as it is the only even prime number so now the answer depends on whether N-2 is odd or not. When N >= 2K and K >= 3: The answer will always be True. This is because when N is even N – 2*(K-2) is also, even so, N – 2*(K – 2) can be written as the sum of two prime numbers p, q and N can be written as 2, 2 …..K – 2 times, p, q. When N is odd N – 3 -2*(K – 3) is even so it can be written as the sum of two prime numbers p, q and N can be written as 2, 2 …..K-3 times, 3, p, q // FUNCTION to check if a number is primebool checkPrime(int x){ for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true;}bool checkSumOfKPrimes(int N, int k){ // We return false if N < 2*k if (N < 2*k) return false; // If k = 1 we check if N is prime or not if (k == 1) return checkPrime(N); if (k == 2) { // if N is even the answer is true; if (N % 2 == 0) return true; // If N is odd, we check if N - 2 is prime or not return checkPrime(N - 2); } // If k >= 3, we return true; return true;}Explanation: In the above CODE, we created a function checkPrime which takes one input PARAMETER and we check if the given input is a prime number or not. We also created a function checkSumOfKPrimes which takes two input parameters N and k. In the function, we check if N can be represented as a sum of k prime numbers using the conditions of the Goldman Conjecture. |
|
| 7. |
Given a 2D matrix with n rows and m columns, your task is to print the elements of the matrix in a zigzag manner as shown in the image below : |
|
Answer» For example, Output: Input: Output: The strategy is straightforward. We will keep iterating over each diagonal element one by one and change the direction based on the previous match. #include<bits/stdc++.h>using namespace std;// function to print the matrix in a zigzag patternvoid zigZagTraversal(VECTOR<vector<int>> arr, int n, int m){ int cur_row = 0, cur_col = 0; //denotes the current row and COLUMN // false denotes cur_col NEEDS to be INCREMENTED, true denotes cur_row needs to be incremented bool flag = false; // Printing matrix of lower half zig-zag pattern int mn = min(m, n); for (int length = 1; length <= mn; ++length) { for (int i = 0; i < length; ++i) { cout << arr[cur_row][cur_col] << " "; if (i + 1 == length) break; // If flag is true we increment cur_row and decrement cur_col else decrement cur_row and increment cur_col if (flag == true){ cur_row = cur_row + 1; cur_col = cur_col - 1; } else{ cur_row = cur_row - 1; cur_col = cur_col + 1; } } if (length == mn) break; // We update cur_row or cur_col value according to the last increment if (flag){ cur_row = cur_row + 1; flag = false; } else{ cur_col = cur_col + 1; flag = true; } } // Updating the indexes of cur_row and cur_col variable if (cur_row == 0) { if (cur_col == m - 1) cur_row = cur_row + 1; else cur_col = cur_col + 1; flag = 1; } else { if (cur_row == n - 1) cur_col = cur_col + 1; else cur_row = cur_row + 1; flag = 0; } // Printing the next half zig-zag pattern int mx = max(m, n) - 1; for (int length, diagonal = mx; diagonal > 0; --diagonal) { if (diagonal > mn) length = mn; else length = diagonal; for (int i = 0; i < length; ++i) { cout << arr[cur_row][cur_col] << " "; if (i + 1 == length) break; // We update cur_row or cur_col value according to the last increment if (flag == true){ cur_row = cur_row + 1; cur_col = cur_col - 1; } else{ cur_col = cur_col + 1; cur_row = cur_row - 1; } } // Updating the cur_row and cur_col variables if (cur_row == 0 || cur_col == m - 1) { if (cur_col == m - 1) cur_row = cur_row + 1; else cur_col = cur_col + 1; flag = true; } else if (cur_col == 0 || cur_row == n - 1) { if (cur_row == n - 1) cur_col = cur_col + 1; else cur_row = cur_row + 1; flag = false; } }} int main(){ vector<vector<int>> arr = vector<vector<int>>(3, vector<int>(3, 0)); for(int i = 0; i < 3; i ++) { for(int j = 0; j < 3; j ++) arr[i][j] = 3 * i + (j + 1); } zigZagTraversal(arr, 3, 3); return 0;}Explanation: We follow a simulation approach in the above code. First, we print the elements in the first upper half of the matrix. Then, we print the elements of the lower half of the matrix. We iterate each diagonal one by one and print its elements. |
|
| 8. |
What is Binet's formula in context to the Fibonacci series? |
|
Answer» For COMPUTING the nth Fibonacci number, there is a simple MATHEMATICAL PROCEDURE called Binet's formula that does not require the CALCULATION of the PRECEDING numbers. Hence, we can find out the nth fibonacci number in constant time, without using recursion or iteration. It features the Golden Ratio (x) (ratio of any two successive Fibonacci numbers): |
|
| 9. |
Explain BFS (Breadth First Search) vs DFS (Depth First Search) in context of graph traversal. |
||||||||||||||
Answer»
|
|||||||||||||||
| 10. |
What is a Red-Black Tree in context to data structures? |
|
Answer» A red-BLACK tree is a self-balancing binary search tree with one extra bit at each node, which is commonly read as the colour (red or black). These colours are used to keep the tree balanced as insertions and deletions are made. The tree's balance isn't ideal, but it's good ENOUGH to cut down on searching TIME and keep it around O(log N), where n is the total number of nodes in the tree. Rudolf Bayer invented this tree in 1972. It's worth noting that, because each node only needs 1 bit of memory to store the colour information, these trees have the same memory footprint as a traditional (uncoloured) binary search tree. Points to remember:
|
|
| 11. |
Can a constructor be private in C++? |
|
Answer» A constructor is a particular member function of a class that is responsible for initialising the class's objects. When a class object is CREATED in C++, the constructor is automatically invoked. Constructors are typically defined in the public SECTION of a class. So, the question is whether CONSTRUCTION can be defined in the class's private section. The answer to this is a yes. A constructor can be defined in the class's private section. The ways to use a constructor in the private section of the class:
|
|
| 12. |
What are the differences between pointers and reference variables in C++? |
|
Answer» In C++, a pointer is a variable that keeps track of the memory address of another variable. A reference is an alias for a variable that ALREADY exists. A reference to a variable that has been initialised cannot be MODIFIED to refer to another variable. As a result, a reference is analogous to a const pointer. Pointer
A reference is supposed to be initialized at its time of declaration. int x = 6;int &ref = x;
A reference to a variable OBJECT cannot be modified once it has been initialised to a variable. |
|
| 13. |
How can you implement static and dynamic polymorphism in C++? |
|
Answer» Polymorphism refers to the fact that something exists in multiple forms. Polymorphism, in simple terms, is the ability of a MESSAGE to be displayed in multiple formats. On the basis of the time it takes to resolve the procedure call, it can be characterised in two ways.
|
|
| 14. |
What is the difference between an interface and abstract class in Java? |
||||||||||||||||
|
Answer» For classes and methods, the abstract keyword is a non-access MODIFIER. An abstract class is a special kind of class that can't be used to create objects (to access it, it must be inherited from another class). Talking about an abstract method, it has no body and it can only be used in an abstract class. The subclass provides the body (inherited from). In Java, an interface is a blueprint for a class. It features abstract methods and static constants. In Java, the interface is a means of achieving abstraction. The Java interface can only have abstract methods, not method bodies. In Java, it is used to achieve abstraction as well as MULTIPLE inheritances. To put it another way, interfaces can have abstract methods and variables. It isn't allowed to have a method body. The differences between the two have been TABULATED below:
|
|||||||||||||||||
| 15. |
What is Garbage Collection in Java? |
|
Answer» Garbage collection is the PROCESS of examining heap MEMORY, DETERMINING which items are in use and which are not, and then eliminating the objects that aren't. An in-use object, also known as a referenced object, signifies that it is still referenced by some portion of your program. An unreferenced object, also known as an unused object, is one that is no longer referenced by any portion of your program. An unreferenced object's memory can thus be reclaimed. The most significant benefit of garbage collection is that it relieves us of a load of manual memory allocation and deallocation, allowing us to focus on the problem at hand. Once we've made an object garbage-collectable, the garbage collector won't be able to DESTROY it right away. Only the object is destroyed when JVM executes the Garbage Collector program. However, we cannot predict when JVM will execute the Garbage Collector. We can also ask JVM to run Garbage Collector on our behalf. It can be DONE in two ways:
|
|
| 16. |
Explain the internal architecture of Java Virtual Machine (JVM). |
|
Answer» The JVM (Java Virtual Machine) is a run-time engine that allows Java APPLICATIONS to run. The JVM is the program that executes the main method in java programming. JVM is a component of JRE (Java Runtime Environment). WORA refers to Java programmes (Write Once Run Anywhere). This means that a PROGRAMMER can write Java code on one SYSTEM and expect it to operate without modification on any other Java-enabled device. All of this is feasible because of JVM. When we compile a .java file, the Java compiler creates .class files (byte-code files) with the same class NAMES as the .java file. When we run this.class file, it goes through a series of phases. These steps sum up the JVM as a whole. The execution engine scans the byte-code line by line, extracts data and information from various memory areas, and then executes the instructions. The Java NATIVE Interface (JNI) is a user interface that interacts with Native Method Libraries and provides native libraries (C, C++) for execution. It allows JVM to call C/C++ libraries and be called by C/C++ libraries that are hardware-specific. Native Method Libraries are a set of Native Libraries (C, C++) that the Execution Engine requires. |
|
| 17. |
Differentiate between StringBuffer and StringBuilder classes in the context of Java. |
||||||||
|
Answer» Strings are JAVA objects that are INTERNALLY supported by a char array. Strings are immutable because arrays are immutable (they can't grow). Every time you make a change to a String, a new String is produced. Java, on the other hand, has a number of classes that can be used to manipulate strings. StringBuffer and StringBuilder are two such classes.
|
|||||||||
| 18. |
Discuss the final keyword in Java. |
|
Answer» In Java, the final keyword is USED to restrict the user. The final keyword in Java can be used in a variety of SITUATIONS. The final keyword could be used with a variable, METHOD or class. Once a variable, function, or class has been declared final, it can only be assigned once. That is to SAY,
|
|
| 19. |
Explain hashCode() and equals() in Java. |
|
Answer» HashMap is part of the Java collection framework. HashMap uses a technique known as hashing. Hashing is the process of CONVERTING an object into an integer value. Indexing and search speed are AIDED by the integer value. It is in charge of CREATING the map interface. It stores the data in a Key/Value PAIR. In HashMap, the node is represented as a class that holds an array of nodes. Internally, it uses an array and LinkedList data structure to hold Key and Value. There are four fields in HashMap.
|
|
| 20. |
What is multithreading in Java? How are threads formed? |
|
Answer» Multithreading is a Java feature that permits the EXECUTION of two or more sections of a PROGRAM simultaneously to maximise CPU efficiency. In other words, it is the process of executing multiple threads at the same time. A thread is a component of such a program. Threads are hence lightweight processes within processes. Threads can be formed using two different mechanisms:
The advantages of multithreading are:
|
|