Core Java

Core JAVA Interview Questions/Answers Questions: How to crack JAVA interview?

March 12, 2017 Interview Questions/Answers
Java Interview

“In this article, I am going to talk about some most important and most frequently ask Core JAVA interview question/answers to crack interview easily. If you want to stand out of thousands of candidates then read this article it will clear your basics java concepts.”

Q1. What is the difference between JDK, JRE, and JVM?

JVM(Java Virtual Machine) is an abstract machine. It’s a specification that gives run-time atmosphere within which java bytecode are often executed.

The JVM performs following main tasks:

  • Loads code
  • Verifies code
  • Executes code
  • Provides run-time atmosphere

JRE is a word form for Java Runtime atmosphere.It is used to give runtime atmosphere. It is the implementation of JVM,  physically exists and also contains set of libraries + different files that JVM uses at run-time.

JDK is a word form for Java Development Kit. It physically exists. It contains JRE + development tools.

Q2. How many types of memory areas are allocated by JVM?

Classloader: Classloader is a small part of JVM that is used to load class files.

Heap: It is the run-time data area process in which all the objects are allotted.

Stack: Java Sack stores frames. It keeps local variables as well as partial results, and also actively take a part in method invocation process and return value process.

Native Method Stack: It has all the native methods which are used for the application.

Program Counter Register: PC (program counter) register has the address of the JVM instruction which is currently being executed.

Class (Method) Area: Class (Method) Area stores class by class structures, for example, the run-time constant pool, field and method data, the code for methods.

Execution Engine: It contains three main activity a virtual processor, Interpreter which is ready bytecode stream then execute instructions and the last one is Just-In-Time(JIT) compiler.

Q3. What is JIT ( Just-In-Time) compiler?

Just-In-Time(JIT) compiler is a very useful tool which used to improve the overall performance. It compiles bytecode line by line which has similar functionality at the same time because it reduces the amount of total time needed for compilation process.

Q4. What is ‘write once and run anywhere’ in JAVA?

Java platform has JVM (Java virtual machine ) and JAVA API. First of all, JAVA API supports to write java files and then java compiler converts java file in to a class file (byte stream) and in last  JVM converts byte stream inn to machine language so the computer can read it easily. Java is platform independent (Write once run anywhere) means I can write java code on any computer and compiled it and convert into byte stream then run on any platforms which have JVM.

Q5. What is classloader?

Classloader is a small part of JVM that is used for loading class files.

The Java Classloader is a very important part of the Java Run-time Environment which dynamically loads Java classes into the Java Virtual Machine. The Java run time system doesn’t need to know about files or file systems because of classloaders will help with it.

Q6. If I don’t provide any arguments on the command line, then the String array of Main method will be empty or null?

The main method always receives its parameter, which is known as an array of String. If you don’t pass any command-line arguments, args is empty, but it’s still there.

Q7. What is the default value of the local variables?

JAVA doesn’t give a Default value for local variables. You must provide value to a local variable before using it otherwise it will give you an error.

The local variables are not initialized to any default values. We shouldn’t use local variables with out initialization. Even the java compiler throws an error if you doesn’t provide value to local variables.

Q8. What will be the initial value of an object reference which is defined as an instance variable?

All object references are initialized to null in Java. But to do anything useful with these references, you should set references to a valid object, else you will get NullPointerExceptions every time when you try to use default initialized references.

Q9. What is a constructor?

constructor is a special method of a class in object-oriented programming, which initializes an object of the same type. A constructor is an instance method which has the same name as the class name, and it can be used to set the values of the object members, which is to default or to user-defined values.

Q10. What are Rules to define a constructor?

The first and most important rule is that the name of any constructor must be same as the of a class name. By mistake, if you give the different name it will give you compile time error.

The second important rule is constructors don’t have a return type. If you give return type to any constructor, it will be harmful to another method. But compiler always gives a warning that this method has a constructor name, which means, it is allowed to have a method name, constructor name and same as class name same but it is not recommended.

Every class should have at least one constructor. If you don’t have any constructor for a particular class, no needs to worry, the compiler will give you default constructor which is always public and it has no arguments.

A constructor can be declared as private. But if you declare constructor as private, you can’t use it outside of that particular class.

One class can have more than one constructors which are called Constructor Overloading. 

Duplicate Constructors are not allowed in same class. By mistake, if you create duplicate constructors, it gives you compile time error.

Keywords like a public, protected and private are allowed before a constructor name. If you use any other keyword before a constructor name, it gives compile time error.

Q11. What is the purpose of default constructor?

Constructors are very important for any class because they are used to set up properties and t perform some tasks. Each object that which created is done using the only constructor. When any object is created by using ” new ” keyword the constructor for that particular class is called.

Q12. Is constructor inherited?

