Methods interview questions and answers in java

A method represents group of statements that performs a task. Here, Task represents a calculation or processing of data or generating a report etc. This page also contains the information of the methods related interview questions and answers in java. If any one are facing more than these interview questions please share with me through "Contact me Here" gadget or drop me an email to "subbareddynallamachu@gmail.com".

Q1: How many types of methods are there in java?

A: In java mainly two types of methods are there. That are,

                1. Instance methods
                2. Static methods
                3. Factory methods

Q2: What are instance method?

A: Instance methods are methods which act on the instance variable of the class. To call the instance methods. We should use the form: objectname.methodname().

Q3: What are static methods?

A: Static methods are methods which do not act up on the instance variable of a class. static methods are declared as static.

Q4: What is the difference between instance variable and Class variable(static variable)?

A: 1. Instance variable is a variable whose separate copy is available to each object. A class variable is a variable whose single copy in memory is shared by all objects.
2. Instance variables are created in the object on heap memory. Class variables are stored on method area.

Q5: Is it possible to compile and run a java program without writing main() method?

A: Yes, it is possible by using the static block in the java program.

Q6: How are objects are passed to methods in java?

A: Primitive datatypes, objects, even object references - everything is passed to methods using 'pass by value' or 'call by value' concept. This means their bit by bit copy is passed to the methods.

Q7: What are factory methods?

A: A factory method is a method that will create and returns an object to the class to which it belongs. A single factory method replaces several constructors in the class by accepting different options from the user, which creating the object.

Q8: In how many ways you can create an object in java?

A: There are 4 ways of creating an object in java:

1. Using new operator

     Employee emp = new Employee();

Here, we are creating Employee class object 'emp' using new operator.

2. Using factory methods

    NumberFormat obj = NumberFormat.getNumberInstance();

Here, we are creating NumberFormat object using the factory method getNumberInstance().

3. Using newInstance() method. Here, we should follow two steps, as

a) First, store the class name Employee as a string into an object. For this purpose, factory method forName() of the class Class will be useful.

      Class c = Class.forName("Employee");

we should note that there is a class with name Class in java.lang package.

b) Next, create another object to the class whose name is the object c. For this purpose we need newInstance() method of the class Class as,

      Employee emp = (Employee)c.newInstance();

4. By cloning an already available object, we can create another object. Creating exact copy of an existing object is called 'cloning'.

      Employee emp1 = new Employee();

      Employee emp2 = (Employee)emp1.clone();

Earlier we created emp2 by cloning the Employee object emp1.clone() method of object class is used to clone an object. We should note that there is a class by the name Object in java.lang package. 

Related posts:

Comments

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example