0% found this document useful (0 votes)
24 views

MCQ on array

Uploaded by

rohanbiswas0707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

MCQ on array

Uploaded by

rohanbiswas0707
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. What is the default value of an element in an integer array?

a) 0
b) null
c) undefined
d) NaN

2. How do you declare a one-dimensional array in Java?


a) int arr[] = new int[];
b) int arr[] = new int[size];
c) int arr = new int[size];
d) arr = new int[size];

3. Which method is used to find the length of an array?


a) size()
b) length()
c) length
d) getLength()

4. What is the index of the last element in an array of size n?


a) n
b) n - 1
c) n + 1
d) 0

5. Which of the following is NOT a valid way to initialize an array?


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

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;

9. How do you create a 2D array in Java?


a) int arr[][] = new int[][];
b) int arr[][] = new int[size1][size2];
c) int arr = new int[size1][size2];
d) arr = new int[size1][size2];

10. Which loop is generally used to traverse an array?


a) while
b) for-each
c) do-while
d) All of the above

Exception Handling

11. Which is the parent class of all exceptions in Java?


a) Error
b) Throwable
c) Exception
d) RuntimeException

12. What is the purpose of a try block in Java?


a) To monitor code for exceptions
b) To handle exceptions
c) To throw exceptions
d) To ignore exceptions

13. Which keyword is used to manually throw an exception?


a) throws
b) throw
c) catch
d) try

14. Which exception is thrown when a divide-by-zero operation occurs?


a) ArithmeticException
b) NullPointerException
c) IOException
d) IllegalArgumentException

15. What is the role of the finally block in Java?


a) To execute code only when an exception occurs
b) To execute code whether an exception occurs or not
c) To rethrow exceptions
d) None of the above

16. Which of the following is an unchecked exception?


a) IOException
b) FileNotFoundException
c) NullPointerException
d) SQLException

17. What happens when an exception is not handled?


a) Program continues execution
b) Program halts execution
c) Program skips the problematic code
d) None of the above

18. Which of these exceptions are NOT part of java.lang?


a) ClassNotFoundException
b) ArithmeticException
c) FileNotFoundException
d) ArrayIndexOutOfBoundsException

19. Which block is used to handle an exception?


a) throw
b) try
c) catch
d) finally

20. Which of these is a checked exception?


a) IOException
b) ArithmeticException
c) ArrayIndexOutOfBoundsException
d) ClassCastException

Packages in Java

21. What is the default package available in every Java program?


a) java.lang
b) java.util
c) java.io
d) java.math

22. Which keyword is used to define a package in Java?


a) package
b) import
c) namespace
d) module

23. Which package contains the Scanner class?


a) java.io
b) java.util
c) java.net
d) java.lang

24. How do you import all classes from a package?


a) import package.*;
b) import package.all;
c) include package.*;
d) use package.all;

25. Which package contains the File class?


a) java.net
b) java.util
c) java.io
d) java.nio

26. What is the main benefit of using packages in Java?


a) Code reusability
b) Faster execution
c) More storage
d) Better exception handling

27. Which package contains classes for database connectivity?


a) java.db
b) java.sql
c) java.util
d) java.io

28. What does the java.time package provide?


a) File operations
b) Date and time classes
c) Mathematical functions
d) Networking utilities

29. Which of these is NOT part of the Java API?


a) java.lang
b) java.graphics
c) java.util
d) java.io

30. Which package contains the Random class?


a) java.lang
b) java.util
c) java.io
d) java.math

Advanced Questions

31. How do you prevent a class from being part of a package?


a) Do not use the package keyword
b) Use final keyword
c) Use static keyword
d) Use abstract keyword

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

33. Which of these packages contains classes for networking?


a) java.net
b) java.io
c) java.util
d) java.lang

Arrays (Advanced)

34. Which method is used to sort an array in Java?


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

35. What is the output of the following code?

36. int arr[] = {1, 2, 3};

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};

38. What will be the output of the following code?

39. int arr[][] = {{1, 2, 3}, {4, 5}};

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?

41. int[] arr = {10, 20, 30};

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)

41. Can a finally block be executed without a catch block?


a) Yes
b) No
c) Only if the exception is handled elsewhere
d) Only with try-with-resources

42. What happens if a catch block does not handle an exception?


a) The exception is ignored
b) The program terminates
c) The exception is propagated to the calling method
d) The program moves to the finally block

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

44. What will be the output of the following code?

45. try {

46. int a = 5 / 0;

47. } catch (ArithmeticException e) {

48. System.out.println("Exception caught");

49. } finally {

50. System.out.println("Finally executed");

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

50. Which of these classes is NOT part of the java.lang package?


a) Math
b) String
c) System
d) Scanner

You might also like