Java Programming Objective Questions
I. Exception Handling — Objective Questions
1. Which of the following is a checked exception in Java?
Answer: b) IOException
2. Which keyword is used to manually throw an exception?
Answer: a) throw
3. What happens if an exception is not caught in a program?
Answer: b) Program crashes and prints a stack trace
4. Which block always executes, whether an exception occurs or not?
Answer: d) finally
5. The superclass of all exception classes is:
Answer: b) Throwable
6. What is the output of the given try-catch-finally code?
Answer: c) Error Done
7. Which keyword is used in a method declaration to indicate that it might throw an exception?
Answer: b) throws
8. Can we have multiple catch blocks after one try?
Answer: a) Yes
II. Arrays — Objective Questions
1. What is the index of the first element in a Java array?
Answer: b) 0
2. What happens if you try to access an index outside the array size?
Answer: c) Throws ArrayIndexOutOfBoundsException
3. Arrays in Java are:
Answer: d) Both (a) and (c)
4. The length of an array arr can be found by:
Answer: b) [Link]
5. What will be the output of [Link](a[2]) for int[] a = {1, 2, 3, 4};?
Answer: c) 3
6. Which of these can be used to sort an array?
Answer: b) [Link]()
7. What is the default value for an int array element?
Answer: a) 0
8. Can we change the size of an array after creation?
Answer: b) No
III. String Class — Objective Questions
1. Which of these is immutable in Java?
Answer: a) String
2. What will be the output of 'String s1 = "Java"; String s2 = "Java"; [Link](s1 == s2);'?
Answer: a) true
3. Which method compares the content of two strings?
Answer: a) equals()
4. Which of the following creates a new string object each time it is modified?
Answer: a) String
5. Which method converts string to lowercase?
Answer: c) toLowerCase()
6. What is the output of 'String s = "Hello"; [Link]("World"); [Link](s);'?
Answer: a) Hello
7. Which method returns the character at a given index?
Answer: a) charAt()
8. What does length() method return for string 'Java'?
Answer: b) 4