Skip to main content

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

JNDI configuration for Tomcat 9 with Oracle

In this article, I am going to place the required source code to get data from the table by using the JNDI configuration. Below are the environment details that I have configured currently. Windows - 7 Oracle - 10g Tomcat - 9 JDK - 8 Eclipse Oxygen Ojdbc6 jar required First, we need to create the Dynamic Web Project. If you don't know how to do <Click Here>. I have faced a lot of issues before getting the output like 405, No driver class to load, etc. I am using JSP & Servlets in the current explanation. Before started writing the application logic, we need to do the below configuration in the installed tomcat directory. Place OJDBC6.jar in the Tomcat LIB directory. Add this Resource under <GlobalNamingResources> in Server.xml file which is present under the conf directory in Tomcat. < Resource name = "jdbc/myoracle" global= "jdbc/myoracle" auth = "Container" type= "javax.sql.DataSource" driverClass...

Multithreading in java with example

Multithreading  is one of the most important concept in core java. In this article we will learn what is multithreading? , what is the use of it? and What is the use of Synchronization and when to use it?  with detailed examples. At a time, two or more threads are accessing the same object is called as Multithreading  in Java .  First, we will create two threads for two objects. It is also possible to run two or more threads on a single class object. In this case, there is a possibility to get unreliable results. If the two threads are perform same task, then they need same object to be executed each time. For your better understanding, take an example of any reservations like, railway, movie ticket booking,etc. Let us think only one berth is available in a train and two passengers are asking for that berth. The first person has sent a request to allocate that ticket/berth to him. At the same time, the second person also sent a request to allocate that ...

Prime, Fibonacci and Factorial number with example in java

Prime number, Fibonacci series and Factorial number programs are most commonly asked questions in interview. Read this article to know what is and how to write programs for prime number, fibonacci series and factorial number. Prime Number: prime number is natural number greater than 1 that has no positive divisor other than 1 and itself. A natural number greater than 1 is not a prime number, is called Composite number . For example, 7 is a prime number. Because it can divide with 1 and 7 only. Where as 8 is composite number. Since it has the divisor 2 and 4 in addition to the 1 and 8. The below example represents the finding the passing number is prime number or not. If the passing number is prime number it will print true otherwise it will print false. package com . javatbrains . practice ; public class PrimeNumber { public boolean isPrimeNumber ( int number ) { if ( number <= 1 ) return false ; // There's only one ...