0% found this document useful (0 votes)
23 views49 pages

Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-10-03 - Reference-Material-I

Uploaded by

Nunna Kalyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views49 pages

Fall Semester 2024-25 - STS3004 - TH - AP2024252001206 - 2024-10-03 - Reference-Material-I

Uploaded by

Nunna Kalyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Arrays Operations In Java

WHAT ARE ARRAYS IN JAVA?

● Arrays are objects which store multiple variables of the same


type

● It can hold primitive types as well as object references

● In fact most of the collection types in Java which are the part
of java.util package use arrays internally in their functioning

● Since Arrays are objects, they are created during runtime .The
array length is fixed
FEATURES OF objects
● Arrays are ARRAYS

● They can even hold the reference variables of other objects

● They are created during runtime

● They are dynamic, created on the heap

● The Array length is fixed


ARRAY DECLARATION

● The declaration of array states the type of the element that


the array holds followed by the identifier and square braces
which indicates the identifier is array type.
int aiMyArray[];

● The above statement creates an array reference on the stack.


ARRAY CREATION

The above statement does two things −


● It creates an array using new dataType[arraySize].
● It assigns the reference of the newly created array to the
variable arrayRefVar.
Declaring an array variable, creating an array, and assigning the
reference of the array to the variable can be combined in one
statement, as shown below
arrayRefVar = new
dataType[arraySize];
ARRAY INTIALIZATION

int[] age = {12, 4, 5, 2,


5};

• This statement creates an array and initializes it during


declaration.

• The length of the array is determined by the number of


values provided which is separated by commas. In our
example, the length of age array is 5.
EXAMPLE

class ArrayExample {
public static void main(String[] args) {
int[] age = {12, 4, 5, 2, 5};
for (int i = 0; i < 5; ++i) {
System.out.println("Element at index " +
i +": " + age[i]);
}
}
}
PASSING ARRAYS TO METHOD

