tag:blogger.com,1999:blog-4733010443685050402024-02-21T02:17:45.575+05:30The Geeks AreaTop 1000+ Interview Question Java JSP Spring Hibernate java collection multithreading java string serialization and desensitization final keyword in java package java design patterns maven git repository cloud computation UNIX shell interview questionUnknownnoreply@blogger.comBlogger38125tag:blogger.com,1999:blog-473301044368505040.post-1054928910414971842020-06-22T11:07:00.003+05:302020-06-22T11:12:57.544+05:30Top 80+ Android Interview Questions and Answers<h1><font color="#7baaf7">Top 80+ Android Interview Questions Answers</font></h1>Basic Android interview questions for experienced and freshers developers and testers:<br /><br />In this technological era, there is a huge demand for Android Application developers and it is one of the highly paid jobs too.<br /><br />Hence many tech-savvy people are trying to make their way into this career. There are several tricky interview questions that are asked in many MNC’s and some small scale companies. And this article includes a set of such Android interview questions and answers that will be helpful to millions of job seekers.<br /><br />Let’s Explore!!<br /><h2><font color="#ba67c8">Android Interview Questions</font></h2>This article includes a set of most commonly asked android interview questions and answers covering almost all the important concepts in simple terms in order to enable any job seeker to clear the interview successfully.<br /><br /><b>Most Popular Android Interview Questions & Answers</b><br />Given below is the list of most commonly asked Android Interview questions<br /><br /><br /><br /><b><font color="#d52c1f">01. What is android?</font></b><br /> Android is a mobile operating system developed by Google and later the OHA (Open Handset Alliance). It is an open-sourced operating system that is used primarily on mobile devices, such as cell phones and tablets. It is based on a modified version of the Linux kernel and other open source software, and is designed primarily for touchscreen mobile devices such as tablets,computers and smartphones.<div><br /><font color="#d52c1f"><b>02. What Is the Google Android SDK?</b></font><br /> The Google Android Software Development kit (SDK) is a toolset that developers need in order to write apps on Android enabled devices. It contains a vartual graphical interface that emulates an Android driven handheld environment, allowing them to test and debug their developed codes in vartual divice.</div><div><br /><font color="#d52c1f"><b>03. What is Android Programming</b></font><br /> An Android app is a software application running on the Android platform. Because the Android platform is built for mobile devices, a typical Android app is designed for a smartphone or a tablet Computers running on the Android OS.</div><div><br /><b><font color="#d52c1f">04. Which of the following is a commonly used programming language in Android application?</font></b><br /> The official language for Android development is Java. Large parts of Android are written in Java and its APIs are designed to be called primarily from Java. It is possible to develop C and C++ app using the Android Native Development Kit (NDK), however it isn't something that Google promotes<br />05. What are the different versions of Android OS that you remember?<br /> Given below are the various versions of Android.</div><div><table border="1" bordercolor="#888" cellspacing="0" style="border-collapse: collapse; border-color: rgb(136, 136, 136); border-width: 1px;"><tbody><tr><td style="min-width: 60px;"> <b>Version </b></td><td style="min-width: 60px;"> <b>Name</b></td></tr><tr><td style="min-width: 60px;"> Android 11.0</td><td style="min-width: 60px;"> </td></tr><tr><td> Android 10.0</td><td> </td></tr><tr><td> Android 9.0</td><td> pie</td></tr><tr><td> Android 8.0</td><td> Oreo</td></tr><tr><td> Android 7.0 – 7.1.2</td><td> Nougat</td></tr><tr><td> Android 6 – 6.0.1</td><td> Marshmallow</td></tr><tr><td> Android 5 – 5.1.1</td><td> Lollipop</td></tr><tr><td> Android 4.4 – 4.4.4</td><td> KitKat</td></tr><tr><td> Android 4.1 – 4.3</td><td> Jelly Bean</td></tr><tr><td> Android 4.0-4.0.4</td><td> Ice Cream Sandwich</td></tr></tbody></table></div><div><br /></div><div><b><font color="#d52c1f">06. What is the difference between Mobile Application Testing and Mobile Testing?</font></b><br /> Mobile app testing is the testing of applications on a Mobile device which mainly focuses on functions and features of the application. And Mobile Testing is the testing of the actual mobile device and focuses on the mobile features like Call, Sound, Display, SMS, Contacts, Media Player, inbuilt browsers etc.</div><div><br /><b><font color="#d52c1f">07. Why is Android used?</font></b><br /> The first reason why Android is so widely used is that it is compatible with all the major browsers within your mobile ecosystem which endears it to the mobile users. Android is an open source platform and which is one of its biggest strengths as compared to any other operating system of the past or the present.</div><div><br /><font color="#d52c1f"><b>08. What is the Android Architecture?</b></font><br /> Android Architecture is made up of 4 key components:<br /> Linux Kernel<br /> Libraries<br /> Android Framework<br /> Android Applications</div><div><br /><b><font color="#d52c1f">09. Can You Describe the Android Framework.</font></b><br /> The Android Framework is an important aspect of the Android Architecture. Here you can find all the classes and methods that developers would need in order to write applications on the any Android environment.</div><div><br /><font color="#d52c1f"><b>10. What is AAPT?</b></font><br /> AAPT means Android Asset Packaging Tool. This tool provides developers with the ability to deal with zip-compatible archives, which includes creating, extracting as well as viewing its contents.</div><div><br /><font color="#d52c1f"><b>11. What is the importance of having an emulator within the Android environment?</b></font><br /> The emulator lets developers "play" around an interface that acts as if it were an actual mobile device. They can write and test codes, even debug and get better performance. Emulators are a safe place for testing codes especially if it is in the early design phase of development.</div><div><br /><font color="#d52c1f"><b>12. What is the use of an activityCreator?</b></font><br /> An activityCreator is the first step towards the creation of a new project in Android. It is made up of a shell script that will be used to create new file system structure necessary for writing codes within the Android IDE.</div><div><br /><font color="#d52c1f"><b>13. Can You Describe Activities?</b></font><br /> Activities are what you refer to as the window to a user interface. Just as you create windows in order to display output or to ask for an input in the form of dialog boxes, activities play the same role, though it may not always be in the form of a user interface.</div><div><br /><font color="#d52c1f"><b>14. What are Intents?</b></font><br /> Intents displays notification messages to the user from within the Android enabled device. It can be used to alert the user of a particular state that occurred. Users can be made to respond to intents.</div><div><br /><font color="#d52c1f"><b>15. Differentiate Activities from Services?</b></font><br /> Activities can be closed, or terminated anytime the user wishes. On the other hand, services are designed to run behind the scenes, and can act independently. Most services run continuously, regardless of whether there are certain or no activities being executed.</div><div><br /><font color="#d52c1f"><b>16. Explain Android Architecture briefly?</b></font><br /> Android architecture is in the form of software stack components.The below diagram describes the different layers in the Android architecture.<br /><b> Linux Kernel</b>: Linux Kernel is placed at the bottom of the software stack and is the foundation of the Android architecture. Using Linux kernel, Android provides a connection between the other layers of the software. It helps to develop drivers like the keypad, display, audio for device manufacture etc.<br /><b> Hardware Abstraction Layer (HAL)</b>: HAL provides an interface between device drivers and API framework. It consists of library modules which are specific to the hardware component.<br /><b> Android Runtime</b>: Linux kernel provides multi-tasking execution environment so that multiple processes can execute each process runs on its own instance of Android Runtime (ART). Android has core runtime libraries like Dalvik VM specific libraries, Java Interoperability Libraries, Android Libraries and C/C++ libraries.<br /><b> Application Framework (Java API Framework)</b>: The entire android functionalities are available through the API. It consists of multiple services like Activity Manager, Resource Manager, Notification Manager, etc., which form the environment in which the android application runs.<br /><b> Applications</b>: The Android application is a top layer and all types of in-built applications such as SMS, Browsers, Contact, etc are included in this top layer. It also includes third party applications which are installed by the user such as Games, etc.</div><div><br /><font color="#d52c1f"><b>17. Define and explain Android Framework?</b></font><br /> Android framework is a set of API’s using which the Android developers write code for the mobile apps. It contains the methods and classes to write the programming code.<br /> Android framework includes a different set of tools to create image pane, text field, buttons, etc. It also includes “Activities” with which the user interacts and “Services”, which are the programs that run in the background. It is a package of different components like Intents, Broadcast Receivers, Content Providers, etc.</div><div><br /><b><font color="#d52c1f">18. Which components are necessary for a New Android project?</font></b><br /> Whenever a new Android project is created, the below components are required:<br /> manifest: It contains xml file.<br /> build/: It contains build output.<br /> src/: It contains the code and resource files.<br /> res/: It contains bitmap images, UI Strings and XML Layout i.e. all non-code resources.<br /> assets/: It contains a file which should be compiled into a .apk file.<font color="#ffffff"><br /> The below image shows the Project View once an Android project is created:</font></div><div><br /><font color="#d52c1f"><b>19. Provide the important core components of Android?</b></font><br /> The core components of Android operating systems are:<br /> Activity<br /> Intents<br /> Services<br /> Content Provider<br /> Fragment</div><div><br /><b><font color="#d52c1f">20. Explain briefly – what is meant by Activities?</font></b><br /> Activities are the part of the mobile app which the user can see and interact with.For Example, if you open an SMS app which has multiple activities like create new SMS, add a contact from the address book, write the content in the SMS body, send SMS to the selected contact, etc.<br /> Activity keeps a track of the following:<br /> Keeps track of what a user is currently looking for in an app. Keeps a track of previously used processes, so that the user can switch between ongoing process and previous process. It helps to kill the processes so that the user can return to their previous state An activity is implemented as a subclass of Activity class as shown below:<br /><pre style="text-align: left;"> Public class MyActivity extends Activity<br /> {<br /> }</pre><b><font color="#d52c1f">21. What is meant by Services?</font></b><br /> Service is an Android component which runs in the background and acts independently. It does not provide any user interface.<br /> Though the services are running behind the scene, a user can continue their work on different apps. Most of the time, the users are not aware of the services which are running in the background. These services allow the system to kill the process without interrupting the user’s ongoing work.<br /> A service is implemented as a subclass of Service class:<br /><pre style="text-align: left;"> Public class MainService extends Service<br /> {<br /> }</pre><font color="#d52c1f"><b>22. Explain Activity Lifecycle briefly?</b></font><br /> When a user interacts with the app and moves here and there, out of the app, returns to the app, etc. During all this process “Activity” instances also move in the different stages in their lifecycle.<br /> There are seven different states like – onCreate(), onStart(), onRestart(), onResume(), onPause(), onStop(), and onDestroy(). These are termed as a ‘callback’. Android system invokes these callbacks to know that the state has been changed.<br /> The below-given diagram describes the Activity Lifecycle:<br /> When a user is working on an app, then there are many activities involved in it like Open, Close, Save, Delete, Send, etc.<br /> Based on the user action these activities are partially disconnected from the UI but these activities always reside in the memory so that when the user calls back the same activity, the user will be in the same state where he has left off.</div><div><br /><font color="#d52c1f"><b>23. What is an Intent?</b></font><br /> Android has an Intent class when the user has to navigate from one Activity to another. Intent displays notifications from the device to the user and then the user can respond to the notification if required.<br /> Given below are the two types:<br /> Implicit Intents<br /> Explicit Intents</div><div><br /><b><font color="#d52c1f">24. Explain Implicit and Explicit Intents?</font></b><br /> Implicit Intent calls the system components while explicit Intents invoke the Activity class.</div><div><br /><font color="#d52c1f"><b>25. What is the importance of setting up permission in app development?</b></font><br /> Once the permissions are set for the app development, then the data and code are restricted to the authorized users only.<br /> If the code is kept without any restriction or if it is accessible to anyone then there are chances of compromise of code which results in defect leakage.</div><div><br /><font color="#d52c1f"><b>26. What is .apk extension in Android?</b></font><br /> It is a default file format that is used by Android Operating System. Application Package Kit (APK) is used for installation of mobile apps. The .apk contains resource file, certificate, manifest file and other code.<br /> APK files are archive files in the zip format with .apk extension.</div><div><br /><font color="#d52c1f"><b>27. What is the database used for Android platform?</b></font><br /> SQLite is the database that is used for Android platform. It is an open-source, serverless database.</div><div><br /><font color="#d52c1f"><b>28. What is ANR in Android?</b></font><br /> ANR stands for Application Not Responding. It is a notification or pop-up displayed by the Android platform whenever the application is performing too many functions at a time and if it is suddenly not responding for a long time to the user action.</div><div><br /><font color="#d52c1f"><b>29. Which are the dialog boxes supported by Android platform?</b></font><br /> Android supports four types of dialog boxes:<br /> AlertDialog: It has a maximum of 3 buttons and sometimes AlertDialog includes check boxes and Radio buttons to select the element.<br /> ProgressDialog: It displays the progress bar or wheels.<br /> TimePickerDialog: Using this dialog box, a user selects the Time.<br /> DatePickerDialog: Using this dialog box, a user selects the Date<br />30. What is ADB?<br /> Android Debug Bridge (ADB) is a command line tool which performs shell commands.<br /> ADB is used for direct communication between the emulator ports. It gives the direct control of the communication between the emulator instances to the developer.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>31. What items are important in every Android project?</b></font><br /> These are the essential items that are present each time an Android project is created:<br /> AndroidManifest.xml<br /> build.xml<br /> bin/<br /> src/<br /> res/<br /> assets/<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>32. What is the importance of XML-based layouts?</b></font><br /> The use of XML-based layouts provides a consistent and somewhat standard means of setting GUI definition format. In common practice, layout details are placed in XML files while other items are placed in source files.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>33. What are containers?</b></font><br /> Containers, as the name itself implies, holds objects and widgets together, depending on which specific items are needed and in what particular arrangement that is wanted. Containers may hold labels, fields, buttons, or even child containers, as examples.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>34. What is Orientation?</b></font><br /> Orientation, which can be set using setOrientation(), dictates if the LinearLayout is represented as a row or as a column. Values are set as either HORIZONTAL or VERTICAL.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>35. What is the importance of Android in the mobile market?</b></font><br /> Developers can write and register apps that will specifically run under the Android environment. This means that every mobile device that is Android enabled will be able to support and run these apps. With the growing popularity of Android mobile devices, developers can take advantage of this trend by creating and uploading their apps on the Android Market for distribution to anyone who wants to download it.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>36. What is adb?</b></font><br /> Adb is short for Android Debug Bridge. It allows developers the power to execute remote shell commands. Its basic function is to allow and control communication towards and from the emulator port.<br /><font color="#d52c1f"><b>37. What are the four essential states of an activity?</b></font><br /> Active – if the activity is at the foreground<br /> Paused – if the activity is at the background and still visible<br /> Stopped – if the activity is not visible and therefore is hidden or obscured by another activity<br /> Destroyed – when the activity process is killed or completed terminated<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>38. What is ANR?</b></font><br /> ANR is short for Application Not Responding. This is actually a dialog that appears to the user whenever an application have been unresponsive for a long period of time.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>39. Which elements can occur only once and must be present?</b></font><br /> Among the different elements, the and elements must be present and can occur only once. The rest are optional, and can occur as many times as needed.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>40. How are escape characters used as attribute?</b></font><br /> Escape characters are preceded by double backslashes. For example, a newline character is created using '\\n'<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>41. What is the importance of settings permissions in app development?</b></font><br /> Permissions allow certain restrictions to be imposed primarily to protect data and code. Without these, codes could be compromised, resulting to defects in functionality.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>42. What is the function of an intent filter?</b></font><br /> Because every component needs to indicate which intents they can respond to, intent filters are used to filter out intents that these components are willing to receive. One or more intent filters are possible, depending on the services and activities that is going to make use of it.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>43. Enumerate the three key loops when monitoring an activity?</b></font><br /> Entire lifetime – activity happens between onCreate and onDestroy<br /> Visible lifetime – activity happens between onStart and onStop<br /> Foreground lifetime – activity happens between onResume and onPause<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>44. When is the onStop() method invoked?</b></font><br /> A call to onStop method happens when an activity is no longer visible to the user, either because another activity has taken over or if in front of that activity.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>45. Is there a case wherein other qualifiers in multiple resources take precedence over locale?</b></font><br /> Yes, there are actually instances wherein some qualifiers can take precedence over locale. There are two known exceptions, which are the MCC (mobile country code) and MNC (mobile network code) qualifiers.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>46. What are the different states wherein a process is based?</b></font><br /> There are 4 possible states:<br /> foreground activity<br /> visible activity<br /> background activity<br /> empty process<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>47. How can the ANR be prevented?</b></font><br /> One technique that prevents the Android system from concluding a code that has been responsive for a long period of time is to create a child thread. Within the child thread, most of the actual workings of the codes can be placed, so that the main thread runs with minimal periods of unresponsive times.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>48. What role does Dalvik play in Android development?</b></font><br /> Dalvik serves as a virtual machine, and it is where every Android application runs. Through Dalvik, a device is able to execute multiple virtual machines efficiently through better memory management.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>49. What is the AndroidManifest.xml?</b></font><br /> This file is essential in every application. It is declared in the root directory and contains information about the application that the Android system must know before the codes can be executed.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>50. What is the proper way of setting up an Android-powered device for app development?</b></font><br /> The following are steps to be followed prior to actual application development in an Android-powered device:<br /> -Declare your application as "debuggable" in your Android Manifest.<br /> -Turn on "USB Debugging" on your device.<br /> -Set up your system to detect your device.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>51. Enumerate the steps in creating a bounded service through AIDL.</b></font><br /></div>1. create the .aidl file, which defines the programming interface<br />2. implement the interface, which involves extending the inner abstract Stub class as well as implanting its methods.<br /> 3. expose the interface, which involves implementing the service to the clients.<br /><div style="text-align: left;"><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>52. What is the importance of Default Resources?</b></font><br /> When default resources, which contain default strings and files, are not present, an error will occur and the app will not run. Resources are placed in specially named subdirectories under the project res/ directory.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>53. When dealing with multiple resources, which one takes precedence?</b></font><br /> Assuming that all of these multiple resources are able to match the configuration of a device, the 'locale' qualifier almost always takes the highest precedence over the others.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>54. When does ANR occur?</b></font><br /> The ANR dialog is displayed to the user based on two possible conditions. One is when there is no response to an input event within 5 seconds, and the other is when a broadcast receiver is not done executing within 10 seconds.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>55. What is AIDL?</b></font><br /> AIDL, or Android Interface Definition Language, handles the interface requirements between a client and a service so both can communicate at the same level through interprocess communication or IPC. This process involves breaking down objects into primitives that Android can understand. This part is required simply because a process cannot access the memory of the other process.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>56. What data types are supported by AIDL?</b></font><br /> AIDL has support for the following data types:<br /> -string<br /> -charSequence<br /> -List<br /> -Map<br /> -all native Java data types like int,long, char and Boolean<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>57. What is a Fragment?</b></font><br /> A fragment is a part or portion of an activity. It is modular in a sense that you can move around or combine with other fragments in a single activity. Fragments are also reusable.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>58. What is a visible activity?</b></font><br /> A visible activity is one that sits behind a foreground dialog. It is actually visible to the user, but not necessarily being in the foreground itself.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>59. When is the best time to kill a foreground activity?</b></font><br /> The foreground activity, being the most important among the other states, is only killed or terminated as a last resort, especially if it is already consuming too much memory. When a memory paging state has been reach by a foreground activity, then it is killed so that the user interface can retain its responsiveness to the user.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>60. Is it possible to use or add a fragment without using a user interface?</b></font><br /> Yes, it is possible to do that, such as when you want to create a background behavior for a particular activity. You can do this by using add(Fragment,string) method to add a fragment from the activity.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>61. How do you remove icons and widgets from the main screen of the Android device?</b></font><br /> To remove an icon or shortcut, press and hold that icon. You then drag it downwards to the lower part of the screen where a remove button appears.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>62. What are the core components under the Android application architecture?</b></font><br /> There are 5 key components under the Android application architecture:<br /> - services<br /> - intent<br /> - resource externalization<br /> - notifications<br /> - content providers<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>63. What composes a typical Android application project?</b></font><br /> A project under Android development, upon compilation, becomes an .apk file. This apk file format is actually made up of the AndroidManifest.xml file, application code, resource files, and other related files.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>64. What is a Sticky Intent?</b></font><br /> A Sticky Intent is a broadcast from sendStickyBroadcast() method such that the intent floats around even after the broadcast, allowing others to collect data from it.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>65. Do all mobile phones support the latest Android operating system?</b></font><br /> Some Android-powered phone allows you to upgrade to the higher Android operating system version. However, not all upgrades would allow you to get the latest version. It depends largely on the capability and specs of the phone, whether it can support the newer features available under the latest Android version.<br /><font color="#d52c1f"><br /></font></div><div><font color="#d52c1f"><b>66. What is portable wi-fi hotspot?</b></font><br /> Portable Wi-Fi Hotspot allows you to share your mobile internet connection to other wireless device. For example, using your Android-powered phone as a Wi-Fi Hotspot, you can use your laptop to connect to the Internet using that access point.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>67. What is an action?</b></font><br /> In Android development, an action is what the intent sender wants to do or expected to get as a </div><div>response. Most application functionality is based on the intended action.<br /><br /></div><div><font color="#d52c1f"><b>69. What is the difference between a regular bitmap and a nine-patch image?</b></font><br /> In general, a Nine-patch image allows resizing that can be used as background or other image size requirements for the target device. The Nine-patch refers to the way you can resize the image: 4 corners that are unscaled, 4 edges that are scaled in 1 axis, and the middle one that can be scaled into both axes.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>70. What language is supported by Android for application development?</b></font><br /> The main language supported is Java programming language. Java is the most popular language for app development, which makes it ideal even for new Android developers to quickly learn to create and deploy applications in the Android environment.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>71. What is ActivityCreator?</b></font><br /> ActivityCreator is a batch file and shell script which was used to create a new Android project. It is now replaced by “Create New Project” in Android SDK.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>72. What is Orientation?</b></font><br /> Orientation is the key feature in Smartphones nowadays. It has the ability to rotate the screen between Horizontal or Vertical mode.<br /> Android supports two types of screen Orientations as mentioned below:<br /> Portrait: When your device is vertically aligned.<br /> Landscape: When your device is horizontally aligned.<br /> setOrientation() is a method using which you can set a screen alignments. HORIZONTAL and VERTICAL are two values which can be set in the setOrientation() method. Whenever there is a change in the display orientation i.e. from Horizontal to Vertical or vice versa then onCreate() method of the Activity gets fired.<br /> Basically, when the orientation of the Android mobile device gets changed then the current activity gets destroyed and then the same activity is recreated in the new display orientation. Android developers define the orientation in the AndroidManifest.xml file.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>73. What is AIDL?</b></font><br /> In the Android platform, there are remote methods which facilitate the use of methods from one program to another. To create and implement the remote methods the first step is to define communication interface in AIDL.<br /> AIDL stands for Android Interface Definition Language. It facilitates the communication between the client and service. It also communicates the information through inter-process communication.<br /> For communication between processes, the data is broken down into chunks which are easily understandable by the Android platform.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>74. What are the data types supported by AIDL?</b></font><br /> Data Types supported by AIDL are as follows:<br /> String<br /> List<br /> Map<br /> charSequence<br /> Java data types such as INT, Long, Char, Boolean etc<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>75. Explain AndroidManifest.xml file and why do you need this?</b></font><br /> Every application must have AndroidManifest.xml file in the root directory. It contains the information about your app and provides the same to the Android system.<br /> The information includes the package name, Android components such as Activity, Services, Broadcast Receivers, Content Providers, etc. Every Android system must have this information before running any app code.<br /> AndroidManifest.xml file performs the following tasks:<br /> It provides a name to the Java package and this name is a unique identifier for the application.<br /> It describes the various components of the application which include Activity, Services, Content Providers, etc. Also, it defines the classes which implement these components.<br /> It is responsible to protect the application and it declares the permission for accessing the protected part of the app.<br /> It also declares the Android API which is going to be used by the application.<br /> It contains the library file details which are used and linked to the application.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>76. What all devices have you worked on?</b></font><br /> There are many mobile devices available in the market with different operating systems.<br /> Specifically, I have worked on Android, Windows, Symbian, iPhone, etc<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>77. Which tools are used for debugging on the Android platform?</b></font><br /> To understand the cause of the failure or cause of any issue, debugging is important. On the Android platform Android Monitor.bat utility is used while on iOS platform, iPhone Configuration utility is used for debugging purposes.<br /> There are different tools for debugging which include – Android DDMS, Android Debug Bridge, iOS simulator, Debugging from Eclipse with ADT, Remote debugging on Android with Chrome etc.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>78. Which scenario can test only on real devices but not on emulator?</b></font><br /> Emulators are used for performing similar kind of testing which is performed on the real devices. Basically, emulators are used as a replacement for real devices as sometimes real devices are not available for testing, use of real mobile devices for testing purpose is costlier at times.<br /> But there are few scenarios which cannot be tested using emulator, these can be tested only using real devices. These scenarios are interrupted scenarios i.e. message, phone call interruption while using the app, low battery, Bluetooth, memory card mount and unmount etc.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>79. Name the mobile automation tools that are available in the market?</b></font><br /> There are quite a few mobile automation testing tools that are available in the market but these are used only if the project requires it and if the application supports the automation.<br /> These tools are paid as well as free tools, hence analysis needs to be done within the project team and then the appropriate mobile automation tool needs to be selected. Silk Mobile, SeeTest, Ranorex are the paid mobile automation tool while Appium, KIF, Robotium, Calabash are few free tools.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>80. How do you troubleshoot android application which is crashing frequently?</b></font><br /> Given below are the few steps that we need to follow while troubleshooting the crashing issue:<br /> Free up memory space: There is only a limited space available on the mobile devices for mobile apps. To avoid crashing issue or memory related issue, you need to first check the memory space.<br /> Clear app data usage: You can clear the app data using the Application Manager under “Settings”. This will clear the cache memory and allow some free space to install another app or it will boost up your current app.<br /> Memory Management: Some apps run perfectly on one type of mobile device but the same app may not work on another type of device as for such devices the processing power, memory management, and CPU speed is different. For any app to run properly on any type of mobile device, you should manage the memory on the device.<br /> Compatibility issue: It is always not possible to test mobile app on all mobile devices, browsers, operating systems etc. So you need test your mobile app on as many mobile devices as you can in order to avoid any compatibility issue.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>81. How do you find memory leaks in the mobile app on Android platform?</b></font><br /> Android Studio is using Android Device Manager (ADM), this ADM is used to detect the memory leaks in the Android platform.<br /> When you open ADM in the Android Studio then on the left-hand side of the ADM, you will find your device or emulator in which a heap sign will be displayed. When you are running any mobile app then you will see the heap size, memory analysis and other statistics displayed on it.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>82. What is DDMS?</b></font><br /> Android Studio has debugging tools known as DDMS i.e. Dalvik Debug Monitor Server.<br /> It has wide debugging features which include:<br /> Port forwarding services.<br /> Screen capture on the device.<br /> Thread and Heap information.<br /> Incoming call and SMS spoofing.<br /> Logcat<br /> Radio state information.<br /> Location data spoofing.<br /> DDMS is integrated with the Android studio. To launch the DDMS, you need to open Android Device Monitor (ADM) first and then click on the DDMS menu button. Once DDMS is launched, then on the left-hand side the list of connected devices are displayed along with the processes which are running on each device.<br /> With the help of DDMS, you can debug both on real devices and emulators.<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>83. What are the different data storage options available on the Android platform?</b></font><br /> Android platform provides a wide range of data storage options. These options must be used based on the need such as data is secure and used with the permission only or can be accessed publicly.<br /> Below is the list of data storage options on the Android platform:<br /> SharedPreference: It stores data in XML files. It is the simplest way to store private data in the key-value pair.<br /> SQLite: It stores structure data in the private database.<br /><b> Internal Storage</b>: It stores data in the device file system and any other app cannot read this data.<br /><b> External Storage:</b> Data is stored in the file system but it is accessible to all apps in the device<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>84. Explain Sensors in Android?</b></font><br /> Android-enabled devices have built-in Sensors which measures Orientation, Motion and other conditions.<br /> These sensors provide data with high accuracy, which will help to monitor positioning and movement of the device. Some of the sensors are hardware based and few are software based.<br /> There are three categories of sensors as mentioned below:<br /> Motion Sensors: These sensors measure the rotational & acceleration forces and it includes gravity sensors, rotational vector sensor, accelerometers, etc.<br /> Environmental Sensors: It measures air temperature, pressure, humidity, etc.<br /> Position Sensors: It measures the physical position of the device and includes orientation sensors and magnetometers.<br /> There are four types of Java Classes as shown below:<br /> Sensor Manager<br /> Sensor<br /> SensorEvent<br /> SensorEventListener<br /><font color="#d52c1f"><b><br /></b></font></div><div><font color="#d52c1f"><b>85. What do you think are some disadvantages of Android?</b></font><br /> Given that Android is an open-source platform, and the fact that different Android operating systems have been released on different mobile devices, there's no clear cut policy to how applications can adapt with various OS versions and upgrades. One app that runs on this particular version of Android OS may or may not run on another version. Another disadvantage is that since mobile devices such as phones and tabs come in different sizes and forms, it poses a challenge for developers to create apps that can adjust correctly to the right screen size and other varying features and specs.<br /><br /><br /><br /><br /><br /><br /> </div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-78591688201656786542019-05-26T10:00:00.000+05:302019-05-26T10:00:04.788+05:30DOCKER Interview questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<b><span style="color: #4472c4; font-size: 16.0pt;">DOCKER<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">1. What is Docker?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Docker is Open Source
software. It provides the automation of Linux application deployment in a
software container. We can do operating system level virtualization on Linux
with Docker. Docker can package software in a complete file system that
contains software code, runtime environment, system tools, & libraries that
are required to install and run the software on a server.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">2. What is the difference between Docker
image and Docker container?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Docker container is simply
an instance of Docker image. A Docker image is an immutable file, which is a
snapshot of container. We create an image with build command. When we use run
command, an Image will produce a container. In programming language, an Image
is a Class and a Container is an instance of the class.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">3. How will you remove an image from
Docker?</span></b><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">We can use docker rmi
command to delete an image from our local system. Exact command is:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">% docker rmi <Image
Id><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">If we want to find IDs of
all the Docker images in our local system, we can user docker images command.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">% docker images<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">If we want to remove a
docker container then we use docker rm command.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">% docker rm <Container
Id><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">4. How is a Docker container different
from a hypervisor?</span></b><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">In a Hypervisor environment
we first create a Virtual Machine and then install an Operating System on it.
After that we deploy the application. The virtual machine may also be installed
on different hardware configurations. In a Docker environment, we just deploy
the application in Docker. There is no OS layer in this environment. We specify
libraries, and rest of the kernel is provided by Docker engine. In a way,
Docker container and hypervisor are complementary to each other.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">5. Can we write compose file in json file
instead of yaml?</span></b><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Yes. Yaml format is a
superset of json format. Therefore any json file is also a valid Yaml file. If
we use a json file then we have to specify in docker command that we are using
a json file as follows:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">% docker-compose -f
docker-compose.json up<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">6. Can we run multiple apps on one server
with Docker?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Yes, theoretically we can
run multiples apps on one Docker server. But in practice, it is better to run
different components on separate containers. With this we get cleaner
environment and it can be used for multiple uses.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">7. What are the common use cases of
Docker?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Some of the common use cases
of Docker are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Setting up Development Environment</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: We can use Docker to set the development
environment with the applications on which our code is dependent.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Testing Automation Setup</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: Docker can also help in creating the Testing
Automation setup. We can setup different services and apps with Docker to
create the automation testing environment.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Production Deployment</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: Docker also helps in implementing the Production
deployment for an application. We can use it to create the exact environment
and process that will be used for doing the production deployment.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">8. What are the main features of
Docker-compose?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Some of the main features of
Docker-compose are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Multiple environments on same Host</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: We can use it to create multiple
environments on the same host server.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Preserve Volume Data on Container Creation</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: Docker compose also
preserves the volume data when we create a container.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Recreate the changed Containers</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: We can also use compose to recreate the changed
containers.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Variables in Compose file</span></b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">: Docker compose also supports variables in compose
file. In this way we can create variations of our containers.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">9. What is the most popular use of Docker?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">The most popular use of Docker
is in build pipeline. With the use of Docker it is much easier to automate the
development to deployment process in build pipeline. We use Docker for the
complete build flow from development work, test run and deployment to
production environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">10. What is the role of open source
development in the popularity of Docker?</span></b><b><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Since Linux was an open
source operating system, it opened new opportunities for developers who want to
contribute to open source systems. One of the very good outcomes of open source
software is Docker. It has very powerful features. Docker has wide acceptance
due to its usability as well as its open source approach of integrating with
different systems.<o:p></o:p></span></div>
</div>
Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-473301044368505040.post-79270906450562057472019-05-19T07:30:00.000+05:302019-05-19T07:30:06.672+05:30UNIX Shell Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<b><span style="font-size: 16pt;"><span style="color: blue;">UNIX Shell</span><span style="color: #4472c4;"><o:p></o:p></span></span></b></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">1. How will you remove all files in current directory? Including the
files that are two levels down in a subdirectory.</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Unix we have rm command to remove files and
sub-directories. With rm command we have –r option that stands for recursive.
The –r option can delete all files in a directory recursively. It means if we
our current directory structure is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">My_dir<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">->Level_1_dir<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">-> Level_1_dir ->Level_2_dir<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">-> Level_1_dir ->Level_2_dir->a.txt<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">With rm –r * command we can delete the file a.txt as
well as subdirectories Level_1_dir and Level_2_dir. Command:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">rm – r *<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The asterisk (*) is a wild card character that stands
for all the files with any name.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">2. What is the difference between the –v and –x options in Bash
shell scripts?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a BASH Unix shell we can specify the options –v and
–x on top of a script as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">#!/bin/bash -x –v<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">With –x option BASH shell will echo the commands like
for, select, case etc. after substituting the arguments and variables. So it
will be an expanded form of the command that shows all the actions of the
script. It is very useful for debugging a shell script. With –v option BASH
shell will echo every command before substituting the values of arguments and
variables. In –v option Unix will print each line as it reads. In –v option, If
we run the script, the shell prints the entire file and then executes. If we
run the script interactively, it shows each command after pressing enter.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">3. What is a Filter in Unix command?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Unix there are many Filter commands like- cat, awk,
grep, head, tail cut etc. A Filter is a software program that takes an input
and produces an output, and it can be used in a stream operation.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">E.g.</span></b><span style="font-size: 14.0pt;"> cut -d : -f 2 /etc/passwd | grep abc<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can mix and match multiple filters to create a
complex command that can solve a problem. Awk and Sed are complex filters that
provide fully programmable features. Even Data scientists use Unix filters to
get the overview of data stored in the files.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">4. What is Kernel in Unix operating system?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Kernel is the central core component of a Unix
operating system (OS). A Kernel is the main component that can control
everything within Unix OS. It is the first program that is loaded on startup of
Unix OS. Once it is loaded it will manage the rest of the startup process.
Kernel manages memory, scheduling as well as communication with peripherals
like printers, keyboards etc. But Kernel does not directly interact with a
user. For a new task, Kernel will spawn a shell and user will work in a shell.
Kernel provides many system calls. A software program interacts with Kernel by
using system calls. Kernel has a protected memory area that cannot be
overwritten accidentally by any process.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">5. What is a Shell in Unix OS?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Shell in Unix is a user interface that is used by a
user to access Unix services. Generally a Unix Shell is a command line
interface (CLI) in which users enter commands by typing or uploading a file. We
use a Shell to run different commands and programs on Unix operating system. A
Shell also has a command interpreter that can take our commands and send these
to be executed by Unix operating system. Some of the popular Shells on Unix
are: Korn shell, BASH, C shell etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">6. What are the different shells in Unix that you know about?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Unix has many flavors of Shell. Some of these are as
follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Bourne shell</span></b><span style="font-size: 14.0pt;">:
We use sh for Bourne shell.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Bourne Again shell</span></b><span style="font-size: 14.0pt;">: We use bash to run this shell.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Korn shell</span></b><span style="font-size: 14.0pt;">:
We can use ksh to for Korn shell.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Z shell</span></b><span style="font-size: 14.0pt;">: The
command to use this is zsh<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">C shell</span></b><span style="font-size: 14.0pt;">: We
use csh to run C shell.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Enhanced C shell</span></b><span style="font-size: 14.0pt;">: tcsh is the command for enhanced C shell.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">7. What is the first character of the output in ls –l command ?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We use ls -l command to list the files and directories
in a directory. With -l option we get long listing format. In this format the
first character identifies the entry type. The entry type can be one of the
following:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">b Block special file<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">c Character special file<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">d Directory<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">l Symbolic link<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">s Socket link<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">p FIFO<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">- Regular file<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In general we see d for directory and - for a regular
file.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">8. What is the difference between Multi-tasking and Multi-user
environment?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a Multi-tasking environment, same user can submit
more than one tasks and operating system will execute them at the same time. In
a Multi-user environment, more than one user can interact with the operating
system at the same time.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">9. What is Command Substitution in Unix?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Command substitution is a mechanism by which Shell
passes the output of a command as an argument to another command. We can even
use it to set a variable or use an argument list in a for loop.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">E.g</span></b><span style="font-size: 14.0pt;">. rm `cat files_to_delete`<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In this example files_to_delete is a file containing
the list of files to be deleted. cat command outputs this file and gives the
output to rm command. rm command deletes the files. In general Command
Substitution is represented by back quotes `.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">10. What is an Inode in Unix?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">An Inode is a Data Structure in Unix that denotes a
file or a directory on file system. It contains information about file likelocation
of file on the disk, access mode, ownership, file type etc. Each Inode has a
number that is used in the index table. Unix kernel uses Inode number to access
the contents of an Inode. We can use ls -i command to get the inode number of a
file.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">11. What is the difference between absolute path and relative path
in Unix file system?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Absolute path is the complete path of a file or
directory from the root directory. In general root directory is represented by
/ symbol. If we are in a directory and want to know the absolute path, we can
use pwd command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Relative path is the path relative the current
location in directory. E.g. In a directory structure /var/user/kevin/mail if we
are in kevin directory then pwd command will give absolute path as /var/user/kevin.
Absolute path of mail folder is /var/user/kevin/mail. For mail folder ./mail is
the relative path of mail directory from kevin folder.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="font-size: 14.0pt;">12.
What are the main responsibilities of a Unix Shell?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Some of the main responsibilities of a Unix Shell are
as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Program
Execution</span></b><span style="font-size: 14.0pt;">: A shell is responsible for
executing the commands and script files in Unix. User can either interactively
enter the commands in Command Line Interface called terminal or they can run a
script file containing a program.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Environment
Setup</span></b><span style="font-size: 14.0pt;">: A shell can define the
environment for a user. We can set many environment variables in a shell and
use the value of these variables in our program.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Interpreter</span></b><span style="font-size: 14.0pt;">: A shell acts as an interpreter for our scripts. It
has a built in programming language that can be used to implement the logic.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Pipeline</span></b><span style="font-size: 14.0pt;">: A shell also can hookup a pipeline of commands. When
we run multiple commands separated by | pipe character, the shell takes the
output of a command and passes it to next one in the pipeline.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">I/O Redirection</span></b><span style="font-size: 14.0pt;">: Shell is also responsible for taking input from
command line interface (CLI) and sending the output back to CLI. We use >,
<, >> characters for this purpose.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="font-size: 14.0pt;">13.
What is a Shell variable?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A Unix Shell variable is an internal variable that a
shell maintains. It is local to that Shell. It is not made available to the
parent shell or child shell. We generally use lower case names for shell
variables in C shell. We can set the value of a shell variable by set command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">E.g.</span></b><span style="font-size: 14.0pt;"> % set max_threads=10<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">To delete a Shell variable we can use unset command.
To use a Shell variable in a script we use $ sign in front of the variable
name.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">E.g.</span></b><span style="font-size: 14.0pt;"> echo $max_threads<o:p></o:p></span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-90935597836784396812019-05-13T09:30:00.003+05:302020-06-27T17:29:36.151+05:30Microservices Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h1 style="text-align: center;"><font color="#7baaf7">Microservices Interview Questions</font></h1>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">1. What is a Microservice in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">A Microservice is a small and autonomous piece of code
that does one thing very well. It is focused on doing well one specific task in
a big system. It is also an autonomous entity that can be designed, developed
and deployed independently. Generally, it is implemented as a REST service on
HTTP protocol, with technology-agnostic APIs. Ideally, it does not share
database with any other service.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">2. What are the benefits of Microservices architecture?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Microservices provide many benefits. Some of the key
benefits are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Scaling</span></b><span style="font-size: 14pt;">: Since there are multiple Microservices instead of
one monolith, it is easier to scale up the service that is being used more. Eg.
Let say, you have a Product Lookup service and Product Buy service. The
frequency of Product Lookup is much higher than Product Buy service. In this
case, you can just scale up the Product Lookup service to run on powerful
hardware with multiple servers. Meanwhile, Product Buy service can remain on
less powerful hardware.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Resilience</span></b><span style="font-size: 14pt;">: In Microservice architecture, if your one service
goes down, it may not affect the rest of the system. The other parts can keep
functioning, business as usual (BAU). Eg. Let say, you have Product
Recommendation service and Product Buy service. If Product Recommendation
service goes down, the Product Buy service can still keep running.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Technology Mix</span></b><span style="font-size: 14pt;">: With so many changes in technology everyday, you can
keep using the latest technology for your new Microservices. You can adopt new
technologies with less risk compared to Monolithic architecture. This is one of
the best benefits of Microservices architecture.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Reuse</span></b><span style="font-size: 14pt;">: Microservices help you in reusing the lessons learnt
from one service to another.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Easy
Deployment</span></b><span style="font-size: 14pt;">: Microservices
architecture, if done correctly, helps in making the deployment process smooth.
If anything goes wrong, it can be rolled back easily and quickly in
Microservices.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">3. What is the role of architect in Microservices architecture?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Architects, in Microservices architecture, play the
role of Town planners. They decide in broad strokes about the layout of the
overall software system. They help in deciding the zoning of the components.
They make sure components are mutually cohesive but not tightly coupled. They
need not worry about what is inside each zone. Since they have to remain up to
date with the new developments and problems, they have to code with developers
to learn the challenges faced in day-to-day life. They can make recommendations
for certain tools and technologies, but the team developing a micro service is
ultimately empowered to create and design the service. Remember, a micro
service implementation can change with time. They have to provide technical
governance so that the teams in their technical development follow principles
of Microservice. At times they work as custodians of overall Microservices
architecture.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">4. What is the advantage of Microservices architecture over Service
Oriented Architecture (SOA)?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Service Oriented Architecture (SOA) is an approach to
develop software by creating multiple services. It creates small parts of
services and promotes reusability of software. But SOA development can be slow
due to use of things like communication protocols SOAP, middleware and lack of
principles. On the other hand, Microservices are agnostic to most of these
things. You can use any technology stack, any hardware/middleware, any protocol
etc. as long as you follow the principles of Microservices. Microservices
architecture also provides more flexibility, stability and speed of development
over SOA architecture.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">5. Is it a good idea to provide a Tailored Service Template for
Microservices development in an organization?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">If your organization is using similar set of
technologies, then it is a good idea to provide a Service Template that can be
tailored by development teams. It can make development faster. Also it can help
in promoting adoption of various good practices that are already built into
template. But if your organization uses wide variety of technologies, then it
may not be wise to produce and maintain a template for each service. Instead of
that, it is better to introduce tools that help in maintaining same set of
practices related to Microservices among all such technologies. There are many
organizations that provide tailored templates for Microservices. Eg. Dropwizard,
Karyon etc. You can use these templates to make faster development of services
in your organization. Also remember that template code should not promote
shared code. This can lead to tight coupling between Microservices.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">6. What are the disadvantages of using Shared libraries approach to
decompose a monolith application?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">You can create shared libraries to increase reuse and
sharing of features among teams. But there are some downsides to it. Since
shared libraries are implemented in same language, it constrains you from using
multiple types of technologies. It does not help you with scaling the parts of
system that need better performance. Deployment of shared libraries is same as
deployment of Monolith application, so it comes with same deployment issues.
Shared libraries introduce shared code that can increase coupling in software.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">7. What are the characteristics of a Good Microservice?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Good Microservices have these characteristics:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Loose coupling</span></b><span style="font-size: 14pt;">: A Microservice knows little about any other service.
It is as much independent as possible. The change made in one Microservice does
not require changes in other Microservices.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Highly
cohesive</span></b><span style="font-size: 14pt;">: Microservices are highly
cohesive so that each one of them can provide one set of behavior independently.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Bounded
Context</span></b><span style="font-size: 14pt;">: A Microservice serves a
bounded context in a domain and communicates with rest of the domain by using
an interface for that Bounded context.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l1 level1 lfo2; text-indent: -18pt;">
<!--[if !supportLists]--><span style="font-size: 14pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7pt "times new roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14pt;">Business
Capability</span></b><span style="font-size: 14pt;">: Microservices
individually add business capability that is part of big picture in
organization.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">8. What is Bounded Context?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">A bounded context is like a specific responsibility
that is developed within a boundary. In a domain there can be multiple bounded
contexts that are internally implemented. Eg. A hospital system can have
bounded contexts like- Emergency Ward handling, Regular vaccination, Out
patient treatment etc. Within each bounded context, each sub-system can be
independently designed and implemented.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">9. What are the points to remember during integration of
Microservices?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Some of the important points to remember during
integration of Microservices are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36pt;">
<b><span style="font-size: 14pt;">Technology Agnostic APIs</span></b><span style="font-size: 14pt;">: Developing Microservices in a technology agnostic
way helps in integration of multiple Microservices. With time, the technology
implementation can change but the interface between Microservices can remain
same.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36pt;">
<b><span style="font-size: 14pt;">Breaking Changes</span></b><span style="font-size: 14pt;">: Every change in Microservice should not become a breaking change for
client. It is better to minimize the impact of a change on an existing client.
So that existing clients’ do not have to keep changing their code to adapt to
changes in a Microservice.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36pt;">
<b><span style="font-size: 14pt;">Implementation Hiding</span></b><span style="font-size: 14pt;">: Each Microservice should hide its internal
implementation details from another one. This helps in minimizing the coupling
between Microservices that are integrated for a common solution.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36pt;">
<b><span style="font-size: 14pt;">Simple to use</span></b><span style="font-size: 14pt;">:
A Microservice should be simple to use for a consumer, so that the integration
points are simpler. It should allow clients to choose their own technology
stack.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">10. Is it a good idea for Microservices to share a common database?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Sharing a common database between multiple
Microservices increases coupling between them. One service can start accessing
data tables of another service. This can defeat the purpose of bounded context.
So it is not a good idea to share a common database between Microservices.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">11. What is the preferred type of communication between
Microservices? Synchronous or Asynchronous?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Synchronous communication is a blocking call in which
client blocks itself from doing anything else, till the response comes back. In
Asynchronous communication, client can move ahead with its work after making an
asynchronous call. Therefore client is not blocked.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">In synchronous communication, a Microservice can
provide instant response about success or failure. In real-time systems,
synchronous service is very useful. In Asynchronous communication, a service
has to react based on the response received in future. Synchronous systems are
also known as request/response based. Asynchronous systems are event-based.
Synchronous Microservices are not loosely coupled. Depending on the need and
critical nature of business domain, Microservices can choose synchronous or
asynchronous form of communication.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">12. What is the difference between Orchestration and Choreography in
Microservices architecture?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">In Orchestration, we rely on a central system to
control and call various Microservices to complete a task. In Choreography,
each Microservice works like a State Machine and reacts based on the input from
other parts. Orchestration is a tightly coupled approach for integrating
Microservices. But Choreography introduces loose coupling. Also, Choreography
based systems are more flexible and easy to change than Orchestration based
systems. Orchestration is often done by synchronous calls. But choreography is
done by asynchronous calls. The synchronous calls are much simpler compared to
asynchronous communication.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">13. What are the issues in using REST over HTTP for Microservices?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">In REST over HTTP, it is difficult to generate a
client stub. Some Web-Servers also do not support all the HTTP verbs likeGET,
PUT, POST, DELETE etc. Due to JSON or plain text in response, performance of
REST over HTTP is better than SOAP. But it is not as good as plain binary
communication. There is an overhead of HTTP in each request for communication.
HTTP is not well suited for low-latency communications. There is more work in
consumption of payload. There may be overhead of serialization, deserialization
in HTTP.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14pt;">14. Can we create Microservices as State Machines?</span></b><b><span style="font-size: 14pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<span style="font-size: 14pt;">Yes, Microservices are independent entities that serve
a specific context. For that context, the Microservice can work as a State
Machine. In a State Machine, there are lifecycle events that cause change in
the state of the system.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36pt;">
<b><span style="font-size: 14pt;">Eg.</span></b><span style="font-size: 14pt;"> In a
Library service, there is a book that changes state based on different events
like- issue a book, return a book, lose a book, late return of a book, add a
new book to catalog etc. These events and book can form a state machine for
Library Microservice.<o:p></o:p></span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-6055027824946017262019-05-05T09:00:00.000+05:302019-05-14T16:58:49.415+05:30Cloud Computing Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<b><span style="color: #4472c4; font-size: 18.0pt;">Cloud Computing<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">1. What are the benefits of Cloud Computing?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are ten main benefits of Cloud Computing:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Flexibility</span></b><span style="font-size: 14.0pt;">:
The businesses that have fluctuating bandwidth demands need the flexibility of
Cloud Computing. If you need high bandwidth, you can scale up your cloud
capacity. When you do not need high bandwidth, you can just scale down. There
is no need to be tied into an inflexible fixed capacity infrastructure.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Disaster Recovery</span></b><span style="font-size: 14.0pt;">: Cloud Computing provides robust backup and recovery solutions that
are hosted in cloud. Due to this there is no need to spend extra resources on
homegrown disaster recovery. It also saves time in setting up disaster
recovery.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Automatic Software Updates</span></b><span style="font-size: 14.0pt;">: Most of the Cloud providers give automatic software
updates. This reduces the extra task of installing new software version and
always catching up with the latest software installs.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Low Capital Expenditure</span></b><span style="font-size: 14.0pt;">: In Cloud computing the model is Pay as you Go. This
means there is very less upfront capital expenditure. There is a variable
payment that is based on the usage.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Collaboration</span></b><span style="font-size: 14.0pt;">:
In a cloud environment, applications can be shared between teams. This
increases collaboration and communication among team members.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Remote Work</span></b><span style="font-size: 14.0pt;">:
Cloud solutions provide flexibility of working remotely. There is no on site
work. One can just connect from anywhere and start working.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Security</span></b><span style="font-size: 14.0pt;">:
Cloud computing solutions are more secure than regular onsite work. Data stored
in local servers and computers is prone to security attacks. In Cloud Computing,
there are very few loose ends. Cloud providers give a secure working
environment to its users.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Document Control</span></b><span style="font-size: 14.0pt;">: Once the documents are stored in a common repository, it increases
the visibility and transparency among companies and their clients. Since there
is one shared copy, there are fewer chances of discrepancies.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Competitive Pricing</span></b><span style="font-size: 14.0pt;">: In Cloud computing there are multiple players, so they keep competing
among themselves and provide very good pricing. This comes out much cheaper
compared to other options.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Environment Friendly</span></b><span style="font-size: 14.0pt;">: Cloud computing saves precious environmental resources also. By not
blocking the resources and bandwidth.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">2. What is On-demand computing in Cloud Computing?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">On-demand Computing is the latest model in enterprise
systems. It is related to Cloud computing. It means IT resources can be
provided on demand by a Cloud provider. In an enterprise system demand for
computing resources varies from time to time. In such a scenario, On-demand
computing makes sure that servers and IT resources are provisioned to handle
the increase/decrease in demand. A cloud provider maintains a poll of
resources. The pool of resources contains networks, servers, storage,
applications and services. This pool can serve the varying demand of resources
and computing by various enterprise clients. There are many concepts like- grid
computing, utility computing, autonomic computing etc. that are similar to
on-demand computing. This is the most popular trend in computing model as of
now.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">3. What are the different layers of Cloud computing?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Three main layers of Cloud computing are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Infrastructure as a Service (<span style="color: red;">IAAS</span>)</span></b><span style="font-size: 14.0pt;">: IAAS providers give low-level abstractions of
physical devices. Amazon Web Services (AWS) is an example of IAAS. AWS provides
EC2 for computing, S3 buckets for storage etc. Mainly the resources in this
layer are hardware like memory, processor speed, network bandwidth etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Platform as a Service (<span style="color: red;">PAAS</span>)</span></b><span style="font-size: 14.0pt;">: PAAS providers offer managed services like Rails,
Django etc. One good example of PAAS is Google App Engineer. These are the
environments in which developers can develop sophisticated software with ease.
Developers just focus on developing software, whereas scaling and performance
is handled by PAAS provider.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Software as a Service (<span style="color: red;">SAAS</span>)</span></b><span style="font-size: 14.0pt;">: SAAS provider offer an actual working software
application to clients. Salesforce and Github are two good examples of SAAS.
They hide the underlying details of the software and just provide an interface
to work on the system. Behind the scenes the version of Software can be easily
changed.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">4. What resources are provided by Infrastructure as a Service (IAAS)
provider?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">An IAAS provider can give physical, virtual or both
kinds of resources. These resources are used to build cloud. IAAS provider
handles the complexity of maintaining and deploying these services. IAAS
provider also handles security and backup recovery for these services. The main
resources in IAAS are servers, storage, routers, switches and other related
hardware etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">5. What is the benefit of Platform as a Service?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Platform as a service (PaaS) is a kind of cloud
computing service. A PaaS provider offers a platform on which clients can
develop, run and manage applications without the need of building the
infrastructure. In PAAS clients save time by not creating and managing
infrastructure environment associated with the app that they want to develop.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">6. What are the main advantages of PaaS?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The advantages of PaaS are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">It allows development work on higher level programming
with very lesscomplexity. Teams can focus on just the development of the
application that makes the application very effective. Maintenance and
enhancement of the application is much easier. It is suitable for situations in
which multiple developers work on a single project but are not co-located.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">7. What is the main disadvantage of PaaS?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Biggest disadvantage of PaaS is that a developer can
only use the tools that PaaS provider makes available. A developer cannot use
the full range of conventional tools. Some PaaS providers lock in the clients
in their platform. This also decreases the flexibility of clients using PaaS.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">8. What are the different deployment models in Cloud computing?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Cloud computing supports following deployment models:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Private Cloud</span></b><span style="font-size: 14.0pt;">:
Some companies build their private cloud. A private cloud is a fully functional
platform that is owned, operated and used by only one organization. Primary
reason for private cloud is security. Many companies feel secure in private
cloud. The other reasons for building private cloud are strategic decisions or
control of operations. There is also a concept of Virtual Private Cloud (VPC).
In VPC, private cloud is built and operated by a hosting company. But it is
exclusively used by one organization.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Public Cloud</span></b><span style="font-size: 14.0pt;">:
There are cloud platforms by some companies that are open for general public as
well as big companies for use and deployment. E.g. Google Apps, Amazon Web
Services etc. The public cloud providers focus on layers and application like-
cloud application, infrastructure management etc. In this model resources are
shared among different organizations.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Hybrid Cloud</span></b><span style="font-size: 14.0pt;">:
The combination of public and private cloud is known as Hybrid cloud. This
approach provides benefits of both the approaches- private and public cloud. So
it is very robust platform. A client gets functionalities and features of both
the cloud platforms. By using Hybrid cloud an organization can create its own
cloud as well as they can pass the control of their cloud to another third
party.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">9. What is the difference between Scalability and Elasticity?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Scalability is the ability of a system to handle the
increased load on its current hardware and software resources. In a highly
scalable system it is possible to increase the workload without increasing the
resource capacity. Scalability supports any sudden surge in the demand/traffic
with current set of resources. Elasticity is the ability of a system to
increase the workload by increasing the hardware/software resources
dynamically. Highly elastic systems can handle the increased demand and traffic
by dynamically commission and decommission resources. Elasticity is an
important characteristic of Cloud Computing applications. Elasticity means how
well your architecture is adaptable to workload in real time.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">E.g.</span></b><span style="font-size: 14.0pt;"> If in a
system, one server can handle 100 users, 2 servers can handle 200 users and 10
servers can handle 1000 users. But in case for adding every X users, if you
need 2X the amount of servers, then it is not a scalable design. Let say, you
have just one user login every hour on your site. Your one server can handle
this load. But, if suddenly, 1000 users login at once, can your system quickly
start new web servers on the fly to handle this load? Your design is elastic if
it can handle such sudden increase in traffic so quickly.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">10. What is Software as a Service?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Software as Service is a category of cloud computing
in which Software is centrally hosted and it is licensed on a subscription
basis. It is also known as On-demand software. Generally, clients access the
software by using a thinclient like a web browser. Many applications like
Google docs, Microsoft office etc. provide SaaS model for their software. The
benefit of SaaS is that a client can add more users on the fly based on its
current needs. And client does not need to install or maintain any software on its
premises to use this software.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">11. What are the different types of Datacenters in Cloud computing?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Cloud computing consists of different types of
Datacenters linked in a grid structure. The main types of Datacenters in Cloud
computing are: <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Containerized Datacenter As the name suggests,
containerized datacenter provides high level of customization for an
organization. These are traditional kind of datacenters. We can choose the
different types of servers, memory, network and other infrastructure resources
in this datacenter. Also we have to plan temperature control, network
management and power management in this kind of datacenter. Low-Density
Datacenters In a Low-density datacenter, we get high level of performance. In
such a datacenter if we increase the density of servers, the issue with power
comes. With high density of servers, the area gets heated. In such a scenario,
effective heat and power management is done. To reach high level of
performance, we have to optimize the number of servers’ in the datacenter.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">12. Explain the various modes of Software as a Service (SaaS) cloud
environment?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Software as a Service (SaaS) is used to offer
different kinds of software applications in a Cloud environment. Generally
these are offered on subscription basis. Different modes of SaaS are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Simple multi-tenancy</span></b><span style="font-size: 14.0pt;">: In this setup, each client gets its own resources. These resources
are not shared with other clients. It is more secure option, since there is no
sharing of resources. But it an inefficient option, since for each client more
money is needed to scale it with the rising demands. Also it takes time to
scale up the application in this mode.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Fine grain multi-tenancy</span></b><span style="font-size: 14.0pt;">: In this mode, the feature provided to each client is
same. The resources are shared among multiple clients. It is an efficient mode
of cloud service, in which data is kept private among different clients but
computing resources are shared. Also it is easier and quicker to scale up the
SaaS implementation for different clients.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">13. What are the important things to care about in Security in a
cloud environment?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a cloud-computing environment, security is one of
the most important aspects. With growing concern of hacking, every organization
wants to make its software system and data secure. Since in a cloud computing
environment, Software and hardware is not on the premises of an organization,
it becomes more important to implement the best security practices.
Organizations have to keep their Data most secure during the transfer between
two locations. Also they have to keep data secure when it is stored at a
location. Hackers can hack into application or they can get an unauthorized
copy of the data. So it becomes important to encrypt the data during transit as
well as during rest to protect it from unwanted hackers.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">14. Why do we use API in cloud computing environment?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Application Programming Interfaces (API) is used in
cloud computing environment for accessing many services. APIs are very easy to
use. They provide a quick option to create different set of applications in
cloud environment. An API provides a simple interface that can be used in
multiple scenarios. There are different types of clients for cloud computing
APIs. It is easier to serve different needs of multiple clients with APIs in cloud
computing environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">15. What are the different areas of Security Management in cloud?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Different areas of Security management in cloud are as
follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Identity Management</span></b><span style="font-size: 14.0pt;">: This aspect creates different level of users, roles and their
credentials to access the services in cloud.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Access Control</span></b><span style="font-size: 14.0pt;">:
In this area, we create multiple levels of permissions and access areas that
can be given to a user or role for accessing a service in cloud environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Authentication</span></b><span style="font-size: 14.0pt;">:
In this area, we check the credentials of a user and confirm that it is the
correct user. Generally this is done by user password and multi-factor
authentication like-verification by a one-time use code on cell phone. <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Authorization</span></b><span style="font-size: 14.0pt;">:
In this aspect, we check for the permissions that are given to a user or role.
If a user is authorized to access a service, they are allowed to use it in the
cloud environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">16. What are the main cost factors of cloud based data center?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Costs in a Cloud based data center are different from
a traditional data center. Main cost factors of cloud based data center are as
follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Labor cost</span></b><span style="font-size: 14.0pt;">:
We need skilled staff that can work with the cloud-based datacenter that we
have selected for our operation. Since cloud is not a very old technology, it
may get difficult to get the right skill people for handling cloud based
datacenter.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Power cost</span></b><span style="font-size: 14.0pt;">:
In some cloud operations, power costs are borne by the client. Since it is a
variable cost, it can increase with the increase in scale and usage.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Computing cost</span></b><span style="font-size: 14.0pt;">:
The biggest cost in Cloud environment is the cost that we pay to Cloud provider
for giving us computing resources. This cost is much higher compared to the
labor or power costs.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">17. How can we measure the cloud-based services?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a cloud-computing environment we pay for the
services that we use. So main criteria to measure a cloud based service its
usage. For computing resource we measure by usage in terms of time and the
power of computing resource. For a storage resource we measure by usage in
terms of bytes (giga bytes) and bandwidth used in data transfer. Another
important aspect of measuring a cloud service is its availability. A cloud
provider has to specify the service level agreement (SLA) for the time for
which service will be available in cloud.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">18. How a traditional datacenter is different from a cloud
environment?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a traditional datacenter the cost of increasing the
scale of computing environment is much higher than a Cloud computing
environment. Also in a traditional data center, there are not much benefits of
scaling down the operation when demand decreases. Since most of the expenditure
is in capital spent of buying servers etc., scaling down just saves power cost,
which is very less compared to other fixed costs. Also in a Cloud environment
there is no need to higher a large number of operations staff to maintain the
datacenter. Cloud provider takes care of maintaining and upgrading the
resources in Cloud environment. With a traditional datacenter, people cost is
very high since we have to hire a large number of technical operation people
for in-house datacenter.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">19. How will you optimize availability of your application in a
Cloud environment?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a Cloud environment, it is important to optimize
the availability of an application by implementing disaster recovery strategy.
For disaster recovery we create a backup application in another location of
cloud environment. In case of complete failure at a data center we use the
disaster recovery site to run the application. Another aspect of cloud
environment is that servers often fail or go down. In such a scenario it is
important to implement the application in such a way that we just kill the slow
server and restart another server to handle the traffic seamlessly.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">20. What are the requirements for implementing IaaS strategy in
Cloud?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Main requirements to implement IAAS are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Operating System (OS)</span></b><span style="font-size: 14.0pt;">: We need an OS to support hypervisor in IaaS. We can use
open source OS like Linux for this purpose.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Networking</span></b><span style="font-size: 14.0pt;">:
We have to define and implement networking topology for IaaSimplementation. We
can use public or private network for this. <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Cloud Model</span></b><span style="font-size: 14.0pt;">:
We have to select the right cloud model for implementing IaaS strategy. It can
be SaaS, PaaS or CaaS.<o:p></o:p></span></div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-955003516632510022019-03-31T12:30:00.000+05:302019-05-22T18:27:56.126+05:30Amazon Web Services(AWS) Interview questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="text-align: center;">
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.87); font-family: "arial" , , sans-serif; font-size: 24px;"><b><br /></b></span></div>
<div class="MsoNormal" style="text-align: center;">
<span style="background-color: white; color: rgba(0 , 0 , 0 , 0.87); font-family: "arial" , , sans-serif; font-size: 24px;"><b>Amazon Web Services</b></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">1. What do you know about AWS Region?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">An AWS Region is a completely independent entity in a
geographical area. There are two more Availability Zones in an AWS Region.
Within a region, Availability Zones are connected through lowlatency links.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Since each AWS Region is isolated from another Region, it
provides very high fault tolerance and stability. For launching an EC2
instance, we have to select an AMI within the same region.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">2. What are the important components of IAM?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">The important components of IAM are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l9 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">IAM User</span></b><span style="font-size: 16.0pt; line-height: 107%;">: An IAM User is a person or service
that will interact with AWS. User can sign into AWS Management Console for
performing tasks in AWS.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l9 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">IAM Group</span></b><span style="font-size: 16.0pt; line-height: 107%;">: An IAM Group is a collection of IAM
users. We can specify permission to an IAM Group. This helps in managing large
number of IAM users. We can simply add or remove an IAM User to an IAM Group to
manage the permissions.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l9 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">IAM Role</span></b><span style="font-size: 16.0pt; line-height: 107%;">: An IAM Role is an identity to which
we give permissions. A Role does not have any credentials (password or access
keys). We can temporarily give an IAM Role to an IAM User to perform certain
tasks in AWS.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l9 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">IAM Permission</span></b><span style="font-size: 16.0pt; line-height: 107%;">: In IAM we can create two types of
Permissions. Identity based and Resource based. We can create a Permission to
access or perform an action on an AWS Resource and assign it to a User, Role or
Group. We can also create Permissions on resources like S3 bucket, Glacier
vault etc and specify who has access to the resource.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l9 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">IAM Policy</span></b><span style="font-size: 16.0pt; line-height: 107%;">: An IAM Policy is a document in
which we list permissions to specify Actions, Resources and Effects. This
document is in JSON format. We can attach a Policy to an IAM User or Group.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">3. What are the important points about AWS IAM?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Some of the important points about AWS IAM are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l8 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">A new User in IAM does not have any
permission.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l8 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">AWS IAM assigns an Access Key and a
Secret Access Key to a new User.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l8 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">An Access Key cannot be used to login
to AWS Console.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l8 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">We use Access Key to access AWS via
an APIs or Command Line interface.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l8 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">IAM is a universal application. It is
common across all the regions in AWS.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l8 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">When we first setup our AWS account,
we get a root account that has complete Admin access.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">4. What are the important features of Amazon S3?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Some of the important features of Amazon S3 are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 provides unlimited storage
for files.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">File size in Amazon S3 can vary from
0 Bytes to 5 Terabytes.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">We have store files in Buckets in
Amazon S3.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">In Amazon S3, names of buckets have
to be unique globally.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 is Object Based storage.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">5. What is the scale of durability in Amazon S3?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 supports durability at the scale of 99.999999999%
of time. This is 9 nines after decimal.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">6. What are the Consistency levels supported by Amazon S3?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 supports Read after Write consistency when we
create a new object by PUT. It means as soon as we Write a new object, we can
access it. Amazon S3 supports Eventual Consistency when we overwrite an existing
object by PUT. Eventual Consistency means that the effect of verwrite will not
be immediate but will happen after some time. For deletion of an object, Amazon
S3 supports Eventual Consistency after DELETE.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">7. What are the different tiers in Amazon S3 storage?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Different Storage tiers in Amazon S3 are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l0 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">S3 Standard</span></b><span style="font-size: 16.0pt; line-height: 107%;">: In this tier, S3 supports durable
storage of files that become immediately available. This is used for frequently
used files.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l0 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">S3 Standard -Infrequent Access (IA)</span></b><span style="font-size: 16.0pt; line-height: 107%;">: In this tier, S3 provides durable
storage that is immediately available. But in this tier files are infrequently
accessed.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l0 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">S3 Reduced Redundancy Storage (RRS)</span></b><span style="font-size: 16.0pt; line-height: 107%;">: In this tier, S3 provides the
option to customers to store data at lower levels of redundancy. In this case
data is copied to multiple locations but not on as many locations as standard
S3.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">8. How will you upload a file greater than 100 megabytes in Amazon
S3?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 supports storing objects or files up to 5
terabytes. To upload a file greater than 100 megabytes, we have to use
Multipart upload utility from AWS. By using Multipart upload we can upload a
large file in multiple parts. Each part will be independently uploaded. It
doesn’t matter in what order each part is uploaded. It even supports uploading
these parts in parallel to decrease overall time. Once all the parts are
uploaded, this utility makes these as one single object or file from which the
parts were created.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">9. What happens to an Object when we delete it from Amazon S3?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 provides DELETE API to delete an object. If the
bucket in which the object exists is version controlled, then we can specify
the version of the object that we want to delete. The other versions of the
Object still exist within the bucket. If we do not specify the version, and
just pass the key name, Amazon S3 will delete the object and return the version
id. And the object will not appear on the bucket. In case the bucket is
Multi-factor authentication (MFA) enabled, then the DELETE request will fail if
we do not specify a MFA token.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">10. What is the use of Amazon Glacier?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Amazon Glacier is an extremely low cost cloud based storage
service provided by Amazon. We mainly use Amazon Glacier for long-term backup
purpose. Amazon Glacier can be used for storing data archives for months, years
or even decades.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">It can also be used for long term immutable storage based on
regulatory and archiving requirements. It provides Vault Lock support for this
purpose. In this option, we write once but can read many times same data. One
use case is for storing certificates that can be issued only once and only the
original person keeps the main copy.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">11. Can we disable versioning on a version-enabled bucket in Amazon
S3?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">No, we cannot disable versioning on a version-enabled bucket
in Amazon S3. We can just suspend the versioning on a bucket in S3. Once we
suspend versioning, Amazon S3 will stop creating new versions of the object. It
just stores the object with null version ID.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">On overwriting an existing object, it just replaces the
object with null version ID. So any existing versions of the object still
remain in the bucket. But there will be no more new versions of the same object
except for the null version ID object.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">12. What are the use cases of Cross Region Replication Amazon S3?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">We can use Cross Region Replication Amazon S3 to make copies
of an object across buckets in different AWS Regions. This copying takes place
automatically and in an asynchronous mode. We have to add replication
configuration on our source bucket in S3 to make use of Cross Region
Replication. It will create exact replicas of the objects from source bucket to
destination buckets in different regions.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Some of the main use cases of Cross Region Replication are as
follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l1 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Compliance</span></b><span style="font-size: 16.0pt; line-height: 107%;">: Sometimes there are laws/regulatory
requirements that ask for storing data at farther geographic locations. This
kind of compliance can be achieved by using AWS Regions that are spread across
the world.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l1 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Failover</span></b><span style="font-size: 16.0pt; line-height: 107%;">: At times, we want to minimize the
probability of system failure due to complete blackout in a region. We can use Cross
Region Replication in such a scenario.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l1 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Latency</span></b><span style="font-size: 16.0pt; line-height: 107%;">: In case we are serving multiple
geographies, it makes sense to replicate objects in the geographical Regions
that are closer to end customer. This helps in reducing the latency.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">13. Can we do Cross Region replication in Amazon S3 without enabling
versioning on a bucket?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">No, we have to enable versioning on a bucket to perform Cross
Region Replication.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">14. What are the different types of actions in Object Lifecycle
Management in Amazon S3?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">There are mainly two types of Object Lifecycle Management
actions in Amazon S3.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l4 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Transition Actions</span></b><span style="font-size: 16.0pt; line-height: 107%;">: These actions define the state when
an Object transitions from one storage class to another storage class. E.g. a
new object may transition to STANDARD_IA (infrequent access) class after 60
days of creation. And it can transition to GLACIER after 180 days of creation.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l4 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Expiration Actions</span></b><span style="font-size: 16.0pt; line-height: 107%;">: These actions specify what happens
when an Object expires. We can ask S3 to delete an object completely on
expiration.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">15. How do we get higher performance in our application by using
Amazon CloudFront?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">If our application is content rich and used across multiple
locations, we can use Amazon CloudFront to increase its performance. Some of
the techniques used by Amazon CloudFront are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="font-size: 16.0pt; line-height: 107%;">Caching</span></b><span style="font-size: 16.0pt; line-height: 107%;">: Amazon CloudFront caches the copies of our application’s content at
locations closer to our viewers. By this caching our users get our content very
fast. Also due to caching the load on our main server decreases.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="font-size: 16.0pt; line-height: 107%;">Edge / Regional Locations</span></b><span style="font-size: 16.0pt; line-height: 107%;">: CloudFront uses a global network of Edge and
Regional edge locations to cache our content. These locations cater to almost
all of the geographical areas across the world.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="font-size: 16.0pt; line-height: 107%;">Persistent Connections</span></b><span style="font-size: 16.0pt; line-height: 107%;">: In certain cases, CloudFront keeps persistent connections
with the main server to fetch the content quickly.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="font-size: 16.0pt; line-height: 107%;">Other Optimization</span></b><span style="font-size: 16.0pt; line-height: 107%;">: Amazon CloudFront also uses other optimization techniques
like TCP initial congestion window etc to deliver high performance experience.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">16. What is the mechanism behind Regional Edge Cache in Amazon
CloudFront?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">A Regional Edge Cache location lies between the main
webserver and the global edge location. When the popularity of an
object/content decreases, the global edge location may take it out from the
cache. But Regional Edge location maintains a larger cache. Due to this the
object/content can stay for long time in Regional Edge location. Due to this
CloudFront does not have to go back to main webserver. When it does not find
any object in Global Edge location it just looks for in Regional Edge location.
This improves the performance for serving content to our users in Amazon
CloudFront.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">17. What are the benefits of Streaming content?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">We can get following benefits by Streaming content:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Control</span></b><span style="font-size: 16.0pt; line-height: 107%;">: We can provide more control to our
users for what they want to watch. In a video streaming, users can select the
locations in video where they want to start watching from.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Content</span></b><span style="font-size: 16.0pt; line-height: 107%;">: With streaming our entire content
does not stay at a user’s device. Users gets only the part they are watching.
Once the session is over, content is removed from the user’s device.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Cost</span></b><span style="font-size: 16.0pt; line-height: 107%;">: With streaming there is no need to
download all the content to a user’s device. A user can start viewing content
as soon as some part is available for viewing. This saves costs since we do not
have to download a large media file before starting each viewing session.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">18. What is Lambda@Edge in AWS?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">In AWS, we can use Lambda@Edge utility to solve the problem
of low network latency for end users. In Lambda@Edge there is no need to
provision or manage servers. We can just upload our Node.js code to AWS Lambda
and create functions that will be triggered on CloudFront requests. When a
request for content is received by CloudFront edge location, the Lambda code is
ready to execute. This is a very good option for scaling up the operations in
CloudFront without managing servers.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">19. What are the different types of events triggered by Amazon
CloudFront?<o:p></o:p></span></b></div>
<div class="MsoNormal">
<span style="font-size: 16.0pt; line-height: 107%;">Different
types of events triggered by Amazon CloudFront are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l2 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Viewer Request</span></b><span style="font-size: 16.0pt; line-height: 107%;">: When an end user or a client
program makes an HTTP/HTTPS request to CloudFront, this event is triggered at
the Edge Location closer to the end user.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l2 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Viewer Response</span></b><span style="font-size: 16.0pt; line-height: 107%;">: When a CloudFront server is ready
to respond to a request, this event is triggered.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l2 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Origin Request</span></b><span style="font-size: 16.0pt; line-height: 107%;">: When CloudFront server does not
have the requested object in its cache, the request is forwarded to Origin
server. At this time this event is triggered.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l2 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Origin Response</span></b><span style="font-size: 16.0pt; line-height: 107%;">: When CloudFront server at an Edge
location receives the response from Origin server, this event is triggered.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">20. What is Geo Targeting in Amazon CloudFront?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">In Amazon CloudFront we can detect the country from where end
users are requesting our content. This information can be passed to our Origin
server by Amazon CloudFront. It is sent in a new HTTP header. Based on
different countries we can generate different content for different versions of
the same content. These versions can be cached at different Edge Locations that
are closer to the end users of that country. In this way we are able to target
our end users based on their geographic locations.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">21. What are the main features of Amazon CloudFront?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Some of the main features of Amazon CloudFront are as
follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Device Detection<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Protocol Detection<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Geo Targeting<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Cache Behavior<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Cross Origin Resource Sharing<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Multiple Origin Servers<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">7.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">HTTP Cookies<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">8.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Query String Parameters<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l7 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">9.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 16.0pt; line-height: 107%;">Custom SSL<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-size: 16.0pt; line-height: 107%;">22. What are the security mechanisms available in Amazon S3?</span></b><b><span style="font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="font-size: 16.0pt; line-height: 107%;">Amazon S3 is a very secure storage service. Some of the main
security mechanisms available in Amazon S3 are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l5 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Access</span></b><span style="font-size: 16.0pt; line-height: 107%;">: When we create a bucket or an
object, only the owner get the access to the bucket and objects.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l5 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Authentication</span></b><span style="font-size: 16.0pt; line-height: 107%;">: Amazon S3 also support user
authentication to control who has access to a specific object or bucket.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l5 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Access Control List</span></b><span style="font-size: 16.0pt; line-height: 107%;">: We can create Access Control Lists
(ACL) to provide selective permissions to users and groups.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l5 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">HTTPS</span></b><span style="font-size: 16.0pt; line-height: 107%;">: Amazon S3 also supports HTTPS
protocol to securely upload and download data from cloud.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 54.0pt; mso-add-space: auto; mso-list: l5 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 16.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 16.0pt; line-height: 107%;">Encryption</span></b><span style="font-size: 16.0pt; line-height: 107%;">: We can also use Server Side
Encryption (SSE) in Amazon S3 to encrypt data.<o:p></o:p></span></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
</div>
Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-473301044368505040.post-53495753157213212642019-03-27T09:30:00.000+05:302019-05-22T18:28:44.583+05:30git interview questions part-2<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="text-align: center;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><b>GIT </b><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">1.Why do we need git add command in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT gives us a very
good feature of staging our changes before commit. To stage the changes we use
git add command. This adds our changes from working directory to the index.
When we are working on multiple tasks and we want to just commit the finished tasks,
we first add finished changes to staging area and then commit it. At this time
git add command is very helpful.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">2. Why do we use git reset command?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We use git reset
command to reset current HEAD to a specific state. By default it reverses the
action of git add command. So we use git reset command to undo the changes of
git add command.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">3. What does a commit object contain?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Whenever we do a
commit in GIT by using git commit command, GIT creates a new commit object.
This commit objects is saved to GIT repository.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">The commit object
contains following information:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">HASH</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: The SHA1 hash of
the Git tree that refers to the state of index at commit time.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Commit Author</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: The name of person/process doing the commit and date/time.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Comment</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: Some text messages that contains the reason for the commit .<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">4. How can we convert git log messages to
a different format?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use pretty
option in git log command for this.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git log – pretty<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">This option converts
the output format from default to other formats. There are pre-built formats
available for our use.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git log –pretty=oneline <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">E.g. git log
--pretty=format:"%h - %an, %ar : %s" ba72a6c - Dave Adams, 3 years
ago : changed the version number<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">5. What are the programming languages in
which git hooks can be written?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Git hooks are
generally written in shell and PERL scripts. But these can be written in any
other language as long as it has an executable. Git hooks can also be written
in Python script.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">6. What is a commit message in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">A commit message is a
comment that we add to a commit. We can provide meaningful information about
the reason for commit by using a commit message. In most of the organizations,
it is mandatory to put a commit message along with each commit. Often, commit
messages contain JIRA ticket, bug id, defect id etc. for a project.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">7. How GIT protects the code in a
repository?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT is made very
secure since it contains the source code of an organization. All the objects in
a GIT repository are encrypted with a hashing algorithm called SHA1. This
algorithm is quite strong and fast. It protects source code and other contents
of repository against the possible malicious attacks. This algorithm also
maintains the integrity of GIT repository by protecting the change history
against accidental changes.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">8. How GIT provides flexibility in
version control?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT is very flexible
version control system. It supports non-linear development workflows. It
supports flows that are compatible with external protocols and existing
systems. GIT also supports both branching and tagging that promotes multiple
kinds of workflows in version control.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">9. How can we change a commit message in
GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">If a commit has not
been pushed to GitHub, we can use git commit -- ammend command to change the
commit message. When we push the commit, a new message appears on GitHub.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">10. Why is it advisable to create an
additional commit instead of amending an existing commit?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Git amend internally
creates a new commit and replaces the old commit. If commits have already been
pushed to central repository, it should not be used to modify the previous
commits. It should be generally used for only amending the git comment.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">11.What is a bare repository in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">A repository created
with git init –bare command is a bare repository in GIT. The bare repository
does not contain any working or checked out copy of source files. A bare
repository stores git revision history in the root folder of repository instead
of in a .git subfolder. It is mainly used for sharing and collaborating with
other developers. We can create a bare repository in which all developers can
push their code. There is no working tree in bare repository, since no one
directly edits files in a bare repository.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">12. How do we put a local repository on
GitHub server?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">To put a local
repository on GitHub, we first add all the files of working directory into
local repository and commit the changes. After that we call git remote add
<Remote Repo URL> command to add the local repository on GitHub server.
Once it is added, we use git push command to push the contents of local
repository to remote GitHub server.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">13. How will you delete a branch in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We use git branch –d
<branchname> command to delete a branch in GIT. In case a local branch is
not fully merged, but we want to delete it by force, then we use git branch –D
<branchname> command.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">14. How can we set up a Git repository to
run code sanity checks and UAT tests just before a commit?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use git hooks
for this kind of purpose. We can write the code sanity checks in script. This
script can be called by pre-commit hook of the repository. If this hook passes,
then only commit will be successful.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">15. How can we revert a commit that was
pushed earlier and is public now?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use git revert
command for this purpose. Internally, git revert command creates a new commit
with patches that reverse the changes done in previous commits. The other
option is to checkout a previous commit version and then commit it as a new
commit.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">16. In GIT, how will you compress last n
commits into a single commit</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">?<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Tom compress last n
commits a single commit, we use git rebase command. This command compresses
multiple commits and creates a new commit. It overwrites the history of commits.
It should be done carefully, since it can lead to unexpected results.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">17. How will you switch from one branch
to a new branch in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT, we can use
git checkout <new branchname> command to switch to a new branch.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">18. How can we clean unwanted files from
our working directory in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT provides git
clean command to recursively clean the working tree. It removes the files that
are not under version control in GIT. If we use git clean –x, then ignored
files are also removed.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">19. What is the purpose of git tag
command?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We use git tag
command to add, delete, list or verify a tag object in GIT. Tag objects created
with options –a, -s, -u are also known as annotated tags. Annotated tags are
generally used for release.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">20. What is cherry-pick in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">A git cherry-pick is
a very useful feature in GIT. By using this command we can selectively apply
the changes done by existing commits. In case we want to selectively release a
feature, we can remove the unwanted files and apply only selected commits.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">21.What is shortlog in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">A shortlog in GIT is
a command that summarizes the git log output. The output of git shortlog is in
a format suitable for release announcements.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">22. How can you find the names of files
that were changed in a specific commit?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Every commit in GIT
has a hash code. This hash code uniquely represents the GIT commit object.<span style="mso-spacerun: yes;"> </span>We can use git diff-tree command to list the
name of files that were changed in a commit. The command will be as follows:
git diff-tree -r <hash of commit> By using -r flag, we just get the list
of individual files.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">23. How can we attach an automated script
to run on the event of a new commit by push command?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT we can use a
hook to run an automated script on a specific event. We can choose between
pre-receive, update or post-receive hook and attach our script on any of these
hooks. GIT will automatically run the script on the event of any of these
hooks.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">24. What is the difference between
pre-receive, update and post-receive hooks in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Pre-receive hook is
invoked when a commit is pushed to a destination repository. Any script
attached to this hook is executed before updating any reference. This is mainly
used to enforce development best practices and policies. Update hook is similar
to pre-receive hook. It is triggered just before any updates are done. This
hook is invoked once for every commit that is pushed to a destination
repository. Post-receive hook is invoked after the updates have been done and
accepted by a destination repository. This is mainly used to configure
deployment scripts. It can also invoke Continuous Integration (CI) systems and
send notification emails to relevant parties of a repository.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">25. Do we have to store Scripts for GIT
hooks within same repository?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">A Hook is local to a
GIT repository. But the script attached to a hook can be created either inside
the hooks directory or it can be stored in a separate repository. But we have
to link the script to a hook in our local repository. In this way we can
maintain versions of a script in a separate repository, but use them in our
repository where hooks are stored. Also when we store scripts in a separate
common repository, we can reuse same scripts for different purposes in multiple
repositories.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">26. How can we determine the commit that is
the source of a bug in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT we can use git
bisect command to find the commit that has introduced a bug in the system. GIT
bisect command internally uses binary search algorithm to find the commit that
introduced a bug. We first tell a bad commit that contains the bug and a good
commit that was present before the bug was introduced. Then git bisect picks a
commit between those two endpoints and asks us whether the selected commit is
good or bad. It continues to narrow down the range until it discovers the exact
commit responsible for introducing the bug.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">27. How can we see differences between
two commits in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use git diff
command to see the differences between two commits. The syntax for a simple git
diff command to compare two commits is:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git diff <commit#1> <commit#2><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">28. What are the different ways to
identify a commit in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Each commit object in
GIT has a unique hash. This hash is a 40 characters checksum hash. It is based
on SHA1 hashing algorithm. We can use a hash to uniquely identify a GIT commit.
Git also provides support for creating an alias for a commit. This alias is
known as refs. Every tag in GIT is a ref. These refs can also be used to
identify a commit. Some of the special tags in GIT are HEAD, FETCH_HEAD and
MERGE_HEAD.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">29. When we run git branch
<branchname>, how does GIT know the SHA-1 of the last commit?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT uses the
reference named HEAD for this purpose. The HEAD file in GIT is a symbolic
reference to the current branch we are working on. A symbolic reference is not
a normal reference that contains a SHA-1 value. A symbolic reference contains a
pointer to another reference.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">When we open head file we see:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">$ cat .git/HEAD<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">ref: refs/heads/master<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">If we run git checkout branchA, Git updates the
file to look like this:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">$ cat .git/HEAD<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">ref: refs/heads/branchA<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">30. What are the different types of Tags
you can create in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT, we can create
two types of Tags. Lightweight Tag: A lightweight tag is a reference that never
moves. We can make a lightweight tag by running a command similar to following:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">$ git update-ref refs/tags/v1.0<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">dad0dab538c970e37ea1e769cbbde608743bc96d<o:p></o:p></span></div>
<div class="MsoNormal">
<b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Annotated Tag</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: An annotated tag is more complex object in GIT. When we create an
annotated tag, GIT creates a tag object and writes a reference to point to it
rather than directly to the commit. We can create an annotated tag as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">$ git tag -a v1.1
1d410eabc13591cb07496601ebc7c059dd55bfe9 -m 'test tag'<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">31. How can we rename a remote
repository?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use command
git remote rename for changing the name of a remote repository. This changes
the short name associated with a remote repository in your local. Command would
look as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git remote rename
repoOldName repoNewName<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">32. Some people use git checkout and some
use git co for checkout. How is that possible?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can create aliases
in GIT for commands by modifying the git configuration. In case of calling git
co instead of git checkout we can run following command:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git config --global
alias.co checkout So the people using git co have made the alias for git
checkout in their own environment.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">33. How can we see the last commit on
each of our branch in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">When we run git
branch command, it lists all the branches in our local repository. To see the
latest commit associated with each branch, we use option –v.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Exact command for this is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git branch –v<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">It lists branches as:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">issue75 83b576c fix issue * master 7b96605 Merge branch 'issue75'
testing 972ac34 add dave to the developer list<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">34. Is origin a special branch in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">No, origin is not a
special branch in GIT. Branch origin is similar to branch master. It does not
have any special meaning in GIT. Master is the default name for a starting
branch when we run git init command. Origin is the default name for a remote
when we run git clone command. If we run git clone -o myOrigin instead, then we
will have myOrigin/master as our default remote branch.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">35. How can we configure GIT to not ask
for password every time?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">When we use HTTPS URL
to push, the GIT server asks for username and password for authentication. It
prompts us on the terminal for this information. If we don’t want to type
username/password with every single time push, we can set up a “credential
cache”. It is kept in memory for a few minutes. We can set it by running:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git config --global credential.helper cache<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">36. What are the four major protocols
used by GIT for data transfer?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT uses following
major protocols for data transfer:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">1. Local<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">2. HTTP<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">3. Secure Shell (SSH)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">4. Git<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">37. What is GIT protocol?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Git protocol is a
mechanism for transferring data in GIT. It is a special daemon. It comes
pre-packaged with GIT. It listens on a dedicated port 9418. It provides
services similar to SSH protocol. But Git protocol does not support any
authentication. So on plus side, this is a very fast network transfer protocol.
But it lacks authentication.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">38. How can we work on a project where we
do not have push access?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In case of projects
where we do not have push access, we can just fork the repository. By running
git fork command, GIT will create a personal copy of the repository in our
namespace. Once our work is done, we can create a pull request to merge our
changes on the real project.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">39. What is git grep?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT is shipped along
with a grep command that allows us to search for a string or regular expression
in any committed tree or the working directory. By default, it works on the
files in your current working directory.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">40. How can your reorder commits in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use git rebase
command to reorder commits in GIT. It can work interactively and you can also
select the ordering of commits.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">41.How will you split a commit into
multiple commits?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">To split a commit, we
have to use git rebase command in interactive mode. Once we reach the commit
that needs to be split, we reset that commit and take the changes that have
been reset. Now we can create multiple commits out of that.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">42. What is filter-branch in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT, filter-branch
is another option to rewrite history. It can scrub the entire history. When we
have large number of commits, we can use this tool. It gives many options like
removing the commit related changes to a specific file from history. You can
even set you name and email in the commit history by using filter-branch.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">43. What are the three main trees
maintained by GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT maintains
following three trees:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">HEAD</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: This is the last commit snapshot.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Index</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: This is the proposed next commit snapshot.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Working Directory</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">: This is the sandbox for doing changes.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">44. What are the three main steps of
working GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT has following
three main steps in a simple workflow:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">1. Checkout the
project from HEAD to Working Directory.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">2. Stage the files
from Working Directory to Index.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">3. Commit the changes
from Index to HEAD.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">45. What are ours and theirs merge
options in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT, we get two
simple options for resolving merge conflicts: ours and theirs These options
tell the GIT which side to favor in merge conflicts. In ours, we run a command
like git merge -Xours branchA As the name suggests, in ours, the changes in our
branch are favored over the other branch during a merge conflict.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">46. How can we ignore merge conflicts due
to Whitespace?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">GIT provides an
option ignore-space-change in git merge command to ignore the conflicts related
to whitespaces. The command to do so is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git merge -Xignore-space-change whitespace<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">47. What is git blame?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT, git blame is
a very good option to find the person who changed a specific line. When we call
git blame on a file, it displays the commit and name of a person responsible
for making change in that line.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Following is a sample:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">$ git blame -L 12,19
HelloWorld.java<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">^1822fe2 (Dave Adams
2016-03-15 10:31:28 -0700 12) <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">public class
HelloWorld {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">^1822fe2 (Dave Adams 2016-03-15 10:31:28 -0700 13)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">^1822fe2 (Dave Adams 2016-03-15 10:31:28 -0700 14) <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">Public static void main(String[] args) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">af6560e4 (Dave Adams 2016-03-17 21:52:20 -0700 16) <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="color: #afabab; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">// Prints "Hello, World" to
the terminal window.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">a9eaf55d (Dave Adams 2016-04-06 10:15:08 -0700 17)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">System.out.println("Hello, World");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">af6560e4 (Dave Adams 2016-03-17 21:52:20 -0700 18) <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">af6560e4 (Dave Adams 2016-03-17 21:52:20 -0700 19) <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">}<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">48. What is a submodule in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">In GIT, we can create
sub modules inside a repository by using git submodule command. By using
submodule command, we can keep a Git repository as a subdirectory of another
Git repository. It allows us to keep our commits to submodule separate from the
commits to main Git repository.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">49.How can we see n most recent commits
in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use git log
command to see the latest commits. To see the three most recent commits we use
following command:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git log -3<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="color: red; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">50. How can we know if a branch is
already merged into master in GIT?</span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">We can use following
commands for this purpose:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="color: #548235; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git branch </span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">--merged master</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"> : This prints the branches merged into
master<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="color: #548235; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git branch </span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">--merged lists</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"> : This prints the branches merged into
HEAD (i.e. tip of current branch)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<b><span style="color: #548235; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">git branch </span></b><b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;">--no-merged</span></b><span style="color: black; font-family: "times new roman" , serif; font-size: 16.0pt; line-height: 107%;"> : This prints the branches that have
not been merged By default this applies only to local branches. We can use -a
flag to show both local and remote branches. Or we can use -r flag to show only
the remote branches.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<br />
<br />
<br />
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-73086464825827852902019-03-23T10:30:00.000+05:302019-05-22T18:29:07.671+05:30git interview questions<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="line-height: normal; text-align: center;">
<span style="color: blue; font-size: large;">GIT</span></h2>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">1.What is the purpose of git stash drop?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In case we do not need a specific stash, we use git
stash drop command to remove it from the list of stashes. By default, this
command removes to latest added stash To remove a specific stash we specify as
argument in the git stash drop <stashname> command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">2. What is the HEAD in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A HEAD is a reference to the currently checked out
commit. It is a symbolic reference to the branch that we have checked out. At
any given time, one head is selected as the ‘current head’ This head is also
known as HEAD (always in uppercase).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">3. What is the most popular branching strategy in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are many ways to do branching in GIT. One of the
popular ways is to maintain two branches:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">master</span></b><span style="font-size: 14.0pt;">: This
branch is used for production. In this branch HEAD is always in production
ready state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">develop</span></b><span style="font-size: 14.0pt;">:
This branch is used for development. In this branch we store the latest code
developed in project. This is work in progress code. Once the code is ready for
deployment to production, it is merged into master branch from develop branch.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">4. What is SubGit?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">SubGit is software tool used for migrating SVN to Git.
It is very easy to use. By using this we can create a writable Git mirror of a
Subversion repository. It creates a bi-directional mirror that can be used for
pushing to Git as well as committing to Subversion. SubGit also takes care of
synchronization between Git and Subversion.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">5. What is the use of git instaweb?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Git-instaweb is a script by which we can browse a git
repository in a web browser. It sets up the gitweb and a web-server that makes
the working repository available online.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">6. What are git hooks?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Git hooks are scripts that can run automatically on
the occurrence of an event in a Git repository. These are used for automation
of workflow in GIT. Git hooks also help in customizing the internal behavior of
GIT. These are generally used for enforcing a GIT commit policy.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">7. What is GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">GIT is a mature Distributed Version Control System
(DVCS). It is used for Source Code Management (SCM). It is open source
software. It was developed by Linus Torvalds, the creator of Linux operating
system. GIT works well with a large number of IDEs (Integrated Development
Environments) like- Eclipse, InteliJ etc. GIT can be used to handle small and
large projects.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">8. What is a repository in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A repository in GIT is the place in which we store our
software work. It contains a sub-directory called .git. There is only one .git
directory in the root of the project. In .git, GIT stores all the metadata for
the repository. The contents of .git directory are of internal use to GIT.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">9. What are the main benefits of GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are following main benefits of GIT:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l2 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Distributed
System</span></b><span style="font-size: 14.0pt;">: GIT is a Distributed Version
Control System (DVCS). So you can keep your private work in version control but
completely hidden from others. You can work offline as well.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l2 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Flexible
Workflow</span></b><span style="font-size: 14.0pt;">: GIT allows you to create
your own workflow. You can use the process that is suitable for your project.
You can go for centralized or master-slave or any other workflow.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l2 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Fast</span></b><span style="font-size: 14.0pt;">: GIT is very fast when compared to other version
control systems.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l2 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Data Integrity</span></b><span style="font-size: 14.0pt;">: Since GIT uses SHA1, data is not easier to corrupt.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l2 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Free</span></b><span style="font-size: 14.0pt;">: It is free for personal use. So many amateurs use it
for their initial projects. It also works very well with large size project.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l2 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Collaboration</span></b><span style="font-size: 14.0pt;">: GIT is very easy to use for projects in which
collaboration is required. Many popular open source software across the globe
use GIT.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">10. What are the disadvantages of GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">GIT has very few disadvantages. These are the
scenarios when GIT is difficult to use. Some of these are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Binary Files</span></b><span style="font-size: 14.0pt;">: If we have a lot binary files (non-text) in our
project, then GIT becomes very slow. E.g. Projects with a lot of images or Word
documents.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Steep Learning
Curve</span></b><span style="font-size: 14.0pt;">: It takes some time for a
newcomer to learn GIT. Some of the GIT commands are non-intuitive to a fresher.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Slow remote
speed</span></b><span style="font-size: 14.0pt;">: Sometimes the use of remote
repositories in slow due to network latency. Still GIT is better than other VCS
in speed.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">11.What are the main differences between GIT and SVN?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">The main differences between GIT and SVN are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l1 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Decentralized</span></b><span style="font-size: 14.0pt;">: GIT is decentralized. You have a local copy that is
a repository in which you can commit. In SVN you have to always connect to a
central repository for check-in.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Complex to
learn</span></b><span style="font-size: 14.0pt;">: GIT is a bit difficult to
learn for some developers. It has more concepts and commands to learn. SVN is
much easier to learn.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Unable to
handle Binary files</span></b><span style="font-size: 14.0pt;">: GIT becomes slow
when it deals with large binary files that change frequently. SVN can handle
large binary files easily.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Internal
directory</span></b><span style="font-size: 14.0pt;">: GIT creates only .git
directory. SVN creates .svn directory in each folder.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l1 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">User Interface</span></b><span style="font-size: 14.0pt;">: GIT does not have good UI. But SVN has good user
interfaces.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">12. How will you start GIT for your project?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We use git init command in an existing project
directory to start version control for our project. After this we can use git
add and git commit commands to add files to our GIT repository.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">13. What is git clone in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, we use git clone command to create a copy of
an existing GIT repository in our local. This is the most popular way to create
a copy of the repository among developers. It is similar to svn checkout. But
in this case the working copy is a full-fledged repository.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">14. How will you create a repository in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">To create a new repository in GIT, first we create a
directory for the project. Then we run ‘git init’ command. Now, GIT creates
.git directory in our project directory. This is how our new GIT repository is
created.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">15. What are the different ways to start work in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can start work in GIT in following ways:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">New Project</span></b><span style="font-size: 14.0pt;">:
To create a new repository we use git init command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Existing Project</span></b><span style="font-size: 14.0pt;">: To work on an existing repository we use git clone command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">16. GIT is written in which language?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Most of the GIT distributions are written in C
language with Bourne shell. Some of the commands are written in Perl language.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">17. What does ‘git pull’ command in GIT do internally?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, git pull internally does a git fetch first and
then does a git merge. So pull is a combination of two commands: fetch and
merge. We use git pull command to bring our local branch up to date with its
remote version.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">18. What does ‘git push’ command in GIT do internally?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, git push command does following two commands:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">1. <b>fetch</b>: First GIT, copies all the extra
commits from server into local repo and moves origin/master branch pointer to
the end of commit chain.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">2. <b>merge</b>: Then it merges the origin/master
branch into the master branch. Now the master branch pointer moves to the newly
created commit. But the origin/master pointer remains there.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">19. What is git stash?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, sometimes we do not want to commit our code
but we do not want to lose also the unfinished code. In this case we use git
stash command to record the current state of the working directory and index in
a stash. This stores the unfinished work in a stash, and cleans the current
branch from uncommitted changes. Now we can work on a clean working directory.
Later we can use the stash and apply those changes back to our working
directory. At times we are in the middle of some work and do not want to lose
the unfinished work, we use git stash command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">20. What is the meaning of ‘stage’ in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, stage is a step before commit. To stage means
that the files are ready for commit. Let say, you are working on two features
in GIT. One of the features is finished and the other is not yet ready. You want
to commit and leave for home in the evening. But you can commit since both of
them are not fully ready. In this case you can just stage the feature that is
ready and commit that part. Second feature will remain as work in progress.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">21. What is the purpose of git config command?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can set the configuration options for GIT
installation by using git config command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">22. How can we see the configuration settings of GIT installation?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can use ‘git config --list’ command to print all
the GIT configuration settings in GIT installation.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">23. How will you write a message with commit command in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We call following command for commit with a message:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt;">$/> git commit –m <message><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">24. What is stored inside a commit object in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">GIT commit object contains following information:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">SHA1 name</span></b><span style="font-size: 14.0pt;">: A
40 character string to identify a commit<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Files</span></b><span style="font-size: 14.0pt;">: List
of files that represent the state of a project at a specific point of time<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Reference</span></b><span style="font-size: 14.0pt;">:
Any reference to parent commit objects<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">25. How many heads can you create in a GIT repository?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There can be any number of heads in a repository. By
default there is one head known as HEAD in each repository in GIT.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">26. Why do we create branches in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">If we are simultaneously working on multiple tasks,
projects, defects or features, we need multiple branches. In GIT we can create
a separate branch for each separate purpose.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Let say we are working on a feature, we create a
feature branch for that. In between we get a defect to work on then we create
another branch for defect and work on it. Once the defect work is done, we
merge that branch and come back to work on feature branch again. So working on
multiple tasks is the main reason for using multiple branches.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">27. What are the different kinds of branches that can be created in
GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can create different kinds of branches for
following purposes in GIT:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Feature branches</span></b><span style="font-size: 14.0pt;">: These are used for developing a feature.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Release branches</span></b><span style="font-size: 14.0pt;">: These are used for releasing code to production.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">Hotfix branches</span></b><span style="font-size: 14.0pt;">:
These are used for releasing a hotfix to production for a defect or emergency
fix.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">28. How will you create a new branch in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We use following command to create a new branch in
GIT:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt;">$/> git checkout –b <branchname><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">29. How will you add a new feature to the main branch?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We do the development work on a feature branch that is
created from master branch. Once the development work is ready we use git merge
command to merge it into master branch.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">30. What is a pull request in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A pull request in GIT is the list of changes that have
been pushed to GIT repository. Generally these changes are pushed in a feature
branch or hotfix branch. After pushing these changes we create a pull request
that contains the changes between master and our feature branch. This pull
request is sent to reviewers for reviewing the code and then merging it into
develop or release branch.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">31.What is merge conflict in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A merge conflict in GIT is the result of merging two
commits. Sometimes the commit to be merged and current commit have changes in
same location. In this scenario, GIT is not able to decide which change is more
important. Due to this GIT reports a merge conflict. It means merge is not
successful. We may have to manually check and resolve the merge conflict.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">32. How can we resolve a merge conflict in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">When GIT reports merge conflict in a file, it marks
the lines as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">E.g. the business days in this week are<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;"><<<<<<< HEAD<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">five<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">=======<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">six<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">>>>>>>>
branch-feature<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">To resolve the merge conflict in a file, we edit the
file and fix the conflicting change. In above example we can either keep five
or six. After editing the file we run git add command followed by git commit
command. Since GIT is aware that it was merge conflict, it links this change to
the correct commit.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">33. What command will you use to delete a branch?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">After the successful merge of feature branch in main
branch, we do not need the feature branch. To delete an unwanted branch we use
following command:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt;">git branch –d <branchname><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">34. What command will you use to delete a branch that has unmerged
changes?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">To forcibly delete an unwanted branch with unmerged
changes, we use following command:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt;">git branch –D <branchname><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">35. What is the alternative command to merging in GIT?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Another alternative of merging in GIT is rebasing. It
is done by git rebase command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">36. What is Rebasing in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Rebasing is the process of moving a branch to a new
base commit. It is like rewriting the history of a branch. In Rebasing, we move
a branch from one commit to another. By this we can maintain linear project
history. Once the commits are pushed to a public repository, it is not a good
practice to use Rebasing.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">37. What is the ‘Golden Rule of Rebasing’ in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The golden rule of Rebasing is that we should never
use git rebase on public branches. If other people are using the same branch
then they may get confused by looking at the changes in Master branch after GIT
rebasing. Therefore, it is not recommended to do rebasing on a public branch that
is also used by other collaborators.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">38. Why do we use Interactive Rebasing in place of Auto Rebasing?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">By using Interactive rebasing we can alter the commits
before moving them to a new branch. This is more powerful than an automated
rebase. It gives us complete control over the branch’s commit history.
Generally, we use Interactive Rebasing to clean up the messy history of commits
just before merging a feature branch into master.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">39. What is the command for Rebasing in Git?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Git command for rebasing is:<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="line-height: normal; text-align: center;">
<span style="font-size: 14.0pt;">git rebase <new-commit><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">40. What is the main difference between git clone and git remote?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The main difference between git clone and git remote
is that git clone is used to create a new local repository whereas git remote
is used in an existing repository. git remote adds a new reference to existing
remote repository for tracking further changes. git clone creates a new local
repository by copying another repository from a URL.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">41.What is GIT version control?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">GIT version control helps us in managing the changes
to source code over time by a software team. It keeps track of all the changes
in a special kind of database. If we make a mistake, we can go back in time and
see previous changes to fix the mistake. GIT version control helps the team in
collaborating on developing a software and work efficiently. Every one can
merge the changes with confidence that everything is tracked and remains intact
in GIT version control. Any bug introduced by a change can be discovered and
reverted back by going back to a working version.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">42. What GUI do you use for working on GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are many GUI for GIT that we can use. Some of
these are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">GitHub Desktop<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">GITX-dev<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">Gitbox<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">Git-cola<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">SourceTree<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">Git Extensions<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">SmartGit<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">GitUp<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">43. What is the use of git diff command in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, git diff command is used to display the
differences between 2 versions, or between working directory and an index, or
between index and most recent commit. It can also display changes between two
blob objects, or between two files on disk in GIT. It helps in finding the
changes that can be used for code review for a feature or bug fix.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">44. What is git rerere?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, rerere is a hidden feature. The full form of
rerere is “reuse recorded resolution”. By using rerere, GIT remembers how we’ve
resolved a hunk conflict. The next time GIT sees the same conflict, it can
automatically resolve it for us.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">45. What are the three most popular version of git diff command?</span></b><b><span style="font-size: 14.0pt;"><o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Three most popular git diff commands are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">git diff</span></b><span style="font-size: 14.0pt;">: It
displays the differences between working directory and the index.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">git diff –cached</span></b><span style="font-size: 14.0pt;">: It displays the differences between the index and the most recent
commit.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">git diff HEAD</span></b><span style="font-size: 14.0pt;">:
It displays the differences between working directory and the most recent
commit<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">46. What is the use of git status command?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, git status command mainly shows the status of
working tree. It shows following items:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l3 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">The paths that
have differences between the index file and the current HEAD commit.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l3 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">The paths that
have differences between the working tree and the index file<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l3 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">The paths in the
working tree that are not tracked by GIT.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Among the above three items, first item is the one
that we commit by using git commit command. Item two and three can be committed
only after running git add command.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">47. What is the main difference between git diff and git status?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, git diff shows the differences between
different commits or between the working directory and index. Whereas, git
status command just shows the current status of working tree.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">48. What is the use of git rmcommand in GIT?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In GIT, git rm command is used for removing a file
from the working tree and the index. We use git rm –r to recursively remove all
files from a leading directory.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">49. What is the command to apply a stash?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sometimes we want to save our unfinished work. For
this purpose we use git stash command. Once we want to come back and continue
working from the last place where we left, we use git stash apply command to
bring back the unfinished work. So the command to apply a stash is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">git stash apply<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Or we can use<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">git stash apply <stashname><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<b><span style="color: red; font-size: 14.0pt;">50. Why do we use git log command?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We use git log command to search for specific commits
in project history. We can search git history by author, date or content. It
can even list the commits that were done x days before or after a specific
date.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-80780267658769859062019-03-20T16:04:00.000+05:302019-03-20T16:04:09.392+05:30Maven based Interview Question <div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-size: large;"><br /></span>
<br />
<br />
<span style="color: red; font-size: large;"><b>1. What is Maven?</b></span><br />
<span style="font-size: large;"> Maven is a software project management tool. It is open source software from Apache software foundation. It is used for building, reporting and documenting a Software project. It is mainly based on POM (Project Object Model).</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>2. What are the main features of Maven?</b></span><br />
<span style="font-size: large;"> Some of the main features of Maven are:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;"><b>Simple</b>: Maven provides simple project setup that is based on best practices.</span></li>
<li><span style="font-size: large;"><b>Fast</b>: You can get a new project or module started in a few seconds in Maven.</span></li>
<li><span style="font-size: large;"><b>Easy to learn</b>: Maven usage and commands are easy to learn across all projects. Therefore ramp up time for new developers coming onto a project is very less.</span></li>
<li><span style="font-size: large;"><b>Dependency management</b>: Maven provides superior dependency management including automatic updates and transitive dependencies.</span></li>
<li><span style="font-size: large;"><b>Multiple Projects</b>: You can easily work with multiple projects at the same time by using Maven.</span></li>
<li><span style="font-size: large;"><b>Large Library</b>: Maven has a large and growing repository of libraries and metadata to use out of the box.</span></li>
<li><span style="font-size: large;"><b>Extensible</b>: Maven supports the ability to easily write plugins in Java or scripting languages for extending its core functionality.</span></li>
<li><span style="font-size: large;"><b>Instant</b>: Maven is online and it provides instant access to new features with very less configuration.</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>3. What areas of a Project can you manage by using Maven?</b></span><br />
<span style="font-size: large;"> Maven can help us manage following areas of a project:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;"> Build</span></li>
<li><span style="font-size: large;"> Testing</span></li>
<li><span style="font-size: large;"> Release</span></li>
<li><span style="font-size: large;"> Reporting</span></li>
<li><span style="font-size: large;"> Software Change Management (SCM)</span></li>
<li><span style="font-size: large;"> Documentation</span></li>
<li><span style="font-size: large;"> Distribution</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>4. What are the main advantages of Maven?</b></span><br />
<span style="font-size: large;"> Maven has a long list of advantages for Software development. Some of the main advantages are:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;"><b>Common Project Structure</b>: By using Maven, every developer has a common project structure that helps in understanding the code as well as developing new features in a new project.</span></li>
<li><span style="font-size: large;"><b>Modular Design</b>: Maven promotes modular design that divides a complex project into multiple modules that are easier to manage. By using Maven, it is easier to manage multiple modules for build, test, release etc.</span></li>
<li><span style="font-size: large;"><b>Centralized Dependency Management</b>: With Maven, each developer does not have to include the jars separately in each project or module. Maven provides a centralized dependency management that can help improve efficiency of software development.</span></li>
<li><span style="font-size: large;"><b>Fewer Decisions</b>: With Maven a developer has to make fewer decisions about things unrelated to software development work. The project structure comes ready with Maven, dependency management is a uniform approach and build/release are handled by Maven. So a developer can focus on core work of developing software.</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>5. Why do we say “Maven uses convention over configuration”?</b></span><br />
<span style="font-size: large;"> Convention over configuration is a Software Design Paradigm that decreases the number of decisions made by a software developer, without losing flexibility.</span><br />
<span style="font-size: large;"> In Maven, there are many conventions for setting up the project, building the artifacts, running unit tests and releasing the code. These conventions lead to common process for Software development.</span><br />
<span style="font-size: large;"> In case of other tools, there are a lot of configuration options are present. But most of the time, a developer uses same set of configuration options. So it is better to make these as a default options. Maven uses default options from best practices and provides right conventions for Software development.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>6. What are the responsibilities of a Build tool like Maven?</b></span><br />
<span style="font-size: large;"> A Build tool like Maven helps us with following tasks:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;"><b>Source Code</b>: A Build tool can generate source code based on templates.</span></li>
<li><span style="font-size: large;"><b>Documentation</b>: We can get documentation files from source code by using a build tool. E.g. Javadoc</span></li>
<li><span style="font-size: large;"><b>Compilation</b>: Primary responsibility of a Build tool is to compile source code into executable code.</span></li>
<li><span style="font-size: large;"><b>Packaging</b>: A Build tool packages compiled code into a deployable file like- jar, zip war etc.</span></li>
<li><span style="font-size: large;"><b>Deployment</b>: We can deploy the packaged code on server by using a Build tool.</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>7. What are the differences between Ant and Maven?</b></span><br />
<span style="font-size: large;"> Key differences between Ant and Maven are:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;">Ant is a Java library and command line toolbox for build process. Maven is a framework for many aspects of software development like- project setup, compile, build, documentation etc.</span></li>
<li><span style="font-size: large;">Ant does not have any conventions for project structure or build processes. Maven has conventions for setting up project structure as well as for build processes.</span></li>
<li><span style="font-size: large;">Ant is based on procedural programming. We have to write code for compilation build, copy etc. Maven is based on declarative programming. We have to just configure it for our project setup and programming.</span></li>
<li><span style="font-size: large;">Ant does not impose any lifecycle. We need to create the sequence of tasks manually. Maven has a lifecycle for software build processes. There are well-defined phases that we can use in Maven.</span></li>
<li><span style="font-size: large;">Ant scripts are not reusable in multiple projects. Maven has plugins that are reusable across multiple projects.</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>8. What is MOJO in Maven?</b></span><br />
<span style="font-size: large;"> MOJO stands for Maven plain Old Java Object. Every MOJO is an executable goal in Maven. It is like an annotated Java class. It specifies metadata about a goal like- goal name, phase of lifecycle for goal and parameters required by goal. A Maven plugin can contain multiple MOJOs.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>9. What is a Repository in Maven?</b></span><br />
<span style="font-size: large;"> A repository is a location on file system where build artifacts, jars, dependencies and pom.xml files are stored.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>10. What are the different types of repositories in Maven?</b></span><br />
<span style="font-size: large;"> There are mainly two types of repositories in Maven:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">1. <b>Local Repository</b>: This is your local folder in which a copy of your installation and dependencies is stored.</span><span style="font-size: large;">2. <b>Remote Repository</b>: This is a remote folder in which jars and other build artifacts are stored. These can be located on servers within your organization.</span><span style="font-size: large;">3. <b>Central Remote Repository</b>: This is the central Maven repository that is located on repo.maven.apache.org or uk.maven.org or any other third party location. This where we can find artifacts from different providers that are available for download and use. Like- Hibernate, Spring libraries etc.</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>11. What is a local repository in Maven?</b></span><br />
<span style="font-size: large;"> Maven local repository is a folder in your local files system in which your project’s installation, dependency jars, plugins etc. are stored. Default location of Maven local repository is .m2 folder. It can be located under following location on file system:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">Windows – C:\Documents and Settings\{ username}\.m2</span><span style="font-size: large;">Unix/Linux/Mac – ~/.m2</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>12. What is a central repository in Maven?</b></span><br />
<span style="font-size: large;"> Maven central repository is a truly remote repository that is located on repo.maven.apache.org or uk.maven.org or any other third party location. This contains the jars and artifacts provided by various software providers. Central repository contains a large amount of data. Therefore it is not allowed to scrape the whole site. But you can use the relevant jars that you want for download and use in your Maven project.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>13. What is a Remote repository in Maven?</b></span><br />
<span style="font-size: large;"> A Remote repository is a remote location on the internet where the jars and dependencies from different vendors are stored. These files can be accessed by protocols like- file:// or http:// etc.</span><br />
<span style="font-size: large;">These can be truly remote repositories set up by third party vendors or locations inside your organization that contains the relevant jars required by your project.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>14. Why we should not store jarsin CVS or any other version control system instead of Maven repository?</b></span><br />
<span style="font-size: large;"> Maven recommends storing jars in local repository instead of CVS </span><span style="font-size: large;">or any other </span><span style="font-size: large;">version control system. There are </span><span style="font-size: large;">following advantages </span><span style="font-size: large;">of storing it in Maven repo vs. CVS:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><b>Less Storage</b>: A repository is very large, but it takes less space </span><span style="font-size: large;">because each JAR is stored only in one place. E.g. If we have 10 </span><span style="font-size: large;">modules dependent on Spring jar, then they all refer to same Spring </span><span style="font-size: large;">jar stored in local repository.</span><span style="font-size: large;"><b>Quicker Checkout</b>: Project checkout is quicker from local</span><span style="font-size: large;">repository, since there is not need to checkout jars if they are </span><span style="font-size: large;">already present in repo.</span><span style="font-size: large;"><b>No need for versioning</b>: There is no need to version JARS since </span><span style="font-size: large;">external dependencies do not change so often.</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>15. Can anyone upload JARS or artifacts to Central Repository?</b></span><br />
<span style="font-size: large;"> No, we need special permissions to upload JARS and artifacts to Central Maven Repository?</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>16. What is a POM?</b></span><br />
<span style="font-size: large;"> POM is an abbreviation for Project Object Model. This is the basic unit of work in Maven. It is an XML file with name pom.xml. It contains details of project and project configuration that are used by Maven to build the project.</span><br />
<span style="font-size: large;"> It also contains default values for many projects. E.g. target is the name of build directory for Java Maven project.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>17. What is Super POM?</b></span><br />
<span style="font-size: large;"> Super POM is Maven’s default POM. All the POM files extend from Super POM.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>18. What are the main required elements in POM file?</b></span><br />
<span style="font-size: large;"> Every POM file should have following required elements:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">1. <b>project root</b></span><span style="font-size: large;">2. <b>modelVersion</b></span><span style="font-size: large;">3. <b>groupID</b>: the id of the project's group.</span><span style="font-size: large;">4. <b>artifactID</b>: the id of the artifact (project)</span><span style="font-size: large;">5. <b>version</b>: the version of the artifact under the specified group</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>19. What are the phases in Build lifecycle in Maven?</b></span><br />
<span style="font-size: large;"> In Maven, each build lifecycle consists of many phases. Default build lifecycle has following phases:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;"><b>validate</b>: In this phase, Maven validates that the project is correct and all necessary information is available to run next phase.</span></li>
<li><span style="font-size: large;"><b>compile</b>: Maven compiles the source code of the project in this phase.</span></li>
<li><span style="font-size: large;"><b>test</b>: This is the phase to run unit tests on the compiled source. There should not be any need to package or deploy the code to run these tests.</span></li>
<li><span style="font-size: large;"><b>package</b>: In this phase, Maven takes the compiled code and packages it in its distributable format, such as a JAR.</span></li>
<li><span style="font-size: large;"><b>verify</b>: Maven runs any checks on results of integration tests to ensure that quality criteria are met.</span></li>
<li><span style="font-size: large;"><b>install</b>: In this phase, Maven installs the package into local repository. After this it can be used as a dependency in other projects locally.</span></li>
<li><span style="font-size: large;"><b>deploy</b>: In the build environment, Maven copies the final package to the remote repository for sharing with other developers and projects.</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>20. What command will you use to package your Maven project?</b></span><br />
<span style="font-size: large;"> To package a project into a distributable format we use following command:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">mvn -package</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>21. What is the format of fully qualified artifact name of a Mavenproject?</b></span><br />
<span style="font-size: large;"> A Maven project has artifact name with following format:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><groupId>:<artifactId>:<version></span></blockquote>
<span style="font-size: large;">Following is the convention used by some organizations:</span><br />
<span style="font-size: large;">Parent pom</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><b>groupId</b>: org.Orgname.Projectname</span><span style="font-size: large;"><b>artifactId</b>: org.Orgname.Projectname version: x.x.x</span><span style="font-size: large;"><b>E.g.</b> org.Orgname.Projectname:org.Orgname.Projectname-1.0.0.pom Modules</span><span style="font-size: large;"><b>groupId</b>: org.Orgname.Projectname</span><span style="font-size: large;"><b>artifactId</b>: org.Orgname.Projectname.Modulename version: x.x.x</span><span style="font-size: large;"><b>E.g.</b> org.Orgname.Projectname:org.Orgname.Projectname.Modulename-1.0.0.jar</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>22. What is an Archetype in Maven?</b></span><br />
<span style="font-size: large;"> As per official definition, an Archetype is a Maven project templating toolkit. By using an Archetype, an author of Archetype can create a Project template. Users of this project template (archetype) can pass different parameters to this template and start using it. Archetype promotes consistency in the process of creating and working on a project. It also helps in reducing the ramp up time fornew developers to come on board on a project.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>23.What is the command in Maven to generate an Archetype?</b></span><br />
<span style="font-size: large;"> In Maven, we can use following command to generate an Archetype:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">mvn archetype:generate</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>24. What are the three main build lifecycles of Maven?</b></span><br />
<span style="font-size: large;"> Maven has following three build lifecycles that further contain multiple phases:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">1. <b>clean</b>: In this lifecycle any files generated by previous builds are removed.</span><span style="font-size: large;">2. <b>default</b>: This lifecycle is used for validating, compiling and creating the application. It has multiple phases likecompile, test, package inside it.</span><span style="font-size: large;">3. <b>site</b>: Maven generates and deploys the documentation of a site in this phase.</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>25. What are the main uses of a Maven plugin?</b></span><br />
<span style="font-size: large;"> Maven is mainly a plugin execution framework. At the code of Maven all the work is done by plugins. A Maven plugin can be used for following purposes:</span><br />
<br />
<ol style="text-align: left;">
<li><span style="font-size: large;">Cleaning up the code</span></li>
<li><span style="font-size: large;">Compiling the code</span></li>
<li><span style="font-size: large;">Creating a JAR file</span></li>
<li><span style="font-size: large;">Deploying the artifacts</span></li>
<li><span style="font-size: large;">Running the unit tests</span></li>
<li><span style="font-size: large;">Documenting the project</span></li>
<li><span style="font-size: large;">Generating the site of a project</span></li>
<li><span style="font-size: large;">Generating a WAR file</span></li>
<li><span style="font-size: large;">Generate a checkstyle report</span></li>
</ol>
<br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>26. How will you find the version of a plugin being used?</b></span><br />
<span style="font-size: large;"> Maven Help Plugin has a describe goal. This can be used for listing the version of a plugin. Sample command for this is:</span><br />
<span style="font-size: large;">mvn -Dplugin=install help:describe</span><br />
<span style="font-size: large;"><b>Note</b>: In the above command replace Dplugin with the plugin prefix as the argument. Do not use the artifact ID of plugin here.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>27. What are the different types of profile in Maven? Where will you define these profiles?</b></span><br />
<span style="font-size: large;">In Maven, we can have following types of Profile:</span><br />
<span style="font-size: large;">Per Project</span><br />
<span style="font-size: large;">It is defined in the POM itself (pom.xml).</span><br />
<span style="font-size: large;">Per User</span><br />
<span style="font-size: large;">We can define it in the Maven-settings</span><br />
<span style="font-size: large;">(%USER_HOME%/.m2/settings.xml).</span><br />
<span style="font-size: large;">Global</span><br />
<span style="font-size: large;">It is defined in the global Maven-settings</span><br />
<span style="font-size: large;">(${maven.home}/conf/settings.xml).</span><br />
<span style="font-size: large;">Profile descriptor </span><br />
<span style="font-size: large;">Descriptor is located in project basedir (profiles.xml) (It is not supported in Maven 3.0)</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>28. What are the different setting files in Maven? Where will you find these files?</b></span><br />
<span style="font-size: large;"> Maven is very simple to use. At the core it has a setting file names settings.xml. This file contains the setting element that is used to configure the Maven with different options.</span><br />
<span style="font-size: large;">The main locations where this file can be found are:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><b>Maven Installation directory</b>: ${maven.home}/conf/settings.xml</span><span style="font-size: large;"><b>User Home directory</b>: ${user.home}/ .m2 / settings.xml</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>29. What are the main elements we can find in settings.xml?</b></span><br />
<span style="font-size: large;"> In settings.xml we can have all the configuration information for Maven. Some of the important elements are:</span><br />
<span style="font-size: large;"><b>localRepository</b>: The value of this element is the path of this build system’s local repository. The default value is ${user.home}/.m2/repository. It is used for a main build server to allow all logged-in users to build from a common local repository.</span><br />
<span style="font-size: large;"><b>interactiveMode</b>: If it is true then Maven should attempt to interact with the user for input. If it is false then Maven does not interact with the user. Default setting is true.</span><br />
<span style="font-size: large;"><b>usePluginRegistry</b>: If it is true Maven uses the ${user.home}/.m2/plugin-registry.xml file to manage plugin versions. By defaults it is false.</span><br />
<span style="font-size: large;"><b>offline</b>: If it is true this build system should be able to operate in offline mode. By default it is false. This element is used for build servers that cannot connect to a remote repository due to network setup or security reasons.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>30. How will you check the version of Maven in your system?</b></span><br />
<span style="font-size: large;"> We can use following command in console to check the version of Maven in our system.</span><br />
<span style="font-size: large;">mvn -version</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>31. How will you verify if Maven is installed on Windows?</b></span><br />
<span style="font-size: large;"> To check this, type mvn –version in cmd prompt of Windows. This will give you the version of Maven installed on Windows.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>32. What is a Maven artifact?</b></span><br />
<span style="font-size: large;"> A Maven artifact is a file that gets deployed to a Maven repository. In most cases it is a JAR file. When Maven build runs, it creates one or more artifacts. In case of Java projects, it produces a compiled jar and a sources jar. Every artifact in Maven has a group ID, an artifact ID and a version string. These three attributes uniquely identify an artifact. In Maven, we specify a project's dependencies as artifacts.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>33.What are the different dependency scopes in Maven?</b></span><br />
<span style="font-size: large;"> Maven supports following dependency scopes:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><b>compile</b>: This is the default dependency scope in Maven. The compile level dependencies are available in all classpaths of a project. These dependencies are also propagated to dependent projects.</span><span style="font-size: large;"><b>provided</b>: This scope is similar to compile. But in this scope we expect the JDK or a container to provide the dependency at runtime.</span><span style="font-size: large;"><b> E.g. </b>While building a web application for the Java Enterprise Edition, we can set the dependency on the Servlet API and related Java EE APIs to scope provided. The web container will provide these classes at runtime to our application. This scope is only available on the compilation and test classpath, and is not transitive.</span><span style="font-size: large;"><b>runtime</b>: The dependency in this scope is not required for compilation. It is required for execution. It is available in the runtime and test classpaths. It is not present in the compile classpath.</span><span style="font-size: large;"><b>test</b>: This scope is used for dependencies that are required for test compilation and execution phases. This scope is not transitive.</span><span style="font-size: large;"><b>system</b>: This scope is same as provided scope, except that you have to provide the JAR that contains it explicitly. In this case, he artifact is always available. There is no need to look it up in a repository.</span><span style="font-size: large;"><b>import</b>: This scope is only used on a dependency of type pom in the <dependencyManagement> section. In this case, the specified POM has to be replaced with the dependencies in that POM's <dependencyManagement> section. This scope is only available in Maven 2.0.9 or later.</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>34. How can we exclude a dependency in Maven?</b></span><br />
<span style="font-size: large;"> To exclude a dependency we can add the <exclusions> tag under the <dependency> section of the pom.</span><br />
<span style="font-size: large;">E.g.</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><dependencies></span><span style="font-size: large;"><dependency></span><span style="font-size: large;"><groupId>test.ProjectX</groupId></span><span style="font-size: large;"><artifactId>ProjectX</artifactId></span><span style="font-size: large;"><version>1.0</version></span><span style="font-size: large;"><scope>compile</scope></span><span style="font-size: large;"><exclusions></span><span style="font-size: large;"><exclusion> <!-- exclusion is mentioned here --></span><span style="font-size: large;"><groupId>test.ProjectY</groupId></span><span style="font-size: large;"><artifactId>ProjectY</artifactId></span><span style="font-size: large;"></exclusion></span><span style="font-size: large;"></exclusions></span><span style="font-size: large;"></dependency></span><span style="font-size: large;"></dependencies></span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>35. How Maven searches for JAR corresponding to a dependency?</b></span><br />
<span style="font-size: large;"> Maven first looks for a JAR related to a dependency in the local </span><span style="font-size: large;">repository. If it finds it there then it stops. </span><span style="font-size: large;">If it does not find it in local repo, it looks for the JAR in the remote </span><span style="font-size: large;">repository and downloads the corresponding version of JAR file. </span><span style="font-size: large;">From remote repository it stores the JAR into local repository.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>36. What is a transitive dependency in Maven?</b></span><br />
<span style="font-size: large;"> Let say you have a Project A that depends on dependency B. The dependency B further depends on dependency C. So your dependency C is a Transitive Dependency of your project A. In Maven, starting from 2.0, you do not have to specify transitive dependencies. You just mention your immediate dependencies in pom.xml. Maven takes care of resolving the Transitive dependencies and includes them automatically.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>37. What are Excluded dependencies in Maven?</b></span><br />
<span style="font-size: large;"> Let say a project A depends on project B, and project B depends on </span><span style="font-size: large;">project C. The developers of project A can explicitly exclude </span><span style="font-size: large;">project C as a dependency. We can use the "exclusion" element to </span><span style="font-size: large;">exclude it. </span><span style="font-size: large;">Such dependencies are called Excluded dependencies in Maven.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>38. What are Optional dependencies in Maven?</b></span><br />
<span style="font-size: large;"> Let say a project B depends on project C. The developers of project B can mark project C as an optional dependency by using the "optional" element. </span><span style="font-size: large;">In case project A depends on project B, A will depend only on B and not on B's optional dependency C. The developers of project A may then explicitly add a dependency on C. The dependency of B on C is known as Optional dependency in Maven.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>39. Where will you find the class files after compiling a Maven project successfully?</b></span><br />
<span style="font-size: large;"> Once Maven completes the compilation successfully, it stores the files in target folder. The default location for class files is:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">${basedir}/target/classes/</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>40. What are the default locations for source, test and build directories in Maven?</b></span><br />
<span style="font-size: large;"> The default locations are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><b>Source</b>: src/main/java</span><span style="font-size: large;"><b>Test</b>: src/main/test</span><span style="font-size: large;"><b>Build</b>: Target</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>41. What is the result of jar:jar goal in Maven?</b></span><br />
<span style="font-size: large;"> In Maven, jar:jar goal creates a jar file in the Maven build </span><span style="font-size: large;">directory. Jar file is create with the name format ${project.id}- </span><span style="font-size: large;">${project.currentVersion}.jar. </span><span style="font-size: large;">The id and currentVersion are mentioned in the project.xml of the </span><span style="font-size: large;">project being built. </span><span style="font-size: large;"><b>jar:jar</b> does not recompile sources. It just creates a jar from already</span><br />
<span style="font-size: large;">compiled classes.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>42.How can we get the debug or error messages from the execution of Maven?</b></span><br />
<span style="font-size: large;"> At times, project build or compile fails in Maven. At this time it is </span><span style="font-size: large;">very helpful to see the debug or error messages from Maven</span><br />
<span style="font-size: large;">execution.</span><br />
<span style="font-size: large;"> To get the debug messages we can call Maven with -X option. </span><span style="font-size: large;">To get the error/exception messages we can call Maven with -e </span><span style="font-size: large;">option.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>43. What is the difference between a Release version and SNAPSHOT version in Maven?</b></span><br />
<span style="font-size: large;"> A SNAPSHOT version in Maven is the one that has not been released. Before every release version there is a SNAPSHOT version. Before 1.0 release there will be 1.0-SNAPSHOT. If we download 1.0-SNAPSHOT today then we may get different set of files than the one we get on downloading it yesterday. SNAPSHOT version can keep getting changes in it since it is under development . But release version always gives exactly same set files with each download.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>44.How will you run test classes in Maven?</b></span><br />
<span style="font-size: large;"> We need Surefire plugin to run the test classes in Maven. To run a single test we can call following command:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">mvn -Dtest=TestCaseA test</span></blockquote>
<span style="font-size: large;">We can also use patterns to run multiple test cases:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">mvn -Dtest=TestCase* test</span></blockquote>
<span style="font-size: large;">or</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;">mvn -Dtest=TestCaseA,TestCaseB,TestImportant* test</span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>44.Sometimes Maven compiles the test classes but doesn't run them? What could be the reason for it?</b></span><br />
<span style="font-size: large;"> In Maven, Surefire plugin is used for running the Tests. We can configure it to run certain test classes. Sometimes we you may have unintentionally specified an incorrect value to ${test} in settings.xml or <b>pom.xml</b>. </span><span style="font-size: large;">We need to look for following in pom.xml/settings.xml and fix it:</span><br />
<blockquote class="tr_bq">
<span style="font-size: large;"><properties></span><span style="font-size: large;"><property></span><span style="font-size: large;"><name>test</name></span><span style="font-size: large;"><value>some-value</value></span><span style="font-size: large;"></property></span><span style="font-size: large;"></properties></span></blockquote>
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>45. How can we skip the running of tests in Maven?</b></span><br />
<span style="font-size: large;"> We can use the parameter -Dmaven.test.skip=true or - DskipTests=true in the command line for skipping the tests. The parameter -Dmaven.test.skip=true skips the compilation of tests. The parameter -DskipTests=true skips the execution of tests Surefire plugin of Maven honors these parameters.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>46.Can we create our own directory structure for a project in Maven?</b></span><br />
<span style="font-size: large;"> Yes, Maven gives us the flexibility of creating our own directory structure. We just need to configure the elements like <sourceDirectory>, <resources> etc. in the <build> section of pom.xml.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>47.What are the differences between Gradle and Maven?</b></span><br />
<span style="font-size: large;"> Gradle is nowadays getting more popular. Google uses it for Android development and release. Companies like LinkedIn also use Gradle. Gradle is based on Domain Specific Language (DSL). Maven is based on XML. Gradle gives more flexibility to do custom tasks similar to ANT. Maven scripts have predefined structure. So it is less flexible. Maven is mainly used for Java based systems. Gradle is used for a variety of languages. It is a Polyglot build tool.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>48.What is the difference between Inheritance and Multi-module in Maven?</b></span><br />
<span style="font-size: large;"> In Maven, we can create a parent project that will pass its values to its children projects. A multi-module project is created to manage a group of other subprojects or modules. The multi-module relationship is like a tree that starts from the topmost level to the bottom level. In a multimodule project, we specify that a project should include the specific modules for build. Multi-module builds are used to group modules together in a single build. Whereas in Inheritance, the parent-child project relationship starts from the leaf node and goes upwards. It deals more with the definition of a specific project. In this case a child’s pom is derived from its parent’s pom.</span><br />
<span style="font-size: large;"><br /></span>
<span style="color: red; font-size: large;"><b>49.What is Build portability in Maven?</b></span><br />
<span style="font-size: large;"> In Maven, the portability of a build is the measure of how easy it is to take a particular project and build it in different environments. A build that does not require any custom configuration or customization of properties files is more portable than a build that requires a lot of custom work to build it from scratch. Open source projects from Apache Commons are one of the most portable projects. These build can work just out of the box.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><br /></span>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-16841752997401860832019-03-16T13:30:00.000+05:302019-03-16T13:30:01.514+05:30Java Design Patterns Interview Questions part - 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<br />
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large;"><br class="Apple-interchange-newline" />Java Design Patterns</span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large;"><br /></span></b></div>
<div style="text-align: center;">
<br /></div>
<br />
<b>1.What are the examples of </b><b>Interpreter design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> Interpreter design pattern is used to evaluate sentences in a language. E.g. In SQL we can use it to evaluate a query by evaluating each keyword like SELECT, FROM, WHERE clause. In an Interpreter implementation there is a class for each keyword/symbol. A sentence is just a composite of these keywords. But the sentence is represented by Syntax tree that can be interpreted. In JDK there are many places where Interpreter design pattern is used. Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">java.util.Pattern</span><span style="font-family: "timesnewromanpsmt" , serif;">java.text.Normalizer</span><span style="font-family: "timesnewromanpsmt" , serif;">Subclasses of java.text.Format: DateFormat,</span><span style="font-family: "timesnewromanpsmt" , serif;">MessageFormat, NumberFormat</span><span style="font-family: "timesnewromanpsmt" , serif;">Subclasses of javax.el.ELResolver: ArrayELResolver,</span><span style="font-family: "timesnewromanpsmt" , serif;">MapELResolver, CompositeELResolver etc.</span></blockquote>
<br />
<b>2. What are the examples of Mediator design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> By using Mediator pattern we can decouple the multiple objects that interact with each other. With a Mediator object we can create many-to-many relationships in multiple objects. In JDK there are many places where Mediator design pattern is used. </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;"> Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;"><b>java.util.Timer</b>: schedule() methods in Timer class act as Mediator between the clients and the TimerTask to be scheduled.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;"><b>java.util.concurrent.Executor.execute()</b>: The execute() method in an Executor class acts as a Mediator to execute the different tasks</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">.</span><span style="font-family: "timesnewromanpsmt" , serif;"><b>java.util.concurrent.ExecutorService java.lang.reflect.Method.invoke()</b>: In Method class of reflection package, invoke() method acts as a Mediator.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;"> </span><span style="font-family: "timesnewromanpsmt" , serif;"><b>java.util.concurrent.ScheduledExecutorService</b>: Here also schedule() method and its variants are Mediator pattern implementations.</span></blockquote>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<b>3. What are the examples of Strategy design pattern in JDK?</b><span style="font-family: "timesnewromanpsmt" , serif;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In JDK there are many places where Strategy design pattern is used.<br /> Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">1. <b>java.util.Comparator</b>: In a Comparator we can use compare() method to change the strategy used by Collections.sort() method.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">2. <b>javax.servlet.http.HttpServlet</b>: In a HttpServlet class service() and doGet(), doPost() etc. methods take HttpServletRequest and HttpServletResponse and the implementor of Servlet processes it based on the strategy it selects.</span></blockquote>
</div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>4. What are the examples of Visitor design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> By using Visitor design pattern we can add new virtual methods to existing classes without modifying their core structure. In JDK there are many places where Visitor design pattern is used.<br />Some of these are as follows:</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .75pt; mso-border-insideh: .75pt solid windowtext; mso-border-insidev: .75pt solid windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;"><tbody>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">javax.lang.model.element.AnnotationValue</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border: 1pt solid; padding: 0in 5.4pt; width: 24pt;" width="32"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">and</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">AnnotationValueVisitor</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">java.nio.file.FileVisitor and SimpleFileVisitor</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">javax.lang.model.type.TypeMirror and TypeVisitor</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">javax.lang.model.element.Element and ElementVisitor</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">javax.faces.component.visit.VisitContext</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt; width: 24pt;" width="32"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">and</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 322.5pt;" width="430"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">VisitCallback</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
</tbody></table>
<span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span><b><span style="font-family: , serif; line-height: 17.12px;">5. How Decorator design pattern is different from Proxy pattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Main differences between Decorator and Proxy design pattern are:</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Decorator provides an enhanced interface after decorating it with additional features. Proxy provides same interface since it is just acting as a proxy to another object. Decorator is a type of Composite pattern with only one component. But each decorator can add additional features. Since it is one component in Decorator, there is no object aggregation. Proxy can also provide performance improvement by lazy loading. There is nothing like this available in Decorator. Decorator follows recursive composition. Proxy is just one object to another object access. Decorator is mostly used for building a variety of objects. Proxy is mainly used for access to another object.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">6. What are the different scenarios to use Setter and Constructor based injection in Dependency Injection (DI) design pattern?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We use Setter injection to provide optional dependencies of an object. Constructor injection is used to provide mandatory dependency of an object. In Spring IoC, Dependency Injection is heavily used. There we have to differentiate between the scenario suitable for Setter based and Constructor based dependency injection.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">7. What are the different scenarios for using Proxy design pattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Proxy design pattern can be used in a wide variety of scenario in Java. Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">1. <b>Virtual Proxy</b>: This is a virtual object that acts as a proxy for objects that are very expensive to create. It is used in Lazy Loading. When client makes the first request, the real object is created.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">2. <b>Remote Proxy</b>: This is a local object that provides access to a remote object. It is generally used in Remote Method Invocation (RMI) and Remote Procedure Call (RPC). It is also known as a Stub.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">3. <b>Protective Proxy</b>: This is an object that control the access to a Master object. It can authenticate and authorize the client for accessing the Master object. If client has right permissions, it allows client to access the main object.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">4. <b>Smart Proxy</b>: It is an object that can add additional information to the main object. It can track the number of other objects accessing the main object. It can track the different clients from where request is coming. It can even deny access to an object if the number of requests is greater than a threshold.</span></blockquote>
<br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">8. What is the main difference between Adapter and Proxy design pattern?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Adapter pattern provides a different interface to an object. But the Proxy always provides same interface to the object. Adapter is like providing an interface suitable to client’s use. But Proxy is same interface that has additional feature or check.<br /> E.g. In electrical appliances we use Adapter to convert from one type of socket to another type of socket. In case of proxy, we have a plug with built-in surge protector. The interface for plug and the original device remains same.</span><span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span>
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span>
<br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span>
<b><span style="font-family: , serif; line-height: 17.12px;">9. When will you use Adapter design </span></b><b><span style="font-family: , serif; line-height: 17.12px;">pattern in Java?</span></b><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> If we have two classes with incompatible interfaces, we use Adapter pattern to make it work. We create an Adapter object that can adapt the interface of one class to another class. It is generally used for working with third party libraries. We create an Adapter class between third party code and our class. In case of any change in third party code we have to just change the Adapter code. Rest of our code can remain same and just take to Adapter.</span></span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">10. What are the examples of Adapter design pattern in JDK?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In JDK there are many places where Adapter design pattern is used.<br /> Some of these are as follows:</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"></span></span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">java.util.Arrays.asList(): This method can adapt an Array to work as a List.</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">java.util.Collections.list(): This method can adapt any collection to provide List behavior.</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">java.util.Collections.enumeration(): This method returns an enumeration over the collection.</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">java.io.InputStreamReader(InputStream): This method adapts a Stream to Reader class.</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">java.io.OutputStreamWriter(OutputStream): This method adapts an OutputStream to Writer class.</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">javax.xml.bind.annotation.adapters.XmlAdapter.marshal()</span></blockquote>
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span>
<b>11. What is the benefit we get by </b><b>using static factory method to create object?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> By using Static Factory Method we encapsulate the creation process of an object. We can use new() to create an Object from its constructor. Instead we use static method of a Factory to create the object. One main advantage of using Factory is that Factory can choose the correct implementation at runtime and create the right object. The caller of method can specify the desired behavior. E.g. If we have a ShapeFactory with createShape(String type) method. Client can call ShapeFactory.createShape(“Circle”) to get a circular shape. ShapeFactory.createShape(“Square”) will return square shape. In this way, ShapeFactory knows how to create different shapes based on the input by caller. Another use of Factory is in providing access to limited resources to a large set of users.<br /> E.g. In ConnectionPool, we can limit the total number of connections that can be created as well as we can hide the implementation details of creating connection. Here ConnectionPool is the factory. Clients call static method ConnectionPool.getConnection().</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>12. What are the examples of Builder design pattern in JDK?</b><span style="font-family: "timesnewromanpsmt" , serif;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In JDK there are many places where Builder design pattern is used. Some of these are as follows:</span><br />
<ol style="text-align: left;">
<li><span style="font-family: "timesnewromanpsmt" , serif;">java.lang.StringBuilder.append(): StringBuilder is based on Builder pattern.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;">java.nio.IntBuffer.put(): Invocation of put() method return IntBuffer. Also there are many variants of this method to build the IntBuffer.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;">javax.swing.GroupLayout.Group.addComponent(): We can use addComponent() method to build a UI that can contain multiple levels of components.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;">java.lang.Appendable</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;">java.lang.StringBuffer.append(): StringBuffer is similar to StringBuilder and it is also based on Builder design pattern.</span></li>
</ol>
<br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>13. What are the examples of Abstract Factory design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In JDK there are many places where Abstract Factory design pattern is used. Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">javax.xml.xpath.XPathFactory.newInstance()</span><span style="font-family: "timesnewromanpsmt" , serif;">javax.xml.parsers.DocumentBuilderFactory.newInstance()</span><span style="font-family: "timesnewromanpsmt" , serif;">javax.xml.transform.TransformerFactory.newInstance()</span></blockquote>
<b><b><br /></b>14. What are the examples of Decorator design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In JDK there are many places where Decorator design pattern is used. Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">1. In java.io package many classes use Decorator pattern. Subclasses of java.io.InputStream, OutputStream, Reader and Writer have a constructor that can take the instance of same type and decorate it with additional behavior.</span><span style="font-family: "timesnewromanpsmt" , serif;">2. In java.util.Collections, there are methods like checkedCollection(), checkedList(), checkedMap(),synchronizedList(), synchronizedMap(), synchronizedSet(), unmodifiableSet(), unmodifiableMap() and unmodifiableList() methods that can decorate an object and return the same type.</span><span style="font-family: "timesnewromanpsmt" , serif;">3. In javax.servlet package, there are classes like javax.servlet.http.HttpServletRequestWrapper and HttpServletResponseWrapper that are based on Decorator design pattern.</span></blockquote>
<b></b><br />
<div>
<b><br /></b></div>
<b>15. What are the examples of Proxy design pattern in JDK?</b><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"></span></span><br />
<div>
<span style="font-family: "timesnewromanpsmt" , serif;"> Proxy design pattern provides an extra level of indirection for providing access to another object. It can also protect a real object from any extra level of complexity. In JDK there are many places where Proxy design pattern is used.<br /> Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">java.lang.reflect.Proxy</span><span style="font-family: "timesnewromanpsmt" , serif;">java.rmi.*</span><span style="font-family: "timesnewromanpsmt" , serif;">javax.inject.Inject</span><span style="font-family: "timesnewromanpsmt" , serif;">javax.ejb.EJB</span><span style="font-family: "timesnewromanpsmt" , serif;">javax.persistence.PersistenceContext</span></blockquote>
<b>16. What are the examples of Chain of Responsibility design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In JDK there are many places where Chain of Responsibility design pattern is used. Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">1. <b>java.util.logging.Logger.log()</b>: In this case Logger class provides multiple variations of log() method that can take the responsibility of logging from client in different scenarios. The client has to just call the appropriate log() method and Logger will take care of these commands.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">2.<b> javax.servlet.Filter.doFilter()</b>: In the Filter class, the Container calls the doFilter method when a request/response pair is passed through the chain. With filter the request reaches to the appropriate resource at the end of the chain. We can pass FilterChain in doFilter() method to allow the Filter to pass on the request and response to the next level in the chain.</span></blockquote>
<b>17. What are the main uses of Command design pattern?</b><span style="font-family: "timesnewromanpsmt" , serif;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> Command design pattern is a behavioral design pattern. We use it to encapsulate all the information required to trigger an event. Some of the main uses of Command pattern are:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">1. <b>Graphic User Interface (GUI)</b>: In GUI and menu items, we use command pattern. By clicking a button we can read the current information of GUI and take an action.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">2. <b>Macro Recording</b>: If each of user action is implemented as a separate Command, we can record all the user actions in a Macro as a series of Commands. We can use this series to implement the “Playback” feature. In this way, Macro can keep on doing same set of actions with each replay.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">3. <b>Multi-step Undo</b>: When each step is recorded as a Command, we can use it to implement Undo feature in which each step can by undo. It is used in text editors like MS-Word.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">4. <b>Networking</b>: We can also send a complete Command over the network to a remote machine where all the actions encapsulated within a Command are executed.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">5. <b>Progress Bar</b>: We can implement an installation routine as a series of Commands. Each Command provides the estimate time. When we execute the installation routine, with each command we can display the progress bar. </span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">6. <b>Wizard</b>: In a wizard flow we can implement steps as Commands. Each step may have complex task that is just implemented within one command.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">7. <b>Transactions</b>: In a transactional behavior code there are multiple tasks/updates. When all the tasks are done then only transaction is committed. Else we have to rollback the transaction. In such a scenario each step is implemented as separate Command.</span></blockquote>
<div>
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>18. What are the examples of Command design pattern in JDK?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In JDK there are many places where Command design pattern is used. Some of these are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif;">All implementations of java.lang.Runnable</span><span style="font-family: "timesnewromanpsmt" , serif;">All implementations of javax.swing.Action</span></blockquote>
</div>
</div>
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span>
<b><span style="font-family: , serif; line-height: 17.12px;">19.What is the use of Interceptor design pattern?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Interceptor design pattern is used for intercepting a request. Primary use of this pattern is in Security policy implementation. We can use this pattern to intercept the requests by a client to a resource. At the interception we can check for authentication and authorization of client for the resource being accessed. In Java it is used in javax.servlet.Filter interface. This pattern is also used in Spring framework in HandlerInterceptor and MVC interceptor.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">20.What are the Architectural patterns that you have used?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Architectural patterns are used to define the architecture of a Software system. Some of the patterns are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">1. <b>MVC</b>: Model View Controller. This pattern is extensively used in the architecture of Spring framework.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">2. <b>Publish-subscribe</b>: This pattern is the basis of messaging architecture. In this case messages are published to a Topic. And subscribers subscribe to the topic of their interests. Once the message is published to a topic in which a Subscriber has an interest, the message is consumed by the relevant subscriber</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">.</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">3. <b>Service Locator</b>: This design pattern is used in a service like JNDI to locate the available services. It uses as central registry to maintain the list of services. </span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">4. <b>n-Tier</b>: This is a generic design pattern to divide the architecture in multiple tiers. E.g. there is 3-tier architecture with Presentation layer, Application layer and Data access layer. It is also called multi-layer design pattern.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">5. <b>Data Access Object (DAO)</b>: This pattern is used in providing access to database objects. The underlying principle is that we can change the underlying database system, without changing the business logic. Since business logic talks to DAO object, there is no impact of changing Database system on business logic.</span> </blockquote>
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"></span></span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">6. <b>Inversion of Control (IoC)</b>: This is the core of Dependency Injection in Spring framework. We use this design pattern </span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">to increase the modularity of an application. We keep the objects loosely coupled with Dependency Injection.</span><br />
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
</blockquote>
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">21. What are the popular uses of Façade design pattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Some of the popular uses of Façade design pattern are as follows:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">1. A Façade provides convenient methods for common tasks that are used more often.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">2. A Façade can make the software library more readable.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">3. A Façade can reduce the external dependencies on the working of inner code.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">4. A Façade can act as a single well-designed API by wrapping a collection of poorly designed APIs.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">5. A Façade pattern can be used when a System is very complex and difficult to use. It can simplify the usage of complex system.</span></blockquote>
<b><span style="font-family: , serif; line-height: 17.12px;">22.What is the difference between Builder design pattern and Factory design pattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Both Factory and Builder patterns are creational design patterns. They are similar in nature but Factory pattern is a simplified generic version of Builder pattern. We use Factory pattern to create different concrete subtypes of an Object. The client of a Factory may not know the exact subtype. E.g. If we call createDrink() of a Factory, we may get Tea or Coffee drinks.<br /> We can also use Builder pattern to create different concrete subtypes of an object. But in the Builder pattern the composition of the object can be more complex. E.g. If we call createDrink() for Builder, we can getCappuccino Coffee with Vanilla Cream and Sugar, or we can get Latte Coffee with Splenda and milk cream. So a Builder can support creation of a large number of variants of an object. But a Factory can create a broader range of known subtypes of an object.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">23.What is Memento design pattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Memento design pattern is used to implement rollback feature in an object. In a Memento pattern there are three objects:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b>Originator</b>: This is the object that has an internal state.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b>Caretaker</b>: This is the object that can change the state of Originator. But it wants to have control over rolling back the change.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b>Memento</b>: This is the object that Caretaker gets from Originator, before making and change. If Caretaker wants to Rollback the change it gives Memento back to Originator. Originator can use Memento to restore its own state to the original state.</span></blockquote>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> E.g. One good use of memento is in online Forms. If we want to show to user a form pre-populated with some data, we keep this copy in memento. Now user can update the form. But at any time when user wants to reset the form, we use memento to make the form in its original pre-populated state. If user wants to just save the form we save the form and update the memento. Now onwards any new changes to the form can be rolled back to the last saved Memento object.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">24.What is an AntiPattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> An AntiPattern is opposite of a Design Pattern. It is a common practice in an organization that is used to deal with a recurring problem but it has more bad consequences than good ones. AntiPattern can be found in an Organization, Architecture or Software Engineering.<br /> Some of the AntiPatterns in Software Engineering are:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">1. <b>Gold Plating</b>: Keep on adding extra things on a working solution even though these extra things do not add any additional value.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">2. <b>Spaghetti Code</b>: Program that are written in a very complex way and are hard to understand due to misuse of data structures.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">3. <b>Coding By Exception</b>: Adding new code just to handle exception cases and corner case scenarios.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">4. <b>Copy Paste Programming</b>: Just copying the same code multiple times rather than writing generic code that can be parameterized.</span></blockquote>
<br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">25.What is a Data Access Object (DAO) design pattern?</span></b><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> DAO design pattern is used in the data persistent layer of a Java application. It mainly uses OOPS principle of Encapsulation. By using DAO pattern it makes the application loosely coupled and less dependent on actual database. We can even implement some in-memory database like H2 with DAO to handle the unit-testing. In short, DAO hides the underlying database implementation from the class that accesses the data via DAO object. Recently we can combine DAO with Spring framework to inject any DB implementation.</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span><span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">26. How can we implement Producer Consumer design pattern in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We can use BlockingQueue in Java to implement Producer Consumer design pattern. It is a concurrent design pattern.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"></span></span>
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span>
<br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<span style="font-family: "timesnewromanpsmt" , serif;"></span><br />
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_29.html">Multi-threading</a></b> <a href="https://thegeeksarea.blogspot.com/2019/02/java-8-interview-questions.html"><b>Java 8</b></a> </span><br />
<div>
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span></div>
</div>
</div>
</div>
</div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-9509410427761323132019-03-11T11:00:00.000+05:302019-03-11T11:00:07.449+05:30Java Design Patterns Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large;"><br /></span></b>
<b><span style="font-family: , serif; font-size: large;">Java Design Patterns Interview Questions</span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large;"><br /></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large;"><br /></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<br />
<div class="MsoNormal">
<b><span style="line-height: 107%;">1. When will you use Strategy Design
Pattern in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Strategy pattern is very useful for implementing a family of
algorithms. It is a behavioral design pattern. With Strategy pattern we can select
the algorithm at runtime. We can use it to select the sorting strategy for
data. We can use it to save files in different formats like- .txt, .csv, .jpg
etc. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">In Strategy pattern we create an abstraction, which is an
interface through which clients interact with our system. Behind the
abstraction we create multiple implementation of same interface with different
algorithms. For a client, at runtime we can vary the algorithm based on the
type of request we have received. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">So we use Strategy pattern to hide the algorithm
implementation details from client. In Java Collections.sort() method uses
strategy design pattern.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;">2. What is Observer design pattern?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">In Observer design pattern, there is a Subject that maintains
the list of Observers that are waiting for any update on the Subject. Once
there is an update in Subject it notifies all the observers for the change.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">E.g. In real life, students are waiting for the result of
their test. Here students are the observers and test is the subject. Once the
result of test is known, testing organization notifies all the students about
their result. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">The most popular use of Observer pattern is in Model View
Controller (MVC) architectural pattern. Main issue with Observer pattern is
that it can cause memory leaks. The subject holds a strong reference to
observers. If observers are not de-registered in time, it can lead to memory
leak.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;">3. What are the examples of Observer
design pattern in JDK?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">In JDK there are many places where Observer design pattern is
used. Some of these are as follows: <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">1.java.util.Observer, java.util.Observable <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">2.javax.servlet.http.HttpSessionAttributeListener <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">3.javax.servlet.http.HttpSessionBindingListener <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">4.All implementations of java.util.EventListener, and also in
Swing packages <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">5.javax.faces.event.PhaseListener <o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;">4. How Strategy design pattern is
different from State design pattern in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">State design pattern is a behavioral design pattern that is
use for defining the state machine for an object. Each state of an object is
defined in a child class of State class. When different actions are taken on an
Object, it can change its state. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Strategy pattern is also a behavioral pattern, but it is
mainly used for defining multiple algorithms. With same action of a client, the
algorithm to be used can change. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Some people consider State pattern similar to Strategy
pattern, since an Object changes its Strategy with different method
invocations. But the main difference is that in State pattern internal state of
an Object is one of the determining factors for selecting the Strategy for
change of state. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Where as in Strategy pattern, client can pass some external
parameter in input during method invocation that determines the strategy to be
used at run time. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Therefore State pattern is based on the Object’s internal
state, where as Strategy pattern is based on Client’s invocation. State pattern
is very useful in increasing the maintainability of the code in a large
code-base.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;">5. Can you explain Decorator design
pattern with an example in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Some people call Decorator pattern as Wrapper pattern as
well. It is used to add the behavior to an object, without changing the
behavior of other objects of same class. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">One of the very good uses of Decorator pattern is in java.io
package. We can have a FileInputStream to handle a File. To add Buffering
behavior we can decorate FileInputStream with BufferedInputStream. To add the
gzip behavior BufferedInputStream we can decorate it with GzipInputStream. To
add serialization behavior to GzipInputStream, we can decorate it with
ObjectInputStream.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">E.g.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">Open a FileInputStream:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-indent: .5in;">
<span style="line-height: 107%;">FileInputStream fis = new
FileInputStream("/myfile.gz");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">Add buffering:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-indent: .5in;">
<span style="line-height: 107%;">BufferedInputStream bis = new
BufferedInputStream(fis);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">Add Gzip:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-indent: .5in;">
<span style="line-height: 107%;">GzipInputStream gis = new
GzipInputStream(bis);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">Add Serialization:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-indent: .5in;">
<span style="line-height: 107%;">ObjectInputStream ois = new
ObjectInputStream(gis);<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">So with each step we have decorated the FileInputStream with
additional behavior.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;">6. What is a good scenario for using
Composite design Pattern in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Some of the good scenarios where Composite design pattern can
be used are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<b><span style="line-height: 107%;">Tree Structure</span></b><span style="line-height: 107%;">: The most common use of Composite
design pattern is Tree structure. If you want to represent data in a Tree data
structure, Composite pattern can be used. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">E.g. In an Organization, to a Manager has Employees. But
Manager is also an Employee. If we start from CEO level, there is one big tree
for the whole organization structure. Under that big tree there are many
sub-trees. This can be easily represented with Composite design pattern. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<b><span style="line-height: 107%;">Recursion</span></b><span style="line-height: 107%;">: Another use of Composite design
pattern is Recursion. If we have a Recursion based algorithm, we need data to
be passed to algorithm in a data structure that treats individual objects and
compositions at each level of recursion uniformly. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="line-height: 107%;">E.g. To implement a recursive Polynomial Solving algorithm,
we can use Composite design pattern to store the intermediate results. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<b><span style="line-height: 107%;">Graphics</span></b><span style="line-height: 107%;">: Another good use of Composite
design pattern is in Graphics. We can group shapes inside a composite and make
higher-level groups of smaller groups of shapes to complete the graphics to be
displayed on screen.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="line-height: 107%;">7. Have you used Singleton design
pattern in your Java project?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">Yes. Singleton is one of the most popular design patterns in
enterprise level Java applications. Almost in every project we see some
implementation of Singleton. With Singleton pattern we can be sure that there
is only one instance of a class at any time in the application. <o:p></o:p></span></div>
<span style="font-family: "timesnewromanpsmt" , serif;">
</span><br />
<div class="MsoNormal" style="text-indent: .5in;">
<span style="line-height: 107%;">This helps in storing properties that have to be used in the
application in a unique location.<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">8. What are the main uses of Singleton design pattern in Java project?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">Some of the main uses of Singleton design pattern in Java are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Runtime</span></b><span style="line-height: 19.9733px;">: In JDK, java.lang.Runtime is a singleton-based class. There is only one instance of Runtime in an application. This is the only class that interfaces with the environment/machine in which Java process is running.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Enum</span></b><span style="line-height: 19.9733px;">: In Java, enum construct is also based on Singleton pattern. Enum values can be accessed globally in same way by all classes.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Properties</span></b><span style="line-height: 19.9733px;">: In an application it makes sense to keep only one copy of the properties that all classes can access. This can be achieved by making properties class Singleton so that every class gets same copy of properties.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Spring</span></b><span style="line-height: 19.9733px;">: In Spring framework, all the beans are by default Singleton per container. So there is only one instance of bean in a Spring IoC container. But Spring also provides options to make the scope of a bean prototype in a container.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">9. Why java.lang.Runtime is a Singleton in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">In Java, java.lang.Runtime is implemented on Singleton design pattern. Runtime is the class that acts as an interface with the environment in which Java process is running. Runtime contains methods that can interact with the environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">Like- totalmemory() method gives the total memory in JVM. maxMemory() method gives the maximum memory that JVM can use.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">There is an exit() method to exit the Java process. We do not want multiple objects in JVM to have exit() method. Similarly there is gc() method that can run the Garbage Collector. With only one copy of gc() method, we can ensure that no other object can run the Garbage Collector when one instance of GC is already running.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">Due to all these reasons there is only one copy of Runtime in Java. To ensure single copy of Runtime, it is implemented as a Singleton in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">10. What are the examples of Singleton design pattern in JDK?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">In JDK there are many places where Singleton design pattern is used. Some of these are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 19.9733px;">java.lang.Runtime.getRuntime(): This method gives Runtime class that has only one instance in a JVM.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 19.9733px;">java.lang.System.getSecurityManager(): This method returns a SecurityManager for the current platform. java.awt.Desktop.getDesktop()<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">11. What is the way to implement a thread-safe Singleton design pattern in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 19.9733px;">In Java there are many options to implement a thread-safe Singleton pattern. Some of these are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Double Checked Locking</span></b><span style="line-height: 19.9733px;">: This is the most popular methodto implement Singleton in Java. It is based on LazyInitialization. In this we first check the criteria for lockingbefore acquiring a lock to create an object. In Java we useit with volatile keyword.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">Sample code:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">class DoubleCheckSingleton {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">private volatile HelloSingleton helloSingleton; // Use Volatile public HelloSingleton<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">getHelloSingleton() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">HelloSingleton result = helloSingleton;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">if (result == null) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">synchronized(this) { // Synchronize for thread safety<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">result = helloSingleton;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">if (result == null) {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 2in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">result = new HelloSingleton();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 2in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">helloSingleton = result;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 2in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">return result;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Bill Pugh Singleton</span></b><span style="line-height: 19.9733px;">: We can also use the method by BillPugh for implementing Singleton in Java. In this we use anInner Static class to create the Singleton instance.Sample code:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">public class SingletonBillPugh { // Inner class that holds instance<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">private static class InnerSingleton{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">private static final SingletonBillPugh INSTANCE = new SingletonBillPugh();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">} // Private constructor<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">private SingletonBillPugh(){}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">public static SingletonBillPugh getInstance(){<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify; text-indent: 0.5in;">
<span style="line-height: 19.9733px;">return InnerSingleton.INSTANCE;<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">When first time SingletonBillPugh is loaded in memory, InnerSingleton is not loaded. Only when getInstance() method is called, InnerSingleton class is loaded and an Instance is created.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="line-height: 19.9733px;">Enum</span></b><span style="line-height: 19.9733px;">: We can also use Java enum to create thread-safe implementation. Java enum values are accessible globally so these can be used as a Singleton.Sample Code:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">public enum SingletonEnum {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">INSTANCE; public static void doImplementation(){ ….. }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">12. What is Template Method design pattern in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">It is a behavioral design pattern. We can use it to create an outline for an algorithm or a complex operation. We first create the skeleton of a program. Then we delegate the steps of the operation to subclasses. The subclasses can redefine the inner implementation of each step.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">E.g. While designing a Game in Java, we can implement it as an algorithm with Template Method pattern. Each step in the game can be deferred to subclasses responsible for handling that step.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">Let say we implement Monopoly game in Java. We can create methods like initializeGame(), makeMove(), endGame() etc. Each of these methods can be handled in subclasses in an independent manner.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">We can use same algorithm for Chess game with same set of abstract methods. The subclass for Chess game can provide the concrete implementation of methods like initializeGame(), makeMove(), endGame() etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">Template Method pattern is very useful in providing customizable class to users. We can create the core class with a high level implementation. And our users can customize our core class in their custom subclasses.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;"><br /></span></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">13. What are the examples of Template method design pattern in JDK?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">In JDK there are many places where Template method design pattern is used. Some of these are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;"><b>1.</b> In Java Abstract Collection classes like java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap implement a template for their corresponding Collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;"><b>2.</b> javax.servlet.http.HttpServlet: In the HttpServlet class all the doGet(), doPost() etc. methods send a HTTP 405 "Method Not Allowed" error to the response. This error response is like a Template that can be further customized for each of these methods.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;"><b>3.</b> In java.io package there are Stream and Writer classes like java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer that provide non-abstract methods. These methods are implementation of Template method design pattern.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="line-height: 19.9733px;">14. Can you tell some examples of Factory Method design pattern implementation in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 19.9733px;">Factory Method pattern is a creational design pattern. A Factory is an object that is used to create more objects. In general, a Factory object has methods that can be used to create a type of objects. Some people call it Factory Method design pattern as well.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 19.9733px;">Some of the examples of Factory Method pattern in JDK are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">Java.lang.Class.forName()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">java.net.URLStreamHandlerFactory.createURLStreamHandler(String)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">java.util.Calendar.getInstance()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">java.util.ResourceBundle.getBundle()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">java.text.NumberFormat.getInstance()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">java.nio.charset.Charset.forName()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">java.util.EnumSet.of()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 19.9733px;">javax.xml.bind.JAXBContext.createMarshaller()<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;">15.What is the difference between Factory and Abstract Factory design pattern?</span></b><br />
<div style="text-align: left;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> With Factory design pattern we can create concrete products of a type that Factory can manufacture. E.g. If it is CarFactory, we can produce, Ford, Toyota, Honda, Maserati etc. With Abstract Factory design pattern we create a concrete implementation of a Factory. E.g. DeviceFactory can be Abstract and it can give us GoogleDeviceFactory, AppleDeviceFactory etc. With AppleDeviceFactory we will get products like- iPhone, iPad, Mac etc. With GoogleDeviceFactory we will get products likeNexus phone, Google Nexus tablet, Google ChromeBook etc. So it is a subtle difference between Factory and Abstract Factory design pattern. One way to remember is that within Abstract Factory pattern, Factory pattern is already implemented.</span></div>
<div style="text-align: left;">
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">16. What is Open/closed design principle in Software engineering?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Open/closed design principle states “software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification”. Open/closed principle term was originated by Bertrand Meyer in his book Object Oriented Software Construction. As per this principle, if a module is available for extension then it is considered open. If a module is available for use by other modules then it is considered closed. Further Robert C. Martin has mentioned it as O in SOLID principles of Object Oriented design. It is used in State and Strategy design patterns. Context class is closed for modification. But new functionality can be added by writing new strategy code.</span></div>
<div style="text-align: left;">
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">17. What is SOLID design principle?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> SOLID word in <b>SOLID </b>design principle is an acronym for:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">1. <b>S</b>: Single responsibility. A Class should have a single responsibility.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">2. <b>O</b>: Open-closed. Software entities should be open for extension but closed for modification.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">3. <b>L</b>: Liskov substitution. Objects in a program should be replaceable by subclasses of same type without any adverse impact.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">4. <b>I</b>: Interface segregation. Multiple client specific interfaces are preferable over single generic interface.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">5. <b>D</b>: Dependency inversion. Program should depend on abstract entities. It should not depend on concrete implementation of an interface. </span></blockquote>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> This principle was mentioned by Robert C. Martin. These are considered five basic principles of Object Oriented design. If we follow these principles, then we can create a stable program that is easy to maintain and can be extended over time.</span></div>
<div style="text-align: left;">
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">18. What is Builder design pattern?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Builder design pattern is a creational design pattern. We can use Builder pattern to create complex objects with multiple options. E.g. when we have to create a Meal in a restaurant we can use Builder pattern. We can keep adding options like- Starter, Drink, Main Course, and Dessert etc. to create complete meal. When a user selects other options of Starter, Drink Main Course, Dessert another type of meal is created. Main feature of Builder pattern is step-by-step building of a complex object with multiple options.</span></div>
<div style="text-align: left;">
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">19. What are the different categories of Design Patterns used in Object Oriented Design?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Object Oriented design mainly three categories of design patterns are used. These categories are:<br /> Creational Design Patterns:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Builder</span><br />
<blockquote>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Factory Method</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Abstract Factory</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Object Pool</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Singleton</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Prototype</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Structural Design Patterns:</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Adapter</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Bridge</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Façade</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Decorator</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Composite</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Flyweight</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Proxy</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Behavioral Design Patterns:</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Command</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Iterator</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Chain of Responsibility</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Observer</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">State</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Strategy</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Mediator</span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Interpreter</span></blockquote>
</blockquote>
<span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span><b><span style="font-family: , serif; line-height: 17.12px;">20. What is the design pattern </span></b><b><span style="font-family: , serif; line-height: 17.12px;">suitable to access elements of a Collection?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We can use Iterator design pattern to access the individual elements of a Collection. In case of an ordered collection we can get Iterator that returns the elements in an order.<br /> In Java there are many implementation of Iterator in Collections package. We have iterators like- Spliterator, ListIterator etc. that implement Iterator pattern.</span><br />
<br />
<b><span style="font-family: , serif; line-height: 17.12px;">21. What design pattern is suitable to add new features to an existing object?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We can use Decorator design pattern to add new features to an existing object. With a Decorator we work on same object and return the same object with more features. But the structure of the object remains same since all the decorated versions of object implement same interface.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">22. Which design pattern can be used when to decouple abstraction from the implementation?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We can use Bridge design pattern to detach the implementation from the abstraction. Bridge is mainly used for separation of concern in design. We can create an implementation and store it in the interface, which is an abstraction. Where as specific implementation of other features can be done in concrete classes that implement the interface. Often Bridge design pattern is implemented by using Adapter pattern.<br /> E.g. we have Shape interface. We want to make Square and Circle shapes. But further we want to make RedSquare, BlackSquare shapes and GreenCircle, WhiteCircle shapes. In this case rather than creating one hierarchy of all the shapes, we separate the Color concern from Shape hierarchy. </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> So we create two hierarchies. One is Shape to Square and Shape to Circle hierarchy. Another one is Color to Red, Black, Green, White hierarchy. In this way we can create multiple types of shapes with multiple colors with Bridge design pattern.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">23.Which is the design pattern used in Android applications?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Android applications predominantly use Model View Presenter design pattern.</span><br />
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">1. <b>Model</b>: This is the domain model of the Android application. It contains the business logic and business rules.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">2. <b>View</b>: These are the UI components in your application. These are part of the view. Also any events on UI components are part of view module.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">3. <b>Presenter</b>: This is the bridge between Model and View to control the communication. Presenter can query the model and return data to view to update it.</span> </blockquote>
<blockquote class="tr_bq">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> E.g.<b> </b>If we have a Model with large news article data, and view needs only headline, then presenter can query the data from model and only give headline to view. In this way view remains very light in this design pattern.</span></blockquote>
<b><span style="font-family: , serif; line-height: 17.12px;">24. How can we prevent users from creating more than one instance of singleton object by using clone() method?</span></b><br />
<span style="font-family: "times new roman" , serif; line-height: 17.12px;"></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> First we should not implement the Cloneable interface by the object that is a Singleton. Second, if we have to implement Cloneable interface then we can throw exception in clone() method. This will ensure that no one can use clone() method or Cloneable interface to create more than one instance of Singleton object.</span></div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<span style="font-family: "timesnewromanpsmt" , serif;"></span><br />
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_29.html">Multi-threading</a></b> <a href="https://thegeeksarea.blogspot.com/2019/02/java-8-interview-questions.html"><b>Java 8</b></a> </span></div>
</div>
</div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-41710747497234643992019-03-07T07:14:00.000+05:302019-03-10T21:39:29.532+05:3050+ Hibernate Interview Question and Answer <div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><br /><br /><br /> </span><br />
<div style="text-align: center;">
<span style="font-size: x-large;"><b>Hibernate Intervirw Questions</b></span></div>
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><b> Hibernate </b>is one of the most popular persistent frameworks in Java( <b>JPA </b>). Hibernate offers object to relational( <b>ORM </b>) solution which frees Java developers from hard to read and cluttered JDBC code converting SQL columns into Object properties. Apart from freeing Java developer from JDBC connection and database interaction code, <b>Hibernate </b>also offers the out-of-box solution on caching or proxying and lazy loading which drastically improves the performance of your Java Web application. Given it's important in <b>Java Web application development</b> field Hibernate has become of one of the most sought-after skill and goes hand in hand with Spring framework in java. That's why <b>Spring and Hibernate interview questions</b> are also very popular with <b>Java interviews</b> for <b>web development positions</b>.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"> Earlier I have shared some importante <b><a href="https://thegeeksarea.blogspot.com/2019/03/spring-interview-questions.html">Spring MVC interview questions</a></b> and due to popular demand, I am now sharing some 50+ frequently asked <b>Hibernate questions</b> from various Java <b>interviews </b>for web development positions.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"> These questions are very useful for both the phone and the face-to-face round of interviews and both <b>freshers </b>and <b>experienced Java developers</b> up to 2 to 5 years can benefit from these questions.</span><br />
<span style="font-size: large;"><br /></span>
<span style="font-size: large;"><br /></span>
<span style="color: lime; font-size: large;"><b>Top 50+ Hibernate Questions from Java JEE Interviews:</b></span><br />
<span style="font-size: large;"></span><br />
<span style="font-size: large;"> Here is my selected list of <b>50+ Hibernate based questions for Java developers</b>. It contains questions from <b>Hibernate fundamentals, one-to-one, and one-to-many mappings, caching, Hibernate vs JDBC comparison, pros and cons of Hibernate, known problems with Hibernate, and Performance improvement</b>.</span><br />
<div>
<br /></div>
<span style="font-size: large;"><br /></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">1. What is Hibernate framework?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> <b>Hibernate </b>is a popular <b>Object Relational Mapping (ORM)</b> framework of Java. It helps in mapping the Object Oriented Domain model to Relational Database tables. Hibernate is a free software distributed under GNU license. Hibernate also provides implementation of <b>Java Persistence API (JPA)</b>. In simple words, it is a framework to retrieve and store data from database tables from Java.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">2.What is an Object Relational Mapping (ORM)?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> <b>Object Relational Mapping (ORM)</b> is a programming technique to map data from a relational database to Object oriented domain model. This is the core of Hibernate framework. In case of Java, most of the software is based on OOPS design. But the data stored in Database is based on <b>Relation Database Management System (RDBMS)</b>. ORM helps in data retrieval in an Object Oriented way from an RDBMS. It reduces the effort of developers in writing queries to access and insert data.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">3.What is the purpose of Configuration Interface in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> Configuration interface can be implemented in an application to specify the properties and mapping documents for creating a SessionFactory in Hibernate. By default, a new instance of Configuration uses properties mentioned in hibernate.properties file. Configuration is mainly an initialization time object that loads the properties in helps in creating SessionFactory with these properties. In short, Configuration interface is used for configuring Hibernate framework in an application.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">4.What is Object Relational Impedance Mismatch?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> <b>Object Relational Impedance Mismatch (ORIM)</b> is also known as paradigm mismatch. It means that Object model and Relational model do not work well with each other. Relational model or a RDBMS represents data in tabular format like a spreadsheet. Object model or OOPS represents the data as an inter-connected graph of objects. Mixing these two models leads to various problems. The common name for these issues is Object Relational Impedance Mismatch.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">5.What are the main problems of Object Relational Impedance Mismatch?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> Object model and Relational models (RDBMS) have following problems that are part of Object Relational Impedance Mismatch:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Granularity</span></b><span style="font-family: "calibri";">: Object model is more granular than Relational model. There are more classes in object model than the corresponding tables in relational model.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Inheritance</span></b><span style="font-family: "calibri";">: Object model supports inheritance. But Relational model does not have any concept of inheritance.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Identity</span></b><span style="font-family: "calibri";">: Relational model has just one criteria for sameness of data. It is based on primary key. In object model like Java we can have equals as well as == for sameness of objects.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Associations</span></b><span style="font-family: "calibri";">: In Object model associations are uni-directional. In RDBMS, there is a concept of foreign key for association. Also multiplicity of a relationship is hard to judge by looking at object model.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Data navigation</span></b><span style="font-family: "calibri";">: In Object model, you can move from one object to another object for getting data. Egg. you can retrieve and Employee object, then go to its department object and then get the employees in the department object. In RDBMS, we try to minimize the SQL calls, so we get all the data by using joins.</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">6. What are the key characteristics of Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> Hibernate has following key characteristics:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Object/Relational Mapping (ORM)</span></b><span style="font-family: "calibri";">: Hibernate provides ORM capabilities to developers. So then can write code in Object model for connecting with data in Relational model.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">JPA Provider</span></b><span style="font-family: "calibri";">: Hibernate provides an excellent implementation of Java Persistence API (JPA) specification.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Idiomatic persistence</span></b><span style="font-family: "calibri";">: Hibernate provides persistence based on natural Object-oriented idioms with full support for inheritance, polymorphism, association, composition, and the Java collections framework. It can work with any data for persistence.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">High Performance</span></b><span style="font-family: "calibri";">: Hibernate provides high level of performance supporting features like- lazy initialization, multiple fetching strategies, optimistic locking etc. Hibernate does not need its own database tables or fields. It can generate SQL at system initialization to provide better performance at runtime.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Scalability</span></b><span style="font-family: "calibri";">: Hibernate works well in multi server clusters. It has built in scalability support. It can work well for small projects as well as for large business software.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Reliable</span></b><span style="font-family: "calibri";">: Hibernate very reliable and stable framework. This is the reason for its worldwide acceptance and popularity among developer community.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Extensible</span></b><span style="font-family: "calibri";">: Hibernate is quite generic in nature. It can be configured and extended as per the use case of application.</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">7</span></b><b><span style="color: red; font-family: "calibri";">. What are the two locking strategies in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> There are two popular locking strategies that can be used in Hibernate:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Optimistic</span></b><span style="font-family: "calibri";">: In Optimistic locking we assume that multiple transactions can complete without affecting each other. So we let the transactions do their work without locking the resources initially. Just before the commit, we check if any of the resource has changed by another transaction, then we throw exception and rollback the transaction.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Pessimistic</span></b><span style="font-family: "calibri";">: In Pessimistic locking we assume that concurrent transactions will conflict while working with same resources. So a transaction has to first obtain lock on the resources it wants to update.</span></span></blockquote>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> The other transaction can proceed with same resource only after the lock has been released by previous transaction.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">8</span></b><b><span style="color: red; font-family: "calibri";">. What is the use of version number in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Version number is used in optimistic locking in Hibernate. When a transaction modifies an object, it increments its version. Based on version number, second transaction can determine if the object it has read earlier has changed or not.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> If the version number at the time of write is different than the version number at the time of read, then we should not commit the transaction.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">9</span></b><b><span style="color: red; font-family: "calibri";">. What is the use of session.lock() method in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Session.lock() is a deprecated method in Hibernate. We should not use it. </span><span style="font-family: "calibri";">Instead we should call buildLockRequest( LockMode ).lock( entityName, object) method in Hibernate.</span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">10</span></b><b><span style="color: red; font-family: "calibri";">. What inheritance mapping strategies are supported by Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Hibernate supports following inheritance mapping strategies between classes and tables:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Table per class hierarchy</span></b><span style="font-family: "calibri";">: In case of multiple types of books, we can have one book class and one book table. We can store all child classes of book like- HardCoverBook, PaperBackBook etc in same table book. But we can identify the subclasses by a BookType column in Book table.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Table per subclass</span></b><span style="font-family: "calibri";">: In this case we can have separate table for each kind of book. HardCoverBook table for HardCoverBook book class. PaperBackBook table for PaperBackBook book class. And there will be a parent table, Book for Book class.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Table per concrete class</span></b><span style="font-family: "calibri";">: In this case also we have separate table for each kind of book. But in this case we have even inherited properties defined inside each table. There is no parent table Book for Book class, since it is not a concrete class.</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">11</span></b><b><span style="color: red; font-family: "calibri";">. What is ‘the inverse side of association’in a mapping?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Let us consider an example in which a customer can have multiple orders and for every order there has to be a customer. In OO world, customer is the owner of order. In SQL world, an Order has reference to customer id. It is a bi-directional one to many mapping from customer to order. The inverse side in this mapping is the owner of object. In this case customer is the owner or order. Since an order cannot exist without a customer. But a customer can exist without an order. Also customer has no column to save order data. But an Order table can store customer id, which is used for mapping.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">12</span></b><b><span style="color: red; font-family: "calibri";">. What is ORM metadata?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> ORM uses metadata for its internal work. ORM maintains metadata to generate code used for accessing columns and tables. ORM maps classes to tables and stores this information in Metadata. It maps fields in classes to columns in tables. These kinds of mappings are also part of Metadata. Application developers can also access Hibernate Metadata by using ClassMetadata and CollectionMetadata interfaces and Type hierarchy.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">13</span></b><b><span style="color: red; font-family: "calibri";">.What is the difference between load() and get() method in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> In Hibernate, load() and get() methods are quite similar in functionality. The main difference is that load() method will throw an ObjectNotFoundException if row corresponding to an object is not found in the database.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">On the other hand, get() method returns null value when an object is not found in the database.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">It is recommended that we should use load() method only when we are sure that object exists in database.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">14</span></b><b><span style="color: red; font-family: "calibri";">. When should we use get() method or load() method in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> As a thumb rule we can follow these guidelines:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> We should use get() method when we want to load an object. </span><span style="font-family: "calibri";">We should use load() method when we need a reference to an object without running extra SQLqueries.</span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">15.Can you tell us about the core interfaces of Hibernate framework?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> The core interfaces of Hibernate framework are as follows:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Configuration</span></b><span style="font-family: "calibri";">: Configuration interface can be implemented in an application to specify the properties and mapping documents for creating a SessionFactory in Hibernate. Hibernate application bootstraps by using this interface.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">SessionFactory</span></b><span style="font-family: "calibri";">: In Hibernate, SessionFactory is used to create and manage Sessions. Generally, there is one SessionFactory created for one database. It is a thread-safe interface that works well in multithreaded applications.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Session</span></b><span style="font-family: "calibri";">: Session is a lightweight object that is used at runtime between a Java application and Hibernate. It contains methods to create, read and delete operations for entity classes. It is a basic class that abstracts the concept of persistence.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Transaction</span></b><span style="font-family: "calibri";">: This is an optional interface. It is a short lived object that is used for encapsulating the overall work based on unit of work design pattern. A Session can have multiple Transactions.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Query</span></b><span style="font-family: "calibri";">: This interface encapsulates the behavior of an objectoriented query in Hibernate. It can accept parameters and execute the queries to fetch results. Same query can be executed multiple times.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Criteria</span></b><span style="font-family: "calibri";">: This is a simplified API to retrieve objects by creating Criterion objects. It is very easy to use for creating Search like features.</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">16.How will you map the columns of a DB table to the properties of a Java class in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> We can map the class properties and table columns by using one of the two ways:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">XML</span></b><span style="font-family: "calibri";">: We can map the column of a table to the property of a class in XMLfile. It is generally with extension hbm.xml</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Annotation</span></b><span style="font-family: "calibri";">: We can also use annotations @Entity and @Table to map a column to the property of a class.</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<!--[if !supportLists]--><span style="font-size: large;"><span style="color: red; font-family: "calibri"; font-weight: bold;">17. </span><!--[endif]--><b><span style="color: red; font-family: "calibri";">Does Hibernate make it mandatory for a mapping file to have .hbm.xml extension?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> No. It is a convention to have</span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">.hbm.xml extension in the name of a mapping file. It is not a requirement enforced by Hibernate. We can use any other extension of our convenience for this.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">18. What are the steps for creating a SessionFactory in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Steps to create a SessionFactory in Hibernate are:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Configuration</span></b><span style="font-family: "calibri";">: First create a Configuration object. This will refer to the path of configuration file.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Resource</span></b><span style="font-family: "calibri";">: Add config file resource to Configuration object.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Properties</span></b><span style="font-family: "calibri";">: Set properties in the Configuration object.</span></span> </blockquote>
<blockquote class="tr_bq">
<span style="font-size: large;"><b><span style="font-family: "calibri";">SessionFactory</span></b><span style="font-family: "calibri";">: Use Configuration object to build SessionFactory.</span></span></blockquote>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Egg.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><span style="font-family: "calibri";">Configuration config = new Configuration();</span></span><span style="font-size: large;"><span style="font-family: "calibri";">config.addResource(“testInstance/configuration.hbm.xml”);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">config.setProperties( System.getProperties() );</span></span><span style="font-size: large;"><span style="font-family: "calibri";">SessionFactory sessions = config.buildSessionFactory();</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">19.Why do we use POJO in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> POJO stands for Plain Old Java Objects. A POJO is java bean with getter and setter methods for each property of the bean. It is a simple class that encapsulates an object’s properties and provides access through setters and getters.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> Some of the reasons for using POJO in Hibernate are: </span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> POJO emphasizes the fact that this class is a simple Java class, not a heavy class like EJB. POJO is a well-constructed class, so it works well with Hibernate proxies. POJO also comes with a default constructor that makes it easier to persist with a default constructor.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">20. What is Hibernate Query Language (HQL)?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Hibernate Query Language is also known as HQL. It is an Object Oriented language. But it is similar to SQL. HQL works well with persistent objects and their properties. HQL does not work on database tables. HQL queries are translated into native SQL queries specific to a database. HQL supports direct running of native SQL queries also. But it creates an issue in Database portability.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">21.How will you call a stored procedure in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> Hibernate supports executing not only simple queries but also stored procedure of database. There are three ways to call a stored procedure in Hibernate:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">XMLmapping file</span></b><span style="font-family: "calibri";">:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can declare the store procedure inside XMLMapping file.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><span style="font-family: "calibri";"><!-- Employee.hbm.xml --></span></span><span style="font-size: large;"><span style="font-family: "calibri";">...</span></span><span style="font-size: large;"><span style="font-family: "calibri";"><hibernate-mapping></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><class name="com.testHibernate.util.Employee" </span></span><span style="font-size: large;"><span style="font-family: "calibri";">table="employee" ...></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><id name="employeeId" type="java.lang.Integer"></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><column name=“EMPLOYEE_ID" /></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><generator class="identity" /></span></span><span style="font-size: large;"><span style="font-family: "calibri";"></id></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><property name="employeeId" type="string"></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><column name="EMPLOYEE_ID" length="10" notnull="true" unique="true" /></span></span><span style="font-size: large;"><span style="font-family: "calibri";"></property></span></span><span style="font-size: large;"><span style="font-family: "calibri";"></class></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><sql-query name="callEmployeeStoreProcedure"></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><return alias="employee" </span></span><span style="font-size: large;"><span style="font-family: "calibri";">class="com.testHibernate.util.Employee"/></span></span><span style="font-size: large;"><span style="font-family: "calibri";"><![CDATA[CALLGetEmployees(:employeeId)]]></span></span><span style="font-size: large;"><span style="font-family: "calibri";"></sql-query></span></span><span style="font-size: large;"><span style="font-family: "calibri";"></hibernate-mapping></span></span><span style="font-size: large;"><span style="font-family: "calibri";">We can call it with getNamedQuery().</span></span><span style="font-size: large;"><span style="font-family: "calibri";">Query query = session.getNamedQuery("callEmployeeStoreProcedure").setParameter("employeeId", “1234”);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">List result = query.list();</span></span><span style="font-size: large;"><span style="font-family: "calibri";">for(int i=0; i<result.size(); i++){</span></span><span style="font-size: large;"><span style="font-family: "calibri";">Employee employee = (Employee)result.get(i);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">System.out.println(employee.getEmployeeCode());</span></span><span style="font-size: large;"><span style="font-family: "calibri";">}</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Native SQL</span></b><span style="font-family: "calibri";">: We can use Native SQLto call a store procedure query directly. In this example GetEmployees() stored procedure is being called.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><span style="font-family: "calibri";">Query query = session.createSQLQuery("CALLGetEmployees(:employeeId)").addEntity(Employee.class).setParameter("employeeId", “1234);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">List result = query.list();</span></span><span style="font-size: large;"><span style="font-family: "calibri";">for(int i=0; i<result.size(); i++){</span></span><span style="font-size: large;"><span style="font-family: "calibri";">Employee employee = (Employee) result.get(i);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">System.out.println(employee.getEmployeeCode());</span></span><span style="font-size: large;"><span style="font-family: "calibri";">}</span></span></blockquote>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Use annotation</span></b><span style="font-family: "calibri";">:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<blockquote class="tr_bq">
<span style="font-size: large;"><span style="font-family: "calibri";">We can also mark out stored procedure with @NamedNativeQueries annotation.</span></span><span style="font-size: large;"><span style="font-family: "calibri";">//Employee.java</span></span><span style="font-size: large;"><span style="font-family: "calibri";">@NamedNativeQueries({ @NamedNativeQuery(name = "callEmployeeStoreProcedure", query = "CALLGetEmployees(:employeeId)", resultClass = Employee.class ) })</span></span><span style="font-size: large;"><span style="font-family: "calibri";">@Entity</span></span><span style="font-size: large;"><span style="font-family: "calibri";">@Table(name = "employee")</span></span><span style="font-size: large;"><span style="font-family: "calibri";">public class Employee implements java.io.Serializable {</span></span><span style="font-size: large;"><span style="font-family: "calibri";">...</span></span><span style="font-size: large;"><span style="font-family: "calibri";">Call it with getNamedQuery().</span></span><span style="font-size: large;"><span style="font-family: "calibri";">Query query = session.getNamedQuery("callEmployeeStoreProcedure").setParameter("employeeId", “1234”);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">List result = query.list();</span></span><span style="font-size: large;"><span style="font-family: "calibri";">for(int i=0; i<result.size(); i++){</span></span><span style="font-size: large;"><span style="font-family: "calibri";">Employee employee = (Employee)result.get(i);</span></span><span style="font-size: large;"><span style="font-family: "calibri";">System.out.println(employee.getEmployeeCode());</span></span><span style="font-size: large;"><span style="font-family: "calibri";">}</span></span></blockquote>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">22. What is Criteria API in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"></span><span style="font-family: "calibri";"> Criteria is a simplified API in Hibernate to get entities from database by creating Criterion objects. It is a very intuitive and convenient approach for search features. Users can specify different criteria for searching entities and Criteria API can handle these. Criterion instances are obtained through factory methods on Restrictions.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">23. Why do we use HibernateTemplate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">This is a trap question. HibernateTemplate has been deprecated. There were earlier good reasons to use HibernateTemplate. But now the trend has changed towards not using it anymore.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">24. How can you see SQL code generated by Hibernate on console?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">To display the SQL generated by Hibernate, we have to turn on the show_sql flag.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">This can be done in Hibernate configuration as follows:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"><property name=”show_sql”>true</property></span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">25. What are the different types of collections supported by Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate supports following two types of collections:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Indexed Collections: List and Maps Sorted Collections: java.util.SortedMap and java.util.SortedSet</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">26. What is the difference between session.save() and session.saveOrUpdate() methods in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Save method first stores an object in the database. Then it persists the given transient instance by assigning a generated identifier. Finally, it returns the id of the entity that is just created. SaveOrUpdate() method calls either save() or update() method. It selects one of these methods based on the existence of identifier. If an identifier exists for the entity then update() method is called. If there is no identifier for the entity then save() method is called as mentioned earlier.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">27. What are the advantages of Hibernate framework over JDBC?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Main advantages of Hibernate over JDBC are as follows:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Database Portability: Hibernate can be used with multiple types of database with easy portability. In JDBC, developer has to write database specific native queries. These native queries can reduce the database portability of the code.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Connection Pool: Hibernate handles connection pooling very well. JDBC requires connection pooling to be defined by developer.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Complexity: Hibernate handles complex query scenarios very well with its internal API like Criteria. So developer need not gain expertise in writing complex SQL queries. In JDBC application developer writes most of the queries.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">28. How can we get statistics of a SessionFactory in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate we can get the statistics of a SessionFactory by using Statistics interface. We can get information like Close Statement count, Collection Fetch count, Collection Load count, Entity insert count etc.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">29. What is the Transient state of an object in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">When an object is just instantiated using the new operator but is not associated with a Hibernate Session, then the object is in Transient state. </span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Transient state, object does not have a persistent representation in database. Also there is no identifier assigned to an object in Transient state. An object in Transient state can be garbage collected if there is no reference pointing to it.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">30. What is the Detached state of an object in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">An object is in detached state if it was persistent earlier but its Session is closed now.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Any reference to this object is still valid. We can even update this object. Later on we can even attach an object in detached state to a new session and make it persistent.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Detached state is very useful in application transactions where a user takes some time to finish the work.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">31.What is the use of Dirty Checking in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Dirty Checking is very useful feature of Hibernate for write to database operations. Hibernate monitors all the persistent objects for any changes. It can detect if an object has been modified or not. By Dirty Checking, only those fields of an object are updated that require any change in them. It reduces the time-consuming database write operations.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">32. What is the purpose of Callback interface in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Callback interface in Hibernate is mainly used for receiving notifications of different events from an object.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Egg. We can use Callback to get the notification when an object is loaded into or removed from database.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">33. What are the different ORM levels in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">There are following four different ORM levels in Hibernate:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Pure Relational ORM</span></b><span style="font-family: "calibri";">: At this level entire application is designed around the relational model. All the operations are SQL based at this level.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Light Object Mapping</span></b><span style="font-family: "calibri";">: At this level entity classes are mapped manually to relational tables. Business logic code is hidden from data access code. Applications with less number of entities use this level.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Medium Object Mapping</span></b><span style="font-family: "calibri";">: In this case, application is designed around an object model. Most of the SQL code is generated at compile time. Associations between objects are supported by the persistence mechanism. Object-oriented expression language is used to specify queries.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Full Object Mapping</span></b><span style="font-family: "calibri";">: This is one of the most sophisticated object modeling level. It supports composition, inheritance, polymorphism and persistence. The persistent classes do not inherit any special base class at this level. There are efficient fetching and caching strategies implemented transparently to the application.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">34. What are the different ways to configure a Hibernate application?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">There are mainly two ways to configure Hibernate application:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">XML based</span></b><span style="font-family: "calibri";">: We can define the Hibernate configuration in an XML file like ibernate.cfg.xml file</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Programming based</span></b><span style="font-family: "calibri";">: We can also use code logic to configure Hibernate in our application.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">35. What is Query Cache in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate provides Query Cache to improve the performance of queries that run multiple times with same parameters. At times Query Caching can reduce the performance of Transactional processing. By default Query Cache is disabled in Hibernate. It has to be used based on the benefits gained by it in performance of the queries in an application.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">36. What are the different types of Association mappings supported by Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate supports following four types of Association mappings:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Unidirectional association</span></b><span style="font-family: "calibri";">: This kind of association works in only one direction.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Unidirectional association with join tables Bidirectional </span><b><span style="font-family: "calibri";">association</span></b><span style="font-family: "calibri";">: This kind of association works in both directions. Bidirectional association with join tables</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">37. What are the different types of Unidirectional Association mappings in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate there can be following three types of Unidirectional Association mappings:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Many to one</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">One to one</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">One to many</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">38. What is Unit of Work design pattern?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Unit of Work is a design pattern to define business transactions. A Unit of Work is a list of ordered operations that we want to run on a database together. Either all of these go together or none of these goes. Most of the time, we use term business transaction in place of Unit of Work.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Egg. In case of money transfer from account A to B, the unit of work can be two operation Debit account A and Credit account B in a sequence. Both these operations should happen together and in right sequence.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">39. In Hibernate, how can an object go in Detached state?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Once the session attached to an Object is closed, the object goes into Detached state. An Object in Detached state can be attached to another session at a later point of time. This state is quite useful in concurrent applications that have long unit of work.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">40. How will you order the results returned by a Criteria in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate provides an Order criterion that can be used to order the results. This can be order objects based on their property in ascending or descending order. Class is org.hibernate.criterion.Order.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">One example is as follows:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Egg.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">List employees = session.createCriteria(Employee.class).add( Restrictions.like("name", "F%").addOrder( Order.asc("name") ).addOrder( Order.desc("age") ).setMaxResults(10).list();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">41. How does Example criterion work in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate, we can create an object with desired properties. Then we can use this object to search for objects with similar object. For this we can use org.hibernate.criterion.Example criterion. Egg. First we create a sample book object of author Richard and category mystery. Then we search for similar books.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Book book = new Book();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">book.setAuthor(‘Richard’);</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">book.setCategory(Category.MYSTERY);</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">List results = session.createCriteria(Book.class).add( Example.create(book) ).list();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">42. How does Transaction management work in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate we use Session interface to get a new transaction. Once we get the transaction we can run business operations in that transaction. At the end of successful business operations, we commit the transaction. In case of failure, we rollback the transaction.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Sample code is a follows:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Session s = null;</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Transaction trans = null;</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">try {</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">s = sessionFactory.openSession();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">trans = s.beginTransaction();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">doTheAction(s);</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">trans.commit();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">} catch (RuntimeException exc) {</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">trans.rollback();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">} finally {</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">s.close();</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">}</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">43. How can we mark an entity/collection as immutable in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate, by default an entity or collection is mutable. We can add, delete or update an entity/collection.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">To mark an entity/collection as immutable, we can use one of the following:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">@Immutable: We can use the annotation @Immutable to mark an entity/collection immutable.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">XML file: We can also set the property mutable=false in the XML file for an entity to make it immutable.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">44. What are the different options to retrieve an object from database in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate, we can use one of the following options to retrieve objects from database:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Identifier</span></b><span style="font-family: "calibri";">: We can use load() or get() method and pass the identifier like primary key to fetch an object from database.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">HQL</span></b><span style="font-family: "calibri";">: We can create a HQL query and get the object after executing the query.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Criteria API</span></b><span style="font-family: "calibri";">: We can use Criteria API to create the search conditions for getting the objects from database.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Native SQL</span></b><span style="font-family: "calibri";">: We can write native SQL query for a database and just execute it to get the data we want and convert it into desired object.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">45. How can we auto-generate primary key in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can use the primary key generation strategy of type GenerationType.AUTO to auto-generate primary key while persisting an object in Hibernate.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Egg.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">@Id</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">@GeneratedValue(strategy=GenerationType.AUTO)</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">private int id;</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can leave it null/0 while persisting and Hibernate automatically generates a primary key for us. Sometimes, AUTO strategy refers to a SEQUENCE instead of an IDENTITY .</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">46. How will you re-attach an object in Detached state in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can call one of the methods Session.update(), Session.saveOrUpdate(), or Session.merge() to re-attach an object in detached state with another session in Hibernate.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">47. What is the first level of cache in Hibernate?</span></b><b><span style="font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">A Hibernate Session is the first level of cache for persistent data in a transaction. The second level of cache is at JVM or SessionFactory level.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">48. What are the different second level caches available in Hibernate?</span></b><b><span style="font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate, we can use different cache providers for implementing second level cache at JVM/SessionFactory level.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Some of these are:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hashtable</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">EHCache</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">OSCache</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">SwarmCache</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">JBoss Cache 1.x</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">JBoss Cache 2</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">49. Which is the default transaction factory in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Hibernate, default transaction factory is JDBCTransactionFactory. But we can change it by setting the property hibernate.transaction.factory_class.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">50. What are the options to disable second level cache in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">This is a trick question. By default Second level cache is already disabled in Hibernate.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In case, your project is using a second level cache you can use one of the following options to disable second level cache in Hibernate:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can set hibernate.cache.use_second_level_cache to false. We can use CacheMode.IGNORE to stop interaction between the session and second-level cache. Session will interact with cache only to invalidate cache items when updates occur</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">51.What are the different fetching strategies in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate 3 onwards there are following fetching strategies to retrieve associated objects:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Join fetching</span></b><span style="font-family: "calibri";">: In Join strategy Hibernate uses OUTER join to retrieve the associated instance or collection in the same SELECT.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Select fetching</span></b><span style="font-family: "calibri";">: In Select strategy, Hibernate uses a second SELECT to retrieve the associated entity or collection. We can explicitly disable lazy fetching by specifying lazy="false". By default lazy fetching is true.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Subselect fetching</span></b><span style="font-family: "calibri";">: In Subselect strategy, Hibernate uses a second SELECT to retrieve the associated collections for all entities retrieved in a previous query or fetch.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Batch fetching</span></b><span style="font-family: "calibri";">: In Batch strategy, Hibernate uses a single SELECT to retrieve a batch of entity instances or collections by specifying a list of primary or foreign keys. This is a very good performance optimization strategy for select fetching.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">52. What is the difference between Immediate fetching and Lazy collection fetching?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Immediate fetching an association, collection or attribute is retrieved at the same time when the owner is loaded. But in Lazy collection fetching, a collection is fetched only when an operation is invoked on that collection by client application. This is the default fetching strategy for collections in Hibernate. Lazy fetching is better from performance perspective.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">53. What is ‘Extra lazy fetching’in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">In Extra lazy fetching, only individual elements of a collection are fetched from the database when they are required. In this strategy, Hibernate does not fetch the whole collection into memory unless it is essential. It is a good fetching strategy for large collections of objects.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">54. How can we check is a collection is initialized or not under Lazy Initialization strategy?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate provides two convenient methods, Hibernate.initialize() and Hibernate.isInitialized() to check whether a collection is initialized or not. By using Hibernate.initialize() we can force the initialization of a collection in Hibernate.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">55. What are the different strategies for cache mapping in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Hibernate provides following strategies for cache mapping:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Read only</span></b><span style="font-family: "calibri";">: If an application requires caching only for read but not for write operations, then we can use this strategy. It is very simple to use and give very good performance benefit. It is also safe to use in a cluster environment.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Read/Write</span></b><span style="font-family: "calibri";">: If an application also needs caching for write operations, then we use Read/Write strategy. Read/write cache strategy should not be used if there is requirement for serializable transaction isolation level. If we want to use it in a cluster environment, we need to implement locking mechanism.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Nonstrict Read/Write</span></b><span style="font-family: "calibri";">: If an application only occasionally updates the data, then we can use this strategy. It cannot be used in systems with serializable transaction isolation level requirement.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Transactional</span></b><span style="font-family: "calibri";">: This strategy supports full transactional cache providers like JBoss TreeCache.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">56. What is the difference between a Set and a Bag in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">A Bag in Hibernate is an unordered collection. It can have duplicate elements. When we persist an object in a bag, there is no guarantee that bag will maintain any order.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">A Set in Hibernate can only store unique objects. If we add the same element to set second time, it just replaces the old one. By default a Set is unordered collection in Hibernate.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">57. How can we monitor the performance of Hibernate in an application?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can use following ways to monitor Hibernate performance:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Monitoring SessionFactory</span></b><span style="font-family: "calibri";">: Since there is one SessionFactory in an application, we can collect the statistics of a SessionFactory to monitor the performance. Hibernate provides sessionFactory.getStatistics() method to get the statistics of SessionFactory. Hibernate can also use JMX to publish metrics.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Metrics</span></b><span style="font-family: "calibri";">: In Hibernate we can also collect other metrics likenumber of open sessions, retrieved JDBC connections, cache hit, miss etc.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">These metrics give great insight into the performance of Hibernate. We can tune Hibernate settings and strategies based on these metrics.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">58. How can we check if an Object is in Persistent, Detached or Transient state in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">We can use following methods to check the state of an object in Hibernate:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Persistent State</span></b><span style="font-family: "calibri";">: If call to EntityManager.contains(object) returns true, the object is in Persistent state.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";">Detached State: If the call to PersistenceUnitUtil.getIdentifier(object) returns identifier property then the object is in detached state.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="font-family: "calibri";">Transient State</span></b><span style="font-family: "calibri";">: If call to PersistenceUnitUtil.getIdentifier(object) returns null then object is in Transient state. We can get access to PersistenceUnitUtil from the EntityManagerFactory in Hibernate.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">59</span></b><b><span style="color: red; font-family: "calibri";">. What is a derived property in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">In Hibernate, a derived property is not mapped to any column of a database table. A derived property is computed at runtime by evaluation of an expression. These are read only properties.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">Egg. In this example profitMargin is derived from salePrice and buyPrice.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"><property name="profitMargin" formula="( SELECT (i.salePrice – i.buyPrice) FROM item i WHERE i.Id = Id)"/></span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="font-size: large;"><b><span style="color: red; font-family: "calibri";">6</span></b><b><span style="color: red; font-family: "calibri";">0</span></b><b><span style="color: red; font-family: "calibri";">. How can we use Named Query in Hibernate?</span></b><b><span style="color: red; font-family: "calibri";"><o:p></o:p></span></b></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">A Named SQL query is the HQL query that is associated with a string name and can be referenced in the application by name. It can be used in following ways:</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><b><span style="font-family: "calibri";">XMLMapping File</span></b><span style="font-family: "calibri";">: We can define it in XMLmapping file.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div align="justify" class="MsoNormal" style="text-align: justify; text-justify: inter-ideograph;">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">Egg. <query name="findBookByAuthor”></span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"><![CDATA[from Book s where s.author = :author]]></span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"></query></span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><b><span style="font-family: "calibri";">Annotation</span></b><span style="font-family: "calibri";">: We can also mark Named SQLwith annotation.</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">@NamedQueries({</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">@NamedQuery(</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">name = "findBookByAuthor”,</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">query = "from Book s where s.author = :author”</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">)</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-size: large;"><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";"> </span><span style="font-family: "calibri";">})</span><span style="font-family: "calibri";"><o:p></o:p></span></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-26670364260944009322019-03-04T08:00:00.000+05:302019-03-04T13:20:33.050+05:30Spring Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div align="center" class="MsoNormal" style="text-align: center;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;"><span style="font-size: large;">Spring Interview Questions</span><o:p></o:p></span></b></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">1. What is Spring
framework?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Spring is development framework for
Java programming. It is an open source development framework for Enterprise
Java. The core features of Spring Framework can be used in developing a Java
Enterprise application. It has many extensions and jars for developing web
applications on top of Java EE platform. With Spring we can develop large-scale
complex Java applications very easily. It is also based on good design patterns
like Dependency Injection, Aspect oriented programming for developing
extensible feature rich software.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">2. What are the
benefits of Spring framework in software development?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;">Many benefits of Spring framework are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Lightweight Framework</span></b><span style="line-height: 107%;">: Basic Spring framework is very small in size. It is easy to use and
does not add a lot of overhead on software. It just has 2 MB in basic version.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Container</span></b><span style="line-height: 107%;">:
Spring framework provides the basic container that creates and manages the life
cycle of application objects like Plain old Java objects (POJO). It also stores
the configuration files of application objects to be created.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Dependency Injection (DI)</span></b><span style="line-height: 107%;">: Spring provided loose coupling is application by Dependency
Injection. It uses Inversion of Control technique by which objects specify
their dependencies to Spring container instead of creating new objects
themselves.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Aspect Oriented Programming (AOP)</span></b><span style="line-height: 107%;">: Spring framework promotes and provides support for Aspect
oriented programming in Java. This helps in separating application business
logic from system services that are common across all the business logic. E.g.
Logging can be a cross cutting concern in an Application.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Transaction Management</span></b><span style="line-height: 107%;">: Spring provides a framework for transaction management. So
a developer does not have to implement it from scratch. Spring Transaction
Management is so powerful that we can scale it from one local transaction to
global transactions in a cluster.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">MVC Framework</span></b><span style="line-height: 107%;">:
For Web applications, Spring provides MVC framework. This framework is based on
MVC design pattern and has better features compared to other web frameworks. Exception
Handling: Spring also gives support for a common API to handle exceptions in
various technologies like- Hibernate, JDBC etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Exception Handling</span></b><span style="line-height: 107%;">: Spring also gives support for a common API to handle exceptions in
various technologies like- Hibernate, JDBC etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">3. What are the modules
in Core Container of Spring framework?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;">Spring framework has a Core Container. Modules in Core
Container are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 107%;">Core module<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 107%;">Bean module<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 107%;">Context module<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="line-height: 107%;">Spring Expression Language module<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">4. What are the modules
in Data Access/Integration layer of Spring framework?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;">Modules in Data Access/Integration Layer of Spring framework
are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">JDBC module</span></b><span style="line-height: 107%;">:
An abstraction layer to remove tedious JDBC coding. ORM module Integration
layers for Object Relational Mapping<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">OXM module</span></b><span style="line-height: 107%;">: An
abstraction layer to support Object XML mapping.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Java Messaging Service (JMS) module</span></b><span style="line-height: 107%;">: Module for producing and consuming messages. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Transactions module</span></b><span style="line-height: 107%;">: Transaction Management for POJO classes<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">5. What are the modules
in Web layer of Spring framework?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;">Modules in Web Layer of Spring framework are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Web module</span></b><span style="line-height: 107%;">:
This provides basic web-oriented integration features.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Servlet module</span></b><span style="line-height: 107%;">:
Support for Servlet Listeners.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">WebSocket module</span></b><span style="line-height: 107%;">: Support for Web Socket style messaging.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">Portlet module</span></b><span style="line-height: 107%;">:
MVC implementation for Portlet environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b style="mso-bidi-font-weight: normal;"><span style="line-height: 107%;">5.What is the main use
of Core Container module in Spring framework?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">As the name suggests, Spring Core
Container is the core of Spring framework. It gives the basic functionality of
the Spring. All the parts of Spring Framework are built on top of Core
Container. Its main use is to provide Dependency Injection (DI) and Inversion of
control (IOC) features.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>6. What kind of testing can be done in Spring Test Module?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Spring Test Module provides support
for Unit testing as well as Integration testing of Spring components. It allows
using JUnit or TestNG testing frameworks. It also gives ability to mock objects
to use the test code.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>7. What is the use of BeanFactory in Spring framework?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">BeanFactory is the main class that
helps in implementing Inversion of Control pattern in Spring. It is based on
the factory design pattern. It separates the configuration and dependencies of
an application from the rest of application code. Implementations of
BeanFactory like XmlBeanFactory class are used by applications built with
Spring.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>8. Which is the most popular implementation of BeanFactory in
Spring?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">XMLBeanFactory is the most popular
implementation of BeanFactory in Spring.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>9. What is XMLBeanFactory in Spring framework?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">XMLBeanFactory is one of the most
useful implementation of BeanFactory in Spring. This factory loads its beans
based on the definitions mentioned in an XML file. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Spring container reads bean
configuration metadata from an XML file and creates a fully configured
application with the help of XMLBeanFactory class.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>10. What are the uses of AOP module in Spring framework?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">AOP module is also known as Aspect
Oriented Programming module. Its uses are:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Development of aspects in a Spring
based application Provides inter-operability between Spring and other AOP frameworks
Supports metadata programming to Spring<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>11. What are the benefits of JDBC abstraction layer module in
Spring framework?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Spring provides JDBC abstraction
layer module. Main benefits of this module are:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Helps in keeping the database code
clean and simple. Prevents problems that result from a failure to close
database resources. Provides a layer of useful exceptions on top of the error
messages given by different database servers. Based on Spring’s AOP module <span style="mso-spacerun: yes;"> </span>Provides transaction management services for
objects in a Spring Application<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>12. How does Spring support Object Relational Mapping (ORM) integration?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Spring supports Object Relational
Mapping (ORM) by providing ORM Module. This module helps in integrating with
popular ORM framework like Hibernate, JDO, and iBATIS SQL Maps etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Transaction Management module of
Spring framework supports all of these ORM frameworks as well as JDBC.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="line-height: 107%;"><b>13. How does Web module work in Spring framework?</b><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">Spring provides support for developing
web application by using Web module. This module is built on application
context module that provides context for web-based applications.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">This module also supports
web-oriented integration features like-transparently handling multi-part requests
for uploading files, pro-grammatically binding request parameters to business
objects etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;">This module also supports integration
with popular web frameworks like Jakarta Struts, JSF, and Tapestry etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;"><br /></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;"><br /></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;"><br /></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="line-height: 107%;"><br /></span></div>
<br /></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-68593803534192996312019-02-28T21:00:00.000+05:302019-02-28T21:00:09.724+05:30JSP Interview Questions part - 2<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;">JSP Interview Questions</span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span>
<br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">1. How will you
implement error page in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">To implement an error-handling page
in JSP, we first create a JSP with error page handling information. In most of
the cases we gracefully handle error by giving a user-friendly message like
“Sorry! There is system error. Please try again by refreshing page.”<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">In this error page, we show
user-friendly message to user, but we also log important information like stack
trace to our application log file. We have to add parameter isErrorPage=true in
page directive of this page. This tells to JSP container that this is our error
page.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;"><%@page isErrorPage=”true” %><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Now we can use this error page in
other JSP where we want to handle error. In case of an error or exception,
these JSP will direct it to errorPage.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;"> <% page errorPage=”ErrorPage.jsp”
%><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">2. How will you send
XML data from a JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">In general, JSP is used to pass HTML
data to web browser. If we want to send data in XML format, we can easily do it
by setting contentType=”text/xml” in page directive.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <%@page
contentType=”text/xml” %><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">3. What happens when we
request for a JSP page from web browser?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">When a user calls JSP page from web
browser, the request first comes to web server. Web server checks for .jsp extension
of page and passes the request to JSP container like Tomcat. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">The JSP container checks whether it
has precompiled JSP class or not. If this is the first time this JSP is called,
then JSP container will translate JSP into a servlet and compiles it. After
compiling, JSP code if loaded in memory and JSP container will call jspInit()
method and _jspService() methods. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">The _jspService() method will create
the output that will be sent by JSP container to client browser.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">4. How will you
implement Auto Refresh of page in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use setIntHeader() method to
set the refresh frequency with which we want to auto-refresh a JSP page. We can
send key “Refresh” with the time in seconds for auto refresh of the JSP page.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g.
response.setIntHeader(“Refresh”,10)<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">5.What are the
important status codes in HTTP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Every HTTP request comes back with a
status code from the server. The important status codes in HTTP are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">1. <b>200</b>: It means the request is successful.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">2. <b>400</b>: It means the request was bad.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">3. <b>401</b>: It means request was not authorized.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">4. <b>404</b>: It means the resource requested was not found.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">5. <b>503</b>: It means the service is not available.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">6. What is the meaning
of Accept attribute in HTTP header?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">In HTTP header, Accept attribute is
used to specify the MIME types that a HTTP client or browser can handle. MIME
type is the identifier for specifying the type of file/data that we are
planning to pass over the internet.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">7. What is the
difference between Expression and Scriptlet in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We use Expression in a JSP to return
a value and display it at a specific location. It is generally used for
dynamically print information like- time, counter etc in a HTML code. Scriptlet
is for writing Java code in a JSP. We can define variable, methods etc in a
Scriptlet. A Scriptlet can handle much more complex code and can be also
reused.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">8. How will you delete
a Cookie in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use following options to
delete a Cookie in JSP:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">setMaxAge()</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
we can set the maximum age of a cookie. After this time period, Cookie will
expire and will be deleted.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Header</span></b><span style="font-size: 14.0pt; line-height: 107%;">: We can
also set the expiry time in header of response. Respone.setHeader(). This will
also expire the cookie after specified time period.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">9. How will you use a
Cookie in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use a Cookie in JSP by
performing following steps: First we create a Cookie object. We set the name
and value of the cookie to be created. We set the expiry time of the Cookie by
setting the maximum age. We can use setMaxAge() method for this. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Finally, we can send the cookie in a
HTTP Response by sending it in HTTP header. In this way cookie goes to client
browser and gets stored there till the maximum age is not achieved. Once a
Cookie is set in the client browser, we can call getCookies() method to get the
list of all the cookies set in Client. We iterate through the list of all the
cookies and get the value of the cookie that was set in earlier request. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">In this way we can use Cookie to set
some information at client side and retrieve its value.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">10. What is the main
difference between a Session and Cookie in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">A Session is always stored at the
Server side. In JSP, session is a built-in object in JSP container. A Cookie is
always stored at the client side. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use both the methods for
Session tracking . But Cookie method
needs permission from user for storing cookie
at the client location.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">11. How will you
prevent creation of session in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can simply set the session
attribute as false in page directive to prevent creation of session object.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <% @page session=”false”
%><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">12. What is an output
comment in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can write output in JSP in such a
way that it becomes a comment in HTML code. This comment will not be visible in
the web browser. But when we view page source to see HTML, we can see output
comment.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">An HTML comment is of following
format:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;"><!-- comment --><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">If we output comment in above format,
it will be visible to client.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">13. How will you
prevent caching of HTML output by web browser in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use set the header in response
object for Cache-Control to specify no caching.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Sample code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">response.setHeader(“Cache-Control”,
“no-store”);<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">response.setDateHeader(“Expires”,”0”);<o:p></o:p></span></div>
<br />
<br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">14. How will you redirect request to another page in browser in JSP code?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can use sendRedirect() method in JSP to redirect the request to another location or page. In this case the request will not come back to server. It will redirect in the browser itself.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">Sample code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;"><% response.sendRedirect(URL); %><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">15. What is the difference between sendRedirect and forward in a JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">Both forward and sendRedirect are mechanisms of sending a client to another page. The main difference between these two are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">In forward, the processing takes place at server side. In case of sendRedirect() the processing takes place the client side.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">In forward, the request is transferred to another resource within same server. In case of sendRedirect the request can be transferred to resource on some other server.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">In forward only one request call is consumed. In case of sendRedirect two request response calls are created and consumed.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">The forward is declared in RequestDispatcher interface. Where as sendRedirect is declared in HttpServletResponse object.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">16. What is the use of config implicit object in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">In JSP, config object is of type ServletConfig. This object is created by Servlet Container for each JSP page. It is used for setting initialization parameters for a specific JSP page.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">17. What is the difference between init-param and context-param?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can specify both init-param and context-param in web.xml file. We use init-param to specify the parameters that are specific to a servlet or jsp. This information is confined to the scope of that JSP. We use context-param to specify the parameters for overall application scope. This information does not change easily. It can be used by all the JSP/Servlet in that Container.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">18. What is the purpose of RequestDispatcher?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We use RequestDispatcher interface to forward requests to other resources like HTML, JSP etc. It can also be used to include the content of another page in a JSP. It has two methods: forward and include. We have to first get the RequestDispatcher object from the container and then we can call include or forward method on this object.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">19. How can be read data from a Form in a JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">There is a built-in request object in a JSP that provides methods to read Form data. Some of the methods are as follows::<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">getParameterNames()</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: This method returns the list of all the parameters in the Form.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">getParameter()</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: We call this method to get the value of parameter set in the Form. It returns null if the parameter is not found.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">getParameterValues()</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: If a Parameter is mentioned multiple times in a Form, we use request.getParameterValues() method to get all the values. This method returns an array of String values.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">getParameterMap()</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: This method returns the map of all the Parameters in Form.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">20. What is a filter in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can define filters in JSP to intercept requests from a client or to change response from a server. Filter is a Java class that is defined in the deployment descriptor of web.xml of an application. The JSP container reads filter from web.xml and applies a filter as per the URL pattern associated with the filter. JSP Engine loads all the filters in when we start the server.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">21. How can you upload a large file in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">To upload a file by JSP we can use <input type=”file”> in the Form data being passed from HTML. If the file is very large in size, we can set enctype=multipart/formdata. We have to use POST method in the Form to send a file. Once the request is received, we can implement the logic to read mulitpart data in doPost() method of JSP. There are methods in JSP framework to read large files via this method.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">22. In which scenario, Container initializes multiple JSP/Servlet objects?<o:p></o:p></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"></span></span><br />
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">To initialize multiple JSP objects, we have to specify same Servlet object multiple times in web.xml. This indicates to JSP container to initialize separate JSP/Servlet object for each element. Each of the Servlet instance will have its own ServletConfig object and parameters.</span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_29.html">Multi-threading</a></b> <a href="https://thegeeksarea.blogspot.com/2019/02/java-8-interview-questions.html"><b>Java 8</b></a> </span></div>
</div>
</div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="line-height: 17.12px;"><br /></span></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-8157478322982065472019-02-27T08:00:00.000+05:302019-02-27T08:00:00.153+05:30JSP Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;">JSP Interview Questions</span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<div>
<span style="line-height: 17.12px;"><span style="font-family: , serif;"><b><br /></b></span></span></div>
<div>
<span style="line-height: 17.12px;"><span style="font-family: , serif;"><b><br /></b></span></span>
<br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">1. What are the
implicit objects in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">JSP has following implicit objects:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">1. Request<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">2. Response<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">3. Application<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">4. Exception<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">5. Page<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">6. Config<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">7. Session<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">2. How will you extend
JSP code?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can extend JSP code by using Tag
libraries and Custom actions.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">3. How will you handle
runtime exceptions in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We use Errorpage attribute in JSP to
catch runtime exceptions. This attribute forwards user request to the error
page automatically.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">4. How will you prevent
multiple submits of a page that come by clicking refresh button multiple times?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use Post Redirect Get (PRG)
pattern to solve the issue of multiple submission of same data. It works as
follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">First time when a user submits a form
to server by POST or GET method, then we update the state in application
database. Then we send a redirect response to send reply to client. Then we
load a view by using GET command. There is no data is sent in this. Since this
a new JSP page, it is safe from multiple submits. The code that processes the
request is idempotent. So it does not do same action twice for same request.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">5. How will you
implement a thread safe JSP page?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use SingleThreadModel
Interface to implement a thread safe JSP page. We can also add <%@page
isThreadSafe=”false” %> directive in JSP page to make it thread safe.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">6. How will you include
a static file in a JSP page?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can use include directive of JSP
to include a Static page in JSP. In this approach, we use translation phase to
include a static page. We have to specify the URL of the resource to be
included as file attribute in this directive.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <%@ include file="footer.html" %><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">7. What are the
lifecycle methods of a JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">A JSP has following lifecycle
methods:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">1. <b>jspInit():</b> This method is invoked when the JSP is called for the
first time. We can do initial setup for servicing a request in this method.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">2. <b>_jspService():</b> This method is used to serve every request of the
JSP.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">3. <b>jspDestroy():</b> Once we remove a JSP from the container, we call this
method. It is used for cleanup of resources like Database connections etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">8. What are the
advantages of using JSP in web architecture?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We get following advantages by using
JSP in web architecture:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">1. <b>Performance</b>: JSP provides very good performance due to their design
of using same code to service multiple requests.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">2. <b>Fast</b>: Since JSP is pre-compiled, server can serve the pages very
fast.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">3. <b>Extendable</b>: JSP is based on Java Servlets. This helps in extending
JSP architecture with other Java technologies like JDBC, JMS, JNDI etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">4. <b>Design</b>: It is easier to design user interface with JSP, since it is
very close to HTML. UI designers can create a JSP with mock data and developers
can later provide implementation of dynamic data.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">9. What is the advantage of JSP over Javascript?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> In
JSP we can write Java code seamlessly. It allows for writing code that can
interact with the rest of the application. Javascript code is mostly executed
at client side. This limits the tasks that can be done in Javascript code. We
cannot connect to database server from Javascript at the client side.<o:p></o:p></span></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">10. What is the Lifecycle of JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> JSP
has following lifecycle stages:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Compilation</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
When a request is made for a JSP, the corresponding JSP is converted into
Servlet and compiled. If there is already a compiled form of JSP and there is
not change in JSP page since last compilation, this stage does not do anything.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Initialization</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
In this stage, jspInit() method is called to initialize any data or code that
will be later used multiple times in _jspService() method.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Service</span></b><span style="font-size: 14.0pt; line-height: 107%;">: In
this stage, with each request to JSP, _jspService() method is called to service
the request. This is the core logic of JSP that generates response for request.
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Destroy</span></b><span style="font-size: 14.0pt; line-height: 107%;">: In
this stage, JSP is removed from the container/server. Just before removal, this
stage performs the cleanup of any resources held by JSP.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">11.What is a JSP expression?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> A
JSP expression is an element of a JSP page that is used to evaluate a Java
expression and convert into a String. This String is replaced into the
locations wherever the expression occurs in JSP page.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="tab-stops: 62.65pt; text-align: center;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <%= expression =%><o:p></o:p></span></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">12. What are the different types of directive tags in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> JSP
has following directive tags:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">1. <b>Page</b>: This directive is used for page related attributes. It can be
put anywhere in the JSP page. But by convention we put it on the top of the
page.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: center;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <%@
page attribute="value" %><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">2. <b>Taglib</b>: We can create custom tags in JSP and use these by taglib
directive in a JSP page.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: center;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <%@
taglib uri=“abc.html” prefix=“tag_prefix” ><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">3. <b>Include</b>: We use include directive to read a file and merge its
content with the JSP page. This is done during compilation stage.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: center;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. <%@
include file="relative url" ><o:p></o:p></span></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">13. What is session attribute in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> Session
attribute in JSP is used for HTTP session mechanism. If we do not want to use
HTTP session in JSP, then we set this attribute to false. If it is set to true,
we can use built in session object in JSP.<o:p></o:p></span></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">14. What are the different scopes of a JSP object?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> A
JSP object, implicit or explicit, can have one of the following scopes:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Page</span></b><span style="font-size: 14.0pt; line-height: 107%;">: In this
scope, the object is accessible from the page where it was created. Important
point here is that when a user refreshes the page, the objects of this scope
also get created again.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Request</span></b><span style="font-size: 14.0pt; line-height: 107%;">: In
request scope, the object is accessible to the HTTP request that created this
object.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Session</span></b><span style="font-size: 14.0pt; line-height: 107%;">: In
this scope, the object is available throughout the same HTTP session.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; tab-stops: 62.65pt; text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">Application</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
This is the widest scope. The object is available throughout the application in
which JSP was created.<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<b><span style="font-size: 14.0pt; line-height: 107%;">15. What is pageContext in JSP?<o:p></o:p></span></b></div>
<br />
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">In JSP, pageContext is an implicit object . This is used for
storing and accessing all the page scope objects of JSP. It is an instance of
the PageContext class from javax.servlet.jsp package.<o:p></o:p></span></div>
<div class="MsoNormal" style="tab-stops: 62.65pt; text-align: justify;">
<br /></div>
</div>
<div>
<ol style="text-align: left;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">
</span></ol>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">
</span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">16. What is the use of jsp:useBean in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We use jsp:useBean to invoke the methods of a Java Bean class. The Java Bean class has some data and setter/getters to access the data. With this tag, container will try to locate the bean. If bean is not already loaded then it will create an instance of a bean and load it. Later this bean can be used in expressions or JSP code.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">17. What is difference between include Directive and include Action of JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">Some of the main differences between include Directive and include Action are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">1. Include directive is called at translation phase to include content in JSP. Include Action is executed during runtime of JSP.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">2. It is not possible to pass parameters to include directive. Include action can accept parameters by jsp:param tag.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">3. Include directive is just copying of content from another file to JSP code and then it goes through compilation. Include action will dynamically process the resource being called and then include it in the JSP page.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">18. How will you use other Java files of your application in JSP code?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can use import tag to import a Java file in JSP code. Once a file is imported, it can be used by JSP code. It is a very convenient method to use Java classes in JSP code. For better organization of Java code, we should create a package of classes that we are planning to use in JSP code.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">19. How will you use an existing class and extend it to use in the JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can use extends attribute in include tag to use an existing class and extend it in the current JSP.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="text-align: center;">
<span style="font-size: 14pt; line-height: 19.9733px;">E.g.<%@ include page extends=“parent_class” %><o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="text-align: center;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">20. Why _jspService method starts with _ symbol in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">All the code that we write in a JSP goes into _jspService method during translation phase. We cannot override this method. Where as other lifecycle methods jspInit() and jspDestroy() can be overridden. It appears that container uses _ symbol to distinguish the method that cannot be overridden by client code.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">21. Why do we use tag library in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">At times we want to create a UI framework with custom tags. In such a scenario, taglib is a very good feature of JSP. With taglib we can create tags that can provide custom features. Taglib is also a nice way to communicate with UI designers who can use custom tags in the html without going into the details of how the code is implemented. Another benefit of taglib is reusability of the code. This promotes writing code only once and using is multiple times.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">22. What is the different type of tag library groups in JSTL?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">JSTL stands for JavaServer Pages Standard Tag Library. In JSTL, we have a collection of JSP tags that can be used in different scenarios. There are following main groups of tags in JSTL:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">Core tags<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">SQL tags<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">Formatting tags<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">XML tags<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14pt; line-height: 19.9733px;">JSTL Functions<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">23. How will you pass information from one JSP to another JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can pass information from one JSP to another by using implicit objects. If different JSP are called in same session, we can use session object to pass information from one JSP to another. If we want to pass information from one JSP to another JSP included in the main JSP, then we can use jsp:param to pass this information.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">24. How will you call a stored procedure from JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">JSP allows running Java code from a .jsp file. We can call a stored procedure by using JDBC code. We can call a CallableStatement from JSP code to invoke a stored procedure. If we are using Spring framework, then we can use JdbcTemplate class to invoke stored procedure from a JSP.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">25. Can we override _jspService() method in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">No, JSP specification does not allow overriding of _jspService method in JSP. We can override other methods like jspInit() and jspDestroy().<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">26. What is a directive in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">JSP directive is a mechanism to pass message to JSP container. JSP directive does not produce an output to the page. But it communicates with JSP container.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">E.g. <%@include ..%> directive is used for telling JSP container to include the content of another file during translation of JSP. There can be zero or more attributes in a directive to pass additional information to JSP container. Some of the important directives in JSP are: page, include and taglib.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">27. How will you implement Session tracking in JSP?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">We can use different mechanisms to implement Session tracking JSP. Some these mechanisms are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">Cookies</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: We can use cookie to set session information and pass it to web client. In subsequent requests we can use the information in cookie to track session.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">Hidden Form Field</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: We can send session id in a hidden field in HTML form. By using this we can track session.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">Session object</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: We can use the built in session object to track session in JSP.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">URL Rewriting</span></b><span style="font-size: 14pt; line-height: 19.9733px;">: We can also add session id at the end of a URL.<o:p></o:p></span></div>
<div align="center" class="MsoNormal" style="text-align: center;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">Like</span></b><span style="font-size: 14pt; line-height: 19.9733px;">- www.abcserver.com?sessionid=1234<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 19.9733px;">28. How do you debug code in JSP?<o:p></o:p></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 19.9733px;">In simplest form we can write logger statements or System.out.println() statements to write messages to log files. When we call a JSP, the log messages get written to logs. With useful information getting logged we can easily debug the code. Another option in debugging is to link JSP container with an IDE. Once we link IDE debugger to JSP Engine, we can use standard operations of debugging like breakpoint, step through etc.</span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_29.html">Multi-threading</a></b> <a href="https://thegeeksarea.blogspot.com/2019/02/java-8-interview-questions.html"><b>Java 8</b></a> </span></div>
</div>
</div>
</div>
</div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-28489502486419507672019-02-26T20:30:00.000+05:302019-05-26T09:02:15.120+05:30Mixed Java Tricky Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h2 style="line-height: normal; text-align: center;">
<b><span style="color: #2e74b5; font-size: 16.0pt; mso-bidi-font-size: 14.0pt; mso-themecolor: accent1; mso-themeshade: 191;">Tricky Mixed Interview Questions</span></b></h2>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">1. What are Wrapper classes in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Java has concept of Wrapper
classes to allow primitive types to be accessed as objects. Primitive types
like boolean, int, double, float etc. have corresponding Wrappers classes –
Boolean, Integer, Double, Float etc. Many of these Wrapper classes are in
java.lang package. Java 5.0 has launched the concept of Autoboxing and Unboxing
in Java for Wrapper classes.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">E.g.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">public class WrapperTest{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">public static void
main(String args[]){<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 180.0pt; text-indent: 36.0pt;">
<span style="color: #8496b0; font-size: 14.0pt; mso-bidi-font-size: 12.0pt; mso-themecolor: text2; mso-themetint: 153;">//Converting int into Integer<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">int count=50;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Integer
i=Integer.valueOf(count);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 180.0pt; text-indent: 36.0pt;">
<span style="color: #8496b0; font-size: 14.0pt; mso-bidi-font-size: 12.0pt; mso-themecolor: text2; mso-themetint: 153;">//converting int into Integer<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Integer j=a;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 216.0pt;">
<span style="color: #8496b0; font-size: 14.0pt; mso-bidi-font-size: 12.0pt; mso-themecolor: text2; mso-themetint: 153;">//autoboxing, now compiler will write<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Integer.valueOf(count)
internally<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">System.out.println(count+"
"+i+" "+j);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">2. What is the purpose of native method in
Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">The native keyword is used
for applying to a method to indicate that the method is implemented in native
code using JNI(Java Native Interface). Therefore, native methods allow Java
Developer to directly access platform specific APIs. Often, native methods are
linked to native library.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">3. What is System class?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">System.class is a final
class provided by java.lang package. It contains several useful class fields
and methods. The purpose of System class is to provide access to system
resources.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">4. What is System, out and println in
System.out.println method call?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">System is a final class
provided by java.lang package. out refers to PrintStream class and a static
member of System class. println is a method of PrintStream class.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">5. What is the other name of Shallow Copy
in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Object Cloning. A Shallow
Copy just copies the values of references in a Class.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">6. What is the difference between Shallow
Copy and Deep Copy in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">A Shallow copy just copies
the values of the references in the class. A Deep copy copies the values of the
objects as well.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">7. What is a Singleton class?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">A Singleton class in Java
has maximum one instance of the class present in JVM, all the time. The constructor
of this class is written in such a way that it never creates more than one
object of same class.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">8. What is the difference between
Singleton class and Static class?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">A static class in Java has
only static methods. It is a container of functions. It is created based on
procedural programming design. Singleton class is a pattern in Object Oriented
Design. A Singleton class has only one instance of an object in JVM. This
pattern is implemented in such a way that there is always only one instance of
that class present in JVM.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-60302957129709816732019-02-22T12:00:00.000+05:302019-05-26T09:09:44.752+05:30Java Tricky Interview Questions Part - 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h2 style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-align: center;">
<b><span style="color: #2e74b5; font-size: 16.0pt; mso-themecolor: accent1; mso-themeshade: 191;">Java Tricky Questions</span></b></h2>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">1. How can we print an Array in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">We can print an
array by using methods of Arrays class. We can either use Arrays.toString()
method or we can use Arrays.deepToString() method. Since array doesn't
implement toString() method by itself, just passing an array to
System.out.println() will not print its contents. But we can use
Arrays.toString() to print each element of an array.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">2. Is it ok to use random numbers in the
implementation of hashcode() method in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">No. The hashcode
of an object should be always same. If you use random number in hashcode()
method, then you may get a different value of hashcode for same object. This
will break the hashcode contract.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">3. Between two types of dependency
injections, constructor injection and setter dependency injection, which one is
better?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Constructor
injection guarantees that a class will be initialized with all its dependencies
during creation. But setter injection provides flexibility to set an optional
dependency. If we are using an XML file to describe dependencies, the setter
injection is more readable. In general, it is a good practice to use
constructor injection for mandatory dependencies and use setter injection for
optional dependencies.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">4. What is the difference between DOM and
SAX parser in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">In Java,
Document Object Model (DOM) parser loads the whole XML into memory and creates
a tree based on DOM model. This helps it in quickly locating the nodes, and
making a change in the structure of XML. On the other hand, Simple API for XML
(SAX) parser is an event based parser. It doesn't load the whole XML into
memory. Due to this reason DOM is faster than SAX but require more memory and
is not suitable to parse large XML files.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">5. Between Enumeration and Iterator, which
one has better performance in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Enumeration
interface is a read-only interface. It has better performance than Iterator. It
is almost twice as fast as compared to an Iterator. It also uses very less
memory. Also Enumeration does not have remove() method. On the other hand,
Iterator interface is safer than Enumeration, since it can check whether a
collection is modified or not during iteration. If a collection is altered
while an Iterator is iterating, then it throws ConcurrentModificationException.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">6. What is the difference between pass by
reference and pass by value?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Whenever an
object is passed by value, it means that a copy of the object is passed. Even
if changes are made to that object, it doesn’t affect the original value.
Whenever an object is passed by reference, it means that the actual object is
not passed, rather a reference of the object is passed. Therefore, any changes
made by an external method, are also reflected in the actual object and its
reference.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">7. What are the different ways to sort a
collection in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">The most popular
way to sort a collection in Java is by calling Collections.sort() method. You
can provide your custom Comparator to sort() method for sorting the data in
your custom way. The other way is to use a Sorted collection like TreeSet or
TreeMap that stores the information in a sorted order and then you can convert
it to a List.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">8.Why Collection interface doesn’t extend
Cloneable and Serializable interfaces?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Collection
interface just specifies groups of objects known as elements. Each concrete
implementation of a Collection can choose its own way of how to maintain and
order its elements. Some collections may allow duplicate keys, while other
collections may not. A lot of collection implementations have clone method. But
many do not. It is not worthwhile to include it in all, since Collection is an
abstract representation. What matters is the concrete implementation. Cloning
and serialization come into picture while doing concrete implementation.
Therefore, the concrete implementations of collections should decide how they
can be cloned or serialized.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">9. What is the difference between a process
and a thread in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">A process is simply
an execution of a program. A Thread is a single execution sequence within a
process. A process may contain multiple threads. A Thread is also called as a
lightweight process.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">10. What are the benefits of using an
unordered array over an ordered array?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">In an ordered
array the search time has time complexity of O(log n). Whereas, in an unordered
array, search time complexity is O (n). In an ordered array, the insert
operation has a time complexity of O(n). Whereas, the insertion operation for
an unordered array takes constant time of O(1). Therefore, when we have more
writes than reads, it is preferable to use an unordered array.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">11. Between HashSet and TreeSet collections
in Java, which one is better?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">A HashSet is
Implemented using a HashTable. Therefore, its elements are stored in a random
order. The add(), remove(), and contains() methods of a HashSet have constant
time complexity O(1). A TreeSet is implemented using a tree data structure. The
elements in a TreeSet are sorted in a natural order. Therefore, add(),
remove(), and contains() methods have time complexity of O(logn). So from
performance perspective, HashSet has better performance than TreeSet. But if
you want to store elements in a natural sorting order, then TreeSet is a better
collection.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">12. When does JVM call the finalize()
method?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">JVM instructs
the Garbage Collector to call the finalize method, just before releasing an
object from the memory. A programmer can implement finalize() method to
explicitly release the resources held by the object. This will help in better
memory management and avoid any memory leaks.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">13. When would you use Serial Garabage
collector or Throughput Garbage collector in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">The Serial
Garbage collector is used for small applications that require heap memory upto
100 MB. The Throughput Garbage collector is used in medium to large size Java
applications.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">14. In Java, if you set an object reference
to null, will the Garbage Collector immediately free the memory held by that
object?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">No. JVM decides
to run the Garbage Collector whenever it is low on memory. When Garbage
Collector runs, it looks for objects that are available for garbage collection
and then frees the memory associated with this object. So just setting an
Object reference null makes it eligible for Garbage Collection, but it does not
immediately free the memory.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">15. How can you make an Object eligible for
Garbage collection in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">To make an
Object eligible for Garbage collection, just make sure that it is unreachable
to the program in which it is currently defined / created / used. You can set
the object reference to null and make sure no other object refers it. Once the
object cannot be reached, Garbage Collection can clean it during the next run.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">16. When do you use Exception or Error in
Java? What is the difference between these two?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Throwable class
is the superclass of Exception and Error classes in Java. When you want to
catch the exceptional conditions that your program can create or encounter,
then use the Exception class or subclass of Exception. When you come across
situations that are unexpected then use Error class in Java. Also recovering
from Error is not possible in most of cases. So it is better to terminate the
program.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">17. What is the advantage of
PreparedStatement over Statement class in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">PreparedStatements
are precompiled statements for database queries. Due to this their performance
is much better. Also, we can reuse PreparedStatement objects with different
input values to the same query. Where as, Statement class does not provide
these features.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">18. In Java, what is the difference between
throw and throws keywords?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">When we want to
raise an exception in our code, we use the throw keyword with the name of the
exception to be raised. Where as, throws keyword is used in method declaration.
Throws keyword tells us the Exception that can be thrown by this method. Any
caller of this method should be prepared to expect this Exception. Another
minor difference is that throw is used only with one exception, but throws can
be used with comma-separated list of multiple exceptions.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">19. What happens to the Exception object
after the exception handling is done?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Once the
exception handling is complete, the Exception object is not reachable. Then it
is garbage collected in the next run of Garbage Collector.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">20. How do you find which client machine is
sending request to your servlet in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">We can use the
ServletRequest class to find the IP address or host name of the client machine.
There are methods getRemoteAddr() to get the IP address of the client machine
and getRemoteHost() to get the host name of the client machine.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">21. What is the difference between a Cookie
and a Session object in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Both Cookie and
Session are used during communication between Client and Server. The Client can
disable a Cookie. Due to which the Web server cannot send a cookie. But a
client cannot disable a session. So a Session always works irrespective of any
setting at the client side. Also a Session can store any Java object. But the
Cookie can only store small information in a String object.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">22. Which protocol does Browser and Servlet
use to communicate with each other?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">HTTP protocol.
The Browser and Servlet communicate with each other by using the HTTP protocol.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">23. What is HTTP Tunneling?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">There are many
network communication protocols on the Internet. But HTTP is the most popular
among them. HTTP Tunneling is a technique in which HTTP or HTTPS protocol
encapsulated the communication done by any other type of protocol. The masking
of other protocol requests as HTTP requests is known as HTTP Tunneling.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">24. Why do we use JSP instead of Servlet in
Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Since JSP pages
are dynamically compiled into servlets, the programmers can easily make updates
to the presentation layer code. For better performance, JSP pages can be
pre-compiled. Also JSP pages provide flexibility to combine static templates
like HTML or XML snippets. In addition, programmers can make logic changes at
the class level, without editing the JSP pages that use the class logic.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">25. Is empty ‘.java’ file name a valid
source file name in Java? </span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Yes. You can
create a class and store it in a file with name .java. You can try it yourself,
by creating, compiling and running such a file. It will run correctly.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">26. How do you implement Servlet Chaining in
Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">To implement,
Servlet Chaining, there has to be more than one servlet. The output of one
servlet has to be sent to a second servlet. The output of the second servlet
can be sent to a third servlet, and so on. In this way, a chain of servlets is
formed to complete a task. The last servlet in the chain will be responsible
for sending final response to client.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">27.Can you instantiate this class?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<span style="color: black; font-size: 14.0pt;">public class A<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<span style="color: black; font-size: 14.0pt;">{<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">A a = new A();<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<span style="color: black; font-size: 14.0pt;">} <o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">No, this class
cannot be instantiated, since it will result in recursively calling its
constructor.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">28. Why Java does not support operator
overloading?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Java supports
Method overloading but does not support operator overloading. It would make the
design more complex by adding operator loading. Also it will make more complex
compiler. One more reason is that, it will reduce the performance of JVM by
operator overloading, since JCM has to do extra work to find the real meaning
of overloaded operators at run time.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">29. Why String class is Immutable or Final
in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Since String
objects are cached in a String pool, it makes sense to make the String
immutable. The cached String literals are shared between multiple clients. And
there is a possibility that one client's action may affect another client’s
access to String pool. String is also used as a parameter in many Java classes.
Eg. You can pass hostname, port number as String while opening a network
connection. If any one can modify your copy of the String, it can change the
hostname. Due to this reason, it makes sense to make String final as soon as it
is created.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">30. What is the difference between
sendRedirect and forward methods?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">When you use
sendRedirect method, it creates a new request. When you use the forward method,
it just forwards a request to a new target. In case of sendRedirect, the
previous request scope objects are not available, because it creates a new
request. In case of forward method, the previous request scope objects are
available after forwarding. Also the sendRedirect method is considered slower
than the forward method.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">31. How do you fix your Serializable class,
if it contains a member that is not serializable?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">If you want to
make a class Serializable, but find that this class contains members that are
not Serializable, then you have to mark those members as transient. This will
ensure that this member is not persisted to a stream of bytes during
Serialization. Therefore, Transient keyword of Java comes to help in this
scenario.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">32. What is the use of run time polymorphism
in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">During the run
time the behavior of an Object can change based on its run time state. Due to
this run time polymorphism is introduced in Java. If you override a method in a
child class, then you are providing run time polymorphism. Nothing will happen
at the compile time. But at the run time, JVM decides which method will be
called based on the class of the Object.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">33. What are the rules of method overloading
and method overriding in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">When we want to
overload a method, we need to make sure that the method name remains same. But
method signature can vary in the number or datatype of arguments or in the
order of arguments. When we want to override a method, we ensure that the
method is not throwing checked exceptions that are new or higher than those
declared by the overridden method. Also we make sure that the method name,
arguments and return type remain the same. Also we cannot override Static and
Final methods in Java. <o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">34. What is the difference between a class
and an object in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">A Class is a
template or a blue print of an Object to be created. An Object is an instance
of a Class. A Class defines the methods and member variables. But an Object
populates the values of the member variables. Therefore a class is a blueprint
that you use to create objects. An object is an instance of a class – it is a
concrete 'thing' that you made using a specific class. Most of the OOPS
concepts are valid only when an Object is created.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">35. Can we create an abstract class that
extends another abstract class?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Yes. An abstract
class can extend another abstract class. It does not need to define the methods
of parent abstract class. Only the last non-abstract class has to define the
abstract methods of a parent abstract class.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">36. Why do you use Upcasting or Downcasting
in Java ?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">When we want to
cast a Sub class to Super class, we use Upcasting. It is also known as
widening. Upcasting is always allowed in Java. When we want to cast a Super
class to Sub class, we use Downcasting. It is also known as narrowing. At
times, Downcasting can throw the ClassCastException if it fails the type check.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">37. What is the reason to organize classes
and interfaces in a package in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">As the name
suggests, a package contains a collection of classes. It helps in setting the
category of a file. Like- whether it is a Data Access Object (DAO) or an API.
It helps in preventing the collision of Name space. Also we can introduce
access restriction by using package and the right modifiers on a class and its
methods.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">38. What is information hiding in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Information
hiding is OOPS concept. In Java you can use encapsulation to do Information
hiding. An object can use the access modifiers like-public, private, protected
to hide its internal details from another object. This helps in decoupling the
internal logic of an object from outside world. By using Information hiding, an
object can change its internal implementation without impacting the outside
calling client’s code.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">39. Why does Java provide default
constructor?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">In Java all the
interaction takes place between Object instances. To create an Object instance,
JVM needs a constructor. Java does not enforce the rule on a programmer to
define a default constructor for every class. Whenever an object has to be
created and programmer has not provided a constructor, Java uses default
constructor to create the object. Default constructor also initializes member
variables with their default values.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">40. What is the difference between super and
this keywords in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">We use super
keyword to access the methods of the super class from child class. We use this
keyword to access methods of the same class.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">41. What is the advantage of using Unicode
characters in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Unicode
characters have much larger number of characters in the specification. They
also contain Asian and non-western European characters. Most of the modern
technologies, websites and browsers support these Unicode characters.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">42. Can you override an overloaded method in
Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Yes. Java allows
to override an overloaded method, if that method is not a static or final
method.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">43. How can we change the heap size of a
JVM?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Java provides
the command line parameters to set the heap size for JVM. You can specify the
values in –Xms and –Xmx parameters. These parameters stand for initial and
maximum heap size of JVM.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">44. Why should you define a default
constructor in Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">In general, Java
provides a default constructor with each class. But there are certain cases
when we want to define our own version of default constructor. When we want to
construct an object with default values, we create our default constructor. At
times, we can mark the default constructor private. So that any other class
cannot create an instance of our class. This technique is generally used in
Singleton design pattern.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">45. How will you make an Object Immutable in
Java?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">To make an
object immutable follow these two rules. One, do not use any setter methods
that can change the fields of your class. Two, make the fields final. By
following these rules, the member variables cannot be changed after
initialization. This will ensure that member variables of an Object do not
change. And thus the Object will be considered Immutable.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">46. How can you prevent SQL Injection in
Java Code?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">In Java, you can
use PreparedStatement to prevent SQL injection. In a PreparedStatement you can
pass the precompiled SQL queries with pre-defined parameters. This helps in
checking the type of parameters to SQL queries. So it protects your code from
SQL injection attacks.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">47. Which two methods should be always implemented
by HashMap key Object?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">Any object that
we want to use as key for HashMap or in any other hash based collection data
structure e.g. Hashtable, or ConcurrentHashMap must implement equals() and
hashCode() method.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">48.Why an Object used as Key in HashMap
should be Immutable?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">The Key object
should be immutable so that hashCode() method always return the same value for
that object. The Hashcode returned by hashCode() method depends on values of
member variables of an object. If an object is mutable, then the member
variables can change. Once the member variables change, the Hashcode changes.
If the same object returns different hash code at different times, then it is
not reliable to be used in the HashMap. Let say, when you insert the object,
the Hashcode is X, the HashMap will store it in bucket X. But when you search
for it the Hashcode is Y, then HashMap will look for the object in bucket Y. So
you are not getting what you stored. To solve this, a key object should be
immutable. Although, the compiler does not enforce this rule, a good programmer
always remembers this rule.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">49. How can we share an object between
multiple threads?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">There are many
ways to share same object between multiple threads. You can use a BlockingQueue
to pass an object from one thread to another thread. You can also use Exchanger
class for this purpose. An Exchanger is a bidirectional form of a
SynchronousQueue in Java. You can use it to swap the objects as well.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h3 style="margin: 0cm 0cm 8pt; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">50. How can you determine if your program
has a deadlock?</span></b></h3>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="color: black; font-size: 14.0pt;">If we suspect
that our application is stuck due to a Deadlock, then we just take a thread
dump by using the command specific to environment in which your application is
running. Eg. In Linux you can use command kill -3. In case of deadlock, you
will see in thread dump the current status and stack trace of threads in the
JVM, and one or more of them will be stuck with message deadlock. Also you can
do this programmatically by using the ThreadMXBean class that ships with the
JDK. If you don't need programmatic detection you can do this via JConsole. On
the thread tab there is a "detect deadlock" button.<o:p></o:p></span></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div style="margin-bottom: 8.0pt; margin-left: 0cm; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<h4 style="margin: 0cm 0cm 8pt; text-align: left;">
<span style="font-weight: normal;"><span style="color: black; font-size: 14.0pt;">Java tricky interview questions and
answers part-1 ...... </span><a href="https://thegeeksarea.blogspot.com/2019/02/java-tricky-interview-questions.html"><span style="font-size: 14.0pt;">Click here</span></a></span></h4>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-75192273467542517632019-02-19T11:30:00.000+05:302019-07-04T13:53:49.141+05:30Java Tricky Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;">Java Tricky Questions</span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; line-height: 17.12px;"><span style="font-size: large;"><br /></span></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div class="MsoNormal" style="text-align: justify;">
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">1. Is there any difference between a
= a + b and a += b expressions?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">When we add two integral variables e.g. variables of type
byte, short, or int in Java, then they are first promoted to int type, and then
addition happens. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">The += operator implicitly casts the result of addition into
the type of variable used to hold the result. What happens when you put return
statement or System.exit () on try or catch block? Will finally block execute? <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">It is a popular tricky Java interview question. Most of the
programmers think that no matter what the finally block will always execute.
This question challenges that concept by putting a return statement in the try or
catch block or calling System.exit() from try or catch block. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">You can answer by saying that finally block executes even if
we put a return statement in the try block or catch block. But finally block
does not execute if you call System.exit() from try or catch block.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">2. What does the expression 1.0 / 0.0
return? Will there be any compilation error?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Double class is the source of many tricky interview
questions. You may know about the double primitive type and Double class. But
while doing floating point arithmetic some people don't pay enough attention to
Double.INFINITY, NaN, and -0.0. There are rules that govern the floating point
arithmetic calculations involving Double. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">The answer to this question is that 1.0 / 0.0 will compile
successfully. And it will not throw ArithmeticException. It will just return
Double.INFINITY.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">3. Can we use multiple main methods
in multiple classes?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Yes. When we start an application in Java, we just mention
the class name to be run to java command. The JVM looks for the main method
only in the class whose name is passed to java command. Therefore, there is no
conflict amongst the multiple classes having main method.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">4. Does Java allow you to override a
private or static method?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">The question is tricky but the answer is very simple. You
cannot override a private or static method in Java. If we create a similar
method with same return type and same method arguments in child class, then it
will hide the superclass method. This is known as method hiding. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Also, you cannot override a private method in sub class
because Private method is not visible even in a subclass. Therefore, what you
can do is to create another private method with the same name in the child
class. So in both the cases, it is not method overriding. It is either method
hiding or a new method.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">5. How can we find the memory usage
of JVM from Java code?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">We can use memory management related methods provided in
java.lang.Runtime class to get the free memory, total memory and maximum heap
memory in Java. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">By using these methods, you can find out how much of the heap
is used and how much heap space still remains. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Runtime.freeMemory() returns amount of free memory in bytes.
Runtime.totalMemory() returns total memory in bytes. Runtime.maxMemory()
returns maximum memory in bytes.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">6. What is the difference between x
== y and x.equals(y) expressions in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">The x == y expression does object reference matching if both
a and b are an object and only returns true if both are pointing to the same
object in the heap space. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">The x.equals(y) expression is used for logical mapping and it
is expected from an object to override this method to provide logical equality.
<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Eg. A Book object may be logically equal to another copy of
same Book, but it is a different object which will be false while doing x== y.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">7. How can you guarantee that the
garbage collection takes place?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">No. We cannot guarantee the garbage collection in Java. Java
documentation explicitly says that GarbageCollection is not guaranteed. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">You can call System.gc() to request garbage collection,
however, that's what it is - a request. It is upto GC's discretion to run.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">8. What is the relation between
x.hashCode() method and x.equals(y) method of Object class?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">x.hashCode() method returns an int hash value corresponding
to an object instance. It is used in hashCode based collection classes like
Hashtable, HashMap, LinkedHashMap etc. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">hashCode() method is also related to equals() method. As per
Java specification, two objects which are equal to each other using equals()
method must have same hash code. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Therefore, two objects with same hashCode may or may not be
equal to each other. But two equal objects should have same hash code.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">9. What is a compile time constant in
Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">A compile time constant is public static final variable. The
public modifier is optional here. At compile time, they are replaced with
actual values because compiler knows their value up-front and it also knows
that it cannot be changed during run-time. So they are constants.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">10. Explain the difference between
fail-fast and fail-safe iterators?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">The main difference between fail-fast and fail-safe iterators
is whether or not the collection can be modified while it is being iterated. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Fail-safe iterators allow modification of collection in an
iteration task. But fail-fast iterators do not allow any modification to
collection during iteration. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">During iteration, fail-fast iterators fail as soon as they
realize that the collection has been modified. Modification can be addition,
removal or update of a member. And it will throw a
ConcurrentModificationException. Eg. ArrayList, HashSet, and HashMap are
fail-fast. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Fail-safe iterators operate on a copy of the collection.
Therefore they do not throw an exception if the collection is modified during
iteration. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Eg. ConcurrentHashMap, CopyOnWriteArrayList are fail-safe.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;"><br /></span></div>
<div class="MsoNormal">
<b><span style="font-size: 15.0pt; line-height: 107%;">11. You have a character array and a
String. Which one is more secure to store sensitive data (like password, date
of birth, etc.)?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Short answer is, it is safe to store sensitive information in
character array.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">In Java, String is immutable and it is stored in the String
pool. Once a String is created, it stays in the pool in memory until it is
garbage collected. You have no control on garbage collection. Therefore, anyone
having access to a memory dump can potentially extract the sensitive data and
use it.<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15.0pt; line-height: 107%;">Whereas, if you use a mutable object like a character array,
to store the value, you can set it to blank once you are done with it. Once it
is made blank it cannot be used by anyone else.<o:p></o:p></span></div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">12. Is ++ operation thread-safe in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">No, ++ operator is not a thread safe operation. It involves multiple instructions like- reading a value, incrementing it and storing it back into memory.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">These instructions can overlap between multiple threads. So it can cause issues in multi-threading.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;"><br /></span></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">13. How can you access a nonstatic variable from the static context?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">We cannot access a non-static variable from the static context in Java. If you write a code like that, then you will get compile time error. It is one of the most common problems for beginner Java programmers, when they try to access instance variable inside the main method in a class.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Since main method is static in Java, and instance variables are nonstatic, we cannot access instance variable inside main. The solution is to create an instance of the object and then access the instance variables.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">14. Let say there is a method that throws NullPointerException in the superclass. Can we override it with a method that throws RuntimeException?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">This question is checking your understanding of the concepts of method overloading and overriding in Java. We can throw superclass of RuntimeException in an overridden method, but we cannot do the same if it is a checked Exception.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">15. How can you mark an array volatile in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">If you know multi-threading well then you can easily answer it. We can mark an array volatile in Java. But it makes only the reference to array volatile, not the whole array. If one thread changes the reference variable to point to another array, then it will provide a volatile guarantee.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">But if multiple threads are changing individual array elements, they won't be having same reference due to the reference itself being volatile.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">16. What is a thread local variable in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Thread-local variable is a variable restricted to a specific thread. It is like thread's own copy of variable that is not shared among multiple threads. Java provides ThreadLocal class to support thread-local variables. To achieve thread-safety, you can use it. To avoid any memory leak, it is always good to remove a thread-local variable, once its work is done.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">17. What is the difference between sleep() and wait() methods in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal">
<span style="font-size: 14pt; line-height: 33.0646px;"> In Java, we use these methods to pause currently running thread. There is a simple difference between these. sleep() is actually meant for short pause because it doesn't release lock. wait() is meant for conditional wait and it can release a lock that can be acquired by another thread to change the condition on which it is waiting.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">18</span></b><b><span style="font-size: 14pt; line-height: 33.0646px;">. Why do you use volatile keyword in Java?</span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">The volatile keyword guarantees global ordering on reads and writes to a variable. This implies that every thread accessing a volatile field will read the variable’s current value instead of using a cached value.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">By marking the variable volatile, the value of a variable is never cached thread-locally. All reads and writes will go straight to main memory of Java.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">19. What is the difference between poll() and remove() methods of Queue in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">It is a basic question to know the understanding of Queue data structure. Both poll() and remove() methods remove and return the head of the Queue.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">When Queue is empty, poll() method fails and it returns null, but remove() method fails and throws Exception.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">20. Can you catch an exception thrown by another thread in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Yes, it can be done by using Thread.UncaughtExceptionHandler.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Java Documentation says “When a thread is about to terminate due to an uncaught exception the Java Virtual Machine will query the thread for its ncaughtExceptionHandler using Thread.getUncaughtExceptionHandler() will invoke the handler's uncaughtException method, passing the thread and the exception as arguments.”<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">21. How do you decide which type of Inner Class – Static or Non-Static to use in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">An inner class has full access to the fields and methods of the enclosing class. This is convenient for event handlers, but comes at a cost. Every instance of an inner class retains and requires a reference to its enclosing class.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Due to this cost, there are many situations where static nested classes are preferred over inner classes. When instances of the nested class outlive instances of the enclosing class, the nested class should be static to prevent memory leaks. At times, due to their “hidden” reference to enclosing class, Inner classes are harder to construct via reflection.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">22. What are the different types of Classloaders in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Java Classloader is the part of the Java Runtime Environment (JRE) that loads classes on demand into Java Virtual Machine (JVM). When the JVM is started, three types of class loaders are used:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Bootstrap Classloader</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: It loads core java API file rt.jar classes from folder.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Extension Classloader</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: It loads jar files from lib/ext folder.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">System/Application Classloader</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: It loads jar files from path specified in the CLASSPATH environment variable.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Classes may be loaded from the local file system, a remote file system, or even the web.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">23. What are the situations in which you choose HashSet or TreeSet?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">HashSet is better than TressSet in almost every way. It gives O(1) for add(), remove() and contains() operations. Whereas, TressSet gives O(log(N)) for these operations.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Still, TreeSet is useful when you wish to maintain order over the inserted elements or query for a range of elements within the set. We should use TreeSet when we want to maintain order. Or when there are enough read operations to offset the increased cost of write operations.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">20. What is the use of method references in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Java 8 has introduced Method references. It allows constructors and methods to be used as lambdas. The main uses of Method reference are to improve code organization, clarity and terseness.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">25. Do you think Java Enums are more powerful than integer constants?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Yes. Java Enums provide many features that integer constants cannot. Enums can be considered as final classes with a fixed number of instances. Enums can implement interfaces but cannot extend another class. While implementing the strategy pattern, we can use this feature of Enums. Especially, when the number of strategies is fixed.<o:p></o:p></span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">You can also attach meta-data to enum values in Java. Also enum values are typesafe, where as integer constants are not. You can also define custom behavior in enum values.</span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /><b style="text-align: justify;"><span style="font-size: 14pt; line-height: 33.0646px;">26. What happens when you put a key object in a HashMap that is already present?</span></b></span><br />
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In a HashMap there are buckets in which objects are stored. Key objects with same HashCode go to same bucket.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">If you put the same key again in a HashMap, then it will replace the old mapping because HashMap doesn't allow duplicate keys. The same key will have same HashCode as previous key object. Due to same HashCode, it will be stored at the same position in the bucket.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">27. How can you make sure that N threads can access N resources without deadlock?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">This question checks your knowledge of writing multi-threading code. If you have experience with deadlock and race conditions, you can easily answer this. The answer is that by resource ordering you can prevent deadlock. If in our program we always acquire resources in a particular order and release resources in the reverse order, then we can prevent the deadlock.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">So a thread waiting for same resource can not get into deadlock while the other thread is trying to get it and holding the resource required by first thread. If both of them release the resources in right order, one of them can acquire it to finish the work.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">28.How can you determine if JVM is 32-bit or 64-bit from Java Program?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">We can find JVM bit size 32 bit or 64 bit by running java command from the command prompt. Or we can get it from Java program. Sun has a Java System property to determine the bit size of the JVM: 32 or 64:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify;">
<span style="font-size: 14pt; line-height: 33.0646px;">sun.arch.data.model=32 // 32 bit JVM<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify;">
<span style="font-size: 14pt; line-height: 33.0646px;">sun.arch.data.model=64 // 64 bit JVM<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">We can use System.getProperty("sun.arch.data.model") to determine if it is 32/64 bit from Java program.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">29. What is the right data type to represent Money (like Dollar/Pound) in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">To represent money you need decimal points in the numbers like $1.99. BigDecimal class provides good methods to represent Money. Using BigDecimal, we can do the calculation with decimal points and correct rounding. But using BigDecimal is a little bit high on memory usage. We can also use double with predefined precision. But calculation on double can give erroneous results.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">30. How can you do multiple inheritances in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">This is a question to trick people coming from C++ and Scala background to Java. There are many Object Oriented languages that support multiple inheritances. But Java is not one of them. Answer of this question can be that, Java does support multiple inheritances of by allowing an interface to extend other interfaces. You can implement more than one interface. But you cannot extend multiple classes. So Java doesn't support multiple inheritances of implementation. But in Java 8, the default method breaks the rule of multiple inheritances behavior.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">31. Can you create an Immutable object that contains a mutable object?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In Java, it is possible to create an Immutable object that contains a mutable object. We should not share the reference of the mutable object, since it is inside an immutable object. Instead, we can return a copy of it to other methods.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">32. How can you convert an Array of bytes to String?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">You can convert an Array of bytes to String object by using the String constructor that accepts byte[]. We need to make sure that right character encoding is used. Else we may get different results after conversion.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">33. What is difference between CyclicBarrier and CountDownLatch class?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">CyclicBarrier and CountDownLatch classes were introduced from Java 5. We can reuse CyclicBarrier even if it is broken, but we cannot reuse CountDownLatch in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">34. What is the difference between StringBuffer and StringBuilder?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">StringBuilder was introduced in Java 5. The main difference between both of them is that StringBuffer methods e.g. length(), capacity(), append() are synchronized. But corresponding methods in StringBuilder are not synchronized. Due to this difference, concatenation of String using StringBuilder is faster than StringBuffer. Now it is considered bad practice to use StringBuffer, because, in most of the scenarios, we perform string concatenation in the same thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">35. Which class contains clone method? Cloneable or Object class?</span></b><span style="font-size: 14pt; line-height: 33.0646px;"><o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">It is a very basic trick question. clone() method is defined in Object class. Cloneable is a marker interface that doesn't contain any method.<b><o:p></o:p></b></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">36. How will you take thread dump in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">There are platform specific commands to take thread dump in Java. In Linux/Unix, just use kill -3 PID, where PID is the process id of Java process. It will give the thread dump of Java process. In Windows, press Ctrl + Break. This will instruct JVM to print thread dump in standard out or err. It can also go to console or log file depending upon your application configuration.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">37. Can you cast an int variable into a byte variable? And what happens if the value of int is larger than byte?<o:p></o:p></span></b></div>
<div class="MsoNormal">
</div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">An int is 32 bit in Java. But a byte is just 8 bit in Java. We can cast an int to byte. But we will lose higher 24 bits of int while casting. Because a byte can hold only first 8 bits of int. Remaining 24 bits (32-8 = 24) will be lost.<o:p></o:p></span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span><br />
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">38. In Java, can we store a double value in a long variable without explicit casting?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">No, we cannot store a double value into a long variable without casting it to long. The range of double is more than that of long. So we need to type cast.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">To answer this question, just remember which one is bigger between double and long in Java.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">39. What will this return 5*0.1 == 0.5? true or false?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">The answer is false because floating point numbers can not be represented exactly in Java, so 5*0.1 is not same as 0.5.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">40. Out of an int and Integer, which one takes more memory?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">An Integer object takes more memory than an int in Java. An Integer is an object and it stores meta-data overhead about the object. An int is a primitive type so its takes less memory and there is no metadata overhead.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">41. Can we use String in the switch case statement in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Yes. From Java 7 onwards, String can be used in switch case statement. This gives convenience to programmer. But internally hash code of String is used for the switch statement.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">42. Can we use multiple main methods in same class?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Yes. You can have multiple methods with name main in the same class. But there should be only one main method with the signature public static void main(String[] args). JVM looks for main with this signature only. Other methods with name main in same class are just ignored.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">43. When creating an abstract class, is it a good idea to call abstract methods inside its constructor?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">No, we should avoid calling abstract methods in the constructor of an abstract class. Because, it can restrict how these abstract methods can be implemented by child classes. Many IDE give “Overridable method call in constructor” warning for such implementation. This is a problem of object initialization order.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">The superclass constructor will run before the child class constructor. It means child class is not yet initialized. But due to presence of overridden method in superclass, the overridden method of subclass is called when the subclass is not fully initialized.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">44. How can you do constructor chaining in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">When we call one constructor from another constructor of the same class, then it is known as constructor chaining in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;"> When you have multiple overloaded constructors in a class, you can do constructor chaining.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">45. Why do we use static initializers in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In Java, a static initializer can run code during the initial loading of a class and it guarantees that this code will only run once. Also the static code will finish running before a class can be accessed in any way.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Initializing static members from constructors is more work. You have to make sure that every constructor does this. You need to maintain a flag to mark the static work when it is done. You may have to think about synchronization or races conditions for work in static block not initialized from static context.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">46. Your client is complaining that your code is throwing NoClassDefFoundError or NoSuchMethodError, even though you are able to compile your code without error and method exists in your code. What could be the reason behind this?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Sometimes we upgrade our libraries even with same method name. But we forget to let the client know about the new version. Due this different in version, we get NoClassDefFoundError or NoSuchMethodError at runtime when one library was not compatible with such an upgrade.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Java build tools and IDEs can also produce dependency reports that tell you which libraries depend on that JAR. Mostly, identifying and upgrading the library that depends on the older JAR resolve the issue.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">47. How can you check if a String is a number by using regular expression?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Regex is a powerful tool for matching patterns and searching patterns. A numeric String can only contain digits i.e. 0 to 9. It can also contain + and - sign at start of the String. We can create a regular expression for these two rules. One simple example is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Pattern pattern = Pattern.compile(".*\\D.*");<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">48. What is the difference between the expressions String s = "Temporary" and String s = new String("Temporary ")? Which one is better and more efficient?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In general, String s = " Temporary " is more efficient to use than String s = new String("Temporary ").<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In case of String s = " Temporary ", a String with the value “Temporary” is created in String pool. If another String with the same value is created (e.g., String s2 = " Temporary "), it will reference the same object in the String pool.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">But, when you use String s = new String("Temporary "), Java creates a String with the value “Temporary” in the String pool. Also, that String object is then passed to the constructor of the String Object i.e. new String("Temporary "). And this call creates another String object (not in the String pool) with that value. Therefore, each such call creates an additional String object.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">E.g. String s2 = new String("Temporary ") creates an extra String object, rather than just reusing the same String object from the String pool. So String s = “Temporary” is always an efficient way.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<b><span style="font-size: 14pt; line-height: 33.0646px;">49. In Java, can two equal objects have the different hash code?<o:p></o:p></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">No. It is not possible for two equal objects to have different hashcode. But two objects with same hashcode may or may not be equal.<o:p></o:p></span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br />Read more Java tricky interview questions and answers ......... <a href="https://thegeeksarea.blogspot.com/2019/02/java-tricky-interview-questions-2.html">Click here</a><br /><br /><br /><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_31.html">Multi-threading</a></b> <a href="https://thegeeksarea.blogspot.com/2019/02/java-8-interview-questions.html"><b>Java 8</b></a> </span></div>
</div>
</div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-39946561772363234012019-02-15T07:30:00.000+05:302019-02-21T15:27:27.868+05:30Java 8 interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large; line-height: 17.12px;">Java 8</span></b></div>
<div style="text-align: center;">
<b><span style="font-family: , serif; font-size: large; line-height: 17.12px;"><br /></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span><br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">1. What are the new
features released in Java 8? <o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">The new features released in Java 8
are: <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Lambda Expression <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Stream API <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Date and Time API <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Functional Interface <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Interface Default and Static Methods <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Optional <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">7.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Base64 Encoding and Decoding <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">8.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Nashorn JavaScript Engine <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">9.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Collections API Enhancements <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">10.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14.0pt; line-height: 107%;">Concurrency
Enhancements <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">11.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14.0pt; line-height: 107%;">1Fork/Join
Framework Enhancements <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">12.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14.0pt; line-height: 107%;">Spliterator
<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">13.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14.0pt; line-height: 107%;">Internal
Iteration <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">14.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14.0pt; line-height: 107%;">Type
Annotations and Repeatable Annotations <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">15.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Method
Parameter Reflection <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l3 level1 lfo1; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">16.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">JVM
Parameter Changes <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">2.What are the main
benefits of new features introduced in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">The main benefits of Java 8 features
are: <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Support for functional programming by
Lambda and Streams <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Ease of high volume data processing
by Streams <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Ease of use by getting Parameter
names through Reflection <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Reusable code with enhanced
Collection APIs <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Smart exception handling with
Optional <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Control on JVM with new Parameters <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">7.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Enhanced encryption support with Base
64 <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l2 level1 lfo2; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">8.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Faster execution with Nashorn
JavaScript engine support <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">3. What is a Lambda
expression in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Lambda expression is an anonymous
function. It is like a method that does not need any access modifiers, name or
return value declaration. It accepts a set of input parameters and returns
result. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Lambda expression can be passed as a
parameter in a method. So we can treat code in Lambda expression as data. This
piece of code can be passed to other objects and methods.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">4. What are the three
main parts of a Lambda expression in Java?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Three main parts of a Lambda
expression are: <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l1 level1 lfo3; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Parameter list: A Lambda expression
can have zero or more parameters. Parameter list is optional to Lambda. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l1 level1 lfo3; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Lambda arrow operator: “->” is
known as Lambda arrow operator. It separates the list of parameters and the
body of Lambda. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l1 level1 lfo3; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">Lambda expression body: The piece of
code that we want to execute is written in Lambda expression body. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">E.g. In following example:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">Arrays.asList( "a",
"b", "d" ).forEach( e -> System.out.println( e ) );<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">Parameter list = e<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">Arrow = -> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;">Body = System.out.println( e )<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1.0in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">5. What is the data
type of a Lambda expression?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">A Lambda expression fulfills the
purpose of passing code as data. The data type of a Lambda expression is a
Functional interface. In most of the cases this is java.lang.Runnable
interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">6. What is the meaning
of following lambda expression?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">( e -> System.out.println( e ) );<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">This Lambda expression takes a
parameter e and prints it via System.out.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">7. Why did Oracle
release a new version of Java like Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">The main theme of Java 8 is support
for functional programming. With increase in Database size and growth of
multi-code CPU servers, there is need for Java to support such large-scale
systems. With new features of Java 8, it is possible to create functional
programs to interact efficiently with Big Data systems. Support for Streams is
very helpful in this regard. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Lambda expressions are very useful
for cloud computing where we can pass code as data and run the same code on
multiple servers. Optional is a best practice that is borrowed from Google
Guava library for handling the exceptional cases. This has made programs more
robust with support for edge cases.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">8. What are the
advantages of a lambda expression?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">We can pass a lambda expression as an
object to a method. This reduces the overhead involved in passing an anonymous class.
We can also pass a method as a parameter to another method using lambda
expressions.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">9. What is a Functional
interface in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">A Functional interface in Java is an
interface that has exactly one abstract method.It can have default methods with
implementation. A default method is not abstract. In Java 8, java.lang.Runnable
and java.util.concurrent.Callable are two very popular Functional interfaces.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">10. What is a Single
Abstract Method (SAM) interface in Java 8? <o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">A Functional interface is also known
as Single Abstract Method Interface, since it has exactly one abstract method.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">11. How can we define a
Functional interface in Java 8? <o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">To define a Functional interface in
Java 8, we can create an Interface with exactly one abstract method. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Another way is to mark an Interface
with annotation @FunctionalInterface. Even with the annotation we have to
follow the rule of exactly one abstract method. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">The only exception to this rule is
that if we override java.lang.Object class’s method as an abstract method, then
it does not count as an abstract method.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;"><br /></span></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">12. Why do we need
Functional interface in Java? <o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Functional Interfaces are mainly used
in Lambda expressions, Method reference and constructor references. In
functional programming, code can be treated as data. For this purpose Lambda
expressions are introduced. They can be used to pass a block of code to another
method or object. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Functional Interface serves as a data
type for Lambda expressions. Since a Functional interface contains only one
abstract method, the implementation of that method becomes the code that gets
passed as an argument to another method.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">13. Is it mandatory to
use @FunctionalInterface annotation to define a Functional interface in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">No, it is not mandatory to mark a
Functional interface with @FunctionalInterface annotation. Java does not impose
this rule. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">But, if we mark an interface with
@FunctionalInterface annotation then Java Compiler will give us error in case
we define more than one abstract method inside that interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">14. What are the
differences between Collection and Stream API in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> Main differences between Collection
and Stream API in Java 8 are: <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Version</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
Collection API is in use since Java 1.2. Stream API is recent addition to Java
in version 8. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Usage</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
Collection API is used for storing data in different kinds of data structures.
Stream API is used for computation of data on a large set of Objects. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Finite</span></b><span style="font-size: 14.0pt; line-height: 107%;">: With
Collection API we can store a finite number of elements in a data structure.
With Stream API, we can handle streams of data that can contain infinite number
of elements. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Eager vs. Lazy</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
Collection API constructs objects in an eager manner. Stream API creates
objects in a lazy manner. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l0 level1 lfo4; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Multiple consumption</span></b><span style="font-size: 14.0pt; line-height: 107%;">: Most of the Collection APIs support iteration and consumption of
elements multiple times. With Stream API we can consume or iterate elements
only once. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">15. What are the main
uses of Stream API in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="font-size: 14.0pt; line-height: 107%;"> Main uses of Stream API in Java 8
are: <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l4 level1 lfo5; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">It helps in using data in a
declarative way. We can make use of Database functions like Max, Min etc.,
without running a full iteration. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l4 level1 lfo5; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">It makes good use of multi-core
architectures without worrying about multi-threading code. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l4 level1 lfo5; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">We can create a pipeline of data
operations with Java Stream that can run in a sequence or in parallel. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l4 level1 lfo5; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">It provides support for group by,
order by etc. operations. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l4 level1 lfo5; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">It supports writing for code in
Functional programming style. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l4 level1 lfo5; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%;">It provides parallel processing of
data. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14.0pt; line-height: 107%;">16. What are the
differences between Intermediate and Terminal Operations in Java 8 Streams?</span></b><span style="font-size: 14.0pt; line-height: 107%;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14.0pt; line-height: 107%;">Main differences between Intermediate
and Terminal Stream operations are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l5 level1 lfo6; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Evaluation</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
Intermediate operations are not evaluated until we chain it with a Terminal
Operation of Stream. Terminal Operations can be independently evaluated. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l5 level1 lfo6; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Output</span></b><span style="font-size: 14.0pt; line-height: 107%;">: The
output of Intermediate Operations is another Stream. The output of Terminal
Operations is not a Stream. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l5 level1 lfo6; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Lazy</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
Intermediate Operations are evaluated in lazy manner. Terminal Operations are
evaluated in eager manner. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l5 level1 lfo6; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Chaining</span></b><span style="font-size: 14.0pt; line-height: 107%;">: We
can chain multiple Intermediate Operations in a Stream. Terminal Operations
cannot be chained multiple times. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1.0in; mso-add-space: auto; mso-list: l5 level1 lfo6; text-align: justify; text-indent: -.25in;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%;">Multiple</span></b><span style="font-size: 14.0pt; line-height: 107%;">:
There can be multiple Intermediate operations in a Stream operation. There can
be only one Terminal operation in Stream processing statement.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">
</span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">17.What are the main differences between legacy Date/Time API in Java and Date/Time API of Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Main difference between legacy Date/Time API and Java 8 Date/Time API are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Old API is not Thread safe. Java 8 API is Thread safe.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Old API has many mutable objects. New Java 8 API is based on Immutable objects.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Performance of old API is not good. New Java 8 Date/Time API gives better performance.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Old API is less readable and maintainable. New Java 8 API is very well designed and is more readable.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Old API has month values from 0 to 11. New API has months from 1 to 12.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">18. How can we get duration between two dates or time in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In Java8, we have a new class Duration that provides the utility of computing duration between two dates. We can call the static method Duration.between(date1, date2) to get the time period in hours, mins, days etc. between date1 and date2.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">19. What is the new method family introduced in Java 8 for processing of Arrays on multi core machines?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Java 8 has enhanced the Arrays class with methods that can run efficiently on multi core machines. These methods start with keyword parallel.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">E.g. Arrays.parallelSetAll(), Arrays.parallelSort() etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">This parallel set of methods provides parallel processing of Arrays that can run Java code very fast on a multi core machine.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">20. What are the differences between Predicate, Supplier and Consumer in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">The subtle difference between Predicate, Supplier and Consumer in Java 8 is as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo2; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Predicate is an anonymous function that accepts one argument and returns a result.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo2; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Supplier is an anonymous function that accepts no argument and returns a result.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo2; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Consumer is an anonymous function that accepts one argument and returns no result.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">21. Is it possible to have default method definition in an interface without marking it with default keyword?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">No, we have to always mark a default method in interface with default keyword. If we create a method with implementation in an interface, but do not mark it as default, then we will get compile time error.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">22. How does Java 8 solve Diamond problem of Multiple Inheritance?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In Multiple Inheritance if a class extends more than one classes with two different implementation of same method then it causes Diamond problem. <o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Consider following example to see problem and solution for Diamond problem in Java 8:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface BaseInterface{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">default void display() { //code goes here }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface BaseOne extends BaseInterface { }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface BaseTwo extends BaseInterface { }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public class ChildClass implements BaseOne, BaseTwo { }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In the above code, class ChildClass gives compile time error. Java Compiler cannot decide which display method should it invoke in ChildClass.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">To solve this problem, Java SE 8 has given the following remedy:<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface A{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">default void display() { //code goes here }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface B extends A{ }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface C extends A{ }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public class D implements B,C{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">default void display() {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-indent: 0.5in;">
<span style="font-size: 15pt; line-height: 33.778px;">B.super.display();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface BaseInterface{<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">default void display() { //code goes here }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface BaseOne extends BaseInterface { }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public interface BaseTwo extends BaseInterface { }<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">public class ChildClass implements BaseOne, BaseTwo {<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">default void display(){<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-indent: 0.5in;">
<span style="font-size: 15pt; line-height: 33.778px;">BaseOne.super.display();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">The method invocation at BaseOne.super.display(); solves the Diamond problem as it resolves the confusion for compiler.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">23. Can we create a class that implements two Interfaces with default methods of same name and signature?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">No, it is not allowed to create a class that implements interfaces with same name default methods. It will give us compile time error for duplicate default methods.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">24. How Java 8 supports Multiple Inheritance?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In Multiple Inheritance a class can inherit behavior from more than one parent classes. Prior to Java 8, a class can implement multiple interfaces but extend only one class.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In Java 8, we can have method implementation within an interface. So an interface behaves like an Abstract class. Now if we implement more than one interface with method implementation in a class, it means we are inheriting behavior from multiple abstract classes. That is how we get Multiple Inheritance in Java 8.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">25. In case we create a class that extends a base class and implements an interface. If both base class and interface have a default method with same name and arguments, then which definition will be picked by JVM?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In such a scenario, JVM will pick the definition in base class.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">26. If we create same method and define it in a class , in its parent class and in an interface implemented by the class, then definition will be invoked if we access it using the reference of Interface and the object of class?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In all the cases, method defined in the class will be invoked.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">27.Can we access a static method of an interface by using reference of the interface?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">No, a static method of interface has to be invoked by using the name of the interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">28. How can you get the name of Parameter in Java by using reflection?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Java 8 has introduced a method Parameter.getName() to get the name of a parameter by using reflection.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Before using this feature, we need to turn on this feature in Java compiler.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">To turn on this feature, just run javac with –parameters argument.<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">To verify the availability of this feature, we can use Parameter. isNamePresent() method.<o:p></o:p></span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span><br />
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">29. What are the main differences between an interface with default method and an abstract class in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">An interface with a default method appears same as an Abstract class in Java. But there are subtle differences between two.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 15pt; line-height: 33.778px;">Instance variable</span></b><span style="font-size: 15pt; line-height: 33.778px;">: An interface cannot have instance variables. An abstract class can have instance variables.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 15pt; line-height: 33.778px;">Constructor</span></b><span style="font-size: 15pt; line-height: 33.778px;">: An interface cannot have a constructor. An abstract class can have constructor.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 15pt; line-height: 33.778px;">Concrete Method</span></b><span style="font-size: 15pt; line-height: 33.778px;">: An interface cannot have concrete methods other than default method. An abstract class is allowed to define concrete methods with implementation.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 15pt; line-height: 33.778px;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 15pt; line-height: 33.778px;">Lambda</span></b><span style="font-size: 15pt; line-height: 33.778px;">: An interface with exactly one default method can be used for lambda expression. An abstract class cannot be used for lambda expression.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">30. What is Optional in Java 8?<o:p></o:p></span></b></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"></span></span><br />
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Optional is a container object that may have a null or non-null value. If it has a value then isPresent() method returns true. It a value is present, we can call get() method to get the value. Else we will get nothing. It is very useful in handling data that has null values.</span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></span>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">31. What is a Spliterator in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">A Spliterator is a special type of Iterator to traverse and partition the elements of a source in Java. A source can be a collection, an IO channel or a generator function. A Spliterator may traverse elements individually or sequentially in bulk.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">32. What are the differences between Iterator and Spliterator in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Main differences between Iterator and Spliterator are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Spliterator can be used with Streams in Java 8. Where as, Iterator is just used with Collection.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Spliterator uses Internal Iteration to iterate Streams. Iterator uses External Iteration to iterate Collections.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Spliterator can iterate Streams in Parallel as well as Sequential manner. Iterator only iterates in Sequential manner.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Spliterator can traverse elements individually as well as in bulk. Iterator only iterates elements individually.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">33. What is Type Inference in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">A Java compiler can see each method’s invocation and it declaration to determine what are type arguments required for invocation. By Type Inference, Java can determine the types of the arguments as well as the type of the result being returned.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Type inference algorithm also tries to find the most specific type that can work with all types of arguments.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">34. Does Java 7 support Type Inference?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Yes, Java 7 supports Type Inference. In Java 8, Oracle has enhanced the Type Inference concept. Now it can be used to define Lambda expressions, functions and Method references.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">35. How does Internal Iteration work in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In an Iterator, the fundamental question is that which party controls the iteration. Is it Iterator or the Collection on which iterator runs. When a Collection controls the iterator, then it is called External Iteration. When the Iterator controls the iteration then it is called Internal Iteration.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In case of Internal Iteration, the client hands over an operation to Iterator and the Iterator applies the operation to all the elements in aggregate. Internal Iteration is easier to implement, since the Iterator does not have to store the state of the collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">36.What are the main differences between Internal and External Iterator?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify;">
<span style="font-size: 14pt; line-height: 33.0646px;"> Main differences between Internal and External Iterator are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.</span><span style="line-height: 17.12px;"><span style="font-size: xx-small;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">An Internal Iterator controls the iteration itself. In an External Iterator collection controls the iteration.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2. <span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Internal Iterator can iterate elements in individually as well as in<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">3. <span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Bulk (like forEach). External iterator iterates element one by one.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">4.</span><span style="line-height: 17.12px;"><span style="font-size: xx-small;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Internal Iterator does not have to iterate elements only sequentially. External Iterator always iterates sequentially.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">5. <span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Internal Iterator supports declarative programming style that goes well with functional programming. External Iterator follows imperative style OOPS programming.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">6.</span><span style="line-height: 17.12px;"><span style="font-size: xx-small;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Some people consider Internal Iterator code more readable than that of External Iterator.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">37. What are the main advantages of Internal Iterator over External Iterator in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Some of the main advantages of Internal Iterator are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Internal Iterator is based on Functional programming, therefore it can work on declarative style code.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2. <span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">There is no need to sequentially iterate elements in Internal Iterator.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">3. <span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Code is more readable and concise in Internal Iterator.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">4. <span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">Internal Iterator supports concurrency and parallel processing.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">38. What are the applications in which we should use Internal Iteration?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">We need Internal Iterator in applications that require high performance, parallel processing, fast iteration and bulk operations support.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Also in Internal Iteration applications, we do not have much control over iteration. The other features like parallel processing etc. become more important.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">39. What is the main disadvantage of Internal Iteration over External Iteration?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Internal Iteration has many advantages over External Iteration. But it has one big disadvantage. Since Java API is responsible for iterating in Internal iterator, developer does not get any control over iteration.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">40. Can we provide implementation of a method in a Java Interface?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Before Java 8, it was not allowed to provide implementation of a method in an Interface.Java 8 has introduced the flexibility of providing implementation of a method in an interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">There are two options for that:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Default Method</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: We can give default implementation of a method.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Static Method</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: We can create a static method in an interface and provide implementation.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">41. What is a Default Method in an Interface?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">In Java 8, we can provide implementation of a method in an Interface and mark this method with Default keyword. In this way, this implementation of the method becomes default behavior for any class implementing the interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">42. Why do we need Default method in a Java 8 Interface?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Default methods in an Interface provide backward compatibility feature in Java 8.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Let say there is an interface Car that is implemented by BMW, Chevrolet and Toyota classes. Now a Car needs to add capability for flying. It will require change in Car interface. Some of the car classes that do not have flying capability may fail. Therefore a Default Implementation of flying methods is added in Car interface so that cars with no flying capability can continue to implement the original Car interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">43. What is the purpose of a Static method in an Interface in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">A Static method in an Interface is utility or helper method. This is not an object level instance method. Some of the uses of Static method in an Interface are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Single Class</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: There is no need to create a separate Utils class for storing utility or helper methods. We can keep these methods in same interface.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Encapsulation</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: With Static methods, complete behavior of a Class is encapsulated in same class. There is no need to maintain multiple classes.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Extension</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: It is easier to extend a Class/API. If we extend a collection ArrayList, we get all the methods. We need not extend Collections class also.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">44. What are the core ideas behind the Date/Time API of Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">There are three core ideas behind the Date/Time API of Java 8:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Immutable-value classes</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: The new API avoids threadsafety and concurrency issues by ensuring that all the core classes are immutable and represent well-defined values.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Domain-driven design</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: The new API is modeled on precise domain with classes that represent different use cases for Date and Time.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><span style="font-size: 14pt; line-height: 33.0646px;">The emphasis on domain-driven design offers benefits like clarity and understandability.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Separation of chronologies</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: The new API allows people to work with different calendar systems. It supports the needs of users in different areas of the world likes Japan or Thailand that don’t follow ISO-8601.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 14pt; line-height: 33.0646px;">45. What are the advantages of new Date and Time API in Java 8 over old Date API?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">Some of the advantages of Java 8 Date Time API over existing Date API are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Concurrency</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: Existing Date Time classes (such as java.util.Date and SimpleDateFormatter) are not thread-safe. This does not work well in concurrent applications. In new Date Time API, developer does not have to deal with concurrency issues while writing datehandling code.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 1in; text-align: justify; text-indent: -0.25in;">
<span style="font-size: 14pt; line-height: 33.0646px;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;"> </span></span><b><span style="font-size: 14pt; line-height: 33.0646px;">Better Design</span></b><span style="font-size: 14pt; line-height: 33.0646px;">: Date/Time classes prior to Java 8 have poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0. It is not very intuitive. Java 8 Date Time API handles it very well.<o:p></o:p></span></div>
<br />
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 14pt; line-height: 33.0646px;">No need for 3rd Party Libraries: With the popularity of third-party Date/Time libraries like Joda Time, Java has to make its native Date/Time API comparable. Now we can use the Java API instead of using 3rd party libraries.<o:p></o:p></span></div>
<ol style="text-align: left;"></ol>
<br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">46. What are the uses of Optional?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Some of the uses of Optional in Java are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="mso-list: l0 level1 lfo1; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">We can use Optional to avoid NullPointerException in an application.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo1; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Optional performs Null check at compile time, so we do not get run time exception for a null value.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="mso-list: l0 level1 lfo1; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Optional reduces the codebase pollution by removing unnecessary null checks.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="mso-list: l0 level1 lfo1; text-align: justify; text-indent: -.25in;">
<span style="font-family: "symbol"; font-size: 15pt; line-height: 33.778px;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;"> </span></span><span style="font-size: 15pt; line-height: 33.778px;">Optional can also be used to handle default case for data when a value is null.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">47. Which method in Optional provides the fallback mechanism in case of null value?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In case, an Optional has null value, we can use orElseGet() method as fallback mechanism. If we implement orElseGet() method, it will be invoked when the value of Optional is null.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">48. How can we get current time by using Date/Time API of Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In Java 8 we can use Clock class to get the current time. Instead of using old method System.currentTimeMillis(), we can create a Clock object and call millis() method to get the current time in milliseconds.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">We can also call instant() method on Clock object to get the current time in a readable format.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">49. Is it possible to define a static method in an Interface?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Yes, from Java 8, an Interface can also has a static method.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">50. How can we analyze the dependencies in Java classes and packages?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Java 8 comes with a new command line tool jdeps that can help in analyzing the package-level and class-level dependencies. We can pass a jar file name or a class name as an argument to this tool. It will list all the dependencies of that jar or class.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">51. What are the new JVM arguments introduced by Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">In Java 8, PermGen space of ClassLoader is removed. It has been replaced with MetaSpace. Now we can set the initial and maximum size of MetaSpace.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">The JVM options -XX:PermSize and –XX:MaxPermSize are replaced by -XX:MetaSpaceSize and -XX:MaxMetaspaceSize respectively in Java 8.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">52. What are the popular annotations introduced in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">Some of the popular annotations introduced in Java 8 are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">@FunctionalInterface</span></b><span style="font-size: 15pt; line-height: 33.778px;">: This annotation is used to mark an interface as Functional Interface. As mentioned earlier, A FunctionalInterface can be used for lambda expressions.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">@Repeatable</span></b><span style="font-size: 15pt; line-height: 33.778px;">: This annotation is used for marking another annotation. It indicates that the marked annotation can be applied multiple times on a type.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">53. What is a StringJoiner in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">StringJoiner is a new class in Java 8 that can be used to create a String. It can construct a sequence of characters separated by a delimiter. It can also optionally add a prefix and suffix to this sequence. We can use this sequence to get a String.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">E.g.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: .5in; text-align: justify;">
<span style="font-size: 15pt; line-height: 33.778px;">The String "[One:Two:Three]" may be constructed as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify;">
<span style="font-size: 15pt; line-height: 33.778px;">StringJoiner sj = new StringJoiner(":", "[", "]");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify;">
<span style="font-size: 15pt; line-height: 33.778px;">sj.add("One").add("Two").add("Three");<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 1in; text-align: justify;">
<span style="font-size: 15pt; line-height: 33.778px;">String desiredString = sj.toString();<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">54. What is the type of a Lambda expression in Java 8?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">The type of a lambda expression depends on the context it is being used. A lambda is like a method reference. It does not have a type of its own. Generally, a Lambda is an instance of a Functional Interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify;">
<b><span style="font-size: 15pt; line-height: 33.778px;">55. What is the target type of a lambda expression ?<o:p></o:p></span></b></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">The target type of a lambda expression represents a type to which the expression can be converted. The target type for a lambda expression is a functional interface.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<span style="font-size: 15pt; line-height: 33.778px;">The lambda expression must have same parameter type as the parameter in the function of the interface. It must also return a type compatible with the return type of function.<o:p></o:p></span></div>
<div class="MsoNormal" style="text-align: justify;">
<br /></div>
<div class="MsoNormal" style="text-align: justify; text-indent: .5in;">
<div>
<span style="font-size: 15pt; line-height: 33.778px;"><br /></span></div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></span></div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<span style="font-family: "timesnewromanpsmt" , serif;"></span><br />
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_29.html">Multi-threading</a></b> </span></div>
</div>
</div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-45272599011632671412019-02-07T13:00:00.000+05:302019-02-27T16:16:14.152+05:30Multi-threading Interview Questions in Java part- 2<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: center;">
<b><span style="font-size: large;"><br /></span></b></div>
<div style="text-align: center;">
<b><span style="font-size: large;">Multi-threading</span></b></div>
<div style="text-align: center;">
<b><span style="font-size: large;"><br /></span></b></div>
<div style="text-align: center;">
<b><br /></b></div>
<b>1.What are the minimum requirements for a Deadlock situation in a program?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> For a deadlock to occur following are the minimum requirements:</span><br />
<ol style="text-align: left;">
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>Mutual exclusion</b><span style="font-family: "timesnewromanpsmt" , serif;">: There has to be a resource that can be accessed by only one thread at any point of time.</span></span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>Resource holding</b><span style="font-family: "timesnewromanpsmt" , serif;">: One thread locks one resource and holds it, and at the same time it tries to acquire lock on another mutually exclusive resource.</span></span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>No preemption</b><span style="font-family: "timesnewromanpsmt" , serif;">: There is no pre-emption mechanism by which resource held by a thread can be freed after a specific period of time.</span></span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>Circular wait</b><span style="font-family: "timesnewromanpsmt" , serif;">: There can be a scenario in which two or more threads lock one resource each and they wait for each other’s resource to get free. This causes circular wait among threads for same set of resources.</span></span></li>
</ol>
<b>2. How can we prevent a Deadlock?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> To prevent a Deadlock from occurring at least one requirement for a deadlock has to be removed:</span><br />
<ol style="text-align: left;">
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>Mutual exclusion</b><span style="font-family: "timesnewromanpsmt" , serif;">: We can use optimistic locking to prevent mutual exclusion among resources.</span></span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>Resource holding</b><span style="font-family: "timesnewromanpsmt" , serif;">: A thread has to release all its exclusive locks if it does not succeed in acquiring all exclusive locks for resources required.</span></span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>No preemption</b><span style="font-family: "timesnewromanpsmt" , serif;">: We can use timeout period for an exclusive lock to get free after a given amount of time.</span></span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif;"><b>Circular wait</b><span style="font-family: "timesnewromanpsmt" , serif;">: We can check and ensure that circular wait does not occur, when all exclusive locks have been acquired by all the threads in the same sequence.</span></span></li>
</ol>
<b>3. How can we detect a Deadlock situation?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> We can use ThreadMXBean.findDeadlockedThreads() method to detect deadlocks in Java program. This bean comes with JDK .<br /> Sample code is as follows:<br /> ThreadMXBean bean = ManagementFactory.getThreadMXBean();<br /> long[] threadIds = bean.findDeadlockedThreads(); </span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"><span style="color: #999999;"> // It will return null for no deadlock</span><br /> if (threadIds != null) {<br /> ThreadInfo[] infos = bean.getThreadInfo(threadIds);<br /> for (ThreadInfo info : infos) {<br /> StackTraceElement[] stack = info.getStackTrace();<br /><span style="color: #999999;"> // Log or store stack trace information.</span><br /> }<br /> }</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>4. What is a Livelock?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> Livelock is a scenario in which two or more block each other by responding to an action caused by another thread. In a deadlock situation two or more threads wait in one specific state. In a Livelock scenario, two more threads change their state in such a way that it prevents progress on their regular work. E.g. Consider scenario in which two threads try to acquire two locks. They release a lock that they have acquired, when they cannot acquire the second lock. In a Livelock situation, both threads concurrently try to acquire the locks. Only one thread would succeed, the second thread may succeed in acquiring the second lock. Now both threads hold two different locks. And both threads want to have both locks. So they release their lock and try again from the beginning. This situation keeps repeating multiple times..</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "times new roman" , serif;"><br /></span><b>5. What is Thread starvation?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In a priority based scheduling, Threads with lower priority get lesser time for execution than higher priority threads. If a lower priority thread performs a long running computation, it may happen that this thread does not get enough time to finish its computations just in time. In such a scenario, the tread with lower priority would starve. It will remain away from the threads with higher priority.</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>6. How can a synchronized block cause Thread starvation in Java?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> It is not defined for synchronization that which thread will enter a synchronized block. It may happen that if many threads are waiting for the entry to a synchronized block, some threads may have to wait longer than other threads. Hence these threads with lower priority will not get enough time to finish their work in time.</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>7. What is a Race condition?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> A race condition is an unwanted situation in which a program attempts to perform two or more operations at the same time, but because of the logic of the program, the operations have to be performed in proper sequence to run the program correctly. Since it is an undesirable behavior, it is considered as a bug in code.<br /> Most of the time race condition occurs in “check then act” scenario. Both threads check and act on same value. But one of the threads acts in between check and act. See this example to understand race condition.<br /> if (x == 3) // Check<br /> {<br /> y = x * 5;<span style="color: #999999;"> // Act<br /> // If another thread changes x<br /> // between "if (x == 3)” and "y = x * 5”,<br /> // then y will not be equal to 15.</span><br /> }</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>8. What is a Fair lock in multithreading?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In Java there is a class ReentrantLock that is used for implementing Fair lock. This class accepts an optional parameter fairness. When fairness is set to true, the RenentrantLock will give access to the longest waiting thread. The most popular use of Fair lock is in avoiding thread starvation Since longest waiting threads are always given priority in case of contention, no thread can starve. Downside of Fair lock is the low throughput of the program. Since low priority or slow threads are getting locks multiple time, it leads to slower execution of a program. The only exception to a Fair lock is tryLock() method of ReentrantLock. This method does not honor the value of fairness parameter.</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>9. Which two methods of Object class can be used to implement a Producer Consumer scenario?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> In a Producer Consumer scenario, one thread is a Producer and another thread is a Consumer. For this scenario to start working, a Consumer has to know when the Producer has produced. In Object class, there is a wait() method. A Consumer calls wait method to wait on Producer. The Producer used notify() method of Object class to inform Consumer that it has produced. In this way the processor time between produce and consume operations is freed due to the use of wait() and notify() methods.</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<b>10.How JVM determines which thread should wake up on notify()?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> If multiple threads are waiting on an object’s monitor, JVM awakens one of them. As per Java specification the choice of this thread is arbitrary and it is at the discretion of the implementation. So there is no guarantee of rule that a specific thread will be awakened by JVM on notify() method call.</span><br />
<span style="font-family: "times new roman" , serif;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b>11. Check if following code is threadsafe for retrieving an integer value from a Queue?</b><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> public class QueueCheck {<br /> Queue queue;<br /> public Integer getNextInt() {<br /> Integer retVal = null;<br /> synchronized (queue) {<br /> try {<br /> while (queue.isEmpty()) {<br /> queue.wait();<br /> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> }catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> synchronized (queue) {<br /> retVal = queue.poll();<br /> if (retVal == null) {<br /> System.err.println("retVal is null");</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> t</span><span style="font-family: "timesnewromanpsmt" , serif;">hrow new IllegalStateException();</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> return retVal;</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></div>
<table border="1" cellpadding="0" cellspacing="0" class="MsoNormalTable" style="border-collapse: collapse; border: none; mso-border-alt: solid windowtext .75pt; mso-border-insideh: .75pt solid windowtext; mso-border-insidev: .75pt solid windowtext; mso-padding-alt: 0in 5.4pt 0in 5.4pt; mso-yfti-tbllook: 1184;"><tbody>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 295.5pt;" width="394"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">t</span><span style="font-family: "timesnewromanpsmt" , serif;">hrow new IllegalStateException</span><span style="font-family: "timesnewromanpsmt" , serif;">();</span></div>
</td><td style="border: 1pt solid; padding: 0in 5.4pt; width: 86.25pt;" width="115"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">}</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 12.75pt;" width="17"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">}</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
<tr><td style="border: 1pt solid; padding: 0in 5.4pt; width: 309pt;" width="412"><div class="MsoNormal" style="line-height: normal; margin-bottom: 0in;">
<span style="font-family: "timesnewromanpsmt" , serif;">r<br />eturn retVal;</span><span style="font-family: "times new roman" , serif;"><o:p></o:p></span></div>
</td><td style="border-bottom: 1pt solid; border-left: none; border-right: 1pt solid; border-top: none; padding: 0in 5.4pt;"></td></tr>
</tbody></table>
<span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">}} </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In the above code Queue is used as object monitor to handle concurrency issues. But it may not behave correctly in a multithreading scenario. There are two separate synchronized blocks in above code. In case two threads are woken up simultaneously by another thread, both </span><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">threads will enter one after in the second synchronized block. Only one of the two threads will get new value from the queue and make it empty. The second thread will poll on an empty queue and it will not get any non-null return value.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">12.How can we check if a thread has a monitor lock on a given object?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Java, Thread class has a static method holdsLock(Object objToCheck) to check whether thread has a lock on objToLock object. This method will return true if current thread holds the lock on the objToLock object that was passed as an argument to this method.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">13.What is the use of yield() method in Thread class?</span></b><br />
<span style="font-family: "times new roman" , serif;"></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> The yield() method of Thread class is used to give a hint to scheduler that the current thread wants to free the processor. The scheduler can either use this hint or just ignore this hint. Since the scheduler behavior is not guaranteed, it may happen that the current thread again gets the processor time. It can be used for debugging or testing purposes. But there is rarely any concrete use of this method.</span><br />
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<b><span style="font-family: , serif; line-height: 17.12px;">14. What is an important point to consider while passing an object from one thread to another thread?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> This is a multi-threading scenario. In a multi-threading scenario, the most important point is to check whether two threads can update same object at the same time. If it is possible for two threads to update the same object at the same time, it can cause issues like race condition. So it is recommended to make the object Immutable. This will help in avoiding any concurrency issues on this object.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">15.What are the rules for creating Immutable Objects?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> As per Java specification, following are the rules for creating an Immutable object:<br /> Do not provide "setter" methods that modify fields or objects referred to by fields.Make all fields final and private. Do not allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods. If the instance fields include references to mutable objects, do not allow those objects to be changed. Do not provide methods that modify the mutable objects. Do not share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">16.What is the use of ThreadLocal class?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> ThreadLocal class provides thread-local variables. Each thread accesses only its own local variables. It has its own copy of the variable. By using ThreadLocal, if thread X stores a variable with value x and another thread Y stores same variable with the value y, then X gets x from its ThreadLocal instance and Y gets y from its ThreadLocal instance. Typically, ThreadLocal instances are private static fields that are associated with the state of a thread.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">17.What are the scenarios suitable for using ThreadLocal class?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We can use instance of ThreadLocal class to transport information within an application. One use case is to transport security or login information within an instance of ThreadLocal so that every method can access it. Another use case is to transport transaction information across an application, without using the method-to-method communication.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">18.How will you improve the performance of an application by multi-threading?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In an environment with more than one CPU, we can parallelize the computation tasks on multiple CPUs. This leads to parallel processing of a bigger task that takes lesser time due to multiple threads dividing the work among themselves. One example is that if we have to process 1000 data files and calculate the sum of numbers in each file. If each file takes 5 minutes, then 1000 files will take 5000 minutes for processing. But by using multi-threading we can process these files in 10 parallel threads. So each thread will take 100 files each. Since now work is happening in 10 parallel threads, the time taken will be around 500 minutes.</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> </span><br />
<b><span style="font-family: , serif; line-height: 17.12px;">19.What is scalability in a Software program?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Scalability is the capability of a program to handle growing amount of work or its potential to be enlarged in order to accommodate growth. A program is considered scalable, if it is suitable to handle a large amount of input data or a large number of users or a large number of nodes. When we say a program does not scale, it means that program fails on increasing the size of task.</span><span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span><b><span style="font-family: , serif; line-height: 17.12px;"><br /></span></b><br />
<b><span style="font-family: , serif; line-height: 17.12px;">20. How will you calculate the maximum speed up of an application by using multiple processors?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Amdahl’s law gives the theoretical speedup in latency of the execution of a task at fixed workload. It gives the formula to compute the theoretical maximum speed up that can be achieved by providing multiple processors to an application.<br /> If S is the theoretical speedup then the formula is:<br /> S(n) = 1 / (B + (1-B)/n)<br /> where n is the number of processors B is the fraction of the program that cannot be executed in parallel. When n converges against infinity, the term (1-B)/n converges against zero. Therefore, the formula can be reduced in this special case to 1/B. In general, the theoretical maximum speedup behaves in inverse proportion to the fraction that has to be executed serially. Thismeans the lower this fraction is, the more theoretical speedup can be achieved.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">21.What is Lock contention in multithreading?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Lock contention is the situation when one thread is waiting for a lock/object that being held by another thread. The waiting thread cannot use this object until the other thread releases the lock on that object. It is also known as Thread contention. Ideally locks reduce the thread contention. Without locks, multiple threads can operate on same object and cause undesirable behavior. If locking is implemented correctly it reduces the occurrence of contention between multiple threads.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">22. What are the techniques to reduce Lock contention?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> There are following main techniques to reduce Lock contention:</span><br />
<ol style="text-align: left;">
<li><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Reduce the scope of lock.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Reduce object pooling.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Reduce the number of times a certain lock can be acquired.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Avoid synchronization at unnecessary places.</span></li>
<li><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;">Implement hardware supported Optimistic locking in place of synchronization.</span></li>
</ol>
<b><span style="font-family: , serif; line-height: 17.12px;">23. What technique can be used in following code to reduce Lock contention?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> synchronized (map) {<br /> Random r = new Random();<br /> Integer value = Integer.valueOf(42);<br /> String key = r.nextString(5);<br /> map.put(key, value);<br /> } </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> The code uses Random() to get a random string and it also used Integer to convert 42 in an object. Since these lines of code are specific to this thread, these can be moved out of Synchronization block.<br /> Random r = new Random();<br /> Integer value = Integer.valueOf(42);<br /> String key = r.nextString(5);<br /> synchronized (map) {<br /> map.put(key, value);<br /> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<b><span style="font-family: , serif; line-height: 17.12px;">24. What is Lock splitting technique?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Lock splitting is a technique to reduce Lock contention in multithreading. It is applicable in scenario when one lock is used to synchronize access to different aspects of the same application. Sometimes we put one lock to protect the whole array. There can be multiple threads trying to get the lock for same array. This single lock on array can cause Lock contention among threads. To resolve this we can give one lock to each element of the array. Or we can use modulus function to assign different locks to a small group of array elements. In this way we can reduced the chance of Lock contention. This is Lock splitting technique.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">25. Which technique is used in ReadWriteLock class for reducing Lock contention?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> ReadWriteLock uses two locks. One lock for read-only operations, another lock for write operations. Its implementation is based on the premise that concurrent threads do not need a lock when they want to read a value while no other thread is trying to write. In this implementation, read-only lock can be obtained by multiple threads. And the implementation guarantees that all read operation will see only the latest updated value as soon as the write lock is released.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">26. What is Lock striping?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Lock splitting we use different locks for different parts of the application. In Lock striping we use multiple locks to protect different parts of the same data structure. ConcurrentHashMap class of Java internally uses different buckets to store its values. Each bucket is chosen based on the value of key. ConcurrentHashMap uses different locks to guard different buckets. When one thread that tries to access a hash bucket, it can acquire the lock for that bucket. While another thread can simultaneously acquire lock for another bucket and access it. In a synchronized version of HashMap, the whole map is has one lock. Lock striping technique gives better performance than Synchronizing the whole data structure.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">27. What is a CAS operation?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> CAS is also known a Compare-And-Swap operation. In a CAS operation, the processor provides a separate instruction that can update the value of a register only if the provided value is equal to the current value. CAS operation can be used as an alternate to synchronization. Let say thread T1 can update a value by passing its current value and the new value to be updated to the CAS operation. In case another thread T2 has updated the current value of previous thread, the previous thread T1’s current value is not equal to the current value of T2. Hence the update operation fails. In this case, thread T1 will read the current value again and try to update it. This is an example of optimistic locking. </span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">28. Which Java classes use CAS operation?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Java classes like AtomicInteger or AtomicBoolean internally use CAS operations to support multi-threading. These classes are in package java.util.concurrent.atomic.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">29. Is it always possible to improve performance by object pooling in a multi-threading application?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> By using Object pools in an application we limit the number of new objects to be created for a class. In a single thread operation, it can improve the performance by reusing an already created object from a pool. In a multi-threading application an object pool has to provide synchronized access to multiple threads. Due to this only one thread can access the pool at a time. Also there is additional cost due to Lock contention on pool. These additional costs can outweigh the cost saved by reuse of an object from the pool. Therefore using an Object pool may not always improve the performance in a multi-threading application.</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<b><span style="font-family: , serif; line-height: 17.12px;">30. How can techniques used for performance improvement in a single thread application may degrade the performance in a multi-threading application?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In a single thread applications we can use Object pool for performance optimization. Where as in multi-threading environment, it may not be a good idea to use an Object pool. Increased overhead of synchronization and lock contention can degrade the performance gained by using Object pool in a multi-threading application. </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Another example is the implementation in which a List keeps a separate variable to hold the number of elements. This technique is useful in single thread application where size() method can return the value from this variable, without the need to count all the elements of list. </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> But in a multi-threading application, this separate variable can rather degrade the performance. This variable has to be access controlled by a lock since multiple concurrent threads can insert an element in a list. The additional cost of lock on this variable can outweigh the benefit gained by it in a multi-threading application.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">31.What is the relation between Executor and ExecutorService interface?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Executor interface has only execute(Runnable) method. The implementing class of this interface has to execute the given Runnable instance passed to execute() method at some time in the future. ExecutorService interface extends Executor interface. It provides additional methods like- invokeAny(), invokeAll(), shutdown(), awaitTermination(). These method provide the ability to shutdown the thread so that further requests can be rejected. Also it provides ability to invoke a collection of Callable tasks.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">32. What will happen on calling submit() method of an ExecutorService instance whose queue is already full?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> The implementation of ExecutorService will throw RejectedExecutionException, when its queue is already full and a new task is submitted by calling submit() method.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">33. What is a ScheduledExecutorService?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> ScheduledExecutorService interface extends the interface ExecutorService. It provides various schedule() methods that can be used to submit new tasks to be executed at a given point of time. One of the schedule() method provides the ability to schedule a oneshot task that can be executed after given delay. Another version of schedule() method provides the ability to execute ScheduleFuture after a given amount of delay. In addition there are scheduleAtFixedRate() and scheduleWithFixedDelay() methods that can execute an action at a periodic interval of time.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">34. How will you create a Thread pool in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Java, Executors framework provides a method newFixedThreadPool(int nThreads) that can be used to create a Thread pool with a fixed number of threads.<br /> Sample code is as follows:<br /> public static void main(String[] args) throws InterruptedException,ExecutionException<br /> {<br /> ExecutorService myService = Executors.newFixedThreadPool(5);<br /> Future<Integer>[] futureList = new Future[5];<br /> for (int i = 0; i < futureList.length; i++) {<br /> futureList[i] = myService.submit(new MyCallable());<br /> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> for (int i = 0; i < futureList.length; i++) {<br /> Integer retVal = futureList[i].get();<br /> println(retVal);<br /> }</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> my<br /> Service.shutdown();<br /> }</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">35. What is the main difference between Runnable and Callable interface?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Runnable interface defines run() method that does not return any value. Callable interface allows call() method to return a value to its caller. A Callable interface can also throw an exception in case of an error. Also Callable is a newer addition to Java since version 1.5.</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<b><span style="font-family: , serif; line-height: 17.12px;">36. What are the uses of Future interface in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We can use Future interface to represent the result of an asynchronous computation. These are the operations whose result is not immediately available. Therefore Future interface provides isDone() method to check if the asynchronous computation has finished or not. We can also check if the task was cancelled by calling isCancelled() method. Future also provides cancel() method to attempt the cancellation of a task.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">37. What is the difference in concurrency in HashMap and in Hashtable?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In a Hashtable class all methods are synchronized. In a HashMap implementation all the methods are not synchronized. Therefore Hashtable is a thread-safe collection. HashMap is not a thread-safe collection. In a multi-threading it is not advisable to use regular HashMap. We can use ConcurrentHashMap class in multi-threading applications.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">38. How will you create synchronized instance of List or Map Collection?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Java, Collections class provides methods to synchronize any collection. It also provides synchronizedList(List) and synchronizedMap(Map) methods that can be used to convert a List or Map to a synchronized instance.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">39. What is a Semaphore in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> Semaphore class in Java is used to implement a counting semaphore. It is used to restrict the number of threads that can access a physical or logical resource. A Semaphore maintains a set of permits that should be acquired by competing threads. We can also use it to control how many threads can access the critical section of a program or a resource concurrently. The first argument in Semaphore constructor is the total number of permits available. Each invocation of acquire() method tries to obtain one of the available permits. The acquire() method is used to acquire a permit from the semaphore. If we pass number of permits required to acquire() method, then it blocks the thread until that number of permits are available. Once a thread has finished its work, we can use release() method to release the permits.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">40. What is a CountDownLatch in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> CountDownLatch class helps in implementing synchronization in Java. It is used to implement the scenarios in which one or more threads have to wait until other threads have reached the same state such that all thread can start.<br /> There is a synchronized counter that is decremented until it reaches the value zero. Once it reaches zero, it means that all waiting threads can proceed now.<br /> It is a versatile tool that can be used for other Synchronization scenarios as well. It can also work as on/off latch or gate. All threads invoking await() method wait at the gate until it is opened by a thread invoking countdown() method.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">41. What is the difference between CountDownLatch and CyclicBarrier?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> CyclicBarrier takes an optional Runnable task that is run once the common barrier condition is achieved. CountDownLatch is used in simple use cases where a simple start stop is required. A CyclicBarrier is useful in complex scenarios where more coordination is required. E.g. MapReduce algorithm implementation. CyclicBarrier resets the internal value to the initial value once the value reaches zero. CyclicBarrier can be used to implement the scenarios in which threads have to wait for each other multiple times.</span><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span>
<b><span style="font-family: , serif; line-height: 17.12px;">42. What are the scenarios suitable for using Fork/Join framework?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> ForkJoinPool class is in the center of Fork/Join framework. It is a thread pool that can execute instances of ForkJoinTask. ForkJoinTask class provides the fork() and join() methods. The fork() method is used to start the asynchronous execution of a task. The join() method is used to await the result of the computation. Therefore, divide-and-conquer algorithms can be easily implemented with Fork/Join framework.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">43. What is the difference between RecursiveTask and RecursiveAction class?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> RecursiveAction class has compute() method that does not have to return a value. RecursiveAction can be used when the action has to directly operate on a Data structure. It does not need to return any computed value. In RecursiveTask class has compute() method that always returns a value. Both RecursiveTask and RecursiveAction classes are used in ForkJoinTask implementations.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">44. In Java 8, can we process stream operations with a Thread pool?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Java 8, Collections provide parallelStream() method to create a stream that can be processed by a Thread pool. We can also call the intermediate method parallel() on a given stream to convert it into a sequential stream of parallel tasks.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">45. What are the scenarios to use parallel stream in Java 8?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> A parallel stream in Java 8 has a much higher overhead compared to a sequential one. It takes a significant amount of time to coordinate the threads. We can use parallel stream in following scenarios:<br /> When there are a large number of items to process and the processing of each item takes time and is parallelizable. When there is a performance problem in the sequential processing. When current implementation is not already running in a multithread environment. If there is already a multi-threading environment, adding parallel stream can degrade the performance.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">46. How Stack and Heap work in Java multi-threading environment?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Java, Stack and heap are memory areas available to an application. Every thread has its own stack. It is used to store local variables, method parameters and call stack. Local variables stored in Stack of one Thread are not visible to another Thread. Where as, Heap is a common memory area in JVM. Heap is shared by all threads. All objects are created inside heap. To improve performance thread can cache the values from heap into their stack. This can create problem if the same variable is modified by more than one thread. In such a scenario we should used volatile keyword to mark a variable volatile. For a volatile variable the thread always reads the value from main memory.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">47. How can we take Thread dump in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> The steps to take Thread dump of Java process depends on the operating system. On taking Thread dump, Java writes the state of all threads in log files or standard error console. We can press Ctrl + Break key together to take thread dump in Windows. We can execute kill -3 command for taking Thread dump on Linux. Another option to take Thread dump is jstack tool. We can pass process id of java process to this tool for taking Thread dump. This is the simple one, -Xss parameter is used to control stack size of Thread in Java. You can see this list of JVM options to learn more about this parameter. </span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">48. Which parameter can be used to control stack size of a thread in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> We use –Xss parameter to control the stack size of a thread in Java. If we set it as 1 MB, then every thread will get 1MB of stack size.</span><br />
<span style="line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></span><b><span style="font-family: , serif; line-height: 17.12px;">49. There are two threads T1 and T2? How will you ensure that these threads run in sequence T1, T2 in Java?</span></b><br />
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"> In Java there are multiple ways to execute threads in a sequence. One of the simplest way for sequencing is join() method of Thread class. We can call join() method to start a thread when another thread has finished. We start with the last thread to execute first. And make this thread join on the next thread. In this case we start thread T2 first. And then call T1.join() so that thread T2 waits for thread T1 to finish execution. Once T1 completes execution, T2 thread starts executing.</span><span style="font-family: "times new roman" , serif; line-height: 17.12px;"><br /></span><br />
<div>
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><br /></span></div>
<div>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></div>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span><br />
<div style="text-align: center;">
<span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><span style="font-family: "timesnewromanpsmt" , serif; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-basic-interview-questions.html">Basic Java</a> <a href="https://thegeeksarea.blogspot.com/2019/01/oops-interview-question-in-java.html">OOPs</a> <a href="https://thegeeksarea.blogspot.com/2019/01/static-interview-question-in-java.html">Static</a> </b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html"> </a></span><b style="font-family: "Times New Roman"; text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/inheritance-1.html">Inheritance </a> </span></b></span></div>
<span style="font-family: "timesnewromanpsmt" , serif;"></span><br />
<div style="text-align: center;">
<br />
<div>
<div style="text-align: center;">
<span style="line-height: 17.12px;"><span style="text-align: left;"><span style="font-family: "times new roman"; line-height: 17.12px;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/method-overloading-and-overriding.html">Method Overloading and Overriding</a></b></span></span></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/polymorphism-interview-questions-in-java.html">Polymorphism</a> </b><a href="https://thegeeksarea.blogspot.com/2019/01/abstraction-interview-questions-in-java.html"><b>Abstraction </b></a></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b> </b></span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b> <a href="https://thegeeksarea.blogspot.com/2019/01/final-keyword-interview-questions-in.html">Final Keyword</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/java-package-interview-questions.html">Package</a> </b></span><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Serialization and </a></span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"><a href="https://thegeeksarea.blogspot.com/2019/01/serialization-and-deserialization.html">Deserialization</a> </span></b><b style="text-align: left;"><span style="font-family: , serif; line-height: 17.12px;"> <a href="https://thegeeksarea.blogspot.com/2019/01/reflection-interview-questions-in-java.html">Reflection</a></span></b><span style="font-family: "times new roman" , serif; text-align: left;"> </span><br />
<span style="font-family: "times new roman" , serif; text-align: left;"><br /></span>
<br />
<div style="text-align: center;">
<span style="text-align: left;"><span style="font-family: "times new roman" , serif;"><a href="https://thegeeksarea.blogspot.com/2019/01/garbage-collection-interview-questions.html"><b>Garbage Collection</b></a> </span></span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/inner-classes-interview-questions-in.html">Inner Classes</a></b></span><span style="font-family: "times new roman" , serif; text-align: left;"> </span><span style="font-family: "times new roman" , serif;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-string-interview-questions.html">String</a> <a href="https://thegeeksarea.blogspot.com/2019/01/exception-handling-interview-questions.html">Exception Handling</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> </span><br />
<span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><br /></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"><b><a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions.html">Java Collection</a></b></span><span style="font-family: "timesnewromanpsmt" , serif; text-align: left;"> <b><a href="https://thegeeksarea.blogspot.com/2019/01/multi-threading-interview-questions-in_29.html">Multi-threading</a></b> </span></div>
</div>
</div>
</div>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span>
<span style="font-family: "timesnewromanpsmt" , serif;"><br /></span></div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-1104379682772771792019-01-31T20:30:00.000+05:302019-05-26T09:20:15.658+05:30Multi-threading Interview Questions in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h2 style="line-height: normal; text-align: center;">
<b><span style="color: #2e75b6; font-size: 16.0pt; mso-style-textfill-fill-alpha: 100.0%; mso-style-textfill-fill-color: #2E75B6; mso-style-textfill-fill-colortransforms: lumm=75000; mso-style-textfill-fill-themecolor: accent1; mso-themecolor: accent1; mso-themeshade: 191;">Multi-threading</span></b></h2>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">1. What is a Thread in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A thread in Java is a lightweight process that runs
within another process or thread. It is an independent path of execution in an
application. JVM gives each thread its own method-call stack. When we start
JVM, Java starts one thread. This thread calls the main method of the class
passed in argument to java call.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">2. What is the priority of a Thread and how it is used in
scheduling?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, every Thread has a priority. This priority is
specified as a number between 1 to 10. Scheduler in Java schedules different
threads based on the priority of a thread. It is also known as pre-emptive
scheduling. The thread with higher priority gets preference in execution over a
thread with lower priority.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">3. What is the default priority of a thread in Java? </span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, a new thread gets the same priority as the
priority of the parent thread that creates it. Default priority of a thread is
5 (NORM_PRIORITY).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">4. What are the three different priorities that can be set on a Thread
in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can set following three priorities on a Thread
object in Java:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l1 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">MIN_PRIORITY</span></b><span style="font-size: 14.0pt;">: This is the minimum priority that a thread can have.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l1 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">NORM_PRIORITY</span></b><span style="font-size: 14.0pt;">: This is the default priority that is assigned to a
thread.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l1 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">MAX_PRIORITY</span></b><span style="font-size: 14.0pt;">: This is the maximum priority that a thread can have.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Default priority of a thread is 5 NORM_PRIORITY. The
value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">5. What is the purpose of join() method in Thread class?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, Thread Scheduler controls thread scheduling.
But we can use join() method on a thread to make current thread to wait for
another thread to finish. When we use join(), the current thread stops
executing. It wait for the thread on which join() is called to finish. This
makes sure that current thread will continue only after the thread it joined
finished running. Consider following example:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">Public class ThreadJoin {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">Thread importantThread = new Thread(new Runnable() <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">public void run () {//do something}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">});<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Thread currentThread = new Thread(new
Runnable() <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">public void run () {//do something}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">});<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">importantThread.start(); // Line 1<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">importantThread.join(); // Line 2<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">currentThread.start(); // Line 3<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">} <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In the above example, main thread is executing. On
Line 1, a new thread called importantThread is ready to run. But at Line 2,
main thread joins the importantThread. Now it lets importantTread to finish and
then it moves to Line 3. So currentThread at Line 3 will not start till the
importantThread has finished.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">6. What is the fundamental difference between wait() and sleep()
methods?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The main difference between wait() and sleep() is that
wait is an Object level method, whereas sleep() is a static method in Thread
class. A waiting thread can be woken up by another thread by calling notify()
on the monitor which is being waited on. But a sleeping thread cannot be woken
up. A wait() and notify() has to happen within the same block that is
synchronized on the monitor object. When we call wait() the current thread
releases the monitor and goes to waiting state. Then another thread calls
notify() to wake it up. In case of sleep() current thread does not release the
monitor or locks. It just sleeps for some pre-defined time period.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">7.Is it possible to call run() method instead of start() on a thread
in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Yes. We can call run() method of a thread. But it does
not work as a separate thread. It will just work as a normal object in main
thread and there will not be context switching between the threads.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">8. How Multi-threading works in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Java provides support for Multithreading. In a
Multithreading environment, one process can execute multiple threads in
parallel at the same time. In Java, you can create process and then create
multiple threads from that process. Each process can execute in parallel to
perform independent tasks. Java provides methods like- start(), notify(),
wait(), sleep() etc. to maintain a multi-threading environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">9. What are the advantages of Multithreading?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Main advantages of Multithreading are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Improved
performance</span></b><span style="font-size: 14.0pt;">: We can improve
performance of a job by Multi-threading.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Simultaneous
access to Multiple Applications</span></b><span style="font-size: 14.0pt;">: We
can access multiple applications from a process by doing multithreading<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Reduced number
of Servers required</span></b><span style="font-size: 14.0pt;">: With
Multithreading we need lesser number of servers, since one process can spawn
multiple threads.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Simplified
Coding</span></b><span style="font-size: 14.0pt;">: In certain scenarios, it is
easier to code multiple threads than managing it from same thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">10. What are the disadvantages of Multithreading?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are certain downsides to Multithreading. These
are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l2 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Difficult to
Debug: Multithreading code is difficult to debug in case of an issue.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l2 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Difficult to
manage concurrency: Due to multiple threads, we may experience different kinds
of issues.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l2 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Difficulty of
porting code: It is difficult to convert existing single threaded code into
multi-threading code.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l2 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Deadlocks: In
case of multi-threading we can experience deadlocks in threads that are waiting
for same resource.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">11. What is a Thread in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java, a thread is a lightweight process that runs within another process or
thread. It is an independent path of execution in an application. Each thread
runs in a separate stack frame. By default Java starts one thread when the main
method of a class is called.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<span style="font-size: 14.0pt;"><span style="color: red;"><b>12.
What is a Thread’s priority and how it is used in scheduling?</b></span></span></h3>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java, every Thread has a priority. This priority is specified as an integer
value. The priority value is used in scheduling to pick up the thread with
higher priority for execution. The threads with higher priority get more
preference in execution than the threads with lower priority. The task
scheduler schedules the higher priority threads first, followed by the lower
priority threads.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">13.
What are the differences between Pre-emptive Scheduling Scheduler and Time Slicing<span style="mso-spacerun: yes;"> </span>Scheduler? <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Pre-emptive scheduling, the highest priority task will keep getting time to
execute until it goes to waiting state or dead state or a task with higher
priority comes into queue for scheduling. In Time slicing scheduling, every
task gets a predefined slice of time for execution, and then it goes to the
pool of tasks ready for execution. The scheduler picks up the next task for
execution, based on priority and various other factors.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">14.
Is it possible to call run() method instead of start() on a thread in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Yes.
We can call run() method of a thread. But it does not work as a separate
thread. It will just work as a normal object in main thread and there will not
be context-switching between the threads.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">15.
How will you make a user thread into daemon thread if it has already started?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
We cannot make a user thread to daemon thread once it has already started. If
we do it by calling setDaemon(), it will throw IllegalThreadStateException <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">16.
Can we start a thread two times in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
We can call start() method only once on a thread in Java. If we call it twice,
it will give us exception.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">17.In
what scenarios can we interrupt a thread?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can interrupt a thread if we want to wake it up from the sleep or wait state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">18.
In Java, is it possible to lock an object for exclusive use by a thread?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Yes.
We can use synchronized block to lock an object. The locked object is
inaccessible to any other thread. Only the thread that has locked it can access
it.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">19.
How notify() method is different from notifyAll() method?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java, notify() method is used to unblock a specific thread that is in waiting
stated. Whereas, notifyAll() method is used to unblock all the threads that are
in waiting state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">20.
What is a daemon thread in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">A
daemon thread in Java is a low priority thread that does not prevent the JVM
from exiting when the program finishes. The thread keeps running. Garbage
Collection is an example of daemon thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">21.
How can we make a regular thread Daemon thread in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can call setDaemon(boolean) method to change a thread to daemon thread before
the thread starts.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">22.
How will you make a user thread into daemon thread if it has already started?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
We cannot make a user thread to daemon thread once it has already started. If
we do it by calling setDaemon(), it will throw IllegalThreadStateException.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">23.
Can we start a thread two times in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
We can call start() method only once on a thread in Java. If we call it twice,
it will give us exception.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">24.
What is a Shutdown hook in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">The
shutdown hook is a thread that is invoked implicitly by JVM just before the
shut down. It can be used to clean up unused resources etc. We can use
java.lang.Runtime.addShutdownHook (Thread hook) method to register a new
virtual-machine shutdown hook.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">25.
What is synchronization in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">The
concept of Synchronization in Java is used in Multi-threading programming. It
is a feature in Java that helps in controlling the access of multiple threads
to a shared resource. It is used to prevent Deadlock between multiple threads.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">26.
What is the purpose of Synchronized block in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Synchronized
block has many uses in Java multi-threading environment. Some of the uses are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">It
can prevent thread interference It is also used to avoid memory inconsistency
issues In general, scope of synchronized block is smaller than the scope of a
method.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">27.What
is static synchronization?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can make a static method as synchronized in Java. Adding synchronized keyword
to a static method can do this. In static synchronization, the lock is on class
not on object.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">28.
What is a Deadlock situation?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">A
Deadlock is a situation in which two or more threads are waiting on each other
to release a resource. Each thread is waiting for a resource that is held by
the other waiting thread. At times there is a circular wait when more than two
threads are waiting on each other’s resources.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">29.
What is the meaning of concurrency?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Concurrency
is the ability of a program to execute several programs simultaneously. This is
achieved by distributing computations over multiple CPU cores of a machine or
even over different machines within the same network. It can increase the speed
of execution of the overall program in multi-processor or multi-core system.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">30.
What is the main difference between process and thread?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">As
such both process and thread are independent sequences of execution. The main
difference is that a thread runs in a shared memory space, where as a process
runs in its own memory space. A process runs the execution in an environment
provided by the operating system. A process has its own set of private
resources (e.g. memory, open files, etc.). A thread lives within a process and
shares the resources likememory, open files etc. with the other threads of the
same process. This ability to share resources between different threads makes
thread more suitable for tasks where performance is a significant factor.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">31.
What is a process and thread in the context of Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java, a process refers to the running of Java Virtual Machine (JVM). But a
thread lives within a JVM and it can be created or stopped by the Java
application at runtime.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">32.
What is a Scheduler?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">A
scheduler is a program that is the implementation of a scheduling algorithm to
manage access of processes and threads to limited resource like CPU or an I/O
channel. The goal of most scheduling algorithms is to provide load balancing
for the available processes/threads and to guarantee that each process/thread
will get a reasonable time frame to access the requested resource exclusively.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">33.
What is the minimum number of Threads in a Java program?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
a JVM, each Java program is executed within the main process that starts with
java.exe. Therefore each Java application has at least one thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">34.
What are the properties of a Java thread?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Each
Java thread has following properties:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Identifier:
An identifier of type long that is unique within the JVM<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Name:
A name of type String<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Priority:
Priority of type int<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">State:
A state of type java.lang.Thread.State<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Group:
A thread group the thread belongs to <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">35.
What are the different states of a Thread in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Following
are the different states of a Thread in Java:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">New:
In the New state the thread has not yet.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Runnable:
A thread executing in the JVM is in Runnable state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Blocked:
A thread waiting for a monitor lock is in Blocked state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Waiting:
A thread waiting indefinitely for another thread to perform a particular action
is in Waiting state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Timed_waiting:
A thread waiting for another thread to perform an action for up to a specified
waiting time is in Timed_waiting state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Terminated:
A thread that has exited is in Terminated state.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">36.
How will you set the priority of a thread in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">The
priority of a thread in Java can be set by using setPriority(int priority)
method. We can use constant Thread.MAX_PRIORITY to set the maximum priority of
a thread. We can use constant Thread.MIN_PRIORITY to set the minimum priority
of a thread. Or we can use constant Thread.NORM_PRIORITY to set the default
priority of a thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">37.
What is the purpose of Thread Groups in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java, every thread belongs to a group of threads. The JDK class
java.lang.ThreadGroup provides methods to handle a whole group of Threads. With
the help of these methods we can interrupt all threads of a group or set the
maximum priority of all threads of a group. So a thread group is used for
taking collective actions on a group of threads.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">38.
Why we should not stop a thread by calling its stop() method?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">The
stop() method in Thread class is a deprecated method. Its use is not
recommended. When we call stop() method, the thread unlocks all monitors that
it has acquired. If any locked object was in an inconsistent state, this state
gets visible to all other threads. It can cause unexpected behavior when other
threads work on this inconsistent object. So calling stop() method to stop a
thread is not advisable.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">39.
How will you create a Thread in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">There
are two main ways to create a thread in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Extend
Thread class: We can extend java.lang.Thread class and implement run() method.
On calling start() method it will start a new thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Implement
Runnable interface: We can implement java.lang.Runnable interface and pass the
implemented object to the constructor of java.lang.Thread class. On calling
start() it will start a new thread.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">40.
How can we stop a thread in the middle of execution in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can use a volatile variable as an indicator to stop the thread. We can create a
volatile reference pointing to the current thread. This reference can be set to
null by other threads to flag that the current thread should stop execution. In
following example threadStopper is the volatile reference that can be set as
null in stopThread() method by other threads.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Sample
code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
static class MyThread extends Thread {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">private
volatile Thread threadStopper;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
void start() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">threadStopper
= new Thread(this);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">threadStopper.start();}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
void stopThread() {threadStopper = null;}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
void run() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Thread
currThread = Thread.currentThread();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">while(currThread
== threadStopper) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">try
{Thread.sleep(100);} catch (InterruptedException e) {}}}}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">41.
How do you access the current thread in a Java program?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can access the current thread in Java by calling the static method
currentThread() of java.lang.Thread class.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Sample
code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
class MyThread {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
static void main(String[] args) {// Get ID of Current Thread<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">long
id = Thread.currentThread().getId();// Get Name of Current Thread<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">String
name = Thread.currentThread().getName();}}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">42.
What is Busy waiting in Multithreading?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Busy
waiting is also known as busy-looping or spinning. It is a multi-threading
technique in which a process repeatedly checks if a condition is true. For
example, a process can keep checking if any keyboard input is available. In
general, busy waiting is considered as Anti-pattern that wastes processor time,
so it should be avoided.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Sample
code for busy waiting is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Thread
thread = new Thread(new Runnable() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">@Override<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
void run() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">long
timeToStop = System.currentTimeMillis() + 1000;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">long
currentTime = System.currentTimeMillis();// Busy waiting<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">while
(timeToStop > currentTime) {currentTime = System.currentTimeMillis();}}});<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">43.
How can we prevent busy waiting in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">There
is a simple way to prevent busy-waiting in Java. We can just put the current
thread to sleep for given amount of time. It can be done by calling sleep() method
of java.lang.Thread class. We can pass the number of milliseconds to sleep()
method as an argument.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">44.
Can we use Thread.sleep() method for real-time processing in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Java
does not guarantee that Thread.sleep() will cause the thread to sleep for exactly
N number of milliseconds. Sometime the thread can sleep for than N number of
milliseconds. In real-time processing we need precise time period for which a
thread should run or sleep. Therefore the invocation of Thread.sleep() method
is not recommended for use in real-time processing.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">45.
Can we wake up a thread that has been put to sleep by using Thread.sleep()
method?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can use interrupt() method of java.lang.Thread class to interrupt a thread that
is in sleep state. It will get InterruptedException to wake up from the sleep.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Sample
code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
class ThreadInterrupt implements Runnable {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
void run() {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">try
{Thread.sleep(Long.MAX_VALUE);} catch (InterruptedException e)
{SOP(“Interrupted by exception!");}}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
static void main(String[] args) throws InterruptedException{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Thread
myThread = new Thread(new ThreadInterrupt(),“myThread");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">myThread.start();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">SOP(“Sleeping
in main thread for 10 seconds”);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Thread.sleep(10000);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">SOP(“Interrupting
myThread");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">myThread.interrupt();}}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">46.
What are the two ways to check if a Thread has been interrupted?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">These
are the two ways to check for thread interruption:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java, a Thread can call Thread.interrupted() method to check if it has been
interrupted or not.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">The
other option is to call isInterrupted() method of Thread class to check if it
has been interrupted or not.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">47.How
can we make sure that Parent thread waits for termination of Child thread?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">We
can use join() method for this purpose. On calling join() method, current
thread waits for the child thread to which it joins to finish.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Sample
code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Thread
myThread = new Thread(new Runnable() {public void run() {}});<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">myThread.start();//
Join on myThread<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">myThread.join();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">48.
How will you handle InterruptedException in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">In
Java we can get InterruptedException from sleep() or join() methods. Throwing
InterruptedException is way to inform that another thread has interrupted this
thread. In general, the purpose of Interrupt is to ask current thread to stop
its current execution and finish unexpectedly. Therefore ignoring this
exception by catching it and only logging it to the console or some log file is
not the recommended approach. The run() method of the Runnable interface does
not allow that throwing any exceptions. So we cannot re-throw
InterruptedException. Therefore the correct way to handle this exception is
that run() method should check and handle this exception by itself and take
appropriate action.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">49.
Which intrinsic lock is acquired by a synchronized method in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">When
we mark a method as synchronized and then call this method, then this method
will first acquire the intrinsic lock of the object in which that method is
mentioned. Once the synchronized method returns, it releases the lock. In case
the synchronized method throws an exception, the intrinsic lock will be
released.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Sample
code equivalent to a synchronized method is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
void myMethod() {synchronized(this) {}}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">50.
Can we mark a constructor as synchronized in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
We cannot mark a constructor as synchronized. This will lead to compiler error.
The reasoning behind this is that, in this case, only the constructing thread
would have access to the object being constructed.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">51.
Do we have re-entrant property in intrinsic locks?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Yes.
An intrinsic lock can be accessed by the same thread multiple times. So an
Intrinsic lock is re-entrant. If it is not allowed then the code that acquires
a lock would have to avoid acquiring the lock that it has already acquired.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">52.
What is an atomic operation?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">An
atomic operation is an operation that completes in a single step relative to
other threads. An Atomic operation is either executed completely or not at all.
There is no halfway mark in Atomic operation.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">53.
Can we consider the statement i++ as an atomic operation in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
The statement i++ is not an Atomic operation. It has more than one operation.
First JVM loads the current value of i in memory. Then it increments it.
Finally it stores the new value back into variable i. The current thread that
executes this operation may be interrupted between any of the above-mentioned
three steps. Therefore it is not an atomic operation.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">54.
What are the Atomic operations in Java?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Java
language provides some basic Atomic operations. These operations can be used to
make sure that concurrent threads always see the same value.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Some
of these Atomic operations are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Read
operations on reference variables and primitive variables (except long and
double)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Write
operations on reference variables and primitive variables (except long and
double)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Read
operations on all variables declared as volatile<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">Write
operations on all variables declared as volatile<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">55.
Can you check if following code is thread-safe?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
class SingletonDoubleCheck {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">private
SingletonDoubleCheck instance = null;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">public
SingletonDoubleCheck getInstance() {if (instance == null) {synchronized
(SingletonDoubleCheck.class) {if (instance == null) {instance = new
SingletonDoubleCheck();}}}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">return
instance;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">}}
<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">The
above-mentioned code is for creating a Singleton class. But this code is not
thread-safe. In this we check the value of instance second time in the
synchronized block. But the JIT compiler can rearrange the Bytecode in such a
way that the reference to SingletonDoubleCheck instance will be set before the
execution of constructor. Due to this the method getInstance() will return an
object that may not have been initialized properly. We can use the keyword
volatile for instance to make this threadsafe code. Any variables that is
marked as volatile will be visible to other threads only after the completion
of the constructor of the object.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">56.
Can we use primitive values for intrinsic locks?<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<span style="font-size: 14.0pt;">No.
Java does not allow primitive values to be used for intrinsic locks.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-2507132205526422602019-01-30T11:30:00.001+05:302019-05-26T09:17:28.207+05:30Java Collection Interview Questions part- 2<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="margin-left: 36pt; text-align: center;">
<span style="line-height: 107%;"><span style="font-size: large;"><br /></span></span></h2>
<h2 style="margin-left: 36pt; text-align: center;">
<span style="line-height: 107%;"><span style="color: lime; font-size: large;">Java
Collection</span></span></h2>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">1.What are the major differences between a HashSet and a HashMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The main difference between a HashSet and a
HashMap are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l10 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Base class</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
A HashSet class implements the Set interface. Whereas a HashMap class
implements the Map interface.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l10 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Storage</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
A HashSet is used to store distinct objects. A HashMap is used for storing key
& value pairs, so that these can be retrieved by key later on.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l10 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Duplicate Elements</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: A HashSet does not allow storing duplicate
elements. A HashMap also does not allow duplicate keys. But we can store
duplicate values in a HashMap.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l10 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Null Elements</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: In a HashSet we can store a single null
value. In a HashMap we can store single null key, but any number of null
values.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l10 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Element Type</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
A HashSet contains only values of objects as its elements. Whereas a HashMap
contains entries(key value pairs).<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l10 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Iteration</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
By using an Iterator we can iterate a HashSet. But a HashMap has to be
converted into Set for iteration.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">2.What are the similarities between a HashSet and a HashMap in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">As the name suggests, HashSet and HashMap are
Hashing based collections. Similarities between HashSet and HashMap are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Thread Safety</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: Both HashMap and HashSet are not
synchronized collections. Therefore they are not good for thread-safe
operations. To make these thread-safe we need to explicitly use synchronized
versions.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Order of Elements</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: None of these classes guarantee the order of
elements. These are unordered collections.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Internal Implementation</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: A HashMap backs up a HashSet internally. So
HashSet uses a HashMap for performing its operations.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l5 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Performance</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Both of these collections provide constant time performance for basic
operations such as insertion and removal of elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">3. What is the reason for overriding equals() method?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The equals() method in Object class is used to
check whether two objects are same or not. If we want a custom implementation
we can override this method. For example, a Person class has first name, last
name and age. If we want two Person objects to be equal based on name and age,
then we can override equals() method to compare the first name, last name and
age of Person objects. Generally in HashMap implementation, if we want to use
an object as key, then we override equals() method.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">4.How can we synchronize the elements of a List, a Set or a Map?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Sometimes we need to make collections
Thread-safe for use in Multi-threading environment. In Java, Collections class
provides useful static methods to make a List, Set or Map as synchronized
collections. Some of these methods are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static <T> Collection<T>
synchronizedCollection(Collection<T>c)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Returns a synchronized (thread-safe)
collection backed by the specified collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static <T> List<T>
synchronizedList(List<T> list)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Returns a synchronized (thread-safe) list
backed by the specified list.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static <K,V>
Map<K,V>synchronizedMap(Map<K,V> m)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Returns a synchronized (thread-safe) map
backed by the specified map.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static <T> Set<T>
synchronizedSet(Set<T> s)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Returns a synchronized (thread-safe) set
backed by the specified set.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static<K,V>SortedMap<K,V>
synchronizedSortedMap(SortedMap<K,V> m)<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Returns a synchronized (thread-safe) sorted
map backed by the<span style="mso-tab-count: 1;"> </span>specified
sorted map.<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static <T> SortedSet<T>
synchronizedSortedSet(SortedSet<T> s)<span style="mso-tab-count: 1;"> </span><o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Returns a synchronized (thread-safe) sorted
set backed by the specified sorted set.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">5. What is Hash Collision? How Java handles hash-collision in
HashMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In a Hashing scenario, at times two different
objects may have same HashCode but they may not be equal. Therefore, Java will
face issue while storing the two different objects with same HashCode in a
HashMap. This kind of situation is Hash Collision. There are different techniques
of resolving or avoiding Hash Collision. But in HashMap, Java simply replaces
the Object at old Key with new Object in case of Hash Collision.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">6. What are the Hash Collision resolution techniques?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">To resolve a Hash Collision we can use one of
the following techniques:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Separate Chaining
with Linked List<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Separate Chaining
with List Head Cells<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Open Addressing
with Coalesced Hashing<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Open Addressing
with Cuckoo Hashing<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Hopscotch Hashing<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Robinhood Hashing<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">7.What is the difference between Queue and Stack data structures?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Queue is a FIFO data structure. FIFO stands
for First In First Out. It means the element added first will be removed first
from the queue. A real world example of Queue is a line for buying tickets at a
station. The person entering first in the Queue is served first. Stack is a
LIFO data structure. LIFO stands for Last In First Out. The element that is
added last is removed first from the collection. In a Stack elements are added
or removed from the top of stack. A real world example of Stack is back button
in browser. We can go back one by one only and it works in the reverse order of
adding webpages to history .<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">8.What is an Iterator in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Iterator is an interface in Java to access the
elements in a collection. It is in java.util package. It provides methods to
iterate over a Collection class in Java. Iterator interface in Java is based on
Iterator design pattern. By using an Iterator one can traverse a container of
objects and can also access the objects in the container. A container of
objects is a Collection class in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">9. What is the difference between Iterator and Enumeration in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Main differences between Iterator and
Enumeration in Java are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Version</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Enumeration interface is in Java since JDK 1.0. Iterator interface was
introduced in Java 1.2.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">remove() method</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: The main difference between Enumeration and
Iterator interface is remove() method. Enumeration can just traverse a
Collection object. If we use Enumeration, we cannot do any modifications to a
Collection while traversing the collection. Iterator interface provides
remove() method to remove an element while traversing the Collection. There is
not remove() method in Enumeration interface.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Method names</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Names of methods in Iterator interface are hasNext(), next(), remove(). Names
of methods in Enumeration interface are hasMoreElements(), nextElement().<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Legacy Interface</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: Enumeration is considered as a legacy
interface. It is used to traverse legacy classes like Vector, Stack and
HashTable. Iterator is a newer interface that is used to traverse almost all of
the classes in Java Collections framework.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Fail-fast vs. Fail-safe</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: Iterator is based on fail-fast principle. It
throws ConcurrentModificationException if a collection is modified during
iteration over that collection. An Enumeration is based on fail-safe principle.
It doesn’t throw any exception if a collection is modified during traversal.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Safety</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Since Iterator is fail-fast and does not allow modification of a collection by
other threads, it is considered safer than Enumeration.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">10. What is the design pattern used in the implementation of
Enumeration in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Enumeration is based on Iterator design
pattern. Iterator design pattern provides a common interface with methods to
traverse the collection of objects. It hides the underlying implementation
details of the collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">11.Which methods do we need to override to use an object as key in a
HashMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">If we want to use an object as a key in a
HashMap in Java, then we have to make sure that it has the implementation of
equals() and hashCode() methods.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">12.How will you reverse a List in Java? </span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In Collections class, Java provides a method
reverse(List list) that can be used to reverse a List.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">E.g.</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">
Collections.reverse(myList);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">13.How will you convert an array of String objects into a List?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Java provides Arrays class in java.util
package. Arrays class has a method asList() that accepts an Array as input and
returns a List as output.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">public static <T> List<T>
asList(T... a)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">String[] myArray = {"George" ,
"Jack" , "Ryan"};<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">List myList = Arrays.asList(myArray);<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">14.What is the difference between peek(), poll() and remove()
methods of Queue interface in java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In a Java Queue, poll() and remove() methods
can be used for removing the head object of Queue. The main difference arises
in the case when Queue is empty(). If Queue is empty then poll() method returns
null value. If Queue is empty then remove() method throws
NoSuchElementException. In a Java Queue, peek() method retrieves the head of
Queue but it does not remove it. If queue is empty then peek() method returns
null value.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">15. What is the difference between Array and ArrayList in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The main differences between Array and
ArrayList in Java are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Size</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Array in Java is fixed in size. We cannot change the size of array after
creating it. ArrayList is dynamic in size. When we add elements to an
ArrayList, its capacity increases automatically.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Performance</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
In Java Array and ArrayList give different performance for different operations.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">add() or get()</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: Adding an element to or retrieving an
element from an array or ArrayList object has similar performance. These are
constant time operations.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">resize()</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Automatic resize of ArrayList slows down the performance. ArrayList is
internally backed by an Array.<span style="mso-spacerun: yes;"> </span>In
resize() a temporary array is used to copy elements from old array to new
array.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Primitives</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Array can contain both primitive data types as well as objects. But ArrayList
cannot contain primitive data types. It contains only objects.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Iterator</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
In an ArrayList we use an Iterator object to traverse the elements. We use for
loop for iterating elements in an array.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">7.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Type Safety</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Java helps in ensuring Type Safety of elements in an ArrayList by using
Generics. An Array can contain objects of same type of class. If we try to
store a different data type object in an Array then it throws
ArrayStoreException.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">8.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Length</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Size of ArrayList can be obtained by using size() method. Every array object
has length variable that is same as the length/size of the array.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">9.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Adding elements</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: In an ArrayList we can use add() method to
add objects. In an Array assignment operator is used for adding elements. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">10.</span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Multi-dimension</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: An Array can be multi-dimensional. An
ArrayList is always of single dimension.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">16.How will you insert, delete and retrieve elements from a HashMap
collection in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">We use following methods to insert, delete and
retrieve elements in a HashMap.<o:p></o:p></span></div>
<div class="MsoListParagraph" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l9 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Retrieve</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
We use get() method to retrieve elements from a HashMap.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Value get(Object key)<o:p></o:p></span></div>
<div class="MsoListParagraph" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l9 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Insert</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
We use put() method to insert a key value pair in a HashMap.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Value put(Key k, Value v)<o:p></o:p></span></div>
<div class="MsoListParagraph" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l9 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Delete</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
We use remove() method to delete key-value pair from the HashMap.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Value remove(Object key)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">17.What are the main differences between HashMap and
ConcurrentHashMap in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Main differences between HashMap and
ConcurrentHashMap are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l1 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Synchronization</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: A HashMap is not synchronized. But a
ConcurrentHashMap is a synchronized object.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l1 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Null Key</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
A HashMap can have one null key and any number of null values. A
ConcurrentHashMap cannot have null keys or null values.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l1 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Multi-threading</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: A ConcurrentHashMap works well in a
multi-threading environment.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">18.What is the increasing order of performance for following
collection classes in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The increasing order of performance is:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Hashtable<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Collections.SynchronizedMap<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ConcurrentHashMap<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">HashMap<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Hashtable has the
worst performance and HashMap has the best<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">performance.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">19. Why does Map interface not extend Collection interface in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">A Map is a collection objects. But Map
interface is not compatible with Collection interface in Java. A Map requires
key as well as a value. So it requires two parameters to add an element to a
HashMap. But Collection interface provides add(Object o) method with only one
parameter. Map collection has to provide methods like valueSet, keySet etc.
These methods are specific to Map collection. Where as methods in Collection
interface can be reused by a List, Set, Queue etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">20.What are the different ways to iterate elements of a list in
Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">There are mainly two ways to iterate the
elements of list in Java:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l0 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Iterator</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
We can get an Iterator for list and use it to iterate the objects of the list.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l0 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">For-each loop</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: We can use for-each loop to traverse all the
elements of a list.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">21. What is CopyOnWriteArrayList? How it is different from ArrayList
in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">CopyOnWriteArrayList was introduced in Java 5
version. It is a thread-safe collection. It is similar to an ArrayList. In CopyOnWriteArrayList,
all mutative operations (add, set etc.) are implemented by making a fresh copy
of the underlying array. Iterator of CopyOnWriteArrayList is guaranteed to not
throw ConcurrentModificationException. But Iterator also does not reflect any
additions, removals that happened to list after the Iterator was created. All
elements including null are permitted in CopyOnWriteArrayList.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">22.How remove() method is implemented in a HashMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Remove() method in HashMap uses logic similar
to the one used in get() method. First we locate the correct bucket in HashMap
for an entry. Then within that bucket we remove the element e. It is similar to
removing a node from a single-linked list. If e is the first element in the
bucket we set the corresponding element of Hash to e.next. Else we set the next
field of the element just before e to e.next.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">23. What is BlockingQueue in Java Collections?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">BlockingQueue was introduced in Java 1.5. It
extends Queue<span style="mso-spacerun: yes;"> </span>interface in Java.
BlockingQueue supports operations that wait for the queue to become non-empty
when retrieving an element. Also it supports the operations that wait for space
to become available in the queue while storing an element. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Some of the features of BlockingQueue are:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">It does not accept null elements. Its main use
is in producer-consumer<span style="mso-spacerun: yes;"> </span>problems.
BlockingQueue implementation is thread-safe. It can be used in inter-thread
communications. It does not support any kind of "close" or
"shutdown" operation to indicate that no more items will be added.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">24. How is TreeMap class implemented in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Internally, a TreeMap class in Java uses
Red-Black tree. It is a NavigableMap. The map sorts the keys in natural order
or it can use a Comparator supplied at the creation time. The implementation of
TreeMap is not synchronized in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">25. What is the difference between Fail-fast and Fail-safe iterator
in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Differences between Fail-fast and Fail-safe
iterators are as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Fail-fast iterator throws
ConcurrentModificationException. But Fail-safe iterator does not throw this
exception. Fail-fast iterator does not clone the original collection. Fail-safe
iterator creates a copy of the original collection of objects. A Fail-fast
iterator tries to immediately throw Exception when it encounters failure. A
Fail-safe Iterator works on a copy of collection instead of original
collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">26. How does ConcurrentHashMap work in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ConcurrentHashMap extends AbstractMap in Java.
It was introduced in Java 1.5. It provides concurrency in a collection based on
a HashMap. All methods are thread-safe in ConcurrentHashMap. Internally there
is a Hashtable backing a ConcurrentHashMap. This Hashtable supports the
concurrent methods for retrieval of data as well as updates on
ConcurrentHashMap. It has same functional specification as a Hashtable. It also
supports a set of sequential and bulk operations. These operations accept
parallelismThreshold argument.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">27. What is the importance of hashCode() and equals() methods?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In a HashMap collection it is very important
for a key object to implement hashCode() method and equals() method. If
hashCode() method returns same hashcode for all key objects then the hash
collision will be high in HashMap. Also with same hashcode, we will get same
equals method that will make our HashMap inefficient. The problem arises when
HashMap treats both outputs same instead of different. It will overwrite the
most recent key-value pair with the previous key-value pair. So it is important
to implement hashCode() and equals() methods correctly for an efficient HashMap
collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">28. What is the contract of hashCode() and equals() methods in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Contract of hashCode() and equals() methods is
as follows in Java:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">If object1.equals(object2), then
object1.hashCode() == object2.hashCode() should always be true. It means if two
objects are equal then their hashCode should be same. If object1.hashCode() ==
object2.hashCode() is true, it does not guarantee that object1.equals(object2).
It means if two objects have same hashCode, then can still have different
values so that may not be equal objects.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">29. What is an EnumSet in Java?</span></b></h3>
</div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Set</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
EnumSet is a specialized implementation of Set.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Use</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
It is mainly used with enum types.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Single enum type</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: All the elements in an EnumSet must come
from a single enum type when the set is created.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Bit vector</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Internally, EnumSet is represented as bit vector.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Iterator</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
The iterator of EnumSet traverses the elements in their natural order. (It is
the order in which the enum constants are declared).<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Null</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
In an EnumSet, null elements are not permitted. If we try to insert a null
element it throws NullPointerException.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">7.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Thread-safe</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
EnumSet is not a synchronized collection. For use in multi-threading scenarios,
EnumSet should be synchronized.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l7 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">8.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Bit flags</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
EnumSet is a very good alternative to int based “bit flags” implementation.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">30. What are the main Concurrent Collection classes in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Java 1.5 has provided new package
java.util.concurrent. This package contains thread-safe collection classed.
These collection classes can be modified while iterating. The iterator of these
classes is fail-safe.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Main Concurrent Collection classes in Java 8
are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ArrayBlockingQueue<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">CopyOnWriteArrayList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">CopyOnWriteArraySet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ConcurrentHashMap<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ConcurrentLinkedDeque<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ConcurrentLinkedQueue<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">LinkedBlockingQueue<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">LinkedBlockingDeque<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">PriorityBlockingQueue<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">31.How will you convert a Collection to SynchronizedCollection in
Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Java provides an easy method in
java.utils.Collections class to create a ThreadSafe collection from a regular
collection. We can use the method synchronizedCollection() for this purpose.
For any class of type T we can use following method:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">static <T> Collection<T> synchronizedCollection(Collection<T>
c)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">32.How IdentityHashMap is different from a regular Map in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">IndentityHashMap in Java implements Map
interface. But it is not a general purpose implementation. It violates the
general contract of Map interface by a different implementation of equals()
method. In an IdentityHashMap, two keys k1 and k2 are equal if and only if
(k1==k2). (In a normal Map implementation (like HashMap) two keys k1 and k2 are
considered equal if and only if (k1==null ? k2==null : k1.equals(k2)).) It
implements the Map interface with a hash table, using referenceequality in
place of object-equality when comparing keys (and values).<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">33.What is the main use of IdentityHashMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Main uses of IdentityHashMap are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l11 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Topology Preservation</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: The typical use of IdentityHashMap class is
topology-preserving object graph transformations, such as serialization or
deepcopying. In such a scenario, a program must maintain a "node
table" to keep track of all the object references that have already been
processed. <span style="mso-spacerun: yes;"> </span><o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l11 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The node table should not considered distinct
objects as equal even if they happen to be equal. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l11 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Proxy objects</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">: Another use of this class is to maintain
proxy objects. A debugging program has to maintain a proxy object for each
object in the program being debugged.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">34.How can we improve the performance of IdentityHashMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">IdentityHashMap class has one tuning parameter
for performance<span style="mso-spacerun: yes;"> </span>improvement: <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">expectedMaxSize. This parameter is the maximum
number of key-value mappings that the map is expected to hold. We can use this
parameter is used to determine the number of buckets initially in the hash
table. The precise relationship between the expected maximum size and the
number of buckets is unspecified. If the number of key-value mappings exceeds
the expected maximum size, the number of buckets is increased. Increasing the
number of buckets is also known as rehashing. Rehashing may be fairly
expensive. So it is better to create identity hash maps with a sufficiently
large expected maximum size. But iteration over a Map collection requires time
proportional to the number of buckets in the hash table. So iteration may take
extra time due to large number of buckets. Therefore the value of
expectedMaxSize should be set in consideration with both of these aspects.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">35. Is IdentityHashMap threadsafe?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The implementation of IdentityHashMap is not
thread-safe, since its methods are not synchronized. The iterators returned by
the iterator method of IdentityHashMap are fail-fast. But the fail-fast
behavior of an iterator cannot be guaranteed. Since the Iterator is fail-fast,
it throws ConcurrentModificationException.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">36.What is a WeakHashMap in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">WeakHashMap is a class similar to
IdentityHashMap. Internally, it is represented by a Hashtable. It is not a
synchronized class. We can make a WeakHashMap thread safe by using
Collections.synchronizedMap() method. An entry in WeakHashMap is automatically
removed when it is no longer in ordinary use. The presence of a mapping for a
given key does not prevent the key from being discarded by the garbage
collector. WeakHashMap also permits null keys and null values.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">37.How can you make a Collection class read Only in Java? </span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In Java, there are useful methods to make a
Collection class read Only. We can make the Collection read Only by using one
of the following methods:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Collections.unmodifiableMap(Map
m)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Collections.unmodifiableList(List
l)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Collections.unmodifiableSet(Set
s)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Collections.unmodifiableCollection(Collection
c)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">38.When is UnsupportedOperationException thrown in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In a Java collection
UnsupportedOperationException is thrown when the requested operation is not
supported by the collection. It is an unchecked exception that is thrown on
optional operations. If there is an optional add() or remove() methods in a
read only collection, then this exception can be thrown.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">39.What is the difference between Synchronized Collection and
Concurrent Collection?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In Java 1.5 many Concurrent collection classes
were added in SDK. These are ConcurrentHashMap, CopyOnWriteArrayList,
BlockingQueue etc. Java also provides utility methods to get a synchronized
copy of collection like ArrayList, HashMap etc. by using<span style="mso-spacerun: yes;"> </span>Collections.synchronizedList(),
Collections.synchronizedMap() methods. The main difference is in performance.
Concurrent collection classes have better performance than synchronized
collection classes because they lock only a portion of the class to achieve
concurrency and thread-safety.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">40. What is the scenario to use ConcurrentHashMap in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ConcurrentHashMap is more suited for scenarios
where we have multiple reader threads and one writer thread. In this case map
is locked only during the write operation. If we have an equal number of reader
and writer threads then ConcurrentHashMap performance is similar to a Hashtable
or a synchronized HashMap.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">41.How will you create an empty Map in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">There are two ways to create an empty Map in
Java.<o:p></o:p></span></div>
<div class="MsoListParagraph" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l4 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Immutable</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
If we want an immutable empty Map, we can use following code:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">myMap = Collections.emptyMap();<o:p></o:p></span></div>
<div class="MsoListParagraph" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l4 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Any map</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
For all other scenarios, we can use following code by using new method:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">myMap = new HashMap();<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">42. What is the difference between remove() method of Collection and
remove() method of Iterator?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In Collection interface remove(Object o)
method is used to remove objects from a Collection. List interface also
provides remove(int index) method to remove an object at a specific index.
These methods are used to remove an entry from Collection, while no thread is
iterating over it. When we are iterating over a Collection, then we have to
remove() method of Iterator. This method removes current element from
Iterator’s point of view. If we use remove(0 method of Collection or List, then
we will get ConcurrentModificationException. Therefore, it is recommended to
use remove() method of Iterator during the traversal of a Collection by an
Iterator.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">43. Between an Array and ArrayList, which one is the preferred
collection for storing objects?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">An ArrayList is backed up by array internally.
There are many usability advantages of using an ArrayList over an array in
Java. Array has a fixed length at the time of creation. Once it is created we
cannot change its length. ArrayList is dynamic in size. Once it reaches a
threshold, it automatically allocates a new array and copies contents of old
array to new array. Also ArrayList provides support of Generics. But Array does
not support Generics. E.g. If we store an Integer object in a String array at
Runtime it will throw ArrayStoreException. Whereas, if we use ArrayList then as
compile time we will get the error. This helps in preventing errors from
happening at runtime. If we know the size in advance and do not need re-sizing
the collection then Array should be used in place of an ArrayList. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">44. Is it possible to replace Hashtable with ConcurrentHashMap in
Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Yes, a ConcurrentHashMap can be replaced with
Hashtable in Java. But it requires careful observation, since locking behavior
of Hashtable is different than that of ConcurrentHashmap. A Hashtable locks
whole Map instead of a portion of Map. Compound operations like
if(Hashtable.get(key) == null) put(key, value) work in Hashtable but not in
ConcurrentHashMap. In a ConcurrentHashMap we use putIfAbsent() method for such
a scenario.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">45. How CopyOnWriteArrayList class is different from ArrayList and
Vector classes?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">CopyOnWriteArrayList was introduced in Java
1.5. It implements List interface. It provides better concurrent access methods
than a Synchronized List. In CopyOnWriteList, concurrency is achieved by
copying ArrayList over each write and replace with original instead of locking.
CopyOnWriteArrayList also does not throw any ConcurrentModification Exception
during Iteration. It is a thread-safe list. It is different from a Vector in
terms of Concurrency. CopyOnWriteArrayList provides better Concurrency by
reducing contention among readers and writers.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">46. Why ListIterator has add() method but Iterator does not have?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">ListIterator can iterate in the both directions
of a Collection. It maintains two pointer for previous and next element. In
ListIterator we can use add() method to add an element into the list
immediately before the element returned by next() method. So a subsequent call
to next() method will not be affected. And the call to previous() method will
return the newly added element. In Iterator we can only traverse in one
direction. So there is no purpose of add() method there.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">47. Why do we sometime get ConcurrentModificationException during
iteration?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">When we remove an object by using remove()
method of a<span style="mso-spacerun: yes;"> </span>Collection or List while an
Iterator thread is traversing it, we get ConcurrentModificationException. If an
Iterator detects any structural change in Collection it can throw
ConcurrentModificationException. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">48. How will you convert a Map to a List in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In Java, a Map has three collection sets:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l12 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">key set<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l12 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">value set<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 72.0pt; mso-add-space: auto; mso-list: l12 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt; line-height: 107%;"><span style="mso-list: Ignore;">·<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">key-value set<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Each of these Sets can be converted to List by
using a constructor.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Sample code is as follows:<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">List keyList = new ArrayList(map.keySet());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 72.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">List
valueList = new ArrayList(map.values());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">List entryList = new
ArrayList(map.entrySet());<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">49. How can we pass a Collection as an argument to a method and
ensure that method will not be able to modify it?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">To ensure that a method is not able to modify
a Collection passed as an argument, we have to make the Collection read only.
We can make a read only collection by using
Collections.unmodifiableCollection(Collection c) method. This will make sure
that any operation to change the collection will throw
UnsupportedOperationException.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">50. Can you explain how HashMap works in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">In Java, a HashMap works on the concept of
hashing. A HashMap in Java stores both key and value objects, in a bucket. It
is stored as an Entry object that implements Map.Entry interface. The key
object used in a HashMap has to provide implementation for hashCode() and
equals() methods. When put() method is used to store a key-value pair, the
HashMap implementation calls hashCode() method on Key object to calculate a
hash that is used to find a bucket where Entry object will be stored. When
get() method is used to retrieve a value stored against a key object, we first
calculate a hash of Key object. Then we use this hash to find the bucket in
which that particular key is stored. Once Key object’s location is found, it
may happen that more than one Key is stored in same location. So now we use
equals() method to find the exact Key object. Once the exact Key object is
found we use it to get Value object.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">51. Can you explain how HashSet is implemented in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Internally, a HashSet uses a HashMap to store
the elements and to maintain the uniqueness of elements. When we create a
HashSet object, a corresponding HashMap object is also created. When we insert
an element in HashSet, it inserts it into corresponding HashMap.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">52. What is a NavigableMap in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">As
the name suggests, NavigableMap provides the capability to navigate the keys of
a Map in Java. A NavigableMap extends SortedMap interface. Some of the
interesting methods of a NavigableMap are descendingKeySet(), descendingMap(),
headMap() and tailMap().<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">53. What is the difference between descendingKeySet() and
descendingMap() methods of NavigableMap?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">The descendingKeySet() method of NavigableMap
returns a NavigableSet in which the elements are stored in reversed order as
compared to the original key set. The returned view is internally represented
by the original KeySet of NavigableMap. Therefore any changes to the descending
set also get reflected in the original set. But it is not recommended to remove
elements directly from the key set. We should use the Map.remove() method. The
descendingMap() method of NavigableMap returns a NavigableMap which is an
inverse view of the original Map. The order of the elements in this view are in
reverse order of the elements in original map. Any changes to this view are
also reflected in the original map.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt; line-height: 107%;">54. Let say there is a Customer<span style="mso-spacerun: yes;">
</span>class. We add objects of Customer class to an ArrayList. How can we sort
the Customer objects in ArrayList by using customer firstName attribute of
Customer class?</span></b></h3>
</div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">There are two ways to handle this scenario. <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">We can use these options:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l2 level1 lfo15; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Comparable</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Implement the Comparable interface for Customer class and compare customer objects
by firstName attribute. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="margin-left: 90.0pt; mso-add-space: auto; mso-list: l2 level1 lfo15; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";"> </span></span></span><!--[endif]--><b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">Comparator</span></b><span style="font-size: 14.0pt; line-height: 107%; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">:
Implement Comparator for comparing two Customer objects on the basis of
firstName attribute. Then use this comparator object in sort method of
Collections class.<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-45853131618076645492019-01-29T10:30:00.000+05:302019-05-26T08:55:52.817+05:30Java Collection Interview Questions in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal; margin-left: 36pt; text-align: center;">
<h2>
<b><span style="color: #5b9bd5; font-size: 18.0pt;">Java Collection</span></b></h2>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="font-family: "calibri" , sans-serif; font-size: 14pt;"> In this Java
Interview Question blog, we have covered almost 25+ important core Java
interview questions and answar for freshers and experienced candidates.</span><span style="font-family: "calibri" , sans-serif; font-size: 11pt;"><o:p></o:p></span></div>
<div style="margin: 0cm 0cm 0.0001pt 36pt;">
<span style="font-family: "calibri" , sans-serif; font-size: 14pt;"><br />
This post on <b>JAVA Collection
Interview Questions</b> and Answars is prepared to help you understand the
basic concepts of Java programming for top compnyes interview purposes.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<h3 style="text-align: left;">
<span style="font-family: "calibri" , sans-serif; font-size: 14pt;"> <a href="https://thegeeksarea.blogspot.com/2019/01/java-collection-interview-questions-2.html"><b>Java Collection Interview Question Part-2</b></a></span></h3>
</div>
<div style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<h2 style="text-align: left;">
<b><span style="color: #92d050; font-family: "calibri" , sans-serif; font-size: 16.0pt;">Most Popular Java Collection Interview Questions:</span></b></h2>
</div>
<div style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm; text-indent: 36.0pt;">
<span style="font-family: "calibri" , sans-serif; font-size: 14pt;">Given below is a
comprehensive list of most important and commonly asked basic and advanced Java
programming interview questions with detailed answers.<o:p></o:p></span></div>
<div style="margin-bottom: .0001pt; margin-bottom: 0cm; margin-left: 36.0pt; margin-right: 0cm; margin-top: 0cm;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">1. What is the difference between Collection
and Collections Framework in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, a Collection is an object that
contains multiple elements of same type in a single unit. These multiple
elements can be accessed through one Collection object. In Java Collections
Framework is a library that provides common architecture for creating, updating
and accessing different types of collections. In Collections framework there
are common methods that are frequently used by developers for working on a
Collection object.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">2. What are the main benefits of Collections
Framework in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main benefits of Collections Framework
in Java are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l18 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Reusability</span></b><span style="font-size: 14.0pt;">: Java Collections Framework provides common classes
and utility methods than can be used with different types of collections. This
promotes the reusability of the code. A developer does not have to re-invent
the wheel by writing the same method again.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l18 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Quality</span></b><span style="font-size: 14.0pt;">: Using Java Collection Framework improves the program
quality, since the code is already tested and used by thousands of developers.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l18 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Speed</span></b><span style="font-size: 14.0pt;">: Most of programmers report that their development
speed increased since they can focus on core logic and use the generic
collections provided by Java framework.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l18 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Maintenance</span></b><span style="font-size: 14.0pt;">: Since most of the Java Collections framework code is
open source and API documents is widely available, it is easy to maintain the
code written with the help of Java Collections framework. One developer can
easily pick the code of previous developer.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">3. What is the root interface of Collection
hierarchy in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The root interface of Collection
hierarchy in Java is Collection interface. But the Collection interface extends
Iterable interface. Due to this some people consider Iterable interface as the
root interface. Iterable interface is present in java.lang package but
Collection interface is present in java.util package. Oracle Java API docs
mention that Collection interface is a member of the Java Collections
framework. Whereas, Iterable interface is not stated as a part of Java
Collections framework in Java docs. Due to this Collection interface is the
root of Collections Framework.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">4. What are the main differences between
Collection and Collections?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between Collection and
Collections are as follows:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l9 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Collection is an
interface in Java. But Collections is a class in Java.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l9 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Collection is a
base interface. Collections is a utility class in Java.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l9 level1 lfo2; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Collection
defines methods that are used for data structures that contain the objects.
Collections defines the methods that are used for operations like access, find
etc. on a Collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">5. What are the Thread-safe classes in Java
Collections framework?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">The Thread-safe classes in Java
Collections framework are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Stack<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Properties<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Vector<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Hashtable<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">BlockingQueue<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">ConcurrentMap<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l16 level1 lfo3; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">ConcurrentNavigableMap
<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">6. How will you efficiently remove elements
while iterating a Collection?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The right way to remove elements from a
collection while iterating is by using ListIterator.remove() method. <b>E.g</b>.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">ListIterator<Integer> iter = myList.iterator();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">while(iter.hasNext()) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">itr.remove();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">} <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Some developers use following code to
remove an element which is incorrect:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">Iterator<Integer> iter = myList.iterator();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">while(iter.hasNext()) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">itr.remove();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">} <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">By doing so we get
ConcurrentModificationException.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">An iterator is first created to traverse
the list. But at the same time the list is changed by remove() method. In Java,
it is not allowed for a thread to modify a collection while another thread is
iterating it. ListIterator provides the capability of removing an object during
traversal.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">7. What is the advantage of NavigableMap
over Map?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The main advantage of NavigableMap over
Map is the Navigation capability. It provides the capabilities of a Map,
SortedMap and navigation in one collection. It even returns the closest matches
for given search targets. Methods like lowerEntry, floorEntry, ceilingEntry,
and higherEntry return Map.Entry objects associated with keys respectively less
than, less than or equal, greater than or equal, and greater than a given key.
Methods like lowerKey, floorKey, ceilingKey, and higherKey return only the
associated keys. All of these methods are designed for locating, not traversing
entries.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">8. What is the difference between headMap(),
tailMap() and subMap() methods of NavigableMap?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The headMap() method returns a view of
the original NavigableMap that contains the elements that are less than a given
element.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">NavigableMap original = new TreeMap();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">original.put("1", "1");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">original.put("2", "2");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">original.put("3", "3");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">//this headmap1 will contain elements "1"
and "2"<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">SortedMap headmap1 = original.headMap("3");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">//this headmap2 will contain elements "1",
"2", and "3" because "inclusive"=true<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">NavigableMap headmap2 =
original.headMap("3", true);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The tailMap() method works similar to
headMap() method, but it returns all elements that are higher than the given
input element. The subMap() method accepts two parameters demarcating the
boundaries of the view map to return. All the three methods return a subset of
the original map in a view form.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">9. How will you sort objects by Natural
order in a Java List?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can use Collections.sort method to
sort the elements of a List in natural order. To use this method, we have to
make sure that element objects implement compareTo() method. We can also use a
Comparator to define the natural ordering for elements of a List. Then we can
use this Custom Comparator in sort method of Collections class.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">10. How can we get a Stream from a List in
Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">From Java 8 onwards it is a very easy to
get a Stream from a List. We can just use stream() method to get a stream from
a list of elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">11. Can we get a Map from a Stream in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Yes, we can create a Map from the
elements of a Stream. We can use map() method to get a Map.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<b><span style="font-size: 14.0pt;">E.g.</span></b><span style="font-size: 14.0pt;">
items.stream().map( item -> item.toLowerCase() )<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In this example we are creating a map
with each item object mapped to its LowerCase equivalent. This is also used in
Map-Reduce implementation on a Stream.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">12. What are the popular implementations of
Deque in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">The two most popular implementation of
Deque interface in Java are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l2 level1 lfo19; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">ArrayDeque</span></b><span style="font-size: 14.0pt;">: It is a resizable array implementation of Deque. The
capacity of ArrayDeque can increase based on the need of the program. It is not
thread safe implementation. Also the iterator on ArrayDeque is failfast.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l2 level1 lfo19; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">LinkedList</span></b><span style="font-size: 14.0pt;">: This is another popular implementation of Deque
interface in Java. It is also not synchronized, so it is not thread-safe. It
mainly provides functionality of a doubly linked list.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">13.How will you convert a List into an array
of integers like- int[]?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can use ArrayUtils class in Apache
Commons Lang library.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sample code is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">int[]intArray =
ArrayUtils.toPrimitive(myList.toArray(new Integer[0]));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">If we use List.toArray(), it will
convert List to Integer[].<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Another option is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">int[] intArray = new int[myList.size()];<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">for (int i=0; i < myList.size(); i++) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">intArray [i] = myList.get(i);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">14.How will you convert an array of
primitive integers int[] to a List collection?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can use ArrayUtils in Apache Commons
Lang library for this purpose.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sample code is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">List intList =
Arrays.asList(ArrayUtils.toObject(intArray));<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The other option would be to use a for
loop and explicitly adding integers to a List.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sample code is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">int[]intArray = {10,20,30};<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">List<Integer> intList = new
ArrayList<Integer>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">for (int i: intArray) {<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">intList.add(i);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">15.How will you run a filter on a
Collection?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can use CollectionUtils of Apache for
this purpose. We will have to create a Predicate that will define the condition
for our filter. Then we can apply this Predicate in filter() method.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sample code is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In this example we filter any names that
are less than 5 characters long.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">List<String> namesList = asList(
"Red", "Blue", "Green" );<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">List<String> shortNamesList = new
ArrayList<String>();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">shortNamesList.addAll( namesList );<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">CollectionUtils.filter( shortNamesList, new
Predicate(){<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">public boolean evaluate( Object input )
{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 144.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">return ((String) input).length() < 5;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 108.0pt;">
<span style="font-size: 14.0pt;">} );<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">We can also use Google Guava library for
this. In Java 8, we can use Predicate to filter a Collection through Stream.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">16.How will you convert a List to a Set?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are two ways to convert a List to
a Set in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">Option 1</span></b><span style="font-size: 14.0pt;">: Use HashSet<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Set<Integer> mySet = new
HashSet<Integer>(myList);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In this case we put a list into a
HashSet. Internally hashCode() method is used to identify duplicate elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">Option 2</span></b><span style="font-size: 14.0pt;">: Use TreeSet In this case we use our own comparator
to find duplicate objects.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Set<Integer> mySet = new
TreeSet<Integer>(myComparator);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">mySet.addAll(myList);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">17. How will you remove duplicate elements
from an ArrayList?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The trick in this question is to use a
collection that does not allow duplicate elements. So we use a Set for this
purpose.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">Option 1</span></b><span style="font-size: 14.0pt;">: Use Set If ordering of elements is not important
then we just put the elements of ArrayList in a HashSet and then add them back
to the ArrayList.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sample Code is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">ArrayList myList = // ArrayList with
duplicate elements<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Set<Integer> mySet = new
HashSet<Integer>(myList);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">myList.clear();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">myList.addAll(mySet);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">Option 2</span></b><span style="font-size: 14.0pt;">: Use LinkedHashSet If ordering of elements is
important then we put the elements of ArrayList in a LinkedHashSet and then add
them back to the ArrayList.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Sample Code is:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">ArrayList myList = // ArrayList with
duplicate elements<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Set<Integer> mySet = new
LinkedHashSet<Integer>(myList);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">myList.clear();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">myList.addAll(mySet);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">18. How can we create a Map with reverse
view and lookup in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In a Map we can lookup for a value by
using a distinct key. In a Map with reverse view and lookup, even the values
are distinct. So there is one to one mapping between keys and values and vice
version. If we enable this constraint on a Map then we can look up a key by its
value. Such data structure is called bi-directional map. There is no built data
structure similar to reverse lookup Map in JDK. But Apache Common Collections
and Guava libraries provide implementation of bidirectional map. It is called
BidiMap and BiMap. Both of these data structure enforce the constraint of one
to one mapping between keys and values.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">19. How will you create a shallow copy of a
Map?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, most implementations of Map
interface provide a constructor to create copy of another map. But the copy
method is not synchronized. Therefore, when a thread is copying the map, another
thread can modify it. To prevent such a scenario, we should use
Collections.synchronizedMap() method to first create a thread-safe map. Another
way of to create a shallow copy is by using clone() method. But it is not
considered as a recommended approach.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">20.Why we cannot create a generic array in
Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Java does not allow creation of array
with generics as elements. In Java an array has to know the type information of
its elements at runtime. This information is used at runtime to throw
ArrayStoreException if data type of an element to be inserted does not match
the type of Array. In case of Generics, the type information of a collection is
erased at runtime by Type Erasure. Due to this array cannot use generics as
elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">21. What is a PriorityQueue in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A PriorityQueue is data structure based
on Queue. Unlike Queue, the elements on PriorityQueue are not returned in FIFO
order. A PriorityQueue maintains the natural order of its elements or it uses a
Comparator provided at initialization. It is an unbounded queue based on a
priority heap. PriorityQueue does not allow null values. We cannot add any
object that does not provide natural ordering to PriorityQueue. PriorityQueue
in Java is not thread-safe. It gives O(log n) time for enqueing and dequeing
operations.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">22. What are the important points to remember
while using Java Collections Framework?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Some of the important points to remember
while using Java Collections Framework are:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l11 level1 lfo18; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Interfaces</span></b><span style="font-size: 14.0pt;">: For Collections, we should write code with generic
interfaces instead of concrete implementation. Due to this we maintain the
flexibility of changing the implementation at a later point of time.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l11 level1 lfo18; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Generics</span></b><span style="font-size: 14.0pt;">: We should use Generics for type-safety and to avoid
ClassCastException at runtime.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l11 level1 lfo18; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Collections</span></b><span style="font-size: 14.0pt;">: It is recommended to use Collections utility class
for algorithms and various other common methods for Collections.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l11 level1 lfo18; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Right Type</span></b><span style="font-size: 14.0pt;">: We have to choose the right type of Java collection
based on our need. If size is fixed, we can use Array over ArrayList. If we do
not want duplicate elements we use Set. If we need the ability to iterate the
elements of a Map in the order of insertion then we use a TreeMap.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l11 level1 lfo18; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Initial Size</span></b><span style="font-size: 14.0pt;">: In some collection classes we can specify the
initial size/capacity. Therefore we should have an estimate of number of
elements in a Collection before deciding the right collection type. We can use
it to avoid rehashing or resizing.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l11 level1 lfo18; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Map</span></b><span style="font-size: 14.0pt;">: We should use immutable classes provided by Java as
key elements in a Map.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">23.How can you maintain a Collection with
elements in Sorted order?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, there are many ways to maintain
a Collection with elements in sorted order. Some collections like TreeSet store
elements in the natural ordering. In case of natural ordering we have to
implement Comparable interface for comparing the elements. We can also maintain
custom ordering by providing a custom Comparator to a Collection. Another
option is to use the utility method Collections.sort() to sort a List. This
sorting gives nlog(n) order of performance. But if we have to use this method
multiple times then it will be costly on performance. Another option is to use
a PriorityQueue that provides an ordered queue. The main difference between
PriorityQueue and Collections.sort() is that PriorityQueue maintains a queue in
Order all the time, but we can only retrieve head element from queue. We cannot
access the elements of PriorityQueue in Random order. We can use TreeSet to
maintain sorted order of elements in collection if there are no duplicate
elements in collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">24.What are the differences between the two
data structures: a Vector and an ArrayList?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">An ArrayList is a newer class than a
Vector. A Vector is considered a legacy class in Java. The differences are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l17 level1 lfo17; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Synchronization</span></b><span style="font-size: 14.0pt;">: Vector is synchronized, but the ArrayList is not
synchronized. So an ArrayList has faster operations than a Vector.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l17 level1 lfo17; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Data Growth</span></b><span style="font-size: 14.0pt;">: Internally both an ArrayList and Vector use an array
to store data. When an ArrayList is almost full it increases its size by 50% of
the array size. Whereas a Vector increases it by doubling the underlying array size.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">25.What are the differences between
Collection and Collections in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between Collection and
Collections are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l4 level1 lfo16; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Type</span></b><span style="font-size: 14.0pt;">: Collection is an interface in Java. Collections is a
class.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l4 level1 lfo16; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Features</span></b><span style="font-size: 14.0pt;">: Collection interface provides basic features of data
structure to List, Set and Queue interfaces. Collections is a utility class to
sort and synchronize collection elements. It has polymorphic algorithms to
operate on collections.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l4 level1 lfo16; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Method Type</span></b><span style="font-size: 14.0pt;">: Most of the methods in Collection are at instance
level. Collections class has mainly static methods that can work on an instance
of Collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">26. In which scenario, LinkedList is better
than ArrayList in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">ArrayList is more popular than
LinkedList in Java due to its ease of use and random access to elements
feature. But LinkedList is better in the scenario when we do not need random
access to elements or there are a lot of insertion, deletion of elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">27.What are the differences between a List
and Set collection in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between a List and a
Set are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l12 level1 lfo15; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Order</span></b><span style="font-size: 14.0pt;">: List collection is an ordered sequence of elements.
A Set is just a distinct collection of elements that is unordered.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l12 level1 lfo15; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Positional
Access</span></b><span style="font-size: 14.0pt;">: When we use a List, we can
specify where exactly we want to insert an element. In a Set there is no order,
so we can insert element anywhere without worrying about order.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l12 level1 lfo15; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Duplicate</span></b><span style="font-size: 14.0pt;">: In a List we can store duplicate elements. A Set can
hold only unique elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">28.What are the differences between a
HashSet and TreeSet collection in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between a HashSet and
TreeSet are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Ordering</span></b><span style="font-size: 14.0pt;">: In a HashSet elements are stored in a random order.
In a TreeSet, elements are stored according to natural ordering.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Null Value
Element</span></b><span style="font-size: 14.0pt;">: We can store null value
object in a HashSet. A TreeSet does not allow to add a null value object.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Performance</span></b><span style="font-size: 14.0pt;">: HashSet performs basic operations like add(),
remove(), contains(), size() etc in a constant size time. A TreeSet performs
these operations at the order of log(n) time.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Speed</span></b><span style="font-size: 14.0pt;">: A HashSet is better than a TreeSet in performance
for most of operations like add(), remove(), contains(), size() etc .<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Internal
Structure</span></b><span style="font-size: 14.0pt;">: a HashMap in Java
internally backs a HashSet. A NavigableMap backs a TreeSet internally.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Features</span></b><span style="font-size: 14.0pt;">: A TreeSet has more features compared to a HashSet.
It has methods like pollFirst(), pollLast(), first(), last(), ceiling(),
lower() etc.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l13 level1 lfo14; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">7.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Element
Comparison</span></b><span style="font-size: 14.0pt;">: A HashSet uses equals()
method for comparison. A TreeSet uses compareTo() method for comparison to
maintain ordering of elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">29. In Java, how will you decide when to use
a List, Set or a Map collection?</span></b></h3>
</div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l5 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">If we want a
Collection that does not store duplicate values, then we use a Set based collection.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l5 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">If we want to
frequently access elements operations based on an index value then we use a
List based collection. E.g. ArrayList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l5 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">If we want to
maintain the insertion order of elements in a collection then we use a List
based collection.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l5 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">For fast search
operation based on a key, value pair, we use a HashMap based collection.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l5 level1 lfo13; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">If we want to
maintain the elements in a sorted order, then we use a TreeSet based
collection.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">30.What are the differences between a
HashMap and a Hashtable in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Main differences between a HashMap and a
Hashtable are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l10 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Synchronization</span></b><span style="font-size: 14.0pt;">: HashMap is not a synchronized collection. If it is
used in multi-thread environment, it may not provide thread safety. A Hashtable
is a synchronized collection. Not more than one thread can access a Hashtable
at a given moment of time. The thread that works on Hashtable acquires a lock
on it and it makes other threads wait till its work is completed.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l10 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Null values</span></b><span style="font-size: 14.0pt;">: A HashMap allows only one null key and any number of
null values. A Hashtable does not allow null keys and null values.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l10 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Ordering</span></b><span style="font-size: 14.0pt;">: A HashMap implementation by LinkedHashMap maintains
the insertion order of elements. A TreeMap sorts the mappings based on the
ascending order of keys. On the other hand, a Hashtable does not provide
guarantee of any kind of order of elements. It does not maintain the mappings
of key values in any specific order.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l10 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Legacy</span></b><span style="font-size: 14.0pt;">: Hashtable was not the initial part of collection
framework in Java. It has been made a collection framework member, after being
retrofitted to implement the Map interface. A HashMap implements Map interface
and is a part of collection framework since the beginning.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 90.0pt; mso-add-space: auto; mso-list: l10 level1 lfo12; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Iterator</span></b><span style="font-size: 14.0pt;">: The Iterator of HashMap is a fail-fast and it throws
ConcurrentModificationException if any other Thread modifies the map by
inserting or removing any element except iterator’s own remove() method.
Enumerator of the Hashtable is not fail-fast.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">31. What are the differences between
Comparable and Comparator?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between Comparable and
Comparator are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Type</span></b><span style="font-size: 14.0pt;">: Comparable<T> is an interface in Java where T
is the type of objects that this object may be compared to.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Comparator<T>
is also an interface where T is the type of objects that may be compared by
this comparator.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Sorting</span></b><span style="font-size: 14.0pt;">: In Comparable, we can only create one sort sequence.
In Comparator we can create multiple sort sequences.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Method Used</span></b><span style="font-size: 14.0pt;">: Comparator<T> interface in Java has method
public int compare (Object o1, Object o2) that returns a negative integer,
zero, or a positive integer when the object o1 is less than, equal to, or
greater than the object o2. A Comparable<T> interface has method public
int compareTo(Object o) that returns a negative integer, zero, or a positive
integer when this object is less than, equal to, or greater than the object o.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Objects for
Comparison</span></b><span style="font-size: 14.0pt;">: The Comparator compares
two objects given to it as input. Comparable interface compares
"this" reference with the object given as input. <o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l8 level1 lfo11; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Package
location</span></b><span style="font-size: 14.0pt;">: Comparable interface in
Java is defined in java.lang package. Comparator interface in Java is defined
in java.util package.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">32.In Java, what is the purpose of
Properties file?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A Properties file in Java is a list of
key-value pairs that can be parsed by java.util.Properties class. Generally a
Properties file has extension .properties <b>e.g.</b> myapp.properties.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Properties files are used for many
purposes in all kinds of Java applications. Some of the uses are to store
configuration, initial data, application options etc. When we change the value
of a key in a properties file, there is no need to recompile the Java application.
So it provides benefit of changing values at runtime.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">33.What is the reason for overriding
equals() method?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The equals() method in Object class is
used to check whether two objects are same or not. If we want a custom
implementation we can override this method. For example, a Person class has
first name, last name and age. If we want two Person objects to be equal based
on name and age, then we can override equals() method to compare the first
name, last name and age of Person objects. Generally in HashMap implementation,
if we want to use an object as key, then we override equals() method.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">34.How does hashCode() method work in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Object class in Java has hashCode()
method. This method returns a hash code value, which is an integer. The
hashCode() is a native method and its implementation is not pure Java. Java
doesn't generate hashCode(). However, Object generates a HashCode based on the
memory address of the instance of the object. If two objects are same then
their hashCode() is also same.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">35.Is it a good idea to use Generics in
collections?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Yes. A collection is a group of elements
put together in an order or based on a property. Often the type of element can
vary. But the properties and behavior of a Collection remains same. Therefore
it is good to create a Collection with Generics so that it is type-safe and it
can be used with wide variety of elements.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">36.What is the difference between
Collections.emptyList() and creating new instance of Collection?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In both the approaches, we get an empty
list. But Collections.emptyList() returns an Immutable list. We cannot add new
elements to an Immutable empty list. Collections.emptyList() works like
Singleton pattern. It does not create a new instance of List. It reuses an
existing empty list instance. Therefore, Collections.emptylist() gives better
performance if we need to get an emptyList multiple times.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">37.How will you copy elements from a Source
List to another list?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">There are two options to copy a Source
List to another list.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">Option 1</span></b><span style="font-size: 14.0pt;">: Use ArrayList constructor<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">ArrayList<Integer> newList = new
ArrayList<Integer>(sourceList);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<b><span style="font-size: 14.0pt;">Option 2</span></b><span style="font-size: 14.0pt;">: Use Collection.copy() To use Collections.copy()
destination list should be of same or larger size than source list.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">ArrayList<Integer> newList = new
ArrayList<Integer>(sourceList.size());<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Collections.copy(newList, sourceList);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Collections.copy() does not reallocate
the capacity of destination List if it does not have enough space to contain
all elements of source List. It throws IndexOutOfBoundsException. The benefit
of Collection.copy() is that it guarantees that the copy will happen in linear
time. It is also good for the scenario when we want to reuse an array instead
of allocating more memory in the constructor of ArrayList. One limitation of
Collections.copy() is that it can accept only List as source and destination
parameters.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">38. What are the Java Collection classes
that implement List interface?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Java classes that implement List
interface are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">AbstractList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">AbstractSequentialList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">ArrayList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">AttributeList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">CopyOnWriteArrayList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">LinkedList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">RoleList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">RoleUnresolvedList<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Stack<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l14 level1 lfo10; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Vector<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">39.What are the Java Collection classes that
implement Set interface?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Java classes that implement Set
interface are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">AbstractSet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">ConcurrentSkipListSet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">CopyOnWriteArraySet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">EnumSet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">HashSet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">JobStateReasons<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">LinkedHashSet<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l15 level1 lfo9; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-family: "symbol"; font-size: 14.0pt;">·<span style="font-family: "times new roman"; font-size: 7pt; font-stretch: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">TreeSet<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">40.What is the difference between an
Iterator and ListIterator in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Iterator and ListIterator are two
interfaces in Java to traverse data structures. The differences between these
two are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l1 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">ListIterator can
be used to traverse only a List. But Iterator can be used to traverse List,
Set, and Queue etc.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l1 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">An Iterator
traverses the elements in one direction only. It just goes. ListIterator can
traverse the elements in two directions i.e. backward as well as forward
directions.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l1 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Iterator cannot
provide us index of an element in the Data Structure. ListIterator provides us
methods like nextIndex() and previousIndex() to get the index of an element
during traversal.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l1 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Iterator does not
allow us to add an element to collection while traversing it. It throws
ConcurrentModificationException. ListIterator allows use to add an element at
any point of time while traversing a list.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l1 level1 lfo8; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">An existing
element’s value cannot be replaced by using Iterator. ListIterator provides the
method set(e) to replace the value of last element returned by next() or
previous() methods.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">41. What is the difference between Iterator
and Enumeration?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Both Iterator and Enumeration are
interfaces in Java to access Data Structures. The main differences between
these are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Enumeration is an
older interface. Iterator is a newer interface.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Enumeration can
only traverse legacy collections. Iterator can traverse both legacy as well as
newer collections.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Enumeration does
not provide remove() method. So we cannot remove any element during traversal.
Iterator provides remove() method.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Iterator is a
fail-fast interface, it gives ConcurrentModificationException if any thread
tries to modify an element in the collection being iterated. Enumeration is not
fail-fast.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l6 level1 lfo7; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">Method names in
Iterator are shorter than in an Enumeration.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">42.What is the difference between an ArrayList
and a LinkedList data structure?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between ArrayList and
LinkedList data structures are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Data Structure</span></b><span style="font-size: 14.0pt;">: An ArrayList is an indexed based dynamic array. A
LinkedList is a Doubly Linked List data structure.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Insertion</span></b><span style="font-size: 14.0pt;">: It is easier to insert new elements in a LinkedList,
since there is no need to resize an array. Insertion in ArrayList is O(n),
since it may require resizing of array and copying its contents to new array.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Remove
elements</span></b><span style="font-size: 14.0pt;">: LinkedList has better
performance in removal of elements than ArrayList.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Memory Usage</span></b><span style="font-size: 14.0pt;">: LinkedList uses more memory than ArrayList, since it
has to maintain links for next and previous nodes as well.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l3 level1 lfo6; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Access</span></b><span style="font-size: 14.0pt;">: LinkedList is slower in accessing an element, since
we have to traverse the list one by one to access the right location.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">43.What is the difference between a Set and
a Map in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between a Set and a Map
in Java are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l0 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">1.Duplicate
Elements: A Set does not allow inserting duplicate elements. A Map does not
allow using duplicate keys, but it allows inserting duplicate values for unique
keys.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l0 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">2.Null values: A
Set allows inserting maximum one null value. In a Map we can have single null
key at most and any number of null values.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l0 level1 lfo5; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">3.Ordering: A Set
does not maintain any order of elements. Some of sub-classes of a Set can sort
the elements in an order like LinkedHashSet. A Map does not maintain any order
of its elements. Some of its sub-classes like TreeMap store elements of the map
in ascending order of keys.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">44.What is the use of a Dictionary class?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The Dictionary class in Java is used to
store key-value pairs. Any non-null object can be used for key or value. But we
cannot insert a null key or null object in Dictionary. Dictionary class is
deprecated now. So it should not be used in newer implementations.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">45.What is the default size of load factor
in a HashMap collection in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Default value of load factor in a
HashMap is 0.75.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">46.What is the significance of load factor
in a HashMap in Java?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">A HashMap in Java has default initial
capacity 16 and the load factor is 0.75f (i.e. 75% of current map size). The
load factor of a HashMap is the level at which its capacity should be doubled.
For example, in a HashMap of capacity 16 and load factor .75. The capacity will
become 32 when the HashMap is 75% full. Therefore, after storing the 12th key–
value pair (16 * .75 = 12) into HashMap, its capacity becomes 32.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<h3 style="text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">47.What are the differences between a
HashMap and a TreeMap?</span></b></h3>
</div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 18.0pt;">
<span style="font-size: 14.0pt;">Main differences between a HashMap and a
TreeMap in Java are:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">1.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Order</span></b><span style="font-size: 14.0pt;">: A HashMap does not maintain any order of its keys.
In a HashMap there is no guarantee that the element inserted first will be
retrieved first.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">2.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><span style="font-size: 14.0pt;">In a TreeMap
elements are stored according to natural ordering of elements. A TreeMap uses
compareTo() method to store elements in a natural order.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">3.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Internal
Implementation</span></b><span style="font-size: 14.0pt;">: A HashMap uses
Hashing internally. A TreeMap internally uses Red-Black tree implementation.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">4.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Parent
Interfaces</span></b><span style="font-size: 14.0pt;">: A HashMap implements Map
interface. TreeMap implements NavigableMap interface.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">5.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Null values</span></b><span style="font-size: 14.0pt;">: A HashMap can store one null key and multiple null
values. A TreeMap can not contain null key but it may contain multiple null
values.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">6.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Performance</span></b><span style="font-size: 14.0pt;">: A HashMap gives constant time performance for
operations like get() and put(). A TreeMap gives order of log(n) time
performance for get() and put() methods.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;">7.<span style="font-size: 7pt; font-stretch: normal; font-variant-east-asian: normal; font-variant-numeric: normal; line-height: normal;">
</span></span><!--[endif]--><b><span style="font-size: 14.0pt;">Comparison</span></b><span style="font-size: 14.0pt;">: A HashMap uses equals() method to compare keys. A
TreeMap uses compareTo() method for maintaining natural ordering.</span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; margin-left: 72.0pt; mso-add-space: auto; mso-list: l7 level1 lfo4; text-indent: -18.0pt;">
<span style="text-indent: 0px;"><span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px;">8.<b> Features</b></span></span><span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px; text-indent: 0px;">: A TreeMap has more features than a HashMap. It has methods like pollFirstEntry() , pollLastEntry() , tailMap() , firstKey() , lastKey() etc. that are not provided by a HashMap.</span><br />
<span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px; text-indent: 0px;"><br /></span>
<span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px; text-indent: 0px;"><br /></span>
<span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px; text-indent: 0px;"><br /></span>
<span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px; text-indent: 0px;"><br /></span>
<span style="font-family: "calibri" , sans-serif; font-size: 14pt; line-height: 19.9733px; text-indent: 0px;"><br /></span></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-78226155856123578092019-01-28T23:41:00.000+05:302019-05-27T21:16:03.185+05:30Exception Handling Interview Questions in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h2 style="line-height: normal; text-align: center;">
<b><span style="color: #2e74b5; font-size: 16.0pt;">Exception Handling</span></b></h2>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">1.What is Exception Handling in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Java provides Exception
Handling mechanism to handle Runtime errors that occur in JVM. There are
checked exceptions in a program that we expect to occur in certain situations.
Exception handling mechanism catches these checked exceptions and takes
relevant actions.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">2.In Java, what are the differences
between a Checked and Unchecked?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Checked Exceptions extend
Throwable class, but they do not extend RuntimeException or Error classes.
UncheckedException extend RuntimeException class. Checked Exceptions are
checked at compile time in Java. Unchecked Exceptions happen at Runtime, so
they are not checked at compile time. IOException, SQLException etc. are
examples of Checked Exceptions. NullPointerException, ArithmeticException etc.
are examples of Unchecked Exceptions.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">3. What is the base class for Error and
Exception classes in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Error as well as Exception
class is derived from Throwable class in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">4.What is a finally block in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Java provides a finally
block with a try block. This is an optional block. But finally block is always
executed after the execution of try block.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">5.What is the use of finally block in
Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">As per Java specification, a
finally block is always executed, whether an error occurs or not, whether an
exception is handled or not. It helps in doing the cleanup like- Rollback
Transaction, Close Connection, Close a file etc.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">6.Can we create a finally block without
creating a catch block?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Yes. A finally block can
follow a try block or catch block. So we can defined a finally block just after
a try block.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">7.Do we have to always put a catch block
after a try block?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Java does not enforce the
rule to put a catch block after try block. We can write catch block or finally
block after a try block. Any exception that we want to catch is mentioned in
catch block.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">8.In what scenarios, a finally block will
not be executed?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">There are two main scenarios
in which finally block is not executed:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Program exits by calling system.exit() call.<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-font-size: 12.0pt; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">A fatal error causes JVM to crash.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">9. Can we re-throw an Exception in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Yes, Java allows to re-throw
an Exception.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">10.What is the difference between throw
and throws in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Java provides throw keyword
to throw an exception from a method or a static block. Java provides throws
keyword to mention the probable exception thrown by a method in its
declaration. We use throw to explicitly throw an exception. We used throws to
declare an exception in method definition. We cannot propagate checked exceptions
with throw only. But checked exceptions can be propagated with throws keyword.
A throw call is followed by an instance. Class or Exception follows a throws
keyword. Call to throw occurs within a method. throws is just used with method
signature. We can throw only one exception at a time. But we can mention as
many exceptions in throws clause.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">11. What is the concept of Exception
Propagation?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">In Exception Propagation,
uncaught exceptions are propagated in the call stack until stack becomes empty.
This propagation is called Exception Propagation. Let say an exception
propagates from one method to another method. A() calls B(), which calls C(),
which calls D(). And if D() throws an exception, the exception will propagate
from D to C to B to A, unless one of the methods catches the exception.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">12. When we override a method in a Child
class, can we throw an additional Exception that is not thrown by the Parent
class method?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt; mso-bidi-font-size: 12.0pt;">Yes, Java allows us to throw
additional Exception in a child class, but the additional exception should be
an unchecked exception (RuntimeException).<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-473301044368505040.post-65648162428900029672019-01-28T22:38:00.001+05:302019-05-26T08:48:12.179+05:30Java String Interview Questions<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h2 style="line-height: normal; text-align: center;">
<b><span style="color: #2e74b5; font-size: 18.0pt; mso-themecolor: accent1; mso-themeshade: 191;">String</span></b></h2>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">1.What is the meaning of Immutable in the context of String class in
Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">An Immutable object cannot be modified or changed in
Java. String is an Immutable class in Java. Once a String object is created, it
cannot be changed. When we assign the String to a new value, a new object is
created.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">2.Why a String object is considered immutable in java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Java language uses String for a variety of purposes.
For this it has marked String Immutable. There is a concept of String literal
in Java. Let say there are 2 String variables A and B that reference to a
String object “TestData”. All these variables refer to same String literal. If
one reference variable A changes the value of the String literal from
“TestData” to “RealData”, then it will affect the other variable as well. Due
to which String is considered Immutable. In this case, if one variable A
changes the value to “RealData”, then a new String literal with “RealData” is
created and A will point to new String literal. While B will keep pointing to
“TestData”<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">3.How many objects does following code create?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt;">
<span style="font-size: 14.0pt;">Code:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">String s1="HelloWorld";<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">String s2=" HelloWorld ";<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 72.0pt;">
<span style="font-size: 14.0pt;">String s3=" HelloWorld ";<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The above code creates only one object. Since there is
only one String Literal “HelloWorld” created, all the references point to same
object.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">4. How many ways are there in Java to create a String object?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Java provides two ways to create a String object. One
is by using String Literal, the other is by using new operator.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">5.How many objects does following code create?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Code:<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-left: 36.0pt; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">String s = new
String("HelloWorld");<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">The above code creates two objects. One object is
created in String constant pool and the other is created on the heap in
non-pool area.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">6.What is String interning?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">String interning refers to the concept of using only
one copy of a distinct String value that is Immutable. It provides the
advantage of making String processing efficient in Time as well as Space
complexity. But it introduces extra time in creation of String.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">7.Why Java uses String literal concept?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">Java uses String literal concept to make Java more
efficient in memory. If same String already exists in String constant pool, it
can be reused. This saves memory usage. <o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">8. What is the basic difference between a String and StringBuffer
object?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">String is an immutable object. Its value cannot change
after creation. StringBuffer is a mutable object. We can keep appending or
modifying the contents of a StringBuffer in Java.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">9.How will you create an immutable class in Java?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, we can declare a class final to make it
immutable. There are following detailed steps to make it Immutable:<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpFirst" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">1.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Add final
modifier to class to prevent it from getting extended<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">2.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Add private
modifier to all the fields to prevent direct access<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">3.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Do not provide
any setter methods for member variables<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">4.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Add final
modifier to all the mutable fields to assign value only once<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpMiddle" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">5.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">Use Deep Copy to
initialize all the fields by a constructor<o:p></o:p></span></div>
<div class="MsoListParagraphCxSpLast" style="line-height: normal; mso-list: l0 level1 lfo1; text-indent: -18.0pt;">
<!--[if !supportLists]--><span style="font-size: 14.0pt; mso-bidi-font-family: Calibri; mso-bidi-theme-font: minor-latin;"><span style="mso-list: Ignore;">6.<span style="font: 7.0pt "Times New Roman";">
</span></span></span><!--[endif]--><span style="font-size: 14.0pt;">In clone method,
return a copy of object instead of the actual object reference<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">10.What is the use of toString() method in java ?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">In Java, Object class has toString() method. This
method can be used to return the String representation of an Object. When we
print an object, Java implicitly calls toString() method. Java provides a
default implementation for toString() method. But we can override this method
to return the format that we want to print.<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<h3 style="line-height: normal; text-align: left;">
<b><span style="color: red; font-size: 14.0pt;">11. Arrange the three classes String, StringBuffer and StringBuilder
in the order of efficiency for String processing operations?</span></b></h3>
<div class="MsoNormal" style="line-height: normal; text-indent: 36.0pt;">
<span style="font-size: 14.0pt;">StringBuilder is the most efficient class. It does not
have the overhead of Synchronization. StringBuffer is a Synchronized class. It
has better performance than String but it is slower than StringBuilder. String
is the slowest for any String processing operations, since it is leads to
creation of new String literal with each modification. So the decreasing order
of efficiency is: StringBuilder, StringBuffer, String<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
<div class="MsoNormal" style="line-height: normal;">
<br /></div>
</div>
Unknownnoreply@blogger.com0