As we know fields, methods and nested classes are class members but Constructors are not a class member. A subclass inherits with its superclass to only those which are a member of the class(fields, methods, and nested classes). As Constructors are not class members, they can not be inherited by a subclass, but the constructor of the superclass can be invoked from the subclass. 

 Q13. Can you make a constructor final?

When you set a method as final that means you don’t want to override any class. But the constructor can not be overridden, so it is clean.

When you set a method as abstract that means the method doesn’t have a body and it should be created in a child class. But the constructor is called implicitly when the new keyword is used so it can’t lack a body.

When you set a method as static that means the method belongs to the class but not to the particular object. But the constructor is implicitly called to initialize an object that’s why there is no meaning of having a static constructor.

Q14. What is static variable?

Value for all static variable’s is same for all the objects of the class.

In other words, all instances(objects) of the same class share the same copy of static variables.

Q15. What is the static method?

If you use the static keyword with any method, it is known as a static method. A static method belongs from class rather than an object of a class. For a static method, we don’t need to create any instance of a class. A static method can access only static data member and also able to change the value of that member.

Q16. What is static block?

The mostly static block is used for changing the default values of any static variables. Static block gets executed as soon as the class is loaded in the memory.

Any class can have multiple Static blocks and they will execute in the exact sequence in which they have been created by the program.

Q17. Can we execute a program without a main() method?

According to my knowledge, we can’t execute any program without a main method because when you run any java program  Java Virtual Machine(JVM) look for the main method, if JVM couldn’t find the main method it will give you run time error. Exception in thread main could not find the main class.

Q18. What is the difference between static (class) method and instance method?

Class methods are always declared as a static method. You don’t need to create an instance of the class in a static method. Class methods can only operate on class members and not on instance members because class methods are not aware of instance members in the class.

On the other hand, instance methods require an instance of the class, so an instance of a class needs to be created by using the “new” keyword. Instance methods operate on specific instances of classes. You can’t declare instance methods as static. Instance methods are not declared as static.

Q19. What is method overloading?

Method Overloading allows a particular class to have two or more methods with the same name, but their argument lists should be different. Argument lists could differ in:( Number of parameters, Data type of parameters, Sequence of Data type of parameters). It is also known as Static Polymorphism.

Q20. What is “this” in java?

“this” is a keyword in Java. “This” keyword can be used inside of any Method or constructor of Class. It(this) is used for referencing to the current Object whose Method/constructor is being already invoked. The “this” keyword is used to refer to any member of the only current object from within an instance Method.

Q21. What is Inheritance?

In JAVA programming Inheritance allows you to create a class which can be derived from the another class. One class that is derived from another class inherits the other class. The class that is inherited is called the parent class, the base class, or the superclass.

Q22. Why is multiple inheritances not supported in java?

Java does not support inheritance because to remove ambiguity, for example, multiple inheritances can cause ambiguity in few scenarios. One of the most common scenarios is Diamond problem.

Q23. What is composition?

Using instance variables which are references to the other objects is called Composition(HAS-A relationship). For example Maruti has Engine, or House has Bathroom.

Q24. What is object cloning?

In JAVA programming clone() is used for object duplication. In Javaobjects are manipulated through reference variables, and there is no operator for copying an object—so the assignment operator duplicates the reference, not the object. So the clone() method fulfill this missing functionality.

Q25. Can we override static method?

No, Static methods can not be overridden because it is part of a class, not an object. You can overload static method. The static resolves against the class, not the instance.

Q26. What is the difference between method Overloading and Overriding?

Overloading occurs when two or more methods in one class have the same method name with different parametersOverriding occurs when two methods with the same method name and same parameters. One of the methods is in the parent class and the other is in the child class.

Q27. What is Exception Handling?

An exception is a problem that arises during the execution of a program. When an Exception occurs the whole flow of the program is disrupted and the program terminates automatically. So this kind of behavior is not recommended, and these exceptions are to be handled.

Q28. What is final variable, final class, final method?

In Java final variable is used to make its value unchangeable. But after that, you can change the value in the constructor. Once again, if the variable is static then it is a compilation error.

You can not override or hide final method by its superclass. This is used to stop the unexpected behavior from a subclass which can alter a method that may be very crucial to the function.

We can’t be sub classed to the final class. It gives can give you security and efficiency that’s why many of the Java standard library classes are final. for example java.lang.System

 Q29. What is Polymorphism?

The ability of objects to take on many forms is called Polymorphism. For example, Polymorphism is used when a parent class reference is used to refer to child class objects. In simple language, any Java object that can pass more than one IS-A relationship test is considered to be polymorphic.

Q30. What is Runtime Polymorphism?

