[Go to site: main page, start]

0% found this document useful (0 votes)
5 views43 pages

Array Java Notes

The document serves as a checklist and introduction to arrays in Java, covering topics such as syntax, accessing elements, and basic operations. It includes various questions and coding exercises related to arrays, memory allocation, and array manipulation techniques. Additionally, it addresses common misconceptions and provides multiple-choice questions for assessment.

Uploaded by

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

Array Java Notes

The document serves as a checklist and introduction to arrays in Java, covering topics such as syntax, accessing elements, and basic operations. It includes various questions and coding exercises related to arrays, memory allocation, and array manipulation techniques. Additionally, it addresses common misconceptions and provides multiple-choice questions for assessment.

Uploaded by

ghompiyush
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

JAVA

ARRAY O
-1
Today’s checklist
1. Introduction to Arrays
2. Syntax , accessing elements of Arrays
3. Printing Output and Taking Input
4. Length operator
5. Basic problems
6. Memory Allocation of Arrays in Java
What is an array?
Syntax and Declaration
How to access Elements in Array ?
Printing Output & Taking Input
Length operator
Ques:
Q1 : Given an array of marks of students, if the mark of any
student is less than 35 print its roll number. [roll number
here refers to the index of the array.

O I 2 3 Y S 6

aus 81 17 9 3631 100 60

for ( (S

if (arr[i] < 35) sout(i) ;

3
Ques:
Q2 : Are the following array declarations correct?

int a (25) ;

int size = 10, b[size] ;

int c = {0,1,2} ;
Ques:
Q3 : Which element of the array does this expression
reference?

num[4]
Predict the output :
public class Main {
public static void main(String[] args) {
int[] num = new int[26];
num[0] = 100;
num[25] = 200;
int temp = num[25];
num[25] = num[0];
num[0] = temp;
[Link]("\n" + num[0] + " " + num[25]);
}
}
Ques:
Q4 : Calculate the sum of all the elements in the given
array.
Linear search
Q5 : Find the element ‘x’ in the array . Take array
and x as input.
Ques:
Q6 : Find the maximum value out of all the elements in the
array. O I 3 Y S 6 2

arr =
[ 10 ,
8
,
12
,
4, 6
,
23 3 83
-

I
M-1 : intmx = arr[o] ;
m
-

i + + )4
to for (inti = 1; i< n ;

if(arr[i] < mx) mx =


arr(i] ;
23 3
Ques:
Q6 : Find the maximum value out of all the elements in the
array.
Ques:
Q7 : Find the second largest element in the given Array.
MCQ :
What is the difference between the 5’s in these two
expressions?

int num[]=new int[5] ;


num[5] = 11 ;

1. first is particular element, second is type


2. first is array size, second is particular element
3. first is particular element, second is array size
4. both specify array size
MCQ :
What would happen if you assign a value to an element of
an array whose subscript exceeds the size of the array?

1. the element will be set to 0


2. nothing, it’s done all the time
3. other data may be overwritten
4. error message from the compiler
State TRUE or FALSE :
1. The array int[] num = new int[26] has twenty-six elements. True

2. The expression num[1] designates the first element in the array False
second element
3. It is necessary to initialize the array at the time of declaration. Talso
-

4. The expression num[27] designates the 28th element in the array. True
Ques:
Q5: Count the number of elements in given array greater
than a given number x.
Point out the errors(if any) in the
following code:
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int size = [Link]();
int[] arr = new int[size];
for (int i = 0; i < size; i++) {
arr[i] = [Link]();
[Link](arr[i] + " ");
}
}
}
Passing Array to Methods
Passing Array to Methods

'
MCQ :
When you pass an array as an argument to a Method, what
actually gets passed?
1. address of the array
2. values of the elements of the array
3. address of the first element of the array
4. number of elements of the array
ArrayList in Java
Basic Operations on ArrayList
Ques :
Q8 : Find the doublet in the Array whose sum is equal to the
given value x. (Two Sum)
Two Pointers
Q9 : Write a program to reverse the array without using any
extra array.
Two Pointers
Q9 : Write a program to reverse the array without using any
extra array.
Ques :
Q10 : Rotate the given array ‘a’ by k steps, where k is
non-negative.
Note : k can be greater than n as well where n is the size of array ‘a’.
Ques :
Q10 : Rotate the given array ‘a’ by k steps, where k is
non-negative.
Note : k can be greater than n as well where n is the size of array ‘a’.
Ques :
Q10 : Rotate the given array ‘a’ by k steps, where k is
non-negative.
Note : k can be greater than n as well where n is the size of array ‘a’.
Ques :
Q11. Sort the array of 0’s and 1’s .
Ques :
Q11. Sort the array of 0’s and 1’s .
Ques :
Q11. Sort the array of 0’s and 1’s .
Ques :
Q12. Sort the array of 0’s , 1’s and 2’s . (Dutch Flag Algorithm)
Ques :
Q12. Sort the array of 0’s , 1’s and 2’s . (Dutch Flag Algorithm)
Ques :
Q12. Sort the array of 0’s , 1’s and 2’s . (Dutch Flag Algorithm)
Ques :
Q12. Sort the array of 0’s , 1’s and 2’s . (Dutch Flag Algorithm)
Ques :
Q13. Merge two sorted arrays .
Ques :
Q13. Merge two sorted arrays .
Ques :
Q14. Next greatest element.
THANK YOU

You might also like