0% found this document useful (0 votes)
9 views

Set2 JAVA Q&A

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Set2 JAVA Q&A

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Set2 - JAVA QnA

1. What is an Array in java?

A Java variable is a piece of memory that can contain a data value. A variable thus has a data
type. A data type is a classification of data, which can store a specific type of information.

An array is a data structure that contains a group of elements. Typically these elements are all of
the same data type, such as an integer or string. Arrays are commonly used in computer
programs to organize data so that a related set of values can be easily sorted or searched.

For example, a search engine may use an array to store Web pages found in a search performed
by the user. When displaying the results, the program will output one element of the array at a
time. This may be done for a specified number of values or until all the values stored in the array
have been output. While the program could create a new variable for each result found, storing
the results in an array is much more efficient way to manage memory.

Arrays can have more than one dimension. A one-dimensional array is called a vector ; a two-
dimensional array is called a matrix.

2. How to define single dimension array?

One dimensional array is a list of variables of same type that are accessed by a common
name. An individual variable in the array is called an array element. Arrays forms a way
to handle groups of related data.

Syntax of Multi dimensional Arrays

To create an array, you need to perform two steps :

1) Declare the array :

To declare an array below is a general form or syntax.

type varName[];

Where type is valid data type in java and varName is a name of an array.

Example : int arr[];

2) Allocate space for its elements :

To allocate space for an array element use below general form or syntax.

varName = new type[size];

Where varName is the name of the array and type is a valid java type and size specifies
the number of elements in the array.

Example : arr = new int[10];

Above statement will create an integer of an array with ten elements that can be
accessed by arr.
Structure of one dimensional array

arr[0]
arr[1]
arr[2]
arr[3]
arr[4]
arr[5]
arr[6]
arr[7]
arr[8]
arr[9]

Note that array indexes begins with zero. That means if you want to access 1st element
of an array use zero as an index. To access 3rd element refer below example.

arr[2] = 10; // it assigns value 10 to the third element of an array.

java also allows an another way to declare array.

type varName[] = {v0, v1, v2 ........ v10};

Where varName is the name of the array and type is a valid data type and v0, v1 to v10
are the elements of an array. In this case new is not used and memory for the array is
automatically provided.

Example : int arr = {1,2,3,4,5,6,7,8,9,10};

Example : This example shows how to declare initialize and display an array.

// Declaration of allocating memory to an array


int iarr[] = new int[3];

// Initializing elements
iarr[0] = 1;
iarr[1] = 2;
iarr[2] = 3;

//Display array elements


System.out.println(iarr[0]);
System.out.println(iarr[1]);
System.out.println(iarr[2]);

// Or Use for loop to display elements


for (int i = 0; i < iarr.length; i = i + 1)
{
System.out.print(iarr[i]);
System.out.print(" ");
}
3. How to define double dimension array??

Multi dimensional arrays are nothing but arrays of arrays. You can create arrays of two or more
dimensions.

Syntax of Multi dimensional Arrays

To create multi dimensional array, you need to perform two steps :

1) Declare multi dimensional array:

To declare multi dimensional array below is a general form or syntax.

type varName[][];

Where type is valid data type in java and varName is a name of an array. Note that the two sets of
brackets indicates that there are two dimensions for this array.

Example : int arr[][];

2) Allocate space for its element :

To allocate space for multi dimensional array element use below general form or syntax.

varName = new type[size1][size2];

Where varName is the name of the array and type is a valid java type and dimensions are
specified by size1 and size2 in the array.

Example : iarr = new int[2][3];

Above statement will create 2*3 element array of an integer values that can by accessed by iarr.

Structure of multi dimensional array

iarr[0][0] iarr[0][1] iarr[0][2]


iarr[1][0] iarr[1][1] iarr[1][2]

Note that array index begins with zero. That means if you want to access 1st element of an array
use zero as an index. To access 3rd element refer below example.

iarr[0][2] = 10; // it assigns value 10 to the third element of an array.

java also allows an abbreviated syntax to declare an array. General form or syntax of is as shown
below.

type varName[][] = {{v00, v01, v02}, {v10,v11,v12}};

