MCQ on array
MCQ on array
a) 0
b) null
c) undefined
d) NaN
6. What is the time complexity for accessing an element in an array using its index?
a) O(1)
b) O(n)
c) O(n²)
d) O(log n)
7. What will happen if you try to access an index beyond the array's size?
a) Compilation error
b) ArrayIndexOutOfBoundsException
c) NullPointerException
d) No output
8. Which of the following is a multi-dimensional array?
a) int arr[3];
b) int[][] arr;
c) int arr = new int[3];
d) int[] arr;
Exception Handling
Packages in Java
Advanced Questions
32. What is the correct way to use a class from another package?
a) Fully qualify the class name
b) Use import statement
c) Both a and b
d) None of the above
Arrays (Advanced)
37. System.out.println(arr[3]);
a) 3
b) 0
c) ArrayIndexOutOfBoundsException
d) null
Which of the following is true for a jagged array?
a) All rows must have the same length
b) Rows can have different lengths
c) A jagged array can only store integers
d) Java does not support jagged arrays
Which statement correctly creates a jagged array in Java?
a) int[][] arr = new int[3][];
b) int[][] arr = new int[3][3];
c) int[][] arr = new int[][3];
d) int[][] arr = {3};
40. System.out.println(arr[1][2]);
a) 3
b) 5
c) ArrayIndexOutOfBoundsException
d) Compilation Error
Which method can be used to copy an array?
a) System.arraycopy()
b) Arrays.copyOf()
c) Both a and b
d) None of the above
What is the output of the following code?
42. System.out.println(Arrays.toString(arr));
a) 10, 20, 30
b) [10, 20, 30]
c) {10, 20, 30}
d) Compilation Error
Answer: b) [10, 20, 30]
Exception Handling (Advanced)
43. Which exception is thrown when there is an attempt to cast an object to a subclass it does
not belong to?
a) ClassCastException
b) IllegalArgumentException
c) NullPointerException
d) ArrayIndexOutOfBoundsException
45. try {
46. int a = 5 / 0;
49. } finally {
51. }
a) Exception caught
b) Finally executed
c) Exception caught\nFinally executed
d) Compilation Error
Which of the following is NOT a valid exception type in Java?
a) IOException
b) ArithmeticException
c) CompileTimeException
d) NullPointerException
How do you define a custom exception in Java?
a) By extending the Exception class
b) By extending the Throwable class
c) Both a and b
d) By implementing the Runnable interface
What is the purpose of the throws keyword?
a) To manually throw an exception
b) To declare exceptions a method can throw
c) To handle an exception
d) To terminate program execution
Which exception is thrown when attempting to use a null object?
a) NullPointerException
b) IllegalArgumentException
c) IndexOutOfBoundsException
d) NoSuchElementException
Answer: a) NullPointerException
Packages (Advanced)
49. What happens if two classes with the same name but different packages are imported?
a) Compilation error
b) Both classes are available
c) Fully qualified names must be used to resolve conflicts
d) Only one class is imported