Search Interview Questions | ![]() ![]() Click here and help us by providing the answer. ![]() Click Correct / Improve and please let us know. |
|
| ||||
Interview Questions and Answers - Order By Newest | ||||
![]() ![]() | ||||
| ||||
Ans. Both will produce the same output but 1st will consume more resources in terms of memory ( for maintaining intermediate value ) and computing power ( as single mathematical calculation with more operands is more effective than multiple calculations with broken down operands ) | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Yes, doesn't provide exclusive access as we cannot allocate and deallocate memory exclusively as Java internally manages it. The advantage of this is that it relieves the coder for such tasks and helps protect from many bugs that may get introduced with imperfect coding. Moreover as java garbage collector collects all unclaimed memory or objects, it helps the application from memory leaks. On the flip side , as coder doesn't have extensive excess to memory , it is upto java to decide on the state for programming construct and data storage and hence may introduce some security risks. For example - Java keeps string literals in string pool and there is no exclusive way to remove it and hence may stay and sensitive data in string pool may introduce security issues. Moreover when we overwrite a value or object for a variable / reference, it is upto java to purge those values and hence it may stay in memory for a while till java decide that it is no longer referenced and hence should be removed and hence makes it vulnerable for inappropriate access. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Optional is to be used for arguments / atrributes which are indeed optional i.e the request should continue even if they aren't provided. It should not be used for mandatory attributes or arguments as we would like application to shout out ( with error message / exception trace ) to signify a problem. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Though var args are used rarely but they are pretty useful if a method is expected to receive variable number of arguments. For example - it's pretty useful for the main method as the caller has the flexibility to provide arguments in infinite ways.It provides a convenience and flexibility to the caller. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. It means that only 1 thread can access have access to Vector at a time and no parallel access is allowed whereas Array List allows parallel access by multiple threads. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. No, only methods can be declared protected. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Strings are immutable in Java and not final. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. 65 to 90 for capital case alphabets 97 to 122 for lower case alphabets | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. By using synchronized static method or synchronized block | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. String s1 = "akhilesh"; char[] ch = s1.toCharArray(); for (int i = ch.length-1 ; i >=0 ; i--) { System.out.print(ch[i]); } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. LinkedList maintains the insertion order in collection. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. private void permutation(String prefix, String sufix) { int ln = sufix.length(); if(ln == 0) { System.out.println(prefix); } else { IntStream.range(0, ln).forEach(i->permutation(prefix sufix.charAt(i), sufix.substring(0,i) sufix.substring(i 1, ln))); } } call:permutation("", "abcdef"); | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Java could design it in such a manner but would result in ambiguity if there are multiple files with the same name. The only way to get over it to show the message when you use any such class to provide explicit import with the package prefix. The other problem could be that Java might have to change the early import to Late import and check what's being used to decide what needs to be imported as otherwise you will see errors regarding the ambiguous imports with duplicate file name. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Encapsulation facilitate hiding and restricted access and hence more of a security feature. Encapsulation is definitely a great feature as when applications expand criss cross communication between objects / modules could lead to blunders. Inheritance facilitates code reuse. Polymorphism comprise of method overloaded ( which to me is negligible usage ) and method overriding. Method overriding is of great usage as it facilitates concept of interfaces and plugin development. So it’s Security / Organization vs Code Reuse / Support for other features like overriding vs Contracts / Plugin Development facilitating the creation of frameworks / libraries. Which is more important may vary from application to application , its scale , its use , its sensitivity etc. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. No. The whole idea of default method is to provide a default definition in case the implementing class intend to provide definition for only some of the methods. Making any of the interface method private would restrict it from being implemented by the implementing class. So default method is an option to provide implementation and not restricting a new definition. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. There are different types of relationship between classes Inheritance - Also called is-a relationship, Child class object carries the body of the Parent class when initiated. Moreover there are certain privileges attach to method overriding to the classes related this way. This relationship exist for code reuse, method overriding and interfacing ( through abstract class ). Composition - Also called has-a relationship. Class objects carries a reference to other class objects when instantiated. This relationship exist for work flow / work delegation. Inner / Nested class - It’s a relationship that encapsulates the inner class or it’s object within the outer class. This relationship exist for encapsulation / security. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) fibonacci(n - 2); } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. 1. The problem with double (x*100)/100 doesn't return exact x but few fractions lesser than x and then if you are using floor rounding , it makes a big difference 2. Rounding only after getting a result vs rounding each outcome of 2 operand make difference 3. Usage of inappropriate Rounding mode and Rounding scale. 4. Results with double and BigDecimal | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Observable is emiting data and Observer is subscribing to catch the data | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. No | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Yes, to a certain extent. But the objective for Final class could be beyond just enforcing composition as certain classes might have been created without inheritance or composition in mind. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. 1. Enforcing composition over inheritance 2. Restricting overriding of certain methods 3. Final methods are faster than regular instance methods 4. Enforcing Immutability | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. File folder = new File(path); if(!folder.exists()){ try { folder.mkdir(); } catch (Exception e) {} } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Both Arrays and ArrayLists are used to store elements. Elements can be either primitives or objects in case of Arrays, but only objects can be stored in Arraylist. Array is a fixed length data structure while arraylist is variable length collection class. Once created, you cannot change the size of the arrays, but arraylists can dynamically resize itself when needed.Another notable difference between Arrays and Arrayslist is that arary is part of core java programming and array list is part of collection classes | ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No, It will throw compile time error saying "must provide either dimension expressions or an array initializer" Alternatively we can provide array initializer like String[] strArray = new String[]{"Buggy","Bread"}; which will initialize it to size 2 with values as "Buggy" and "Bread" | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Unreachable code is a code segment that can never be executed in any case. For example - int x = 5; return; System.out.println(x); In this code , 2nd line will return the control out of the method and hence 3rd line will never get executed and hence is unreachable code. This will be caught at compile time only. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Because the reference is of Object Array and the resolution of what it holds is identified at the runtime. String[] strArray = new String[2]; strArray[0] = 5; would have given the compile time array as the reference is of String array. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Abstract classes can take care of that to a certain extent. Though they are little heavier than Interfaces but An abstract class with all abstract methods and no instance variables will be able to help with everything that currently an interface does. The only problem is that a class can only extend one class whereas it can implements multiple interfaces and that is the reason Interfaces were introduced in Java, i.e to get over the problem of multiple inheritance. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. JVM - With Cloud and Serverless, this could be a hurdle for it to be preferred technology. Confusion between OOP and Functional Programming. Slower speed of innovation due to wider adoption. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Base Class is a relative term used to signify the Parent class in Parent - Child or Base - derived relationship. Derived Class is a relative term used to signify the Child class in Parent - Child or Base - derived relationship. Abstract Class is a class that is not allowed to be instantiated and hence can serve only a base class for it's usage. Concrete Class is the class which is supposed to be instantiated and hence provide definition for all implementing interface methods and extending abstract methods. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
![]() ![]() | ||||