You are on page 1of 7

Java Interview Questions

What is similarities between an Abstract class and Interface?

Neither Abstract classes or Interface can be instantiated. We can't create a object of both Interface and Abstract class. We create object of the class who inherit either abstract class of interface.

What is a local, member and a class variable?

Variables declared within a method are local variables.Variables declared within the class i.e not within any methods are member variables (global variables).Variables declared within the class i.e not within any methods and are defined as static are class variables. What are User Defined Exceptions?

User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions. for eg: class userExcep extends Exception{userExcep(String msg){super(msg);}}

User defines exception are Exception Subclasses. It allows you to create your own exception types to handle situations specific to your application.... In order to define user defined exceptions::::: define subclass of Exception class(which is a subclass of Throwable)

What modifiers are allowed for methods in an Interface?

Interface methods should PUBLIC ,ABSTRACT Interface Variables Should be PUBLIC ,STATIC,FINAL.

Why operator overloading is not there in java?

What is method overloading?

Method Overloading means to have two or more methods with same name in the same class with differentarguments. The benefit of method overloading is that it allows you to implement methods that support the same semantic operation but differ by argument number or type.

* * * * *

Overloaded methods MUST change the argument list Overloaded methods CAN change the return type Overloaded methods CAN change the access modifier Overloaded methods CAN declare new or broader checked exceptions A method can be overloaded in the same class or in a subclass

Overloaded methods can change the access modifier and their exceptions. Examples: ordinary method: public void doStuff() {} Overloaded method: public string doStuff(String s)throws IOException {}

What is transient variable?

Transient variable can't be serialize. The transient modifier is variable level.If we give a variable as transient,we are telling the JVM to skip the variable when attempting to serialize the object .
Why does Java not support Multiple Inheritance?

java does not support multiple inheritance because different classes may have different variable with same name that may be be contradicted and can cause confusions resulting in errors. Or Java absolutely support multiple inheritance in terms of Interface.We can extend one class only to avoid ambiguity problem.In interface we have to define the functions.So we don't get any ambiguity.In c++ it is big problem with multiple inheritance but in JAVA this thing is improved by introducing Interfaces. Java doesn't support multiple inheritance bcoz of the following reasons: 1)Suppose if there is same method by name doStuff() in the super classes namely A and B from which we want to inherit a class named C . Now when we call the doStuff() method in our new sub-class(C) then the compiler doesn't understand from where the doStuff() method has to be inherited, i.e either from class A or B. This will lead to error, so we are not allowed to extend a class from more than one class. Multiple inheritance is supported in java in form of Interface. Interface is like a contract between class and the methods it(interface)contain.
What is Dynamic Binding?

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
Is it possible to override the main method?

NO, because main is a static method. A static method can't be overridden in Java. We are actually overridding main method every.... We can override static methods if the sub-class should also has the static.
What are the differences between method overloading and method overriding? What are the types of Exceptions in Java ? There are two types of exceptions in Java,

unchecked exceptions and checked exceptions. * Checked exceptions: A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses. Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception. * Unchecked exceptions: All Exceptions that extend the RuntimeException class are unchecked exceptions. Class Error and its subclasses also are unchecked.

.What if there is a break or return statement in try block followed by finally block? If there is

a return statement in the try block, the finally block executes right after the return statement encountered, and before the return executes.If there is a break or return statement in try block.The statements before the break are executed in try block and it will go to final.The statements after the break are not executed.

How does Java implement polymorphism?

(Inheritance, Overloading and Overriding are used to achieve Polymorphism in java). Polymorphism manifests itself in Java in the form of multiple methods having the same name. * In some cases, multiple methods have the same name, but different formal argument lists (overloaded methods). * In other cases, multiple methods have the same name, same return type, and same formal argument list (overridden methods). (Inheritance, Overloading and Overriding are used to achieve Polymorphism in java). Polymorphism manifests itself in Java in the form of multiple methods having the same name.

How can you force garbage collection?

You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. User cannot force the garbage collection, instead user can send a request to JVM asking for doing the garbage collectin with System.gc() method. Once, JVM recieves the gargabe collection request and if memory really needs to be collected (like if memory out of condition situations occur) then JVM automaically runs the thread to do the garabge collection.

When you declare a method as abstract, can other nonabstract methods access it?

Yes, other nonabstract methods can access a method that you declare as abstract. No, Abstract method has only definition no implementation so other methods cannot access it until and unless we provide the body in the sub class. Eg:abstract class Abc {

abstract int hello(); int hai() { hello(); return 1; } } Yes, we can access the abstract method from non abstract methods, this way, this got compiled with no errors

What are the principle concepts of OOPS?

There are four principle concepts upon which object oriented design and programming rest. They are: * Abstraction * Polymorphism * Inheritance * Encapsulation (i.e. easily remembered as A-PIE).

Which is superclass of Exception?

"Throwable", the parent class of all exception related classes.

What are the advantages of using exception handling?

Exception handling provides the following advantages over "traditional" error management techniques: * Separating Error Handling Code from "Regular" Code. * Propagating Errors Up the Call Stack. * Grouping Error Types and Error Differentiation.

Can overloaded methods be override too?

Yes, derived classes still can override the overloaded methods. Polymorphism can still happen. Compiler will not binding the method calls since it is overloaded, because it might be overridden now or in the future.

Do interfaces have member variables?

Interfaces may have member variables, but these are implicitly public, static, and final- in other words, interfaces can declare only constants, not instance variables that are available to all implementations and may be used as key references for method arguments for example.

Can we instantiate an interface?

You can?t instantiate an interface directly, but you can instantiate a class that implements an interface.
What is Java virtual machine?

What is Java Native Interface? What is Java Runtime Environment(JRE)?

JRE is used in all os's like windows,unix.if u want to run the java programs we need the JRE. it consists JVM and Packeges

You might also like