Polymorphism is used when a parent class reference is used to refer to child class objects. Runtime Polymorphism is also known as Dynamic Binding or method overriding. In this Mechanism call to an overridden function is resolved at a Run-Time. In this kind of polymorphism, reference of class P can hold an object of class P or an object of any sub classes of class P. For e.g. if class Q extends class P then both of the following statements are valid.

Q31. What is static binding?

When any type of the objects is determined at the only compiled time, it is known as static binding.If there is any private, final or static method in a class, there is static binding.

Q32. What is dynamic binding?

When any type of the object is determined at only run-time, it is known as dynamic binding.

Q33. What is Abstraction?

Abstraction is used to hide some important details and only show the needed features of the object. In other words, only it deals with the front view of an object.

In sort abstraction is a process of hiding the coding details from the user, the only final product will be provided to the users.

for example when you want to send an e-mail to your friend you just need to type content and provide your  friend’s e-mail address and just click send button but you don’t know what exactly happens when you send an e-mail. So some implementation part will be hidden from the users is called Abstraction.

Q34. What is the difference between abstraction and encapsulation?

Abstraction only focuses on what the object does instead of how it does it.

Encapsulation means hiding the internal details or mechanics of how an object does something. Like when you use your cellphone, you know what the cellphone does but you may not know the process behind it because it is encapsulated.

Q35. What is the difference between Checked Exception and Unchecked Exception?

Checked exceptions are always checked at compile-time. If a method is throwing a checked exception then it will be handling the exception using try-catch block, otherwise, it will give you a compilation error.

Unchecked exceptions are not checked at run time that’s why they are called as unchecked exceptions. If your program is throwing an unchecked exception and even if you didn’t handle that exception, the program won’t give a compilation error. This exception occurs due to the bad data provided by the user.

Q36. What is exception propagation?

In the last Uncaught exceptions are propagated in the call stack until the whole stack becomes empty, this propagation is called Exception Propagation.

Q37. What is nested class?

nested class is a part of its enclosing class. If nested classes are declared private still they allowed non-static nested classes to have access to other members of the enclosing whereas static nested classes don’t allow to access other members of the enclosing class.

Q38. What is the purpose of the toString() method in java?

Overriding the objects of toString method is used to implementing toString method in java. Whenever we need a string representation of an object we have to use the java toString() method. This method is defined in Object class.

Q39. What is Garbage Collection?

The garbage collector is a special program in java, which runs on the Java Virtual Machine. It garbages all of the objects which are not being used since a long time of period by a java application. This process is done automatically which is part of the automatic memory management.

Q40. What is the purpose of finalize() method?

Always finalize method is called before garbage is about to collect. Finalize() method can be invoked at any time after it has become eligible for garbage collection.

Q41. What is the difference between final, finally and finalize?

A final class can not be inherited, the final method can not be overridden and final variable value can not be changed.

Finally, is used to place important code, it will be executed whether an exception is handled or not.

Finalize is used to perform clean up processing just before an object is garbage collected.

Q42. What is the purpose of the Runtime Class?

Runtime class is used to provide access to the Java runtime system. For instance,  runtime information like memory availability, invoking the garbage collector, etc.

Q43. What is a Stream?

The sequence of data is called Stream. There are two kinds of Streams.

InPutStream: InputStream is ReadOnly so it is used to read data from a source.

OutPutStrem: OutputStream is WriteOnly so it is used for writing data to a destination.

Q44. What is serialization?

Writing state of an object into a byte stream is called Serialization in java. Mainly it is used in Hibernate, RMI, JPA, JMS technologies. The reverse process of the serialization is called deserialization.

Q45. What is Socket Class?

The socket is the endpoint for communication between two computers. Actual work of the socket is to performed the instance of the SocketImpl class. java.lang.Object | public class Socket extends Object. This class implements client sockets.

Q46. What is a thread?

thread is an independent and individual path for execution within the same program. Many threads can run simultaneously within a program. Every thread in Java is created and controlled by the java.lang.Thread class. Multithreading refers to two or more tasks executing concurrently within a single program.

Q47. What is Collection? What is a Collection Framework?

A Collection is also called as a container which is an object that groups multiple elements into a single unit.

Collections framework provides a unified architecture for manipulating and representing collections.

Q48. What is Pass by Value and  Pass by Reference?

One of the biggest confusion in Java programming language is whether java is Pass by Value or Pass by Reference. I think 80% of interviewee’s face this question during their interview.

Pass by Value: The method parameter values are copied to another variable and then the copied object is passed, that’s why it’s called pass by value.

Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.

“I hope this question/answers are useful for your Java interview. Because now a days mostly interviewer ask Core JAVA interview question in interview to make sure that you know the basics or not. If you guys have any question please comment below, I will get back to you as soon as possible. And also if you guys want questions on Advance Java topics let me know I will update my block as per your requirements.”