Interview Questions and Answers - Order By Rating Q841. If we parse any string to get another data type and the String doesn't abide by the type of data it should have hold, what would happen ? Core Java
Ans. It will throw Parse Exception. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q842. Why calculations in float or Double sometimes gives unpredictable results ? Core Java
Ans. Because the way float values are stored its not precise.
For example - 0.1 is actually stored as
0.1000000000000000055511151231257827021181583404541015625 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  double  float  data type basic   frequent Q843. how can we get the System Environment variables from within Java program ? Core Java
Ans. We can use System.getenv for getting the environment variables.
For ex -
System.getenv("JAVA_HOME") Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q844. How to check the version of Java through Java program itself ? Core Java
Ans. We can use
System.getProperty("java.version"); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Q845. How can you check if a String is a 10 digit number by using regular expression ? Core Java
Ans. We can use \d{10} as the expression to check that Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  regular expression Q846. How can we catch an exception thrown by another thread ? Core Java
Ans. http://stackoverflow.com/a/6548203/2789764 Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception handling  exception expert   rare Q847. How can we find the memory usage of JVM from Java code? Core Java
Ans. Using Runtime object.
Runtime runtime = Runtime.getRuntime();
runtime.totalMemory();
runtime.freeMemory(); Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jvm  memory management   rare Q848. Is ++ or -- operation thread safe in Java ? Core Java
Ans. No Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve     rare Q849. How can one determine if JVM is 32-bit or 64-bit from Java Program ? Core Java
Ans. There is a Java system property "sun.arch.data.model" that can tell if JVM is 32 bit of 64 bit
System.getProperty("sun.arch.data.model") can be used to get that property. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  jvm intermediate   rare Q850. Watching a directory for changes. Monitoring a directory. Core Java
Ans. http://www.logicbig.com/tutorials/core-java-tutorial/java-nio/java-watch-service/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  file handling  monitoring directory Q851. Have you ever tried compressing data using Java ? Core Java
Ans. Yes, we can use ZipInputStream class for compressing the FileInputstream object. Sample Code for ZipInputStream Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  file handling  FileInputstream  ZipInputStream  file handlingUsually asked with Questions related to Generics. Q852. What are Type Erasures in Java ? Core Java
Ans. Type erasure applies to the use of generics. When generics are used, they're pre compiled into compile time checks and execution-time casts. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  type erasures  generics Asked in 2 Companies intermediate   rare Q853. How do you prefer to store money values in your application ? Core Java
Ans. At Application level we use either Double or BigDecimal , preferably BigDecimal. At Database level we use Number with decimal precision 3. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  data typesBasic and Very Frequently asked. Q854. What is Polymorphism in Java ? Core Java
Ans. Polymorphism means the condition of occurring in several different forms.
Polymorphism in Java is achieved in two manners
1. Static polymorphism is the polymorphic resolution identified at compile time and is achieved through function overloading whereas
2. Dynamic polymorphism is the polymorphic resolution identified at runtime and is achieved through method overriding. Sample Code for overloading Sample Code for overriding Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  polymorphism  object oriented programming (oops)  oops concepts  oops concepts Asked in 108 Companies Basic   frequent Try 2 Question(s) TestVery frequently asked. Q855. What is the use of synchronized keyword ? Core Java
Ans. Synchronize is used to achieve mutual exclusion i.e at one time, the segment of the code, method that has been declared synchronized should be executed by single thread only and hence the lock needs to be retrieved before executing the segment and then released. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  threads  multithreading  synchronized Asked in 5 Companies basic   frequent Frequently asked. Q856. When does the finally block gets executed ? Core Java
Ans. A finally block of code always executes, whether or not an exception has occurred.The only time finally won't be called is if you call System.exit() or if the JVM crashes first. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  finally  exception handling  exceptions Asked in 4 Companies basic   frequent Q857. What is the difference between Collections.emptyList() and creating new instance of List using new ? Core Java
Ans. But Collections.emptyList() returns an Immutable list whereas new arraylist() creates a mutable list.
Advantage of getting an empty list using Collections.emptyList is that it returns a singleton list which can be shared among many references and hence made immutable. This is good fit for situations where we would like to initialize a list to basic minimum empty to avoid null pointer exception. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collections  Collections.emptyList  immutable  immutability  immutability Q858. Is it a valid identifier in Java ?
int 123Abc = 5; Core Java
Ans. No, identifier in java cannot start with a numeral. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  identifiers Q859. Can we have an inner class within Interface ? Core Java
Ans. Yes, we can define an inner class within interface.Inside the interface, the inner class is implicitly public static.
So the following is legit in Java
public interface BuggyBreadInterface {
void doSomething();
public class BuggyBreadClass implements BuggyBreadInterface{
void doSomething(){
System.out.println("Do something");
}
}
} Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  inner classes  inner class in interface  nested classes Q860. Write code for Binary tree traversal ? Core Java
Ans. http://javabeat.net/binary-search-tree-traversal-java/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve   Asked in 1 Companies Q861. Can you assign value to a static final type at runtime ? Core Java
Ans. No Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  static  final  static final Asked in 1 Companies Q862. In case of a synchronized method, Does the method only gets locked or the whole object ? Core Java
Ans. Just the method gets locked. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  synchronization  multithreading Asked in 1 Companies Very Frequently asked to fresh graduates and less experienced. Q863. What are the principle concepts of Object Oriented Programming ? Core Java
Ans. Abstraction
Polymorphism
Inheritance
Encapsulation Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  oops  oops concepts Asked in 1 Companies basic   frequent Q864. Can we have try statement without catch? If try statement contains return will the finally block be executed? What happens if there is an exception inside finally block? Core Java
Ans. Yes, with finally.
Yes, finally block will be executed even if there is no exception in try block.
If finally throws an exception, the exception gets thrown to the calling module. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception handling Asked in 1 Companies intermediate Q865. Explain Exception Hierarchy in Java SE ? Core Java
Ans. <a href="http://www.programcreek.com/2009/02/diagram-for-hierarchy-of-exception-classes/" rel="nofollow">http://www.programcreek.com/2009/02/diagram-for-hierarchy-of-exception-classes/</a> Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  exception hierarchy   exceptions Asked in 3 Companies Q866. Which data structure would you recommend for ordered and sorted data? Core Java
Ans. TreeSet and TreeMap are used for maintaining sorted elements. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  ordered collections  sorted collections Asked in 1 Companies basic   frequent Frequently asked to Fresh graduates. Q867. Write a program to implement Binary search ? Core Java
Ans. http://algs4.cs.princeton.edu/11model/BinarySearch.java.html Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  binary search  search Asked in 5 Companies basic   frequent Try 1 Question(s) Test Q868. Write a java program to implement Quick sort ? Core Java
Ans. http://www.programcreek.com/2012/11/quicksort-array-in-java/ Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  quick sort  sort Asked in 2 Companies   frequent Q869. Which is the root interface of Collection classes in Java ? Core Java
Ans. java.util.Collection Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  collection interface  collections Q870. What are the disadvantages of multithreading ? Core Java
Ans. 1. Switching Overheads - Even though multi threading aims at improving performance by reducing the wait time and hence improving overall throughput, there is a cost of switching resources between threads and sometime this cost can surpass the benefits if there isnt much wait for IO or external communication.
2. Debugging is hard with multi threaded code.
3. Deadlock - Execution of multi threaded code many a times lead to deadlock due to shared resources. Help us improve. Please let us know the company, where you were asked this question : Like Discuss Correct / Improve  multithreading  threads Asked in 4 Companies