Java Interview Questions And Answers Pdf Yes, We can execute any code, even before the main method. We are using a static block. In this guide we will discuss about different types of questions that can be used in a Java interview, in order for the employer to test your skills in Java and. 5) Difference between method overloading and method overriding in java? . .. 9. 6) Difference . Core java Interview questions on Coding Standards.
|Language:||English, Spanish, Arabic|
|Distribution:||Free* [*Register to download]|
Dear readers, these Java Interview Questions have been designed the subject and later they continue based on further discussion and what you answer −. I was getting a lot of emails and comments to create java interview questions and answers PDF eBook. It was hard and time taking, but finally, it's done. Get Interview Questions and answers on JAVA for freshers with pdf, learn OOPS Concepts, Features, diagrammatic representation, Packages.
What is encapsulation in Java? Encapsulation is a mechanism where you bind your data variables and code methods together as a single unit. Here, the data is hidden from the outer world and can be accessed only via current class methods.
This helps in protecting the data from any unnecessary modification. We can achieve encapsulation in Java by: Declaring the variables of a class as private. Providing public setter and getter methods to modify and view the values of the variables. What is an association? Association is a relationship where all object have their own lifecycle and there is no owner. Multiple students can associate with a single teacher and a single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle.
These relationships can be one to one, one to many, many to one and many to many. What do you mean by aggregation?
An aggregation is a specialized form of Association where all object has their own lifecycle but there is ownership and child object can not belong to another parent object. A single teacher can not belong to multiple departments, but if we delete the department teacher object will not destroy.
What is composition in Java? It is a strong type of Aggregation.
Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different houses if we delete the house room will automatically delete. What is a marker interface?
A Marker interface can be defined as the interface having no data member and member functions. In simpler terms, an empty interface is called the Marker interface. The most common examples of Marker interface in Java are Serializable, Cloneable etc. The marker interface can be declared as follows. What is object cloning in Java? Object cloning in Java is the process of creating an exact copy of an object.
It basically means the ability to create an object with a similar state as the original object. This method creates a new instance of the class of the current object and then initializes all its fields with the exact same contents of corresponding fields. To object clone , the marker interface java. Cloneable must be implemented to avoid any runtime exceptions. One thing you must note is Object clone is a protected method, thus you need to override it.
What is a copy constructor in Java? Copy constructor is a member function that is used to initialize an object using another object of the same class. Though there is no need for copy constructor in Java since all objects are passed by reference. Moreover, Java does not even support automatic pass-by-value.
What is a constructor overloading in Java? The compiler uses the number of parameters and their types in the list to differentiate the overloaded constructors. Apart from this Java Interview Questions Blog, if you want to get trained from professionals on this technology, you can opt for a structured training from edureka! Servlets Interview Questions Q1. What is a servlet?
Since Constructors are invoked only when the object is created, there is no sense to make the constructors static. However, if you try to do so, the compiler will show the compiler error. In Java, if we make the abstract methods static, It will become the part of the class, and we can directly call it which is unnecessary.
Calling an undefined method is completely useless therefore it is not allowed. Yes, we can declare static variables and methods in an abstract method. As we know that there is no requirement to make the object to access the static context, therefore, we can access the static context declared inside the abstract class by using the name of the abstract class.
The this keyword is a reference variable that refers to the current object. There are the various uses of this keyword in Java. It can be used to refer to current class properties such as instance methods, variable, constructors, etc. It can also be passed as an argument into the methods or constructors. It can also be returned from the method as the current class instance.
No, this cannot be assigned to any value because it always points to the current class object and this is the final reference in Java. However, if we try to do so, the compiler error will be shown.
Yes, It is possible to use this keyword to refer static members because this is just a reference variable which refers to the current class object. However, as we know that, it is unnecessary to access static variables through objects, therefore, it is not the best practice to use this to refer static members.
Output 10 55 How can constructor chaining be done using this keyword? Constructor chaining enables us to call one constructor from another constructor of the class with respect to the current class object. We can use this keyword to perform constructor chaining within the same class. Consider the following example which illustrates how can we use this keyword to achieve constructor chaining.
As we know, that this refers to the current class object, therefore, it must be similar to the current class object. However, there can be two main advantages of passing this into a method instead of the current class object. Inheritance is a mechanism by which one object acquires all the properties and behavior of another object of another class.
It is used for Code Reusability and Method Overriding. The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When you inherit from an existing class, you can reuse methods and fields of the parent class. Moreover, you can add new methods and fields in your current class also. Inheritance represents the IS-A relationship which is also known as a parent-child relationship. To reduce the complexity and simplify the language, multiple inheritance is not supported in java.
Consider a scenario where A, B, and C are three classes. The C class inherits A and B classes. If A and B classes have the same method and you call it from child class object, there will be ambiguity to call the method of A or B class. Since the compile-time errors are better than runtime errors, Java renders compile-time error if you inherit 2 classes.
So whether you have the same method or different, there will be a compile time error. Aggregation can be defined as the relationship between two classes where the aggregate class contains a reference to the class it owns. Aggregation is best described as a has-a relationship. For example, The aggregate class Employee having various fields such as age, name, and salary also contains an object of Address class having various fields such as Address-Line 1, City, State, and pin-code.
In other words, we can say that Employee class has an object of Address class. Holding the reference of a class within some other class is known as composition. When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition. In other words, we can say that composition is the particular case of aggregation which represents a stronger relationship between two objects.
A class contains students. A student cannot exist without a class. There exists composition between class and students. Aggregation represents the weak relationship whereas composition represents the strong relationship. For example, the bike has an indicator aggregation , but the bike has an engine composition.
The pointer is a variable that refers to the memory address. They are not used in Java because they are unsafe unsecured and complex to understand. The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable.
The super is called in the class constructor implicitly by the compiler if there is no super or this. The super is implicitly invoked by the compiler if no super or this is included explicitly within the derived class constructor. Therefore, in this case, The Person class constructor is called first and then the Employee class constructor is called. The object cloning is used to create the exact copy of an object.
The clone method of the Object class is used to clone an object. The java.
Cloneable interface must be implemented by the class whose object clone we want to create. Method overloading is the polymorphism technique which allows us to create multiple methods with the same name but different signature. We can achieve method overloading in two ways. Method overloading increases the readability of the program. Method overloading is performed to figure out the program quickly.
In Java, method overloading is not possible by changing the return type of the program due to avoid the ambiguity. No, We cannot overload the methods by just applying the static keyword to them number of parameters and types are the same. By Type promotion is method overloading, we mean that one data type can be promoted to another implicitly if no exact matching is found. As displayed in the above diagram, the byte can be promoted to short, int, long, float or double.
The short datatype can be promoted to int, long, float or double. The char datatype can be promoted to int, long, float or double and so on. There are two methods defined with the same name, i.
The first method accepts the integer and long type whereas the second method accepts long and the integer type. We can not tell that which method will be called as there is no clear differentiation mentioned between integer literal and long literal.
This is the case of ambiguity. Therefore, the compiler will throw an error. If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.
It is used for runtime polymorphism and to implement the interface methods. It is because the static method is the part of the class, and it is bound with class whereas instance method is bound with the object, and static gets memory in class area, and instance gets memory in a heap. No, we cannot override the private methods because the scope of private methods is limited to the class and we cannot access them outside of the class.
Yes, we can change the scope of the overridden method in the subclass. However, we must notice that we cannot decrease the accessibility of the method. The following point must be taken care of while changing the accessibility of the method.
Yes, we can modify the throws clause of the superclass method while overriding it in the subclass. However, there are some rules which are to be followed while overriding in case of exception handling. The method is overloaded in class Base whereas it is derived in class Derived with the double type as the parameter.
In the method call, the integer is passed. Yes, all functions in Java are virtual by default. Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type. The covariant return type specifies that the return type may vary in the same direction as the subclass.
Output Derived method called The method of Base class, i. In Test class, the reference variable b of type Base class refers to the instance of the Derived class. Here, Runtime polymorphism is achieved between class Base and Derived. At compile time, the presence of method baseMethod checked in Base class, If it presence then the program compiled otherwise the compiler error will be shown. In this case, baseMethod is present in Base class; therefore, it is compiled successfully.
However, at runtime, It checks whether the baseMethod has been overridden by Derived class, if so then the Derived class method is called otherwise Base class method is called. In this case, the Derived class overrides the baseMethod; therefore, the Derived class method is called. In Java, the final variable is used to restrict the user from updating it. If we initialize the final variable, we can't change its value. In other words, we can say that the final variable once assigned to a value, can never be changed after that.
The final variable which is not assigned to any value can only be assigned through the class constructor. If we change any method to a final method, we can't override it. A final variable, not initialized at the time of declaration, is known as the final blank variable. We can't initialize the final blank variable directly. Instead, we have to initialize it by using the class constructor. It is useful in the case when the user has some data which must not be changed by others, for example, PAN Number.
Consider the following example:. Yes, if it is not static, we can initialize it in the constructor. If it is static blank final variable, it can be initialized only in the static block. Output 20 Explanation. Since i is the blank final variable. It can be initialized only once. We have initialized it to Therefore, 20 will be printed.
Explanation The getDetails method is final; therefore it can not be overridden in the subclass. The constructor can never be declared as final because it is never inherited. Constructors are not ordinary methods; therefore, there is no sense to declare constructors as final.
However, if you try to do so, The compiler will throw an error. No, we cannot declare an interface as final because the interface must be implemented by some class to provide its definition. Therefore, there is no sense to make an interface final. However, if you try to do so, the compiler will show an error.
The main difference between the final method and abstract method is that the abstract method cannot be final as we need to override them in the subclass to give its definition.
JavaTpoint offers too many high quality services. Mail us on hr javatpoint. Please mail your requirement at hr javatpoint. Download Android App All Interview. I am good!!