Where varName is the name of the array and type is a valid data type and v00, v01 to v12 are the
elements of an array. In this case new is not used and memory for the array is automatically
provided.

Example : int iarr = {{1,2,3},{4,5,6}};


Example of Multi dimensional array

Example : This example shows how to declare initialize and display multi dimensional array.

// Declaration of allocating memory to multi dimensional array


int iarr[][] = new int[2][3];

// Initializing elements
iarr[0][0] = 1;
iarr[0][1] = 2;
iarr[0][2] = 3;
iarr[1][0] = 4;
iarr[1][1] = 5;
iarr[1][2] = 6;

//Display array elements


System.out.println(iarr[0][0]);
System.out.println(iarr[0][1]);
System.out.println(iarr[0][2]);
System.out.println(iarr[1][0]);
System.out.println(iarr[1][1]);
System.out.println(iarr[1][2]);

// Or Use for loop to display elements


for (int i = 0; i < iarr.length; i = i + 1)
{
for(int j=0; j < iarr[i].length; j = j + 1)
{
System.out.print(iarr[i][j]);
System.out.print(" ");
}
}

4. What is String class?

String class is encapsulated under java.lang package. In java, every string that you create is actually
an object of type String. One important thing to notice about string object is that string objects are
immutable that means once a string object is created it cannot be altered.

Creating a String object

String can be created in number of ways; here are a few ways of creating string object.

1) Using a String literal

String literal is a simple string enclosed in double quotes " ". A string literal is treated as a String
object.

String str1 = "Hello";

2) Using another String object


String str2 = new String(str1);
3) Using new Keyword
String str3 = new String("Java");

4) Using + operator (Concatenation)


String str4 = str1 + str2;
or,
String str5 = "hello"+"Java";

5. What are the different methods used in String, name any few?

String class function

The following methods are some of the most commonly used methods of String class.

charAt() - charAt() function returns the character located at the specified index.
String str = "Testing";
System.out.println(str.charAt(2));

Output: s

equalsIgnoreCase() - equalsIgnoreCase() determines the equality of two Strings, ignoring thier


case (upper or lower case doesn't matters with this fuction ).
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));

Output: true

length() - length() function returns the number of characters in a String.


String str = "Count me";
System.out.println(str.length());

Output: 8
replace() - replace() method replaces occurances of character with a specified new character.

String str = "Replace";


System.out.println(str.replace('R','r'));

Output : replace

substring() - substring() method returns a part of the string. substring() method has two
forms,
public String substring(int begin);

public String substring(int begin, int end);


The first argument represents the starting point of the subtring. If the substring() method is called with
only one argument, the subtring returned, will contain characters from specified starting point to the
end of original string.

But, if the call to substring() method has two arguments, the second argument specify the end point of
substring.

String str = "0123456789";


System.out.println(str.substring(4));

Output : 456789

System.out.println(str.substring(4,7));

Output : 456

toLowerCase() - toLowerCase() method returns string with all uppercase characters


converted to lowercase.

String str = "ABCDEF";


System.out.println(str.toLowerCase());

Output : abcdef

toUpperCase() - This method returns string with all lowercase character changed to
uppercase.

String str = "abcdef";

System.out.println(str.toUpperCase());

Output : ABCDEF

trim() - This method returns a string from which any leading and trailing whitespaces has been
removed.

String str = " hello ";

System.out.println(str.trim());

Output : hello
6. How to define Method in Java?

Method describes behaviour of an object. A method is a collection of statements that are group
together to perform an operation.

Syntax:

Accessmodifier return-type methodName(parameter-list)

//body of method

Modifier: Modifier are access type of method.

Return Type: A method may return value. Data type of value return by a method is declared in
method heading.

Method name: Actual name of the method.

Parameter: Value passed to a method.

Method body: Collection of statement that defines what method does.

7. How to access Methods in Java?

public class Test


{
public void methodName(int x)
{
x=100;
}
public static void main(String[] args)
{
int x=50;
Test t = new Test();
t.methodName(x); //function call, accessing method of own class
System.out.println(x);
}

You might also like