Java Questions with Answers #4

1) What is method in java? How to define and call the method?

Method is a programming construct used for grouping the statement together to build a function. There are two ways by which the method is handled.

1. Defining a method

2. Calling a method

Here is example that helps to understand the concept of method defining and calling.

public class methDemo {
     public static void main(String args[]) {
            int a=10;
            int b=20;
            int c=sum(a,b);        /* call to the method */
            System.out.println(“The sum of “+a”+” and “+b+” is=”+c);
     public static int sum(int num1,int num2)       /* Definition of method */
            int ans=num1+num2;
            return ans;

2) What are the different access identifiers?

There are main three access identifiers:-public, private and protected.

3) What do you mean by void method?

Void method means it returns nothing or it doesn’t return any kind of data from that method.

4) Describe various methods in Math class.

In java, Math class is an useful class for performing basic mathematical operations. Various methods available in Math class are:
Name of Method  Meaning
It returns you a default exponent value that is closer than any other to e, the base of the natural logarithms.
PI  It returns the default pi value.
round It returns the value in round form.
abs It returns the absolute number.
ceil Returns the smallest value that is not less than input.
pow It returns the power value.
sqrt This function is useful to get the square root value.
exp The exponential value can be obtained by this function floor Returns the largest integral value that is not greater than input.
min  It returns the minimum value between the two numbers.
max It returns the maximum value between the two numbers.
random It returns random double numbers in the range >=0.0 to <1.0

5) What is array? How to declare array and how to allocate the memory to for array?

Array is a collection of similar type of elements. Thus grouping of similar kind of elements is possible using arrays. Typically arrays are written along with the size of them. The syntax of declaring array is:

     data_type array_name [];
and to allocate the memoryarray_
     name=new data_type[size];

    where array_name represent name of the array, new is a keyword used to
allocate the memory for arrays, data_type specifies the data type of array elements and size represents the size of an array. For example:
     int a=new int[10];

6) Explain how to declare Two Dimensional array?

The two dimensional arrays are the arrays in which elements are stored in rows as well as columns. For example:

10 20 30
40 50 60
70 80 90

The two dimensional array can be declared and initialized as follows 


data_type array_name=new data_type[size];
For example:
int a[][]=new int[3][3];

7) What is OOP? Give some detail.

1. Object Oriented Programming is a programming language model organized around “objects” rather than “actions”.

2. The focus of the object oriented programming is data.

3. The first step in OOP is to identify all the objects you want to manipulate. Then is identified that how they relate to each other. This procedure is known as data modeling.

4. The identified object is generalized as class of objects and defines the kind of data it contains and any logic sequences that can manipulate it. Each distinct sequence is known as a method or functions. 

5. An instance of a class is called as an “object”.

6. The programming language like C tends to focus on actions or procedures whereas java is object oriented programming language.

7. In C the unit of programming is procedures whereas in java the unit of programming is class from which the object is instantiated.

8. In java the user defined classes are created.

Java Tips

See also

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of or the content authors.