public static void printArray(int[] array) {


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

For example, the following statement invokes the printArray


method to display 3, 1, 2, 6, 4, and 2

printArray(new int[]{3, 1, 2, 6, 4, 2});


ARRAY OF OBJECTS

An array of objects is created just like an array of primitive type


data items in the following way.
class Student {
int marks;
}

Student[] studentArray = new Student[7];

The above statement creates the array which can hold references
to seven Student objects.
ARRAY OF OBJECTS

● It doesn't create the Student objects themselves.

● They have to be created separately using the constructor of


the Student class.

● The student Array contains seven memory spaces in which the


address of seven Student objects may be stored.

● If we try to access the Student objects even before creating


them, run time errors would occur
ARRAY OF OBJECTS

The Student objects have to be instantiated using the constructor


of the Student class and their references should be assigned to
the array elements in the following way.
studentArray[0] = new Student();

for ( int i=0; i<studentArray.length; i+


+) {
studentArray[i]=new Student();
}

The above for loop creates seven Student objects and assigns
their reference to the array elements.
SORTING TECHNIQUES

● A Sorting Algorithm is used to rearrange a given array or list


elements according to a comparison operator on the elements.

● The comparison operator is used to decide the new order of


element in the respective data structure.

SORTING TECHNIQUES
BUBBLE SORT

● It is the simplest sort method which performs sorting by repeatedly


moving the largest element to the highest index of the array.

● It comprises of comparing each element to its adjacent element and


replace them accordingly.
IMPLEMENTING BUBBLE SORT

● Starting with the first element(index = 0), compare the current


element with the next element of the array.

● If the current element is greater than the next element of the


array, swap them.

● If the current element is less than the next element, move to


the next element. Repeat Step 1.
IMPLEMENTING BUBBLE SORT
SELECTION SORT

The selection sort algorithm sorts an array by repeatedly finding


the minimum element (considering ascending order) from
unsorted part and putting it at the beginning. The algorithm
maintains two sub arrays in a given array.

● The sub array which is already sorted.

● Remaining sub array which is unsorted.

SELECTION SORT
IMPLEMENTING SELECTION SORT

● Find the minimum element in the list.

● Swap it with the element in the first position of the list.

● Repeat the steps above for all remaining elements of the list
starting from the second position.
IMPLEMENTING SELECTION SORT
INSERTION SORT

• Removes an element from an array


• Compares it against the largest value in the array
• Moves the element to its correct location.
IMPLEMENTING INSERTION SORT
SEARCHING TECHNIQUES

● Searching is an operation or a technique that helps finds the


place of a given element or value in the list.

● Any search is said to be successful or unsuccessful depending


upon whether the element that is being searched is found or
not.
SEARCHING TECHNIQUES

● Some of the standard searching technique that is being


followed in the data structure is listed below:

● Linear Search or Sequential Search

● Binary Search
LINEAR SEARCH
● Traverse the array

● Match the key element with array element

● If key element is found, return the index position of the array


element

● Step 4: If key element is not found, return -1


LINEAR SEARCH
BINARY SEARCH

● Binary Search: binary search or half-interval search algorithm finds


the position of a specified value (the input "key") within a sorted
array.

● In each step, the algorithm compares the input key value with the
key value of the middle element of the array.

● If the keys match, then a matching element has been found so its
index, or position, is returned
IMPLEMENTING BINARY SEARCH
2D ARRAY

• An array of more than one dimension is known as a multi-


dimensional array.
• Two of the most common examples of multi-dimensional arrays
are two and three-dimensional array, known as 2D and 3D array,
anything above is rare.
2D ARRAY

• Two – dimensional array is the simplest form of a


multidimensional array.
• A two – dimensional array can be seen as an array of one –
dimensional array for easier understanding.
SYNTAX

DECLARATION:

datatype[][] arrayname = new


datatype[x][y];
For example: int[][] arr = new int[10]
INITIALIZATION:
[20];

array_name[row_index][column_index] =
value;
For example: arr[0][0] = 1;
ACCESSING 2D ARRAY

Syntax:

x[rowindex][columnindex]

For example:

int[][] arr = new int[10][20];


arr[0][0] = 1;
QUESTION 01

Given an array S of n integers, find three integers in S such


that the sum is closest to a given number, target. Return
the sum of the three integers. You may assume that each
input would have exactly one solution.

For example, given array S = {-1 2 1 -4}, and


target = 1.
The sum that is closest to the target is 2. (-1 + 2
+ 1 = 2).
QUESTION 02

Given an array of n positive integers and a positive integer s,


find the minimal length of a sub array of which the sum s. If
there isn’t one, return 0 instead.

For example, given the array [2,3,1,2,4,3] and s = 7, the sub array [4,3]
has the minimal of 2 under the problem constraint.
QUESTION 01

In Java arrays are

A. Objects
B. Object references
C. Primitive data type
D. None of the above

Answer : A
QUESTION 02

What will be the output of the following program?


class ArrayOutput A. 10
{
public static void 100
main(String s[]) 50
{
int i = 50; B. 10
int[] a = new 10
int[10];
10
System.out.println(a.length); C. Compilation Error
a = new int[100];
D. Throws
System.out.println(a.length); ArrayIndexOutOfBoundsExceptio
a = new int[i];
n
System.out.println(a.length); Answer: A
}
}
QUESTION 03

What is the output of the following program


A. My Array length is: 3
public class ArrayOutput Student Marks Array
{ length is: 39
public static void main(String s[]) B. My Array length is: 39
{
Student Marks Array
int student_marks[] = new int[39];
student_marks = new int[]{10, 20, length is: 39
30}; C. My Array length is: 3
int[] my_array = null; Student Marks Array
my_array = student_marks; length is: 3
System.out.println("My Array D. Some other output
length is: " + my_array.length);
System.out.println("Student Marks
Array length is: " + student_marks.length); Answer : C
}
}
QUESTION 04
What will the output of following program

public class AllDimensionArrays { A. 020010221


public static void main(String[] args) { B. 020000221
int[] a1d = {}; C. Some other output
int[] b1d = {1, 3}; D. ArrayIndexOutOfBounds
int[][] a2d = {}; Exception
int[][] b2d = {{}};
int[][] c2d = {{1, 2}, {5}}; Answer : D
System.out.print(a1d.length + "
" + b1d.length + " ");
System.out.print(a2d.length + "
" + a2d[0].length + " " + b2d.length + "
" + b2d[0].length + " ");
System.out.print(c2d.length + "
" + c2d[0].length + " " + c2d[1].length);
}
}
QUESTION 05
What will be the output of the following program?

class Main A. 50
{ 100
public static void main(String s[]) 100
{ B. 50
int i = 50; 100
int[] a = new int[i]; 100
System.out.println(a.length); C. 50
System.out.println(a.length); 50
System.out.println(a.length); 50
} D. Compilatio
} n Error

Answer : C
QUESTION 06

What will be the output of the following


program?
public class Main A. "Prints = 052“
{ B. "Prints = 7“
public static void main(String[] args) C. \"Prints = 7\“
{ D. Compilation Error
int[] print = new int[]{0, 1, 2, 3, 4,
5};
System.out.print("\"Prints = ");
System.out.print(print[0] + print[5] + Answer : B
print[2] + "\"");
}
}
QUESTION 07
What will be the order of the array elements at the end of the program?

public class Main A. 12, 15, 11, 13, 9, 25


{ B. 12, 11, 13, 9, 15, 25
public static void main(String[] args) C. 9, 11, 12, 13, 15, 25,
{ D. Throws
int [] array= new int[] {12,15,11,13,9,25}; ArrayIndexOutOfBoundsExcepti
for(int i=0;i<array.length;i++){ on
for(int j=i+1;j<array.length;j++){
int temp=0;
Answer: C
if(array[i]> array[j]){
temp=array[i];
array[i]=array[j];
array[j]=temp;}}}
for(int i=0;i<array.length;i++)
System.out.println(array[i]);
}
}
QUESTION 08
What will be the output of the program A. x = 4
class Main x=3
{ x = a[0]; x=2
public x=1
static void x--; B. x = 3
main(String[] x=2
args) x=1 Answer: B
{ System.out.println x=0
("x = " + x); x = -1
int a[] = {2}; } C. x = 4
a } x=3
= new int[]{5}; x=2
private static x=1
int x = a[0]; void process(int[] x=0
while (x a) D. Compilation Error
>= 0) {
{ int x =
a[0]--;
x++;
process(a); }
QUESTION 09
What will be the output of the following
program
class Main
{ A. sum = 58
public static void main(String[] args) B. sum = 8
{ C. sum = 73
int a[][] = {{1, 3, 4}, {2, 3}, {7, D. Throws
6, 5, 4, 3, 2, 1}, {9}, {8}};
ArrayIndexOutOfBoundsExc
int sum = 0;
eption
for(int i = 0; i < a.length; i++)
{
for(int j = 0; j <
a[0].length; j++)
Answer: D
{
sum += a[i][j];
}
}
System.out.println("sum = " + sum);
}
}
QUESTION 10
What will be the output of the following
program?
public class Main A. sum = 5
{ B. Sum=38
public static void main(String[] C. sum=15
args) D. sum12
{
int[] a = {12, 15, 11, 13,
9, 25}; Answer: B
int[] a2 = {12, 15, 11, 13,
9, 25};
int sum = 0;
for (int i = 0; i <
a.length; i++)
{
if (a[i] % 3>0)
{
sum += i *
i;
}
}
QUESTION 11
What will be the output of the following program?

class Main A. 12
{ 15
public static void 16
main(String s[]) 17
{ 19
int a[] = {12, 15, 16, 17, B. 12
19}; 15
16
for(int i = 0; i < 5; i++) 17 Answer: A
{ C. 15
System.out.println(a[i]); 16
}
17
}
19
}
D. Compilation
error
QUESTION 12
What will be the output of the following program?

class Main int sum = 0;


{ A. Sum=20
public static void for(int l = 0; B. Sum=30
main(String s[]) l < a.length; l++) C. Sum=40
{ { D. Compilation error
int a[] = sum +=
new int[50]; a[l];
Answer : C
}
int i =
27 % 11;
int j = 5 System.out.println("Sum
* 3; = " + sum);
int k = i }
- j; }

a[i] = i;
a[j] = j;
//a[k] = k;
QUESTION 13
What will be the output of the following program?
class Main
A. 1 8 7 4 5 6 3
{
B. 1 8 7 4 5 6 3
public static void main(String s[])
1874563
{
1874563
int list[] = new int[] {1, 8, 7, 4, 5, 6,
1874563
3};
1874563
int count = 1;
C. 1 1 1 1 1 1 1
int copy[][] = new int[list.length][count];
for(int i = 0; i < list.length; i++) 8888888
{ 7777777
copy[i][0] = list[i]; 4444444
for(int j = 0; j < count; j++) D. Compilation Error
{
Answer: A
System.out.print(copy[i][j] + " ");
}
}
System.out.println();
}
}
QUESTION 14
What will be the output of the following program?

class Main
A. 0 1 2
{
B. 0 13
public static void
main(String S[]) C. 3
{ D. 1
for(int i = 0; i E. 1 23
<= 3; i++)
{

if(i == 2)

continue; Answer : B

System.out.println(i);
}
}
}
QUESTION 15

What will be the output of the following program

class ForEachLoop A. Compilation error


{
B. 215 234 213 189
public static void main(String args[])
C. Exception
{
D. None of the above
int[] scores = new int[10];
scores = new int[3]; Answer : C
scores = {215, 234, 218, 189, 221, 290};

for(int score : scores)


{
System.out.print(score + " ");
}
}
}

You might also like