Core Java interview questions

Core Java interview questions Download Core Java interview questions PDF

Core Java interview questions

Java and Python, both the languages hold an important place in today’s IT industry. But in some factors, one is better than the other. Such factors are:
  • Java is easy to use, whereas Python is very good in this case.
  • The speed of coding in Java is average, whereas in Python it is excellent.
  • In Java, the data types are statically typed, whereas in python the data types are dynamically typed.
  • Java is average in the case of data sciences and machine learning applications, whereas data science and machine learning applications are majorly based on python. Python is very good in this case.
An object is a basic entity in an object-oriented programming language which represents the real-life entities. Many objects are created by a java program which interacts by the invoking methods. The state of an object is represented by its attributes and reflects its properties. The behavior of an object is represented by the methods of the object and reflects the relationship of an object with the other objects. The identity of an object is a unique name provided to an object which helps one object to interact with other objects.
In Java, a class is a user-defined data type from which objects are created. It can also be called as a blueprint or prototype. A class is a collection of various methods and variables which represent a set of properties that are common to all the objects of one type. A class includes components such as: –
  • Modifiers: it states that a class can be public or can have a default access.
  • Class name: the name of a class should begin with a letter and should be same as the file name.
  • Body: the class body is surrounded by braces- {}.
Inheritance in Java is an important concept in any object-oriented programming language. Using inheritance, one class is allowed to inherit the properties and features of another class. The properties and features include the fields and the methods. Inheritance takes place with the help of keyword “extends”. There is a superclass whose features are inherited. It is also known as the base class or parent class. There is a subclass which inherits the properties of the base class. It is also known as derived class, extended class or the child class. Inheritance also supports the concept of “reusability”.
Following are the differences between StringBuffer and StringBuilder in java: –
  • StringBuffer methods are synchronized, whereas StringBuilder methods are non-synchronized.
  • In StringBuffer, a storage area is heap which can be modified easily, whereas in StringBuilder, storage is heap which can be modified easily.
  • StringBuffer is threading safe, whereas StringBuilder is not.
  • Performance of StringBuffer is very slow, whereas the performance of StringBuilder is fast.
Following are some of the differences between array list and vectors in java: –
  • An array list is not synchronized, whereas a vector is synchronized.
  • An array list works fast, whereas a vector does not.
  • There is an increase of 50% in the array size whenever any element is inserted inside the array list, whereas vector defaults to doubling the size of its array.
  • The increment size is not defined in an array list, whereas in vector it is defined.
  • Only iterators can be used in an array list for traversing an array, whereas vectors can use both enumeration and iterators for traversing an array.
An abstract class is a class in java which contains the abstract keyword in its declaration. It can have both abstract and non-abstract methods. The abstract class can have the public, private, protected, constant and default variables. This class needs to be extended and we need to implement the methods. This class can’t be instantiated. A class can be declared as an abstract class if it has at least one abstract method. An abstract class has at least one virtual function. Keyword “abstract” is used to make a class abstract.
A constructor in Java is basically a block of code which initializes the objects which are newly created. Although it resembles an instance method in java it is not since it does not return any value or have a return type. People usually refer constructors as special types of methods in java. Constructors and methods are two different things. In java, or in any other object-oriented programming language, a constructor has the same name as that of the class. There are basically 3 types of constructors in java. They are: –
  • Default constructors
  • Parameterized constructors, and
  • Non-parameterized constructors.
In Java, there are basically two ways by which arguments can be passed in any function. They are: –
  • Pass by value- in this, a copy of the value of the arguments is passed in the function. If any changes are made to the parameters of the sub-routine, then there is no effect on the arguments which are used to call it.
  • Pass by reference- In this, the copy of the address of the parameters are passed in the function and if any changes are made to the parameters, then it will have an effect on the arguments used to call the sub-routine.
The “super” keyword in java is basically related to the parent class. A super keyword can be used for several reasons such as-
  • It can be used to call the superclass or the parent class constructor.
  • It can also be used to access the method of the superclass which has been hidden by a subclass.
  • A super keyword can also be used to call the constructor of the parent class.
When in a program there is more than one method with the same name in a single class but the arguments used in them are different, then such thing is referred to as method overloading.

Overriding concept in java means when there are two methods with the same signature, one is in the parent class and the other one is in the child class. The override annotation can be used in the child class overridden method.