Search Results:
- This skill is particularly important for senior Java developers who will have to work on the architecture of applications to design long-term solutions. This is a simple question example: Among these Java method declarations, which is usually the...
- Java Reliability Questions Remember str. What happens if str is null? Reliability refers to the developer's ability to achieve solutions that address specific cases like corner and edge cases. The higher this skill, the higher the developer...
- Prgramming Java Programming questions with answer Java mcq : who is known as father of java programming language? These section include questions with answers on basic theory of Java to test knowledge of fundamental concept of Java programming language. If you are going to apply for an entry-level positions in Java related job, then you are not expected to answer the tricky java interview questions, rather you should have solid understating of Java basics - the fundamental concept and feature of Java as object oriented programming language.
- These Java mcq sets will get you acquainted with the nature of questions you may encounter during your Java interview or competitive exams for popular programming language Java related career. Java MCQ Question with Answer Here you will find a list of common important questions on java programming in MCQ quiz style with answer for competitive exams and interviews. These frequently asked sample questions on Java are given with correct choice of answer that you can check instantly.
- Presently we have added total 5 sets of questions on java programming for you to practice. We will keep adding more questions and provide this question bank in PDF format, so that you can download them instantly in E-book style. Following section consists of some important multiple choice questions mcq on java programming with answers. Who is known as father of Java Programming Language?
- Whether you are a beginner in Java or an expert programmer, you will be tested for your coding skills in the interview. The best part is that some of the questions are from the latest releases Java If you can write Java programs using the latest features, it means you keep yourself up to date, which will work in your favor.
- How to reverse a String in Java? It might be surprising, but there is no reverse utility method in the String class. We can create a character array from the string and then iterate it from the end to start. We can append the characters to a string builder and finally return the reversed string. Easy to Miss: The indexing in Java starts from 0. How to swap two numbers without using a third variable? For example, below swap function will not change the input integer values. Java Program to check if a vowel is present in the string? We can use regular expression to check if the string contains vowels or not. Java program to check if the given number is Prime? You can read more about it here.
- Fibonacci Series using recursion We can use a for loop to print fibonacci series. We can use recursion to print fibonacci series. Check if a List of integers contains only odd numbers? We can use for loop and check each element one by one if they are odd or not. Palindrome Check A palindrome string is one whose reverse is also the same string. So we can reverse the input string and check if both strings are equal or not. We can also use the String charAt int index method to check for palindrome string. How to remove Whitespaces from String We can use Character. How to remove leading and trailing whitespaces from a string? Java String class contains two methods to remove leading and trailing whitespaces — trim , and strip. The strip method was added to the String class in Java However, the strip method uses Character. The strip method is the recommended way to remove whitespaces because it uses the Unicode standard.
- Sorting an array in Java? This question requires a deep understanding of sorting in Java. If you look at the Arrays utility class, there are many overloaded sort methods to sort primitive as well as to object arrays. Just use the Arrays. If you want to specify the sorting criteria, then you can pass the Comparator for the sorting logic. You should read more about them at — Comparable and Comparator in Java. How to Create a Deadlock Scenario Programatically? Deadlock is a special scenario in the multi-threaded environment where two or more threads are blocked forever.
- The deadlock situation arises with at least two threads and two or more threads. But, they are using the shared resources and started in such a way that they will keep on waiting indefinitely to acquire the lock on the second object. We can use the java thread dump to detect the deadlocks. Read more at Deadlock in Java. Find factorial of an integer? The factorial of an integer is calculated by multiplying all the numbers from 1 to the given number.
- We can use recursion to find the factorial of an integer. Revese a Linked List? LinkedList descendingIterator returns an iterator that iterates over the element in the reverse order. We can use this iterator to create a new Linked List with elements in the reverse order. How to implement Binary Search? The array elements must be sorted for implementing binary search.
- The binary search algorithm is based on the following conditions. If the key is less than the middle element, then we now need to search only in the first half of the array. If the key is greater than the middle element, then we need to only search in the second half of the array. And if the key is equal to the middle element in the array, then the search ends. Finally, if the key is not found in the whole array, then it should return This indicates that the element is not present. Merge Sort in Java? Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquers. It is based on the idea of breaking down a list into several sub-lists until each sublist consists of a single element. Then merging those sublists in a manner that results in a sorted list. Create a Pyramid of Characters in Java? Pattern programs are used a lot in interviews to understand the logical thinking abilities of the interviewee.
- I have written an extensive post for different kind of pyramid patterns examples, read it here. Check if two arrays contains same elements? We will first create a set of elements from both the arrays. Then compare the elements in these sets to find if there is an element that is not present in both the sets? Arrays; import java. HashSet; import java. Sum of all elements in integer array? We can use for loop to iterate over the array elements and add them to get the final sum. Find second largest number in an array? There are many ways to solve this problem. We can sort the array in natural ascending order and take the second last value. But, sorting is an expensive operation. We can also use two variables to find the second largest value in a single iteration. How to Shuffle an Array in Java? We can use Random class to generate random index numbers and shuffle the elements.
- How to find if a string is present in a text file? We can use Scanner class to read the file contents line by line. Then use String contains method to check if the string is present in the file or not. How to print date in specific format? We can use SimpleDateFormat class to get the date string into specific formatting.
- How to merge two lists in java? We can use the addAll method to merge multiple lists in Java. How to Sort HashMap by values? HashMap is not an ordered collection. But, we can sort the entries based on value and store into LinkedHashMap. LinkedHashMap maintains the order of insertion. ArrayList; import java. HashMap; import java. LinkedHashMap; import java. List; import java. Map; import java. Entry; import java. We can use the replace method to create a new string without the given character. All the string manipulation methods return a new string. How to get distinct characters and their count in a String?
- We can create the character array from the string. Then iterate over it and create a HashMap with the character as key and their count as value. How to prove String is immutable programatically? Write a Program to showcase inheritance?
- View answer A single ThreadLocal instance can store different values for each thread independently. Each thread that accesses the get or set method of a ThreadLocal instance is accessing its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread e. The example below, from the ThreadLocal Javadoc , generates unique identifiers local to each thread. What is the volatile keyword? How and why would you use it? View answer In Java, each thread has its own stack, including its own copy of variables it can access. When the thread is created, it copies the value of all accessible variables into its own stack. In all versions of Java, the volatile keyword guarantees global ordering on reads and writes to a variable. In Java 5 or later, volatile reads and writes establish a happens-before relationship, much like acquiring and releasing a mutex.
- Using volatile may be faster than a lock, but it will not work in some situations. The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly. The volatile keyword is also useful for bit types like long and double since they are written in two operations. Without the volatile keyword you risk stale or invalid values. One common example for using volatile is for a flag to terminate a thread.
Java Quizzes Online, Trivia, Questions & Answers - ProProfs Quizzes
By making the flag volatile, you can ensure that the thread that is checking its value will see that it has been set to true without even having to use a synchronized block. Tail recursion is functionally equivalent to iteration. Since Java does not yet support tail call optimization, describe how to transform a simple tail recursive function into a loop and why one is typically preferred over the other. Here is an example of a typical recursive function, computing the arithmetic series 1, 2, 3…N. Notice how the addition is performed after the function call. For each recursive step, we add another frame to the stack. That is, once the base case is complete, the solution is apparent.- In order to implement recursive functions in Java, we need to be aware of this limitation to avoid StackOverflowErrors. In Java, iteration is almost universally preferred to recursion. How can you swap the values of two numeric variables without using any other variables? This can be done using Thread. List and explain the purpose of the three types of class loaders. Classes may be loaded from the local file system, a remote file system, or even the web. When the JVM is started, three class loaders are used: 1. Extension Classloader: Loads jar files from folder. Is a finally block executed when an exception is thrown from a try block that does not have a catch block, and if so, when? A finally block is executed even if an exception is thrown or propagated to the calling code block. View answer This is a problem of initialization order. The subclass constructor will not have had a chance to run yet and there is no way to force it to run it before the parent class.
- This is because the this. What variance is imposed on generic type parameters? How much control does Java give you over this? So even though String extends i. On individual methods, we can use? What are static initializers and when would you use them? View answer A static initializer gives you the opportunity to run code during the initial loading of a class and it guarantees that this code will only run once and will finish running before your class can be accessed in any way. They are useful for performing initialization of complex static objects or to register a type with a static registry, as JDBC drivers do. Suppose you want to create a static, immutable Map containing some feature flags. If one needs a Set, how do you choose between HashSet vs. View answer At first glance, HashSet is superior in almost every way: O 1 add, remove and contains, vs. O log N for TreeSet.
Java Exam Questions And Answers PDF | This Or That Questions, Exam, Question And Answer
However, TreeSet is indispensable when you wish to maintain order over the inserted elements or query for a range of elements within the set. Consider a Set of timestamped Event objects. They could be stored in a HashSet, with equals and hashCode based on that timestamp. This is efficient storage and permits looking up events by a specific timestamp, but how would you get all events that happened on any given day?Free Java Quiz Questions With Answers - Prepare Yourself For Interviews - DataFlair
What are method references, and how are they useful? View answer Method references were introduced in Java 8 and allow constructors and methods static or otherwise to be used as lambdas. They allow one to discard the boilerplate of a lambda when the method reference matches an expected signature. For example, suppose we have a service that must be stopped by a shutdown hook. So we can see that method references are mainly used to improve code organization, clarity and terseness. How are Java enums more powerful than integer constants? How can this capability be used? View answer Enums are essentially final classes with a fixed number of instances. They can implement interfaces but cannot extend another class.- This flexibility is useful in implementing the strategy pattern, for example, when the number of strategies is fixed. Consider an address book that records multiple methods of contact. Generally, the safety and flexibility of enums means they should be used in place of integer constants, and switch statements can be eliminated with liberal use of abstract methods.
- Give an example of when this property is useful. View answer If a collection backs another, it means that changes in one are reflected in the other and vice-versa. For example, suppose we wanted to create a whitelist function that removes invalid keys from a Map. This is made far easier with Map. Note, it is important to consult the documentation of the backing collection to see which modifications will successfully write through. In the example above, map. What is reflection? Give an example of functionality that can only be implemented using reflection. Commonly used information includes: methods and fields available on a class, interfaces implemented by a class, and the runtime-retained annotations on classes, fields and methods.
- These libraries need reflection to inspect those fields and their annotations and also to access the values during serialization. Model-View-Controller frameworks call controller methods based on routing rules. These frameworks must use reflection to find a method corresponding to an action name, check that its signature conforms to what the framework expects e. Dependency injection frameworks lean heavily on reflection. They use it to instantiate arbitrary beans for injection, check fields for annotations such as Inject to discover if they require injection of a bean, and also to set those values. Aside from implementing generic libraries, direct use of reflection is rare but it is still a handy tool to have.
- Knowledge of reflection is also useful for when these mechanisms fail. However, it is often prudent to avoid reflection unless it is strictly necessary, as it can turn straightforward compiler errors into runtime errors. Nested classes can be static or non-static also called an inner class. How do you decide which to use? Does it matter? View answer The key difference between is that inner classes have full access to the fields and methods of the enclosing class. This can be convenient for event handlers, but comes at a cost: every instance of an inner class retains and requires a reference to its enclosing class.
- With this cost in mind, there are many situations where we should prefer static nested classes. When instances of the nested class will outlive instances of the enclosing class, the nested class should be static to prevent memory leaks. However, WidgetParserImpl is not static, and so if WidgetParserFactory is discarded immediately after the WidgetParser is created, the factory will leak, along with all the references it holds. This also makes it easier to extract WidgetParserImpl into a separate class should it outgrow its enclosing class. Which is better and why? If another String with the same value is then created e. Each such call will therefore create an additional String object e. There is more to interviewing than tricky technical questions, so these are intended merely as a guide.
OCA Java SE 8 Programmer II 1Z Actual Exam Questions And Answers: + Practice Exam Questions
At the end of the day, hiring remains an art, a science — and a lot of work. Submit an interview question Submitted questions and answers are subject to review and editing, and may or may not be selected for posting, at the sole discretion of Toptal, LLC.Java Interview Questions For Freshers And Experienced | Java Questions
Why is synchronization necessary? Explain with the help of a relevant example. What do you understand by an instance variable and a local variable? Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost. All the objects of the class will have their copy of the variables for utilization.- If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected. The utilization of the variable is restricted to the block scope. Contiguous memory locations are usually used for storing actual values in an array but not in ArrayList. In the case of ArrayList, data storing in the form of primitive data types like int, float etc. Thus, storing of actual objects or non-primitive data types like Integer, Double etc. However, the same does not apply to the arrays.
No comments:
Post a Comment