Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| |||||||||||
Interview Questions and Answers for 'Compass' - 24 question(s) found - Order By Newest | |||||||||||
Very frequently asked. Among first few questions in almost all interviews. Among Top 5 frequently asked questions. Frequently asked in Indian service companies (HCL,TCS,Infosys,Capgemini etc based on multiple feedback ) and Epam Systems | |||||||||||
| |||||||||||
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality, whereas "==" operator evaluate to see if the object handlers on the left and right are pointing to the same object in memory. x.equals(y) means the references x and y are holding objects that are equal. x==y means that the references x and y have same object. Sample code: String x = new String("str"); String y = new String("str"); System.out.println(x == y); // prints false System.out.println(x.equals(y)); // prints true | |||||||||||
Sample Code for equals | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   string comparison   string   object class   ==   equals   object equality  operator   == vs equals   equals vs == Asked in 294 Companies basic   frequent | |||||||||||
Try 6 Question(s) Test | |||||||||||
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies. | |||||||||||
| |||||||||||
Ans. Underlying data structure for ArrayList is Array whereas LinkedList is the linked list and hence have following differences - 1. ArrayList needs continuous memory locations and hence need to be moved to a bigger space if new elements are to be added to a filled array which is not required for LinkedList. 2. Removal and Insertion at specific place in ArrayList requires moving all elements and hence leads to O(n) insertions and removal whereas its constant O(1) for LinkedList. 3. Random access using index in ArrayList is faster than LinkedList which requires traversing the complete list through references. 4. Though Linear Search takes Similar Time for both, Binary Search using LinkedList requires creating new Model called Binary Search Tree which is slower but offers constant time insertion and deletion. 5. For a set of integers you want to sort using quicksort, it's probably faster to use an array; for a set of large structures you want to sort using selection sort, a linked list will be faster. | |||||||||||
Sample Code for ArrayList Sample Code for LinkedList | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  collections   java   data structures   arraylist   linkedlist   arraylist vs linkedlist Asked in 61 Companies Basic   frequent | |||||||||||
Try 1 Question(s) Test | |||||||||||
Very frequently asked. Favorite question in Walk in Drive of many Indian service companies. | |||||||||||
| |||||||||||
Ans. They are different the way their elements are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly. | |||||||||||
Sample Code for treemap Sample Code for hashmap | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   collections   map   treemap   hashmap   treemap vs hashmap Asked in 31 Companies basic   frequent | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. It in Java is used to indicate that a field should not be serialized. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   serialization   transient   java keywords Asked in 39 Companies intermediate   frequent | |||||||||||
Try 2 Question(s) Test | |||||||||||
| |||||||||||
Ans. The purpose of comparator interface is to compare objects of the same class to identify the sorting order. Sorted Collection Classes ( TreeSet, TreeMap ) have been designed such to look for this method to identify the sorting order, that is why class need to implement Comparator interface to qualify its objects to be part of Sorted Collections. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   collections   treemap   treeset   comparator Asked in 2 Companies Intermediate | |||||||||||
Try 2 Question(s) Test | |||||||||||
| |||||||||||
Ans. Yes, but the overloaded main methods without single String[] argument doesn't get any special status by the JVM. They are just another methods that needs to be called explicitly. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   main method   overloading   yes-no Asked in 6 Companies intermediate   frequent | |||||||||||
| |||||||||||
Ans. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   compareto   final methods   enum   enumeration expert | |||||||||||
Frequently asked in CTS ( Based on 2 feedback ) | |||||||||||
| |||||||||||
Ans. Comparable interface is used for single sequence sorting i.e.sorting the objects based on single data member where as comparator interface is used to sort the object based on multiple data members. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   java.util   comparable   comparator   collections Asked in 23 Companies basic   frequent | |||||||||||
| |||||||||||
Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   collections   comparable interface Asked in 7 Companies intermediate   frequent | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. private static boolean isPalindrome(String str) { if (str == null) return false; StringBuilder strBuilder = new StringBuilder(str); strBuilder.reverse(); return strBuilder.toString().equals(str); } | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   string   stringbuilder   stringbuilder   string class   code   palindrome Asked in 38 Companies Basic   frequent | |||||||||||
| |||||||||||
Ans. Package is a namespace that organizes a set of related classes. Advantages of Packages 1. Better Organization of classes. 2. Saves from the problem of duplicate names as duplicate class names are allowed across different packages. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  package  advantages of packages Asked in 1 Companies Basic | |||||||||||
| |||||||||||
Ans. S3 and S4 are pointing to different memory location and hence Output 1 will be false. Hash code is generated to be used as hash key in some of the collections in Java and is calculated using string characters and its length. As they both are same string literals, and hence their hashcode is same.Output 2 will be true. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   string   hashcode   hash code   string comparison   string pool | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   string   hashcode   hash code   string comparison  hashtable Asked in 17 Companies basic   frequent | |||||||||||
| |||||||||||
Ans. As String implements Comparable, It refers to the String compareTo method to identify the order relationship among those elements. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   collections   comparable interface   treemap   compareto | |||||||||||
| |||||||||||
Ans. No, Sorted collections don't allow addition of heterogeneous elements as they are not comparable. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   collections   comparable interface   treemap | |||||||||||
| |||||||||||
Ans. It won't create any problem if the objects are comparable i.e we have that class implementing Comparable interface. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   collections   treemap   comparable interface | |||||||||||
| |||||||||||
Ans. One. As we haven't specified the type of TreeSet, it being evaluated with the first element insertion. Once it's identified that it's of type String and as no comparator has been defined, the comparison is done using the String compareTo method. String compareTo method compares the elements by the content / value. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   set   treeset   string   compareto   coding   code | |||||||||||
Ans. Order in which constants are declared. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  enum   collections   sorted collections   treemap   treeset   compareto   comparable   java | |||||||||||
| |||||||||||
Ans. Implementing Comparable interface means that the elements of the class are comparable i.e the class provides the implementation of compareTo method that would help comparing the elements. This is usually required if we are planning to sort elements of a collection, If compareTo method is not defined , the sorting class / method could never understand a way to compare its elements in order to sort them. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  comparable interface | |||||||||||
Try 1 Question(s) Test | |||||||||||
| |||||||||||
Ans. true false true Just like Strings, java maintains an integer constant pool too. So 1 will be maintained in integer constant pool and hence reference int2 will point to same integer in pool. String pool is only for string literals ( defined by "" ) and not for newly created objects and hence str1 == str2 will return false as they are separate objects in memory. String pool is used for storing string literals so that they can be reused and str3 and str4 will point to same string literal in string pool. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  string pool  integer constant pool  string comparison  integer comparison | |||||||||||
| |||||||||||
Ans. >= => will result in error. => somewhat looks like lambda operator "->" | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  operators  >= operator  greater than and equal to operator  comparison operators | |||||||||||
| |||||||||||
Ans. Using comparable and comparator and sorted collections like TreeSet or TreeMap. or use stream api from java 8 onwards which internally refers to comparable and comparator through lambda expressions | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  sort  sorting  comprator  comparable  treeset  treemap  sorting collections Asked in 1 Companies Basic   frequent | |||||||||||
| |||||||||||
Ans. "equals" is the method of object class which is supposed to be overridden to check object equality. x.equals(y) means the references x and y are holding objects that are equal. The compareTo() method is used for comparing two objects in Java. It is usually defined for the classes whose objects needs to be ordered through Comparable interface or need to be part of an ordered collection like TreeSet or TreeMap. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  equals  compareTo  equals vs compareTo | |||||||||||
| |||||||||||
Ans. equals, compareTo and hashcode are of use when the objects are used within collections. Equals helps with collections that helps maintaining only unique objects ( like Set ) compare and compareTo helps with collections that helps maintaining objects in order ( TreeSet, TreeMap etc ) hascode helps with collections that facilitates hash searching ( like hashSet, hashMap etc ) | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  equals   compareTo   hashcode method | |||||||||||
| |||||||||||
| |||||||||||