Programming
Sample Input: Sample Output:
Found
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
arr_size = 5
arr[] = {23, 82, 57, 45, 38}
search_elem = 45
import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
int i;
for(i = 0; i < arr_size; i++)
{
arr[i] = sc.nextInt();
}
int search_elem = sc.nextInt();
int is_matched = 0;
for(i = 0; i < arr_size; i++)
{
if(arr[i] == search_elem)
{
is_matched = 1;
break;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if(is_matched == 1)
{
System.out.print("Found");
}
else
{
System.out.print("Not Found");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sample Input: Sample Output:
2
Question 2
Write a Java code to find the number of occurrences of a given number in
an array.
arr_size = 6
arr[] = {3, 82, 57, 45, 3, 8}
search_elem = 3
import java.util.Scanner;
public class MyClass {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
int i;
for(i = 0; i < arr_size; i++){
arr[i] = sc.nextInt();
}
int search_elem = sc.nextInt();
int count = 0;
for(i = 0; i < arr_size; i++)
{
if(arr[i] == search_elem)
{
count++;
}
}
System.out.print(count);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sample Input: Sample Output:
7
Question 3
Write a Java code to find the largest number in an array.
arr_size = 5
arr[] = {1, 7, 3, 4, 5}
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int arr[]=new int[a];
for(int i = 0; i < a; i++)
{
arr[i] = sc.nextInt();
}
int max = 0;
for(int i = 0; i < a; i++)
{
if(arr[i] > max)
{
max = arr[i];
}
}
System.out.print(max);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sample Input: Sample Output:
10, 20
30, 0
Question 4
Write a Java code to find all pairs of elements whose sum is equal to the
given value.
arr_size = 5
arr[] = {50, 10, 30, 20, 0}
value = 30
import java.util.Scanner;
public class Main{
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int arr[] = new int[a];
for(int i = 0; i < a; i++){
arr[i] = sc.nextInt();
}
int value = sc.nextInt();
int temp = 0;
for(int i = 0; i < a; i++) {
for(int j = i+1; j < a; j++) {
temp = arr[i]+arr[j];
if(temp == value){
System.out.print(arr[i]+ ", "+arr[j]);
System.out.println();
}
temp = 0;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sample Input: Sample Output:
3 4 5 1 2
Question 5
Write a Java code to rotate an array.
arr_size = 5
arr[] = {1, 2, 3, 4, 5}
no_of_rotations = 2
(HINT: You have to circularly left shift array by 2 positions)
import java.util.Scanner;
public class Main
{
public static void rotate_arr(int arr_size, int arr[], int no_of_rotate)
{
for(int i = 1; i <= no_of_rotate; i++)
{
int temp = arr[0];
for(int j = 1; j < arr_size; j++)
{
arr[j-1] = arr[j];
}
arr[arr_size-1] = temp;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
for(int index = 0; index < arr_size; index++)
{
arr[index] = sc.nextInt();
}
int no_of_rotate = sc.nextInt();
rotate_arr(arr_size, arr, no_of_rotate);
for(int i = 0; i < arr_size; i++)
{
System.out.print(arr[i] + " ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sample Input: Sample Output:
1 7
Question 6
Write a Java code to find the unique element in the given array.
arr_size = 6
arr[] = {1, 2, 3, 2, 3, 7}
import java.util.Scanner;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
for(int index = 0; index < arr_size; index++)
{
arr[index] = sc.nextInt();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
for(int index1 = 0; index1 <= arr_size-1; index1++)
{
int has_occurred = 0;
for(int index2 = 0; index2 <= arr_size-1; index2++)
{
if((index1 != index2) && (arr[index1] == arr[index2]))
{
has_occurred = 1;
break;
}
}
if(has_occurred == 0)
{
System.out.print(arr[index1] + " ");
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Sample Input: Sample Output:
Yes
Question 7
Write a Java code to check whether the given array is a palindrome or not. If
the given array is a palindrome, then print "Yes". Otherwise, print "No".
arr_size = 5
arr[] = {4, 5, 3, 5, 4}
import java.util.Scanner;
class Main{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int arr_size = sc.nextInt();
int arr[] = new int[arr_size];
for(int idx = 0; idx <= arr_size - 1; idx++)
{
arr[idx] = sc.nextInt();
}
int left = 0;
int right = arr_size - 1;
boolean is_palindrome = true;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
while(left <= right)
{
if(arr[left] != arr[right])
{
is_palindrome = false;
break;
}
left++;
right--;
}
if(is_palindrome == true){
System.out.print("Yes");
}
else{
System.out.print("No");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Predict the output
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int arr[] = {10, 20, 30, 40, 50};
System.out.print(arr[2]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
MCQ
No output
Question 1
A)
ArrayIndexOutOfBoundsException
B)
40
C)
30
D)
// Predict the output
import java.util.Scanner;
class Main
{
public static void main (String[] args)
{
int arr[] = {10, 20, 30, 40};
int a = 50;
call(a,arr);
System.out.println(a);
System.out.println(arr[0]);
System.out.println(arr[1]);
}
public static void call(int a, int arr[])
{
a = a + 2;
arr[0] = 100;
arr[1] = 200;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
50
100
200
Question 2
A)
52
100
200
B)
50
10
20
C)
52
10
20
D)
// Predict the output
import java.util.Scanner;
public class Main
{
public static void main(String args[])
{
int arr[2];
System.out.println(arr[0]);
System.out.println(arr[1]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Garbage value
Garbage value
Question 3
A)
ArrayIndexOutOfBoundsException
B)
Compilation error
C)
0
0
D)
THANK YOU

WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_15-Feb-2021_L6-1D_Program.pptx

  • 2.
  • 3.
    Sample Input: SampleOutput: Found 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 arr_size = 5 arr[] = {23, 82, 57, 45, 38} search_elem = 45
  • 4.
    import java.util.Scanner; public classMyClass { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int arr_size = sc.nextInt(); int arr[] = new int[arr_size]; int i; for(i = 0; i < arr_size; i++) { arr[i] = sc.nextInt(); } int search_elem = sc.nextInt(); int is_matched = 0; for(i = 0; i < arr_size; i++) { if(arr[i] == search_elem) { is_matched = 1; break; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 5.
    if(is_matched == 1) { System.out.print("Found"); } else { System.out.print("NotFound"); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 6.
    Sample Input: SampleOutput: 2 Question 2 Write a Java code to find the number of occurrences of a given number in an array. arr_size = 6 arr[] = {3, 82, 57, 45, 3, 8} search_elem = 3
  • 7.
    import java.util.Scanner; public classMyClass { public static void main(String args[]){ Scanner sc = new Scanner(System.in); int arr_size = sc.nextInt(); int arr[] = new int[arr_size]; int i; for(i = 0; i < arr_size; i++){ arr[i] = sc.nextInt(); } int search_elem = sc.nextInt(); int count = 0; for(i = 0; i < arr_size; i++) { if(arr[i] == search_elem) { count++; } } System.out.print(count); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 8.
    Sample Input: SampleOutput: 7 Question 3 Write a Java code to find the largest number in an array. arr_size = 5 arr[] = {1, 7, 3, 4, 5}
  • 9.
    import java.util.*; public classMain{ public static void main(String args[]){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int arr[]=new int[a]; for(int i = 0; i < a; i++) { arr[i] = sc.nextInt(); } int max = 0; for(int i = 0; i < a; i++) { if(arr[i] > max) { max = arr[i]; } } System.out.print(max); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 10.
    Sample Input: SampleOutput: 10, 20 30, 0 Question 4 Write a Java code to find all pairs of elements whose sum is equal to the given value. arr_size = 5 arr[] = {50, 10, 30, 20, 0} value = 30
  • 11.
    import java.util.Scanner; public classMain{ public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int arr[] = new int[a]; for(int i = 0; i < a; i++){ arr[i] = sc.nextInt(); } int value = sc.nextInt(); int temp = 0; for(int i = 0; i < a; i++) { for(int j = i+1; j < a; j++) { temp = arr[i]+arr[j]; if(temp == value){ System.out.print(arr[i]+ ", "+arr[j]); System.out.println(); } temp = 0; } } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 12.
    Sample Input: SampleOutput: 3 4 5 1 2 Question 5 Write a Java code to rotate an array. arr_size = 5 arr[] = {1, 2, 3, 4, 5} no_of_rotations = 2 (HINT: You have to circularly left shift array by 2 positions)
  • 13.
    import java.util.Scanner; public classMain { public static void rotate_arr(int arr_size, int arr[], int no_of_rotate) { for(int i = 1; i <= no_of_rotate; i++) { int temp = arr[0]; for(int j = 1; j < arr_size; j++) { arr[j-1] = arr[j]; } arr[arr_size-1] = temp; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 14.
    public static voidmain(String args[]) { Scanner sc = new Scanner(System.in); int arr_size = sc.nextInt(); int arr[] = new int[arr_size]; for(int index = 0; index < arr_size; index++) { arr[index] = sc.nextInt(); } int no_of_rotate = sc.nextInt(); rotate_arr(arr_size, arr, no_of_rotate); for(int i = 0; i < arr_size; i++) { System.out.print(arr[i] + " "); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 15.
    Sample Input: SampleOutput: 1 7 Question 6 Write a Java code to find the unique element in the given array. arr_size = 6 arr[] = {1, 2, 3, 2, 3, 7}
  • 16.
    import java.util.Scanner; class Main { publicstatic void main(String args[]) { Scanner sc = new Scanner(System.in); int arr_size = sc.nextInt(); int arr[] = new int[arr_size]; for(int index = 0; index < arr_size; index++) { arr[index] = sc.nextInt(); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 17.
    for(int index1 =0; index1 <= arr_size-1; index1++) { int has_occurred = 0; for(int index2 = 0; index2 <= arr_size-1; index2++) { if((index1 != index2) && (arr[index1] == arr[index2])) { has_occurred = 1; break; } } if(has_occurred == 0) { System.out.print(arr[index1] + " "); } } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 18.
    Sample Input: SampleOutput: Yes Question 7 Write a Java code to check whether the given array is a palindrome or not. If the given array is a palindrome, then print "Yes". Otherwise, print "No". arr_size = 5 arr[] = {4, 5, 3, 5, 4}
  • 19.
    import java.util.Scanner; class Main{ publicstatic void main(String args[]) { Scanner sc = new Scanner(System.in); int arr_size = sc.nextInt(); int arr[] = new int[arr_size]; for(int idx = 0; idx <= arr_size - 1; idx++) { arr[idx] = sc.nextInt(); } int left = 0; int right = arr_size - 1; boolean is_palindrome = true; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 20.
    while(left <= right) { if(arr[left]!= arr[right]) { is_palindrome = false; break; } left++; right--; } if(is_palindrome == true){ System.out.print("Yes"); } else{ System.out.print("No"); } } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 21.
    // Predict theoutput import java.util.Scanner; public class Main { public static void main(String args[]) { int arr[] = {10, 20, 30, 40, 50}; System.out.print(arr[2]); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 22.
  • 23.
  • 24.
    // Predict theoutput import java.util.Scanner; class Main { public static void main (String[] args) { int arr[] = {10, 20, 30, 40}; int a = 50; call(a,arr); System.out.println(a); System.out.println(arr[0]); System.out.println(arr[1]); } public static void call(int a, int arr[]) { a = a + 2; arr[0] = 100; arr[1] = 200; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 25.
  • 26.
    // Predict theoutput import java.util.Scanner; public class Main { public static void main(String args[]) { int arr[2]; System.out.println(arr[0]); System.out.println(arr[1]); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
  • 27.
    Garbage value Garbage value Question3 A) ArrayIndexOutOfBoundsException B) Compilation error C) 0 0 D)
  • 28.

Editor's Notes

  • #2 1st slide (Mandatory)
  • #3 Section End/Start Use this at section’s start or end. For example: “Questions” or “Time for Practice”. To be used for Impacts (get the student’s attention).
  • #6 Input : arr_size = 6 arr[] = {23, 82, 57, 45, 38, 63} search_elem = 12 Output: Not Found
  • #7 Question+ Input + Output (Programming)
  • #9 Question+ Input + Output (Programming)
  • #11 Question+ Input + Output (Programming)
  • #14 Output: 4 7 34 67 100
  • #15 Output: 4 7 34 67 100
  • #23 Section End/Start Use this at section’s start or end. For example: “Questions” or “Time for Practice”. To be used for Impacts (get the student’s attention).
  • #25 We have passed the variable 'a' and array values to the function. Inside the function, value 2 is added with the variable 'a' and array values in the index 0 and 1 are changed with different values. The added value is not printed inside the main function. This is because 'a' is a single variable. But for an array, when we try to print inside the main function the values get changed. This is because while passing an array it's reference will be passed.
  • #29 Thank you slide