0% found this document useful (1 vote)
4K views7 pages

Dsapaper 1

Good

Uploaded by

Venu Kondragunta
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 (1 vote)
4K views7 pages

Dsapaper 1

Good

Uploaded by

Venu Kondragunta
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
You are on page 1/ 7

1. Which of the following is the correct way to declare an array in Java?

a) int arr[];
b) int[] arr;
c) int arr[5];
d) Both a and b

2. What is the default value of elements in an integer array in Java?


a) 0
b) null
c) -1
d) undefined

3. Which of these is an incorrect way to initialize an array?


a) int[] arr = new int[5];
b) int[] arr = {1, 2, 3, 4, 5};
c) int arr[] = new int[5] {1, 2, 3, 4, 5};
d) int arr[] = new int[]{1, 2, 3, 4, 5};

4. What is the output of the following code?


int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[5]);

a) 5
b) 0
c) ArrayIndexOutOfBoundsException
d) Compilation Error

5. What is the time complexity for accessing an element from an array using its
index?
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

6. Which of the following methods is used to sort an array in Java?


a) Collections.sort()
b) Arrays.sort()
c) Array.sort()
d) List.sort()
7. What will happen if you assign a value to an array element with an index that
is outside the array’s valid range?
a) The compiler will throw an error.
b) The array size will increase automatically.
c) The program will crash.
d) An ArrayIndexOutOfBoundsException will be thrown.

Here are some multiple-choice questions (MCQs) on Java arrays:

1. Which of the following is the correct way to declare an array in Java?


a) int arr[];
b) int[] arr;
c) int arr[5];
d) Both a and b
Answer: d) Both a and b

2. What is the default value of elements in an integer array in Java?


a) 0
b) null
c) -1
d) undefined

Answer: a) 0

3. Which of these is an incorrect way to initialize an array?


a) int[] arr = new int[5];
b) int[] arr = {1, 2, 3, 4, 5};
c) int arr[] = new int[5] {1, 2, 3, 4, 5};
d) int arr[] = new int[]{1, 2, 3, 4, 5};

Answer: c) int arr[] = new int[5] {1, 2, 3, 4, 5};

4. What is the output of the following code?


java
Copy code
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr[5]);
a) 5
b) 0
c) ArrayIndexOutOfBoundsException
d) Compilation Error

Answer: c) ArrayIndexOutOfBoundsException

5. What is the time complexity for accessing an element from an array using its
index?
a) O(1)
b) O(n)
c) O(log n)
d) O(n^2)

Answer: a) O(1)

6. Which of the following methods is used to sort an array in Java?


a) Collections.sort()
b) Arrays.sort()
c) Array.sort()
d) List.sort()

Answer: b) Arrays.sort()

7. What will happen if you assign a value to an array element with an index that
is outside the array’s valid range?
a) The compiler will throw an error.
b) The array size will increase automatically.
c) The program will crash.
d) An ArrayIndexOutOfBoundsException will be thrown.

Answer: d) An ArrayIndexOutOfBoundsException will be thrown.

8. How do you determine the length of an array arr in Java?


a) arr.length()
b) arr.length
c) arr.size()
d) arr.getLength()
9. Which of these is a valid multidimensional array declaration in Java?
a) int[][] arr = new int[3][3];
b) int arr[3][3];
c) int arr[][] = new int[3];
d) int[][] arr = new int[3]

10. What is the correct way to copy one array into another in Java?
a) arr2 = arr1;
b) arr2 = Arrays.copy(arr1);
c) System.arraycopy(arr1, 0, arr2, 0, arr1.length);
d) arr2 = Arrays.copyOf(arr1);

Linear Search Questions

1. What is the time complexity of the linear search algorithm in the worst case?
a) O(1)
b) O(log n)
c) O(n)
d) O(n^2)

2. In a linear search, how many comparisons are needed in the worst case to find
an element in an array of size n?
a) 1
b) log n
c) n
d) n/2

3. Which of the following is a key characteristic of the linear search algorithm?


a) It only works on sorted arrays.
b) It requires logarithmic time to search.
c) It checks each element sequentially.
d) It uses divide and conquer strategy.

4. What is the best-case time complexity of the linear search algorithm?


a) O(1)
b) O(n)
c) O(n^2)
d) O(log n)
5. When is linear search preferable over binary search?
a) When the array is sorted.
b) When the array is large.
c) When the array is small and unsorted.
d) Linear search is never preferable over binary search.

6. What does a linear search return if the element is not found in the array?
a) -1
b) 0
c) The index of the last element
d) An exception is thrown

7. Which of the following is true about linear search?


a) It can only be applied to arrays.
b) It can only be applied to sorted data structures.
c) It is inefficient for large datasets.
d) It uses a binary split mechanism.

8. In a linear search, how many comparisons are made if the element is at the first position?
a) 0
b) 1
c) n
d) n/2

9. Which of the following scenarios would benefit the most from using a linear
search?
a) Searching in a sorted list with millions of elements.
b) Searching in a small list with a few elements.
c) Searching in a large list using a divide and conquer approach.
d) Searching in a tree structure.

10. What is the main disadvantage of using a linear search algorithm?


a) It requires a lot of memory.
b) It can only search for numbers.
c) It is slow for large datasets.
d) It requires the data to be sorted.

Binary search

1. What is the key prerequisite for applying binary search?


a) The array must be sorted.
b) The array must contain only integers.
c) The array must be unsorted.
d) The array must be small.

2. What is the time complexity of binary search in the best case?


a) O(n)
b) O(log n)
c) O(1)
d) O(n log n)

3. What is the time complexity of binary search in the worst case?


a) O(n)
b) O(log n)
c) O(1)
d) O(n^2)

4. How does binary search work?


a) It checks each element sequentially until the target is found.
b) It divides the search space into three parts.
c) It compares the target element with the middle element and eliminates half of the search space
based on the comparison.
d) It performs a linear scan of the array.

5. Which of the following conditions is true for terminating a binary search successfully?
a) low > high
b) low == high
c) arr[mid] == target
d) arr[mid] != target

6. In a binary search, if the middle element is greater than the target value, where should you
search next?
a) In the right half of the array
b) In the left half of the array
c) In the entire array
d) No need to search further

7. What is the primary advantage of binary search over linear search?


a) It does not require sorted data.
b) It works well on unsorted data.
c) It has a lower time complexity for large datasets.
d) It is easier to implement.

8. Which of the following is true about binary search?


a) It always returns the index of the first occurrence of the target element.
b) It can only be applied to sorted arrays.
c) It can be applied to both sorted and unsorted arrays.
d) It requires O(n) time in the best case.

9. In a binary search, if the target value is not found, what should be returned?
a) 0
b) The index of the last checked element
c) -1
d) An exception is thrown

10. Which of the following is the correct formula to calculate the middle index in binary
search to avoid overflow issues?
a) mid = (low + high) / 2
b) mid = low + (high - low) / 2
c) mid = (low + high) * 2
d) mid = low - high / 2

You might also like