Java Programming Interview Questions
Here below we have provided Java Programming Interview Questions for Fresher’s as well as experienced candidates. Now interested aspirants can download the PDF for interview programming questions on collections in java.
A set of java programming questions, especially programming questions asked in most of the interviews.Some of the questions are insignificant and problematic. So gets prepared core java programming interview questions and answers before getting embarrassed in interview.
Java Programming Interview Questions
About Interview Programming Questions In Java
Top java programming interview questions are some programming exercises which is asked to be solved in java programming language in limited time during any core Java or J2EE interview.
Thesejava collections programming interview questions are collected from different sources. So those who want to know more about interview questions on java programming, they can go through this complete page which is well structured by the team of recruitmentresult.com.
Java is computer programming language that is designed to have as few implementation dependencies as possible.Java Virtual Machine (JVM) is used to run Java as it executes the Java bytecode. For the security of Java applications, Java platform provides automatic memory management and it also reduces memory corruption and vulnerabilities.
Core Java Interview Programming Questions
“Core Java” is Sun’s term, used to refer to Java SE, the standard edition and a set of connected technologies, like the Java VM, CORBA, etc. Check below listed basic java programming interview questions and prepare well for interview session.
Question 1: Why the main method is static in java?
Answer: The method is static because otherwise there would be ambiguity: which constructor should be called. For example if your class looks like this:
|public class JavaClass|
protected JavaClass(int x)
public void main(String args)
Should the JVM call new Java Class(int)? What should it pass for x? If not, should the JVM instantiate Java Class without running any constructor method? because that will special-case your entire class – sometimes you have an instance that hasn’t been initialized, and you have to check for it in every method that could be called. There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That’s why main is static.
Want to Know? Java Interview Experience
Question 2: Is Java a pure object oriented language?
Answer: Java uses primitive data types and hence is not a pure object oriented language.
Question 3: What do you mean by platform independence?
Answer: Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux, Solaris, etc).
Question 4: When is the super keyword used?
Answer: Super keyword is used to refer:
- immediate parent class constructor
- immediate parent class variable
- immediate parent class method
Question 5: Why multiple inheritance is not supported in java?
Answer: Java supports multiple inheritances but not through classes, it supports only through its interfaces. The reason for not supporting multiple inheritances is to avoid the conflict and complexity arises due to it and keeps Java a Simple Object Oriented Language.
Question 6: Explain Final keyword in java?
Answer: Final keyword in java is used to restrict usage of variable, class and method.
Question 7: Can we override private methods in Java?
Answer:No, a private method cannot be overridden since it is not visible from any other class.
Want to Know? Interview Preparation Tips
Question 8: How are Java objects stored in memory?
Answer: In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate abject using new(), the object is allocated on Heap, otherwise on Stack if not global or static.
In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new (). So the object is always allocated memory on heap.
Question 9: What is object cloning?
Answer: Object cloning means to create an exact copy of the original object. If a class needs to support cloning, it must implement java.lang. Cloneable interface and override clone() method from Object class.Syntax of the clone() method is :
|protected Object clone() throws CloneNotSupportedException|
If the object’s class doesn’t implement Cloneable interface then it throws an exception ‘CloneNotSupportedException’ .
Question 10: Can a main () method be overloaded?
Answer: Yes. You can have any number of main () methods with different method signature and implementation in the class.
Question 11: Can a main () method be declared final?
Answer: Yes. Any inheriting class will not be able to have its own default main () method.
Question 12: Why Java is not pure Object Oriented language?
Answer: Java support primitive types such as int, byte, short, long etc that why it is not said to be pure object oriented language.
Question 13: When a thread blocks on I/O, what state does it enter?
Answer: When it blocks on I/O, A thread enters the waiting state.
Question 14: What is the difference between preemptive scheduling and time slicing?
Answer: Under preemptive scheduling, the highest priority task performs until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task performs for a predefined slice of time and then re-enters the pool of ready tasks.
Question 15: Which java.util classes and interfaces support event handling?
Answer: The Event Object class and the Event Listener interface support event processing.
Question 16:What is “this” keyword in java?
Answer: Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.
Usage of This Keyword
- Used to refer current class instance variable.
- To invoke current class constructor.
- It can be passed as an argument in the method call.
- It can be passed as argument in the constructor call.
- Used to return the current class instance.
- Used to invoke current class method (implicitly)
Read Out: Common Interview Mistakes
Question 17: What is blank final variable?
Answer: A final variable in Java can be assigned a value only once, we can assign a value either in declaration or later.
| final int i = 10;|
i = 30; // Error because i is final.
A blank final variable in Java is a final variable that is not initialized during declaration. Below is a simple example of blank final.
| // A simple blank final example|
final int i;
i = 30;
Question 18: How you can Reverse a String?
Answer: You can use indexOf() or substring() to reverse a String or alternatively, convert the problem to reverse an array by operating on character array instead of String.
Question 19: How do you run a Java application on the command line and set the classpath with multiple jars?
|java -cp /dev/myapp.jar:/dev/mydependency.jar com.codementor.MyApp|
Question 20: Why isn’t String‘s .length() accurate?
Answer: It isn’t accurate because it will only account for the number of characters within the String. In other words, it will fail to account for code points outside of what is called the BMP (Basic Multilingual Plane), that is, code points with a value of U+10000 or greater.
The correct way to count the real numbers of characters within a String, i.e. the number of code points, is either:
or, with Java 8:
Read Now: Interview Questions and Answers
Java interview programming questions and answers provided on this page might be helpful to you to crack interview. If you have any queries regarding programming interview questions in java, then you can type your comment in below provided Comment box. Our experts will soon solve your queries.
Follow us on social media and get all latest updates about java string programming interview questions, array programming interview questions in java and core java programming interview questions and answers for experienced and fresher’s.
Something That You Should Put An Eye On