Java is the most popular platform in today’s competitive world. Whether you are a fresher or highly experienced professional, Java opens up endless opportunities and benefits. Time is changing, and so is the round of java interview. You can brush up your skills by choosing the most popular certification in Java.
Worldwide, Java is used by more than 10 million developers to develop software and applications. Here is the huge list of core Java interview questions and answers that should be in your to-do list before appearing for an interview.
Q1. What are the differences between C++ and Java?
Ans. Differences between C++ and Java are:
|Platform dependent||C++ is platform dependent||Java is platform-independent|
|Multiple inheritance||It supports multiple inheritance.||It doesn’t support multiple inheritance through the class. Interfaces can obtain it in Java.|
|Pointers||It supports pointers. You can write a program using a pointer in C++.||It supports pointer internally and has restricted pointer support. Java does not allow to write pointer program.
|Thread Support||It doesn’t have built-in support for threads.
It depends on third-party libraries for thread support.
|It has built-in thread support.|
|Call by Value and Call by reference||It supports both call by value and call by reference.||It supports call by value only. There is no call by reference in Java.|
|Object-oriented||It is an object-oriented language.||It is also an object-oriented language.|
Q2. What do you understand by Java virtual machine?
Ans. Java Virtual Machine is a virtual machine that enables the computer to run the Java program. The Java code is compiled by JVM to be a Bytecode, which is machine-independent and close to the native code.
Q3. What are the different types of memory areas allocated by JVM?
Ans. Different types of memory areas allocated by JVM are:
- Program counter register
- Native method stack
Q4. What is the difference between StringBuilder and StringBuffer?
- Synchronized means two threads can’t call the methods of StringBuffer simultaneously.
- Less proficient than StringBuilder
- Non-synchronized means two threads can call the threads of StringBuilder simultaneously
- More efficient than StringBuffer
Q5. Which are the four principle concepts upon which object-oriented design and programming rest?
Ans. Following are four principle concepts upon which object-oriented design and programming rest:
Q6. What do you mean by loops?
Ans. Loop is used in programming to execute a statement or a block of statement repeatedly.
Q7. How can you prevent a method from being overridden?
Ans. Use the final modifier on the method declaration to prevent a method from being overridden.
// method statements
Q8. Is it necessary to create an object for an interface?
Q9. Give an example of a ternary operator?
Q10. Name the modifiers allowed for methods in an interface?
Ans. Public and abstract modifiers.
Q11. What do you mean by super?
Ans. It is a keyword used to access the method variables from the superclass.
Q12. When should we use abstract classes?
Ans. We use interfaces when something in our design changes frequently.
Q13. What is the base class of all classes?
Q14. Define a constant variable?
Ans. A constant variable should be declared as final and static. E.g.:
Q15. How can you implement the singleton pattern?
Ans. You can implement singleton pattern by following common concepts which are:
- A private static variable of the same class that is the only instance of the class
- Private constructor to restrict instantiation of the class from other classes
- Public static method that returns the instance of the class
Q16. Give one difference between the continue and break statement?
Ans. Continue statement is used to end the current loop iteration while when the break statement is used inside a loop, the loop gets terminated and return at the next statement.
Q17. What do you understand by thread-safe?
Ans. It is simply the program that behaves correctly when multiple simultaneous threads are using a resource.
Q18. What is the use of the default constructor?
Ans. A constructor is used to initialize the object. E.g.:
Q19. Can you make a constructor final?
Q20. What is object cloning?
Ans. It is a way to create an exact copy of an object. Clone() is defined in the object class.
Q21. Which package is imported by default?
Ans. Java.lang package is imported by default.
Q22. Can the non-static variable be accessed in a static context?
Q23. Can you make an array volatile?
Ans. Yes, you can make an array volatile but only the reference, which is pointing an array.
Q24. Which operator is considered to be with the highest precedence?
Ans. Postfix operators.
Q25. What is the advantage of Encapsulation?
Ans. The main advantage is the ability to modify implemented code without breaking the code of others.
Q26. Can a source file have more than one class declaration?
Ans. Yes, a source file can have more than one class declaration.
Q27. What is the difference between throw and throws?
Ans. Throw is used to trigger an exception, while throws is used in the declaration of exception.
Q28. What is the JAR file?
Ans. A JAR (Java Archive files) holds java classes in a library.
Q29. Define the JIT compiler?
Ans. JIT (just-in-time compiler) is enabled by default and is activated when a java method is called.
Q30. Is Empty .java file name a valid source file name?
Ans. Yes, Empty .java file name a valid source file name as Java allows us to save java file by .java only. First, we need to compile it by using javac .java and run by using java classname.
//compile by javac.java
Q31. Is delete, next, main, exit, or null keyword used in Java?
Ans. No, these keywords are not used in Java.
Q32. What is the output of the following Java program?
Q33. What is the output of the following Java program?
Ans. Here, the above code will give the compile-time error because there is a need for integer value in the second part of for loop where we are putting ‘0’.
Q34. Mention what will be the initial value of an object reference, which is defined as an instance variable?
Ans. In Java, all object references are initialized to null.
Q35. Why is a default constructor used in Java?
Ans. The default constructor is used to assign the default value to the objects. It is created by the Java compiler implicitly if there is no constructor in the class.
Q36. What is the output of the following Java program?
X = 10 y = 15
In this program, the data type of the variables x and y, i.e., byte gets promoted to int, and the first parameterized constructor with the two integer parameters is called.
Q37. What is the output of the following Java program?
Naukri naukri = new Naukri();
Ans. Output: 0
Here, the variable a is initialized to 0 internally where a default constructor implied in the class, the variable i is initialized to 0 since there is no constructor in the class.
Q38. What is the static method in Java?
Ans. Three steps to define a static method:
- A static method specifically belongs to the class, not from the object.
- A static method can be used to access and change the value of the static variable.
- There is no need to create an object to call static methods.
Static block initialized.
Value of x: 10
Value of y: 40
Q39. Is it possible to override the static methods?
Ans. No, It is not possible to override static methods.
Q40. Explain what the static block is?
Ans. A static block is used to initialize the static data member in the program. It is executed before the main method.
static block is invoked
Q41. Is it possible to execute a program without main() method?
Q42. What if the static modifier is removed from the signature of the main method?
Ans. If the static modifier is removed from the signature of the main method, then program compiles. Whereas, at runtime, It throws an error “NoSuchMethodError.”
Q43. Is it possible to declare the static variables and methods in an abstract class?
Ans. Yes, It is possible to declare static variables and methods in an abstract method. There is no need to make the object to access the static context. We can access the static context declared inside the abstract class by using the name of the abstract class.
i = 2020
Q44. How do we do constructor chaining be done using this keyword?
Ans. Constructor chaining is used to call one constructor from another constructor of the class as per the current class object. This keyword is used to perform constructor chaining within the same class.
ID: 10121 Name:Ron age:22 address: Boston
Q45. Explain what is super in Java?
Ans. The super keyword in Java is used to refer to the immediate parent class object as it is a reference variable. When there is any instance created of the subclass, then the instance of the parent class is created implicitly, which is referred by a super reference variable.
Student is created
Jack is created
Q46. Is it possible to use this() and super() both in a constructor?
Ans. No, because this() and super() must be the first statement in the class constructor.
Demo.java:5: error: the call to this must be the first statement in the constructor
Q47. Is it possible to override private methods?
Ans. No, it is not possible to override the private methods because the scope of private methods is limited to the class, and we cannot access these methods outside of the class.
Q48. Explain what the covariant return type is?
Ans. In Java, it is possible to override any method by altering the return type if the return type of the subclass overriding method is the subclass type, which is known as a covariant return type. The covariant return type defines that the return type may vary in the same direction as the subclass.
Output: Welcome to the covariant return type
Q49. Is it possible to initialize the final blank variable?
Ans. Yes, it is possible to initialize the final blank variable in the constructor, if it is not static. If it is a static blank final variable, it can be initialized only in the static block.
Q50. Define immutable objects?
Ans. Immutable objects are simply objects which can’t be modified once created.
These are some of the frequently asked core Java interview questions and answers. Your conceptual understanding will make a big difference on whether or not you get through a job interview. Be confident, energetic, and happy!