Core Java Programming Interview Questions and Answers
by Anuradha, on May 26, 2018 9:35:59 AM
Q1. What is JVM and is it platform independent?
Ans: Java Virtual Machine (JVM) is the heart of java programming language. JVM is responsible for converting byte code into machine readable code. JVM is not platform independent, thats why you have different JVM for different operating systems. We can customize JVM with Java Options, such as allocating minimum and maximum memory to JVM. It’s called virtual because it provides an interface that doesn’t depend on the underlying OS.
Q2. How to reverse a string in java?
Ans: Write a java program to reverse a given string using recursive method.
Q3. What is the difference between JDK and JVM?
Ans: sJava Development Kit (JDK) is for development purpose and JVM is a part of it to execute the java programs.JDK provides all the tools, executables and binaries required to compile, debug and execute a Java Program. The execution part is handled by JVM to provide machine independence.
Q4. How to create a pyramid of numbers in java?
Ans: Write a java program to create a pyramid of numbers in different patterns.
Q5. What is the difference between JVM and JRE?
Ans: Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn’t contain any development tools like java compiler, debugger etc. If you want to execute any java program, you should have JRE installed.
Q6. How do you remove all white spaces from a string in java?
Ans: Write a java program to remove all white spaces from a given string.
Q7. Which class is the superclass of all classes?
Ans: java.lang.Object is the root class for all the java classes and we don’t need to extend it.
Q8. How to find duplicate characters in a string in java?
Ans: Write a java program to find duplicate characters and their count in a given string. For example, in a string “Better Butter”, duplicate characters and their count is t : 4, e : 3, r : 2 and B : 2.
Q9. Why Java doesn’t support multiple inheritances?
Ans: Java doesn’t support multiple inheritance in classes because of “Diamond Problem”. To know more about diamond problem with example, read Multiple Inheritance in Java.However multiple inheritance is supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of diamond problem with interfaces.
Q10. How do you check the equality of two arrays in java?
Ans: Write a java program to check the equality of two arrays using different methods.
Q11. Why Java is not pure Object Oriented language?
Ans: Java is not said to be pure object oriented because it support primitive types such as int, byte, short, long etc. I believe it brings simplicity to the language while writing our code. Obviously java could have wrapper objects for the primitive types but just for the representation, they would not have provided any benefit.As we know, for all the primitive types we have wrapper classes such as Integer, Long etc that provides some additional methods.
Q12. Anagram program in java.
Ans: Write a java program to check whether two given strings are anagram or not. Two strings are said to be anagram if they contain same set of characters but in different order. For example, “Mother In Law” and “Hitler Woman” are anagram
Q13. What is difference between path and class path variables?
Ans: PATH is an environment variable used by operating system to locate the executables. That’s why when we install Java or want any executable to be found by OS, we need to add the directory location in the PATH variable. If you work on Windows OS, read this post to learn how to setup PATH variable on Windows.Classpath is specific to java and used by java executables to locate class files. We can provide the classpath location while running java application and it can be a directory, ZIP files, JAR files etc.
Q14. Armstrong number program in java.
Ans: Write a java program to check whether a given number is Armstrong number or not. A number is called an Armstrong number if it is equal to sum of its digits each raised to the power of number of digits in it. For example: 153, 9474, 54748 are some Armstrong numbers.
Q15. What is the importance of main method in Java?
Ans: main() method is the entry point of any standalone java application. The syntax of main method is public static void main(String args).main method is public and static so that java can access it without initializing the class. The input parameter is an array of String through which we can pass runtime arguments to the java program. Check this post to learn how to compile and run java program.
Q16. How to find duplicate elements in an array?
Ans: Write a java program to find duplicate elements in the given array.
Q17. What is overloading and overriding in java?
Ans: When we have more than one method with same name in a single class but the arguments are different, then it is called as method overloading.Overriding concept comes in picture with inheritance when we have two methods with same signature, one in parent class and another in child class. We can use @Override annotation in the child class overridden method to make sure if parent class method is changed, so as child class.
Q18. How to find sum of all digits of a number in java?
Ans: Write a java program to find sum of all digits of a given number.
Q19. How to find second largest number in an integer array?
Ans: Write a java program to find second largest number in an array of numbers.
Q20. How to perform matrix operations in java?
Ans: Write a java program to perform matrix operations like matrix addition, matrix subtraction, matrix multiplication and transpose of a matrix.
You may also interested in...