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. String class is immutable as well as final. Because of these properties , String objects offer many benefits 1. String Pool - When a string is created and if it exists in the pool, the reference of the existing string will be returned instead of creating a new object. If string is not immutable, changing the string with one reference will lead to the wrong value for the other references. Example - String str1 = "String1"; String str2 = "String1"; // It doesnt create a new String and rather reuses the string literal from pool // Now both str1 and str2 pointing to same string object in pool, changing str1 will change it for str2 too 2. To Cache its Hashcode - If string is not immutable, One can change its hashcode and hence its not fit to be cached. 3. Security - String is widely used as parameter for many java classes, e.g. network connection, opening files, etc. Making it mutable might possess threats due to interception by the other code segment. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Object class as its the parent class of all Java classes. | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. package com.string; import java.util.Scanner; public class String13 { public static void main(String[] args) { System.out.println("Enter Sentence"); Scanner sc=new Scanner(System.in); String sentence=sc.nextLine(); String[] words=sentence.split(" "); int count=0; for (String string : words) { string.trim(); if(!string.equals("")){ count++; System.out.println(string " " count); } } } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. In Java JVM memory settings is done by use the arguments -Xms -Xmx. Use M or G after the numbers for indicating Megs and Gigs of bytes respectively. -Xms indicates the minimum and -Xmx the maximum. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. public static Stream permutations(String str) { if (str.isEmpty()) { return Stream.of(""); } return IntStream.range(0, str.length()).boxed() .flatMap(i -> permutations(str.substring(0, i) str.substring(i 1)).map(t -> str.charAt(i) t)); } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. <a href="http://javahungry.blogspot.com/2015/03/difference-between-array-and-arraylist-in-java-example.html" rel="nofollow">http://javahungry.blogspot.com/2015/03/difference-between-array-and-arraylist-in-java-example.html</a> | ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. An Error indicates serious problems that a reasonable application should not try to catch whereas An Exception indicates conditions that a reasonable application might want to catch. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. It is special literal. It is neither keyword nor identifier. Any reference in java that doesnt point to any object , gets assigned null i.e is a reference to null | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. toString() is an overloaded method of String class that is used to convert many data types to String, Boolean being one of them. toString(Boolean bool) | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Collections in java is a framework of classes that provides an abstracted data structure to store and manipulate the group of objects. Each class is unique in the way it stores , search , sort and modify the elements. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. int[] arr = {1,-1,2,-3,3,-4,4,5,6,-5,-6,-7,-8,8,9,-9}; List positiveNumbers = new ArrayList<>(); List negativeNumbers = new ArrayList<>(); for(int i = 0; i < arr.length(); i ){ if(I < 0){ negativeNumbers.add(i); } else { positiveNumbers.add(i); } } System.out.println("Positive Numbers:" + positiveNumbers); System.out.println("Negative Numbers:" + negativeNumbers); | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. static methods and static elements are shared by all objects of a class and hence could create problem. Static methods are not synchronized by default and hence could create problem when accessed through multiple threads. Static elements are shareable by class and hence state of one object could be altered by another object. Some limitations with Unit testing as not all mocking framework facilitate mocking them. Power mock allows but Mockito doesn't | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Better Control - If the value is being used at multiple locations , that can be controlled better from single place. Any change would only require making single change. Meaning , Aliasing and Better Readability - Sometimes its easy to read the value by its meaning or alias ( 0 as ZERO or 0 as NEUTRAL_VALUE ). | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. Everytime an object is serialized the java serialization mechanism automatically computes a hash value by passing the meta information for the class. This id is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. public class SingleTon { private SingleTon() { if (singleTon != null) { throw new RuntimeException("cant not create the object"); } } public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException("can not be create"); } static private volatile SingleTon singleTon; public static SingleTon getInstance() { SingleTon singleTon = this.sample; if (singleTon == null) { synchronized (this) { singleTon = this.singleTon; if (singleTon == null) { singleTon = this.singleton = new SingleTon(); } } } return singleTon; } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. https://www.tutorialspoint.com/javaexamples/thread_procon.htm | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Abstract classes provide a mechanism of interfacing ( using abstract method ) as well as inheritance ( extending abstract class ). So they should be used in place of interfaces in case there is some code ( methods ) or object body ( member elements ) that can be reused with inheritance. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. In first case the whole loop will terminate as soon as any exception happens in the method calculate ( assuming calculate method is not handling its exception and those are thrown back at the calling method ) In Second case exception will be caught for individual iteration and hence it wont break the loop and will continue for the next iteration. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. B ctor B ctor B ctor A ctor A ctor | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Object Oriented - Java is object oriented but isn't purely object oriented as we have primitives along with objects. Platform Independent - As JVM provides the translation to the Machine code as per the underlying Operating System. Interpreted as well as compiled - Java files are compiled as class files and then class files are interpreted by JVM. Runs on a Virtual Machine - Which is JVM that acts as an independent sub environment. Multi-threaded - As applications can run on single thread as well as multi thread. Modularity - Through usage of Classes , methods and Interfaces. Robust Usage in variety of Application types - Web , Gaming, BigData. | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Static , Final , Synchronized, private , public , protected, volatile, transient, super, this,import , abstract,native,default (effective java 8), new | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Braces, i.e () and [] have the highest precedence | ||||
![]() | ||||
![]() ![]() ![]() | ||||
| ||||
Ans. An Exception in java is the occurrence during computation that is anomalous and is not expected. Exception handling is the mechanism which is used to handle such situations. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. General contract of hashCode is: 1.Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, 2.If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. 3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. static is the keyword that makes it accessible even without creating any object and using class name only. Making it non static would like creation of object upfront before calling the method. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. class Bank { int balance; Bank(){ balance = 0; } void deposit(int amount){ balance = balance amount; } int withdraw(int amount){ balance = balance - amount; return balance; } } | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
![]() | ||||
| ||||
Ans. https://www.geeksforgeeks.org/lru-cache-implementation/ | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. Yes | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
| ||||
Ans. No, constructors can not be synchronized in Java. In fact using the keyword synchronized with a constructor results in compilation error. | ||||
![]() | ||||
![]() ![]() ![]() ![]() ![]() | ||||
| ||||
Ans. finally will execute in all graceful situations - graceful executions as well as graceful exceptions. The only situation when finally block won't execute is when the app is abruptly stopped, killed or unplugged. | ||||
![]() | ||||
![]() | ||||
![]() ![]() ![]() ![]() | ||||
![]() ![]() | ||||