You are on page 1of 34

Real Time Problem

Ramesh is a Teacher. He needs to store marks


of 12 students in his class.

HOW?
Solution 1
Ramesh can create twelve set of
variables to store marks of twelve
students.

Mark1 Mark2 Mark3 Mark12

S1 S2 S3 … … ... S12
Solution 2
He can create one variable called mark in Mark
that mark he can store marks of twelve
Students

S1 S2 S3 S3 S12
Array
Java array is an object which contains elements of a similar data type.
Array Types

• 1D array
• 2D array
How to declare an array
dataType[] arrayName;

Example:
int[] data;
data = new int[10];
(or)
int[] data=new int[10];
Memory Allocation

• Static 1D array
• Dynamic 1D array
Java array index
int data[]=new int[5];

Last element
First element
data[0] data[1] data[2] data[3] data[4]
1 2 3 4 5
0 1 2 3 4

Index Array data of length 5


1 // Predict the output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 int[] arr = {12, 4, 5, 2, 5};
8 for (int i = 0; i < arr.length; i++)
9 {
10 System.out.print(arr[i] + " ");
11 }
12 }
13 }
14
15
16
17
18
19
20
21
22
1 // Predict the output
2 import java.util.*;
3 public class Main
4 {
5 public static void main(String[] args)
6 {
7 Scanner s=new Scanner(System.in);
8 int n = s.nextInt();
9 int arr[]=new int[n];
10 for(int i = 0; i < arr.length; i++)
11 {
12 arr[i] = s.nextInt();
13 }
14 for (int i = 0; i < arr.length; i++)
15 {
16 System.out.print(arr[i] + " " );
17 }
18 }
19 }
20
21
22
Anonymous Array
• An array in Java without any name is called anonymous array.

• It is just for instant use (i.e. one time usage) .

• Anonymous array is passed as an argument of method.

Syntax:
new Datatype[] {val1, val2..valn};
1
2 import java.util.*;
3 public class MyClass {
4 public static void main(String args[])
5 {
6 int s1 = sum(new int[]{1,2,3,4,5});
7 System.out.print(s1);
8 }
9 public static int sum(int a[])
10 {
11 int total = 0;
12 for(int i:a)
13 {
14 total = total+i;
15 }
16 return total;
17 }
18 }
19
20
21
22
Programming
Question 1
Write a Java code to search a given number in an array. If the element is
found then print Found, else print Not Found

Sample Input: Sample Output:


arr_size = 5 Found
arr[] = {23, 82, 57, 45, 38}
search_elem = 45
1 import java.util.Scanner;
2 public class MyClass {
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int arr_size = sc.nextInt();
6 int arr[] = new int[arr_size];
7 int i;
8 for(i = 0; i < arr_size; i++)
9 {
10 arr[i] = sc.nextInt();
11 }
12 int search_elem = sc.nextInt();
13 int is_matched = 0;
14 for(i = 0; i < arr_size; i++)
15 {
16 if(arr[i] == search_elem)
17 {
18 is_matched = 1;
19 break;
20 }
21 }
22
1 if(is_matched == 1)
2 {
3 System.out.print("Found");
4 }
5 else
6 {
7 System.out.print("Not Found");
8 }
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 2
Write a Java code to find the number of occurrences of a given number in
an array.

Sample Input: Sample Output:


arr_size = 6 2
arr[] = {3, 82, 57, 45, 3, 8}
search_elem = 3
1 import java.util.Scanner;
2 public class MyClass {
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int arr_size = sc.nextInt();
6 int arr[] = new int[arr_size];
7 int i;
8 for(i = 0; i < arr_size; i++){
9 arr[i] = sc.nextInt();
10 }
11 int search_elem = sc.nextInt();
12 int count = 0;
13 for(i = 0; i < arr_size; i++)
14 {
15 if(arr[i] == search_elem)
16 {
17 count++;
18 }
19 }
20 System.out.print(count);
21 }
22 }
Question 3
Write a Java code to find the largest number in an array.

Sample Input: Sample Output:


