+91 9404 340 614    gyaanibuddy@gmail.com

Like
1 Like

Most asked Java Interview Questions (part-1)

Last updated on Aug. 26, 2021, 5:01 a.m. by Akshay Sadawarte

Image desc
Here we discuss some of the most asked java interview questions and their respective answers.

Most asked Java Interview Questions (part-1)

Java is a very popular and versatile coding language having various applications.

Here is the list of some most asked interview questions on Java(most asked java interview questions and answers).

Prepare these well and become java interview ready.

(most asked java interview questions and answers)

(Placement preparation) (interview preparation)

Q-1. What is the difference between Overloading and Overriding?
Ans: When two or more methods in the same class have the same name but different parameters, it’s called Overloading. When the method signature (name and parameters) are the same in the superclass and the child class, it’s called Overriding.

  • Overriding implements Runtime Polymorphism whereas Overloading implements Compile time polymorphism.The method Overriding occurs between superclass and subclass.
  • Overloading occurs between the methods in the same class.Overriding methods have the same signature i.e. same name and method arguments.
  • Overloaded method names are the same but the parameters are different.With Overloading, the method to call is determined at the compile-time. With overriding, the method call is determined at the runtime based on the object type.
  • If overriding breaks, it can cause serious issues in our program because the effect will be visible at runtime. Whereas if overloading breaks, the compile-time error will come and it’s easy to fix.

Q-2.  Is it possible to restrict Inheritance?
Ans: Yes, it is. You can restrict Inheritance by:
a. Using the final keyword.
b. Making the method final.
c. Using private constructor.
d. Using (//) Javadoc comment.

(most asked java interview questions and answers)

(Placement preparation) (interview preparation)

Q-3. In Java thread programming, which method is a must implementation for all threads?
Ans: Run() is a method of Runnable interface that must be implemented by all threads.

Q-4. What’s the difference between Abstract Class and Interface in Java?
Ans: Abstract class vs Interface

  • Type of methods: Interface can have only abstract methods. An abstract class can have abstract and non-abstract methods. From Java 8, it can have default and static methods also.
  • Final Variables: Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.
  • Type of variables: Abstract class can have final, non-final, static and non-static variables. The interface has only static and final variables.
  • Implementation: Abstract class can provide the implementation of the interface. Interface can’t provide the implementation of an abstract class.
  • Inheritance vs Abstraction: A Java interface can be implemented using the keyword “implements” and an abstract class can be extended using the keyword “extends”.
  • Multiple implementations: An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  • Accessibility of Data Members: Members of a Java interface are public by default. A Java abstract class can have class members like private, protected, etc.

 

(most asked java interview questions and answers)

(Placement preparation) (interview preparation)

Q-5. Is there a difference between Object-Oriented and Object-Based language?
Ans: Object Based Languages

  • Object based languages supports the usage of object and encapsulation.
  • They does not support inheritance or, polymorphism or, both.
  • Object based languages does not supports built-in objects.
  • Javascript, VB are the examples of object bases languages.

Object Oriented Languages

  • Object Oriented Languages supports all the features of Oops including inheritance and polymorphism.
  • They support built-in objects.
  • C#, Java, VB. Net are the examples of object oriented languages.

Q-6. What's the base class in Java from which all classes are derived?
Ans: java.lang.object

(most asked java interview questions and answers)

(Placement preparation) (interview preparation)

Q-7. What are Java Packages? What's the significance of packages?
Ans: In Java, a package is a collection of classes and interfaces which are bundled together as they are related to each other. A package in Java is used to group related classes.  Use of packages helps developers to modularize the code and group the code for proper re-use. Once code has been packaged in Packages, it can be imported in other classes and used. Think of it as a folder in a file directory. We use packages to avoid name conflicts, and to write a better maintainable code. Packages are divided into two categories: 1) Built-in Packages (packages from the Java API) 2) User-defined Packages (create your own packages)

Q-8. Can we declare the main method of our class as private?
Ans: In java, main method must be public static in order to run any application correctly. If main method is declared as private, developer won't get any compilation error however, it will not get executed and will give a runtime error as “Error: Main method not found in class, please define the main method as: public static void main(String[] args)or a JavaFX application class must extend javafx.application.Application”.

Q-9.  Is String a data type in java?
Ans: String is not a primitive data type in java. When a string is created in java, it's actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object.

 

Q-10. How garbage collection is done in Java?
Ans: In java, when an object is not referenced any more, garbage collection takes place and the object is destroyed automatically by JVM. It is controlled by a thread known as Garbage Collector. Java provides two methods System.gc() and Runtime.gc() that sends request to the JVM for garbage collection. Remember, it is not necessary that garbage collection will happen.

Q-11. How can we use primitive data types as objects?
Ans: Primitive data types like int can be handled as objects by the use of their respective wrapper classes  While working with collections in Java, we use generics for type safety like this: ArrayList<Integer> instead of this ArrayList<int>. The Integer is a wrapper class of int primitive type. We use wrapper class in this case because generics needs objects not primitives. 

Q-12. What is the difference between ArrayList and vector?
Ans: An ArrayList is not suitable for working in a thread based environment. A vector is built for thread-based executions. ArrayList does not support legacy functions whereas a vector has support for legacy functions.

Q-13. Why we can’t create the object of abstract class in java?
Ans:  You can't create an object of an abstract class because there is an abstract method which has nothing. 
If we create an object of that abstract class and call the method having no body(as the method is pure virtual) it will give an error.

Q-14. How is polymorphism achieved in Java?
Ans: Polymorphism in Java has two types: Compile time polymorphism (static binding) and Runtime polymorphism (dynamic binding). Method overloading is an example of static polymorphism, while method overriding is an example of dynamic polymorphism

Q-15. What happens if we make the constructor final?
Ans: If we make the constructors final then the class variables initialized inside the constructor will become unusable. Their state cannot be changed. A final method cannot be overridden by any subclasses. ... But, in inheritance subclass inherits the members of a superclass except constructors. In other words, constructors cannot be inherited in Java therefore, there is no need to write final before constructors

(most asked java interview questions and answers)

(Placement preparation) (interview preparation)

Stay tuned for more such content! Let's spread the Gyaan ;)

...

by Akshay Sadawarte
SCTR's Pune Institute of Computer Technology Pune

I make Vlogs when I am bored! An occasional writer ;)
blog comments powered by Disqus