Core Java - Interview Questions and Answers

21. What is passed by reference ?

Objects are passed by reference.
In java we can create an object pointing to a particular location ie NULL location by specifying :

< class name > < object name >
and also can create object that allocates space for the variables declared in that particular class by specifying :
< object name > = new < class name > ();

22. What are nested classes ?

There are two types : static and non-static.
static class means the members in its enclosing class (class within class) can be accessed by creating an object and cannot be accessed directly without creating the object.

non-static class means inner class and can be accessed directly with the object created for the outer class no need to create again an object like static class.

23. Briefly about super() ?

This is used to initialize constructor of base class from the derived class and also access the variables of base class like

super.i = 10.
method overloading : same method name with different arguments.
method overriding : same method name and same number of arguments.

24. What is Dynamic Method Dispatch ?

This is the mechanism by which a call to an overridden function is resolved at runtime rather than at compile time. And this is how Java implements runtime polymorphism.

25. What are abstract classes ?

To create a superclass that only defines generalized form that will be shared by all its subclasses, leaving it to each subclass to fill in the details.
We cannot declare abstract constructors and abstract static methods.
An abstract class contains at least one abstract method.
And this abstract class is not directly instantiated with new operator.
Can create a reference to abstract class and can be point to subclass object.

26. What is Object class and java.lang ?

Object class is the superclass of all the classes and means that reference variable of type object can refer to an object of any other class. and also defines methods like finalise,wait.

java.lang contains all the basic language functions and is imported in all the programs implicitly.

27. What are packages and why ? how to execute a program in a package ?

Package is a set of classes, which can be accessed by themselves and cannot be accessed outside the package. and can be defined as package < pkg name >.
Package name and the directory name must be the same.
And the execution of programs in package is done by : java mypack.account
where mypack is directory name and account is program name.

28. What does a java source file can contain ?

specifying package name.
importing more than one package
specifying classes and there methods

29. Why interface and what extends what ?

Interface is similar to abstract classes. this define only method declarations and definitions are specified in the classes which implements these interfaces.
and “ one interface multiple methods “ signifies the polymorphism concept.
Here an interface can extend another interface.
and a class implements more than one interface.

30. How Exception handling is done in Java ?

Is managed via 5 keywords :
try : statements that you want to monitor the exceptions contain in try block.
catch : the exception thrown by try is catched by this.
throw : to manually throw exception we go for this.
throws : Exception that is thrown out of a method must be specified by throws after the method declaration.
finally : this block is executed whether or not an exception is thrown. and also it is executed just before the method returns. and this is optional block.

Post a Comment

Mới hơn Cũ hơn