arr_size = 5 7
arr[] = {1, 7, 3, 4, 5}
1 import java.util.*;
2 public class Main{
3 public static void main(String args[]){
4 Scanner sc = new Scanner(System.in);
5 int a = sc.nextInt();
6 int arr[]=new int[a];
7 for(int i = 0; i < a; i++)
8 {
9 arr[i] = sc.nextInt();
10 }
11 int max = 0;
12 for(int i = 0; i < a; i++)
13 {
14 if(arr[i] > max)
15 {
16 max = arr[i];
17 }
18 }
19 System.out.print(max);
20 }
21 }
22
Question 4
Write a Java code to find all pairs of elements whose sum is equal to the
given value.

Sample Input: Sample Output:


arr_size = 5 10, 20
arr[] = {50, 10, 30, 20, 0} 30, 0
value = 30
1 import java.util.Scanner;
2 public class Main{
3 public static void main(String args[]) {
4 Scanner sc = new Scanner(System.in);
5 int a = sc.nextInt();
6 int arr[] = new int[a];
for(int i = 0; i < a; i++){
7
arr[i] = sc.nextInt();
8
}
9
int value = sc.nextInt();
10 int temp = 0;
11 for(int i = 0; i < a; i++) {
12 for(int j = i+1; j < a; j++) {
13 temp = arr[i]+arr[j];
14 if(temp == value){
15 System.out.print(arr[i]+ ", "+arr[j]);
16 System.out.println();
17 }
18 temp = 0;
19 }
20 }
21 }
22 }
Question 5
Write a Java code to rotate an array.

Sample Input: Sample Output:


arr_size = 5 34512
arr[] = {1, 2, 3, 4, 5}
no_of_rotations = 2

(HINT: You have to circularly left shift array by 2 positions)


1 import java.util.Scanner;
2 public class Main
3 {
4 public static void rotate_arr(int arr_size, int arr[], int no_of_rotate)
5 {
6 for(int i = 1; i <= no_of_rotate; i++)
7 {
8 int temp = arr[0];
9 for(int j = 1; j < arr_size; j++)
10 {
11 arr[j-1] = arr[j];
12 }
13 arr[arr_size-1] = temp;
14 }
15 }
16
17
18
19
20
21
22
1 public static void main(String args[])
2 {
3 Scanner sc = new Scanner(System.in);
4 int arr_size = sc.nextInt();
5 int arr[] = new int[arr_size];
6 for(int index = 0; index < arr_size; index++)
7 {
8 arr[index] = sc.nextInt();
9 }
10 int no_of_rotate = sc.nextInt();
11 rotate_arr(arr_size, arr, no_of_rotate);
12 for(int i = 0; i < arr_size; i++)
13 {
14 System.out.print(arr[i] + " ");
15 }
16 }
17 }
18
19
20
21
22
MCQ
1 // Predict the output
2 import java.util.Scanner;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 int arr[] = {10, 20, 30, 40, 50};
8 System.out.print(arr[2]);
9 }
10 }
11
12
13
14
15
16
17
18
19
20
21
22
Question 1
A) No output

B) ArrayIndexOutOfBoundsException

C) 40

D) 30
1 // Predict the output
2 import java.util.Scanner;
3 class Main
4 {
5 public static void main (String[] args)
6 {
7 int arr[] = {10, 20, 30, 40};
int a = 50;
8
call(a,arr);
9
System.out.println(a);
10
System.out.println(arr[0]);
11 System.out.println(arr[1]);
12 }
13 public static void call(int a, int arr[])
14 {
15 a = a + 2;
16 arr[0] = 100;
17 arr[1] = 200;
18 }
19 }
20
21
22
Question 2
A) 50
100
200
B) 52
100
200
C) 50
10
20
D) 52
10
20
1 // Predict the output
2 import java.util.Scanner;
3 public class Main
4 {
5 public static void main(String args[])
6 {
7 int arr[2];
8 System.out.println(arr[0]);
9 System.out.println(arr[1]);
10 }
11 }
12
13
14
15
16
17
18
19
20
21
22
Question 3
A) Garbage value
Garbage value

B) ArrayIndexOutOfBoundsException

C) Compilation error

D) 0
0
THANK YOU

You might also like