What are the various access specifiers for Java classes? Ans: In Java, access specifiers are the keywords used before a class name which. 5) Difference between method overloading and method overriding in java? . .. 9. 6) Difference . Core java Interview questions on Coding Standards. Java Interview Questions And Answers Pdf Yes, We can execute any code, even before the main method. We are using a static block.
|Language:||English, Spanish, German|
|PDF File Size:||16.61 MB|
|Distribution:||Free* [*Regsitration Required]|
Java Interview Questions i. Java Interview Questions .. Both implementations share some common characteristics, but they differ in the following features. 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.
We can have comments before the package statement. When we declare variables variables are created in stack.
So when the variable is out of scope those variables get garbage collected. When do we use synchronized blocks and advantages of using synchronized blocks?
If very few lines of code requires synchronization then it is recommended to use synchronized blocks. The main advantage of synchronized blocks over synchronized methods is it reduces the waiting time of threads and improves performance of the system.
What is the difference between access specifiers and access modifiers in java? But there is no such divison of access specifiers and access modifiers in java. In Java we have access modifiers and non access modifiers.
Access Modifiers: We can use only two access modifiers for class public and default. A class with public modifier can be visible 1 In the same class 2 In the same package subclass 3 In the same package nonsubclass 4 In the different package subclass 5 In the different package non subclass. A class with default modifier can be accesed 1 In the same class 2 In the same package subclass 3 In the same package nonsubclass 4 In the different package subclass 5 In the different package non subclass.
Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such case we declare a class as abstract. To make a class abstract we use key word abstract.
Any class that contains one or more abstract methods is declared as abstract. We get the following error. For example if we take a vehicle class we cannot provide implementation to it because there may be two wheelers , four wheelers etc.
At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends vehicle will provide its method implementation. The important features of abstract classes are: Though we cannot instantiate abstract classes we can create object references. Through superclass references we can point to subclass. But when we cannot instantiate class there is no use in creating a constructor for abstract class. Abstract method is declared with keyword abstract and semicolon in place of method body.
Java Exception Handling Interview questions. NumberFormatException 3 Invalid casting of class Class cast Exception 4 Trying to create object for interface or abstract class Instantiation Exception. In java exception is an object. Exceptions are created when an abnormal situations are arised in our program. Exceptions can be created by JVM or by our application code.
All Exception classes are defined in java. In otherwords we can say Exception as run time error. Error is the subclass of Throwable class in java. When errors are caused by our program we call that as Exception, but some times exceptions are caused due to some environment issues such as running out of memory. Exceptions which cannot be recovered are called as errors in java.
Out of memory issues. It is recommended to handle exceptions with specific Exception instead of handling with Exception root class. If a method throws an exception and it is not handled immediately, then that exception is propagated or thrown to the caller of that method.
This propogation continues till it finds an appropriate exception handler ,if it finds handler it would be handled otherwise program terminates Abruptly. We can create threads in java by any of the two ways: This is first and foremost way to create threads. By implementing runnable interface and implementing run method we can create new thread. Method signature: If multiple threads tries to access a method where method can manipulate the state of object , in such scenario we can declare a method as synchronized.
Finally block is used for cleaning up of resources such as closing connections, sockets etc. If there is exception thrown in try block finally block executes immediately after catch block.
If an exception is thrown,finally block will be executed even if the no catch block handles the exception. From Java 7, we can catch more than one exception with single catch block. Basic Java.
Garbage Collection. Collection Framework. String Handling. Exception Handling. Hot Java was the first Web browser that could download and play execute Java applets.
Although Hot Java was the first browser to support Java applets, many browsers now support or will soon support applets.
Starting with Netscape Navigator 2. 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.