Question 1
Question: What is an exception?
Options:
(A) Not to consider
(B) Error during the compilation of program
(C) All types of error situations
(D) (B) and (C) both
Answer: (D) (B) and (C) both
Explanation: An exception refers to a situation where a program encounters an abnormal condition during execution, either during compilation (compile-time errors) or at runtime (runtime exceptions).
Memory Tip: Remember that an exception is an error that occurs during compilation or execution. You can think of it as any unexpected event that causes problems in the code.
Question 2
Question: What is indicated by an exception?
Options:
(A) Alert message
(B) Error message
(C) Confirm message
(D) Nothing is displayed
Answer: (B) Error message
Explanation: An exception triggers an error message that informs the user about the problem in the program, helping to identify and fix the issue.
Memory Tip: Think of an exception as a “red flag” in programming—it’s a message that says, “Oops, something went wrong!”
Question 3
Question: What is true about the exception?
Options:
(A) Exception occurs infrequently
(B) Exception handling allows a program to continue as if no problem is there
(C) It notifies the user of the problem before terminating in an uncontrolled manner
(D) All of these
Answer: (D) All of these
Explanation: Exceptions generally occur less frequently, but exception handling can ensure that the program doesn’t crash. It allows the program to continue running while notifying the user about the problem.
Memory Tip: Exception handling is like a safety net in your code. It allows the program to continue, notify the user, and catch errors before a crash.
Question 4
Question: Errors are classified into __ categories.
Options:
(A) 2
(B) 3
(C) 4
(D) 5
Answer: (A) 2
Explanation: Errors in Java are mainly classified into two categories: compile-time errors (occur during compilation) and runtime errors (occur during execution).
Memory Tip: Think of errors as two main types: compile (before running) and runtime (while running). C for Compile, R for Runtime.
Question 5
Question: What is the use of a compiler?
Options:
(A) To convert source code to object code.
(B) Convert high level language to machine level language
(C) (A) or (B)
(D) To find grammatical mistakes
Answer: (C) (A) or (B)
Explanation: A compiler translates the program written in a high-level language (like Java) into machine-readable object or bytecode, which can be executed by the computer.
Memory Tip: A compiler is like a translator for your code. It translates high-level language into machine language (the language the computer understands).
Question 6
Question: When .class file is not created?
Options:
(A) If there is syntax error
(B) If undefined variables are used
(C) If the spelling of keyword is wrong
(D) Any situation like this will not create .class file
Answer: (D) Any situation like this will not create .class file
Explanation: If there are any errors (e.g., syntax errors, undefined variables, or misspelled keywords), the code won’t compile, and thus, the .class
file won’t be created.
Memory Tip: Remember: Errors prevent the creation of the .class
file, just like a missed appointment prevents you from attending the event (no .class = no run).
Question 7
Question: If there is no semicolon at the end of the statement, then what will happen?
Options:
(A) It will show error while compiling a program
(B) .class is not created
(C) (A) and (B) both
(D) Nothing happens
Answer: (C) (A) and (B) both
Explanation: A missing semicolon causes a compile-time error, and since the program cannot compile, the .class
file will not be created.
Memory Tip: A semicolon is like a period at the end of a sentence in programming. Without it, the statement is incomplete, causing errors.
Question 8
Question: In computer science, __ indicates whether the program is executed successfully or not.
Options:
(A) Exit code
(B) Exit status
(C) (A) or (B)
(D) (A) and (B) both
Answer: (C) (A) or (B)
Explanation: The exit code (or exit status) indicates the success or failure of a program. A return code (typically 0 for success, and non-zero for failure) is used to signal this.
Memory Tip: Think of the exit code as a report card—0 is pass (success), and any non-zero value is fail (error).
Question 9
Question: Exit code __ suggests that the program is executed successfully.
Options:
(A) 0
(B) 1
(C) -1
(D) ∞
Answer: (A) 0
Explanation: An exit code of 0 indicates that the program ran without errors and completed successfully.
Memory Tip: Exit code 0 = success; it’s like a green light signaling the program ran perfectly.
Question 10
Question: Exit code __ suggests that the program is not executed successfully.
Options:
(A) 0
(B) 1
(C) -1
(D) ∞
Answer: (B) 1
Explanation: An exit code of 1 (or any non-zero number) indicates that the program encountered an error and did not execute successfully.
Memory Tip: Exit code 1 = fail; it’s like a red flag indicating something went wrong during execution.
Question 11
Question: When .class file is generated?
Options:
(A) If a program is not compiled
(B) If a program is compiled
(C) (A) or (B)
(D) .class file is never created
Answer: (B) If a program is compiled
Explanation: A .class file is generated when a Java program is successfully compiled. The compiler converts the source code (.java
) into bytecode (.class
) that can be executed by the Java Virtual Machine (JVM).
Memory Tip: Think of compiling as turning your program into a readable form for the computer (i.e., .class
file is like the translated script of your program).
Question 12
Question: What is available for each type of exception in Java?
Options:
(A) Public class
(B) Related exception class
(C) Private class
(D) All of these
Answer: (B) Related exception class
Explanation: Java provides a specific exception class for each type of error that can occur. For example, ArrayIndexOutOfBoundsException
for out-of-bounds errors, ArithmeticException
for arithmetic issues, etc.
Memory Tip: Each exception has its own dedicated class, like a specialized doctor for each type of problem in your code.
Question 13
Question: __ package has corresponding classes to work with different exceptions.
Options:
(A) java.lang
(B) java.io
(C) java.hang
(D) (A) and (B) both
Answer: (D) (A) and (B) both
Explanation: The java.lang
package includes basic exception classes (like NullPointerException
, ArrayIndexOutOfBoundsException
), while the java.io
package deals with exceptions related to input/output operations (like IOException
).
Memory Tip: Think of java.lang
as the basic exception provider and java.io
as the one that handles input-output exceptions.
Question 14
Question: int a[] = new int[4]; a[13] = 99;
__ exception is applied to the previous code.
Options:
(A) ArrayIndexOutOfBoundsException
(B) ArithmeticException
(C) FileNotFound
(D) All of these
Answer: (A) ArrayIndexOutOfBoundsException
Explanation: The error occurs because you’re trying to access an element at index 13
in an array that only has 4 elements. This results in an ArrayIndexOutOfBoundsException
.
Memory Tip: Remember the array size: “Out of Bounds” means you’re trying to go beyond the limit, like reaching outside the boundary of a box.
Question 15
Question: int a = 50/0;
__ exception is applied to the above code.
Options:
(A) ArrayIndexOutOfBounds
(B) ArithmeticException
(C) NumberFormatException
(D) NullPointerException
Answer: (B) ArithmeticException
Explanation: Division by zero leads to an ArithmeticException
because it’s an illegal operation.
Memory Tip: Division by zero in mathematics is undefined; in programming, it throws an ArithmeticException.
Question 16
Question: String s = “Divyakant Sir”; int i = Integer.parseInt(s);
__ exception is applied to the above code.
Options:
(A) StringFormatException
(B) NumberFormatException
(C) ArithmeticException
(D) ArrayIndexException
Answer: (B) NumberFormatException
Explanation: The code tries to convert a string containing non-numeric characters into an integer, which results in a NumberFormatException
.
Memory Tip: parseInt() expects only numbers. If the string contains letters or symbols, it triggers a NumberFormatException
, like trying to convert a word into a number.
Question 17
Question: What is the meaning of exception handling?
Options:
(A) It is an object-oriented technique for managing errors
(B) Not to consider the exception situation
(C) To manage the exception situation
(D) All of these
Answer: (A) It is an object-oriented technique for managing errors
Explanation: Exception handling is an object-oriented method that allows a program to handle runtime errors, so it doesn’t crash unexpectedly.
Memory Tip: Think of exception handling as a safety cushion for your program. It helps manage errors without letting them take control.
Question 18
Question: __ is the status of exception.
Options:
(A) Static situation
(B) Error situation
(C) Dynamic situation
(D) Status of Java programming
Answer: (B) Error situation
Explanation: An exception represents an error situation that occurs during program execution, and it requires handling.
Memory Tip: Exceptions are errors that disrupt the flow of the program and need to be handled to avoid program crashes.
Question 19
Question: __ is considered while handling the exception situation.
Options:
(A) Program should not enter the loop
(B) Program is terminated in controlled manner
(C) Program is terminated in uncontrolled manner
(D) All of these
Answer: (C) Program is terminated in uncontrolled manner
Explanation: In exception handling, if the exception is not caught, it causes the program to terminate uncontrolled, meaning the program will stop unexpectedly.
Memory Tip: Think of exception handling as a way to stop uncontrolled crashes in your program, like stopping a car from crashing during an emergency.
Question 20
Question: __ keyword is used to make a program using exception handling.
Options:
(A) try
(B) catch
(C) finally
(D) All of these
Answer: (D) All of these
Explanation: In Java, the try, catch, and finally keywords are used together to handle exceptions. try
is used to block the code that might throw an exception, catch
handles the exception, and finally
ensures that code is executed after the try-catch block, regardless of whether an exception was thrown.
Memory Tip: Think of try as attempting something risky, catch as grabbing the exception if it happens, and finally as doing something after the attempt, no matter what.
Question 21
Question: Instructions raising one or more exceptions are included in _.
Options:
(A) try
(B) catch
(C) finally
(D) throw
Answer: (A) try
Explanation: The try block contains the code that might throw exceptions during execution. It’s where you put the code that could potentially cause errors.
Memory Tip: The try block is like a testing ground where the code is tried for any errors.
Question 22
Question: Instructions handling one or more exceptions are included in _.
Options:
(A) try
(B) catch
(C) finally
(D) throw
Answer: (B) catch
Explanation: The catch block is used to handle exceptions thrown by the try block. If an exception occurs in the try block, the control moves to the catch block.
Memory Tip: Catch the exception if it occurs, just like catching a ball in a game.
Question 23
Question: Finally block is always executed _.
Options:
(A) In the beginning of the program
(B) In the end of the program
(C) Before ending the program
(D) In any part of the program
Answer: (C) Before ending the program
Explanation: The finally block is always executed before the program ends, regardless of whether an exception was thrown or not. It is used for cleanup code like closing files or database connections.
Memory Tip: The finally block always executes last; think of it as the final clean-up crew that tidies up before the program ends.
Question 24
Question: If there are no exceptions in the try block then _.
Options:
(A) catch block need not to be written
(B) finally block must be created
(C) (A) and (B) both
(D) (A) or (B)
Answer: (C) (A) and (B) both
Explanation: If there are no exceptions, the catch block is not needed, but the finally block is optional. The finally block will run regardless of whether an exception was thrown.
Memory Tip: Catch block is for errors, so if there are none, it can be skipped. Finally block is for cleaning up, so it’s optional but helpful.
Question 25
Question: _ is incorrect for the try block.
Options:
(A) try block is a block included in braces
(B) try block may create one or more options
(C) try block tries to handle exceptions
(D) the statements of try block are the instructions which we need to look after
Answer: (C) try block tries to handle exceptions
Explanation: The try block doesn’t handle exceptions, it raises exceptions. It is the catch block that actually handles exceptions.
Memory Tip: The try block is where the error happens, but the catch block is where the error is handled.
Question 26
Question: The instructions having possibilities to have errors are written in _.
Options:
(A) try
(B) catch
(C) finally
(D) throw
Answer: (A) try
Explanation: The try block is where the code that might cause an exception is written. If an error occurs here, the exception is passed to the catch block.
Memory Tip: Try to execute the code, and if it fails, pass it to catch.
Question 27
Question: _ block should be there after the try block.
Options:
(A) catch
(B) finally
(C) throw
(D) (A) or (B)
Answer: (D) (A) or (B)
Explanation: After the try block, you can have either a catch block to handle exceptions or a finally block for cleanup. You can also have both, but one of them must come after the try block.
Memory Tip: After trying something risky, you either catch the exception or finally clean up.
Question 28
Question: Catch block must be _.
Options:
(A) before try block
(B) after try block
(C) at the end of the program
(D) after finally block
Answer: (B) after try block
Explanation: The catch block must immediately follow the try block. It handles the exception raised in the try block.
Memory Tip: Catch the exception right after the try; it’s like catching a ball thrown at you.
Question 29
Question: _ is the exception handler.
Options:
(A) Try block
(B) Catch block
(C) Finally block
(D) All of these
Answer: (B) Catch block
Explanation: The catch block is the actual exception handler in Java. It is responsible for catching and handling exceptions raised in the try block.
Memory Tip: The catch block handles the error, just like a catcher in baseball.
Question 30
Question: _ is the reason that the catch block is called exception handler.
Options:
(A) Catch block handles the exception
(B) Catch block is the main block
(C) Program is not possible without catch block
(D) All of these
Answer: (A) Catch block handles the exception
Explanation: The catch block is specifically designed to handle exceptions. This is why it is called the exception handler.
Memory Tip: Catch blocks are for catching and handling errors, not for doing anything else.
Question 31
Question: __ catch blocks can be there for one try block.
Options:
(A) Only one
(B) One or more
(C) 2
(D) As many try blocks as many catch blocks
Answer: (B) One or more
Explanation: You can have multiple catch blocks for one try block. This allows you to handle different types of exceptions separately.
Memory Tip: One or more catch blocks allow you to handle multiple types of exceptions. Think of it as having multiple ways to catch different kinds of problems.
Question 32
Question: _ is incorrect for the catch block.
Options:
(A) One parameter is given after the keyword catch
(B) Instructions for exception handling are written in braces
(C) If there are more than one exception, then also one catch block is enough
(D) Finally block must be before catch block
Answer: (D) Finally block must be before catch block
Explanation: The finally block is optional and is executed after the try and catch blocks. It is placed after the catch block, not before.
Memory Tip: Catch the exception first, then finally clean up; the catch block handles exceptions, while the finally block does the cleanup.
Question 33
Question: _ block creates ‘eobj’ object.
Options:
(A) Try block
(B) Catch block
(C) Finally block
(D) All of these
Answer: (A) Try block
Explanation: The try block contains the code that might throw an exception, and when an exception is thrown, it creates an object of the exception class (e.g., Exception eobj
).
Memory Tip: Think of the try block as creating an error when something goes wrong, and the catch block catches it.
Question 34
Question: _ holds the indication of ‘eobj’ object.
Options:
(A) Try block
(B) Catch block
(C) Finally block
(D) All of these
Answer: (B) Catch block
Explanation: The catch block handles the exception and holds the object (e.g., eobj
) that indicates what went wrong during execution.
Memory Tip: The catch block holds the exception like a container for errors.
Question 35
Question: What is the use of catch block?
Options:
(A) To handle the exceptions
(B) It does not allow the program to terminate unexpectedly
(C) (A) and (B) both
(D) Helpful in terminating the program
Answer: (C) (A) and (B) both
Explanation: The catch block is used to handle exceptions and prevent the program from terminating unexpectedly. It ensures the program can continue running even if an error occurs.
Memory Tip: The catch block prevents crashes by catching exceptions and keeping the program running smoothly.
Question 36
Question: The possibilities of arising exceptions are written in _.
Options:
(A) catch
(B) try
(C) finally
(D) throw
Answer: (B) try
Explanation: The try block is where you write the code that has the potential to throw exceptions. It’s where the exception might arise.
Memory Tip: The try block is where we try to run code that could have problems.
Question 37
Question: When are more catch blocks possible?
Options:
(A) If there are more than one exception occurred in try block
(B) To make the program easier
(C) If there is a possibility to have an error in the program
(D) This is not possible
Answer: (A) If there are more than one exception occurred in the try block
Explanation: Multiple catch blocks are needed if there are multiple exceptions that can occur in the try block, allowing different types of exceptions to be handled differently.
Memory Tip: If you expect multiple errors, you need multiple catches to handle each one.
Question 38
Question: _ block can throw different exceptions.
Options:
(A) The try block
(B) The catch block
(C) The finally block
(D) All of these
Answer: (A) The try block
Explanation: The try block can throw different exceptions based on the execution of its code. If any error occurs in the try block, it raises an exception.
Memory Tip: The try block is where the exceptions start (or are thrown).
Question 39
Question: Is it necessary to make a separate catch block for each type of exception?
Options:
(A) Yes
(B) No
(C) This is not possible
(D) It is dependent on the program
Answer: (A) Yes
Explanation: Yes, it is necessary to have a separate catch block for each type of exception, especially when you need to handle them differently.
Memory Tip: Each catch block handles one specific type of exception, like catching different types of balls in a game.
Question 40
Question: Where is the block for the default type catch division in the program?
Options:
(A) After the first catch block
(B) Last catch block
(C) At the end of the program
(D) This is not possible
Answer: (B) Last catch block
Explanation: The default catch block (also called the Exception catch block) should be placed last because it handles any remaining exceptions that were not caught by the previous catch blocks.
Memory Tip: The last catch block is like the final safety net for any exceptions not handled earlier.
Question 41
Question: _ block handles the exceptions in Java program.
Options:
(A) The catch block
(B) Default type catch block
(C) The finally block
(D) The throw block
Answer: (B) Default type catch block
Explanation: The default type catch block (or the generic Exception
catch block) handles all types of exceptions that are not specifically caught by other catch blocks.
Memory Tip: Think of the default catch block as the catch-all that handles any unexpected issues that aren’t caught earlier.
Question 42
Question: What is the use of finally block?
Options:
(A) To terminate the program
(B) To clean up at the end after executing try block
(C) To make sure that some specific instructions are to be executed
(D) (B) and (C) both
Answer: (D) (B) and (C) both
Explanation: The finally block is used for cleanup tasks like closing files or releasing resources, and it ensures that these actions are always executed, regardless of whether an exception occurred.
Memory Tip: The finally block is like the last step in the process, always making sure important cleanup happens.
Question 43
Question: Finally block does not care about _.
Options:
(A) whether the exceptions are thrown or not by try block
(B) inside catch block
(C) (A) and (B) both
(D) none of these
Answer: (A) whether the exceptions are thrown or not by try block
Explanation: The finally block is executed whether or not an exception is thrown in the try block. It focuses on performing final operations (like releasing resources), regardless of exceptions.
Memory Tip: Finally is always executed, no matter what happens in the try block. It’s the last cleanup action.
Question 44
Question: _ is incorrect about finally block.
Options:
(A) If it is necessary to close the file after executing the program
(B) It is always at the end of the program
(C) [ ] bracket is used in creating finally block
(D) If any resources are to be released at that time it is widely used
Answer: (C) [ ] bracket is used in creating finally block
Explanation: The finally block is created using curly braces {}
, not square brackets [ ]
.
Memory Tip: The finally block follows the normal syntax of Java blocks: curly braces {}
.
Question 45
Question: What is the location of finally in Java program?
Options:
(A) If at the beginning of program
(B) If catch block is not there then after try block
(C) If there is catch block then after try block and catch block
(D) (A) or (B)
Answer: (D) (A) or (B)
Explanation: The finally block is typically placed after the catch block (if it exists) or directly after the try block if there is no catch block.
Memory Tip: Think of the finally block as a clean-up action that happens after the try and catch blocks, no matter what.
Question 46
Question: If finally or catch are not given at the perfect location then _ can happen.
Options:
(A) program gets compiled
(B) program does not compile
(C) program is terminated immediately
(D) program enters the loop
Answer: (B) program does not compile
Explanation: If the finally block or catch block is not placed at the correct position in the code, the program will not compile due to syntax errors.
Memory Tip: Java is strict about where the catch and finally blocks go—if they’re misplaced, the program won’t compile.
Question 47
Question: What is the use of throw keyword?
Options:
(A) To move necessary statements
(B) To throw the object of an exception
(C) To exit the program
(D) To take control of the program in the beginning
Answer: (B) To throw the object of an exception
Explanation: The throw keyword is used to explicitly throw an exception (an object of the exception class) from the program.
Memory Tip: The throw keyword is like an alarm that signals an issue, throwing the exception for further handling.
Question 48
Question: Arithmetic exception is thrown using _.
Options:
(A) throw keyword
(B) with try block
(C) using JVM
(D) throws statement
Answer: (C) using JVM
Explanation: The ArithmeticException (e.g., dividing by zero) is automatically thrown by the JVM when such an error occurs in the program.
Memory Tip: JVM is responsible for throwing runtime exceptions, like ArithmeticException, automatically when errors happen during execution.
Question 49
Question: The object which is thrown using throws keyword must be of _ type.
Options:
(A) java.lang.throwable
(B) java.lang.string
(C) java.lang.scanner
(D) java.lang.throw
Answer: (A) java.lang.throwable
Explanation: The throws keyword is used to declare that a method can throw an exception. The object thrown must be of type Throwable
or its subclass (like Exception
or Error
).
Memory Tip: Throwable is the parent class for all exceptions and errors in Java, so the object must be of that type.
Question 50
Question: _ is the syntax to throw the exception object.
Options:
(A) Throw. exception_object;
(B) Throw exception_object
(C) Throw exception_object;
(D) Throw exception_object;
Answer: (C) Throw exception_object;
Explanation: The correct syntax for throwing an exception is throw exception_object;
.
Memory Tip: Remember, it’s throw, not throws, and it’s followed by the exception object.
Question 51
Question: The exception object which is thrown using throw keyword can be taken care of using _.
Options:
(A) the try block
(B) the catch block
(C) the finally block
(D) the throw block
Answer: (B) the catch block
Explanation: An exception thrown using the throw keyword is caught and handled by the catch block. The catch block follows the try block and processes the exception.
Memory Tip: Think of the catch block as the safety net that catches exceptions thrown by the throw keyword.
Question 52
Question: If exceptions are generated using method or constructor, then try-catch blocks are defined in _.
Options:
(A) try-catch block is given in method or constructor
(B) invoking the method or constructor inside try block
(C) (A) or (B)
(D) invoking the method or constructor directly in catch block
Answer: (C) (A) or (B)
Explanation: If a method or constructor generates exceptions, the try-catch block can either be placed inside the method/constructor itself or the method/constructor can be invoked inside a try block.
Memory Tip: Exceptions can be handled where they occur (inside method/constructor) or at the point of calling.
Question 53
Question: Instruction inside the constructor or method can throw the exception to mention that _ is used while defining method or constructor.
Options:
(A) the throw statement
(B) the throws clause
(C) the JVM statement
(D) the finally block
Answer: (B) the throws clause
Explanation: The throws clause is used in the method or constructor declaration to indicate that the method or constructor may throw certain exceptions.
Memory Tip: The throws clause is like a warning sign, notifying that the method can throw exceptions that need to be handled.
Question 54
Question: The exception which can be thrown using throw is to be indicated in _.
Options:
(A) in header
(B) in footer
(C) in the middle
(D) at the end of program
Answer: (A) in header
Explanation: The throws clause is typically declared in the method header to specify that the method may throw exceptions.
Memory Tip: Think of the header as the first line of defense, where exceptions are declared.
Question 55
Question: _ class is used to accept input from the keyboard.
Options:
(A) java.util.lang
(B) java.util.library
(C) java.lang.scanner
(D) Java.util.property
Answer: (C) java.lang.scanner
Explanation: The Scanner class, part of the java.util package, is used to accept input from the keyboard in Java.
Memory Tip: The Scanner class is your input tool for getting data from the user.
Question 56
Question: Using _ method, integer values can be accepted using scanner class.
Options:
(A) nextInt()
(B) nextFloat()
(C) nextNumber()
(D) nextperseFloat()
Answer: (A) nextInt()
Explanation: The nextInt() method of the Scanner class is used to read integer values from the user input.
Memory Tip: Think of nextInt() as the method to get the next integer from the user.
Question 57
Question: It is usually understood that a compiled program is error free and will always _ successfully.
Options:
(A) complete
(B) execute
(C) perform
(D) accomplish
Answer: (B) execute
Explanation: A compiled program is expected to execute successfully, though runtime errors may still occur.
Memory Tip: A compiled program is ready to execute, but it may still encounter issues during execution.
Question 58
Question: An _ is an indication of a problem that occurs during a program’s execution.
Options:
(A) exception
(B) inaccuracy
(C) error
(D) e-fault
Answer: (A) exception
Explanation: An exception is an event or indication of a problem that occurs during the execution of a program.
Memory Tip: Think of an exception as a signal that something went wrong during the program’s run.
Question 59
Question: In Java, all kinds of error conditions are called _.
Options:
(A) methods
(B) programs
(C) errors
(D) exceptions
Answer: (D) exceptions
Explanation: In Java, all error conditions are treated as exceptions, which can be caught and handled during the program’s execution.
Memory Tip: In Java, errors are exceptions, and you need to catch and handle them.
Question 60
Question: Errors can be broadly classified into _ categories.
Options:
(A) four
(B) three
(C) two
(D) five
Answer: (C) two
Explanation: Errors in Java can be broadly classified into two categories: checked exceptions and unchecked exceptions.
Memory Tip: There are two broad categories of errors: checked (must be caught or declared) and unchecked (runtime errors).
Question 61
Question: _ is a type of error produced in Java programs.
Options:
(A) Compile-time errors
(B) Run-time errors
(C) (A) and (B) both
(D) None of these
Answer: (C) (A) and (B) both
Explanation: Java programs can produce compile-time errors (errors detected during compilation) and run-time errors (errors that occur during execution).
Memory Tip: There are two main types of errors: compile-time errors (found before running) and run-time errors (found during execution).
Question 62
Question: Examples of some common syntax errors are missing like a _.
Options:
(A) semicolon
(B) use of undeclared variable
(C) wrong spellings of identifier or keyword
(D) all of these
Answer: (D) all of these
Explanation: Common syntax errors include missing semicolons, undeclared variables, and wrong spelling of keywords or identifiers.
Memory Tip: Syntax errors are basic mistakes like missing semicolon, undeclared variables, or spelling mistakes in keywords.
Question 63
Question: The array _ contains the name of four different cities.
Options:
(A) string
(B) int
(C) long
(D) boolean
Answer: (A) string
Explanation: The array contains a list of strings, as it holds names like “Divyakant Sir”, “Tejas Sir”, etc.
Memory Tip: String arrays are used when you need to store words or text values.
Question 64
Question: An attempt to access a non-existing file uses _ exception class.
Options:
(A) ArrayIndexOutOfBoundsException
(B) ArithmeticException
(C) FileNotFoundException
(D) All of these
Answer: (C) FileNotFoundException
Explanation: When a program tries to access a file that doesn’t exist, the FileNotFoundException is thrown.
Memory Tip: If you can’t find the file, you’ll get a FileNotFoundException.
Question 65
Question: An attempt to convert a string to a number type uses _ exception class.
Options:
(A) StringFormatException
(B) ArithmeticException
(C) NumberFormatException
(D) ArrayIndexException
Answer: (C) NumberFormatException
Explanation: When trying to convert a string to a number type (e.g., parsing “abc” as an integer), a NumberFormatException is thrown.
Memory Tip: If the string can’t be converted to a number, it will throw a NumberFormatException.
Question 66
Question: An I/O error has occurred while printing uses _ exception class.
Options:
(A) PrinterIOException
(B) NullPointerException
(C) NumberFormatException
(D) None of these
Answer: (A) PrinterIOException
Explanation: If there’s an I/O error during printing, the exception class used is PrinterIOException.
Memory Tip: PrinterIOException occurs when there’s an I/O error with printing.
Question 67
Question: Any program which tries to access an array element by specifying an index position that is outside the range leads to a _.
Options:
(A) illegal message
(B) exclusion
(C) error
(D) exception
Answer: (D) exception
Explanation: If an array index is out of range, it causes an ArrayIndexOutOfBoundsException, which is an exception.
Memory Tip: If you try to access an index that doesn’t exist, it will throw an exception.
Question 68
Question: The last _ can handle any type of exception.
Options:
(A) try block
(B) catch block
(C) finally block
(D) none of these
Answer: (B) catch block
Explanation: The catch block is where exceptions are caught and handled. You can handle any exception here.
Memory Tip: The catch block is the last resort for handling exceptions.
Question 69
Question: _ try blocks can be nested together, but care must be taken to write a corresponding catch block for each try block.
Options:
(A) Various
(B) Single
(C) Multiple
(D) Some
Answer: (C) Multiple
Explanation: You can nest multiple try blocks, but you must ensure that each try block has a corresponding catch block.
Memory Tip: You can have multiple try blocks, but each needs a corresponding catch block.
Question 70
Question: A _ is used when the programmer wants to be sure that some particular code is to be run, no matter what exceptions are thrown within the associated try block.
Options:
(A) try block
(B) catch block
(C) finally block
(D) all of these
Answer: (C) finally block
Explanation: The finally block ensures that a particular section of code is executed no matter what happens in the try block.
Memory Tip: The finally block is like a guaranteed execution block that runs regardless of exceptions.
Question 71
Question: If there are no _ then the finally block can be positioned immediately after the try block.
Options:
(A) try blocks
(B) catch blocks
(C) finally blocks
(D) none of these
Answer: (B) catch blocks
Explanation: If there’s no catch block, you can place the finally block directly after the try block. However, if a catch block is present, it must come between the try and finally blocks.
Memory Tip: Finally can come right after try, but only without a catch block.
Question 72
Question: Java does provide a mechanism to create a _ object and throw it explicitly.
Options:
(A) illusion
(B) exclusion
(C) error
(D) exception
Answer: (D) exception
Explanation: In Java, you can explicitly create and throw exceptions to handle specific error cases.
Memory Tip: You throw exceptions in Java to indicate something went wrong.
Question 73
Question: The syntax to throw an exception object is _ .
Options:
(A) throw exception_object;
(B) throws exception_object;
(C) throws exception_object
(D) throw object;
Answer: (A) throw exception_object;
Explanation: The throw keyword is used to throw an exception in Java.
Memory Tip: Use throw to throw exceptions.
Question 74
Question: The _ keyword is used with the declaration of a method.
Options:
(A) throwable
(B) throws
(C) throw
(D) throws
Answer: (D) throws
Explanation: The throws keyword is used in a method header to declare that the method might throw exceptions.
Memory Tip: Throws is used to declare that a method can throw exceptions.
Question 75
Question: _ does not provide built-in exception classes for application-specific exceptions.
Options:
(A) C++
(B) C
(C) Java
(D) PHP
Answer: (B) C
Explanation: C does not have built-in exception handling like Java does, but Java provides exception classes for custom exceptions.
Memory Tip: C lacks built-in exceptions; Java has them.
Question 76
Question: A method header can be like _ .
Options:
(A) performDivision throws ArithmeticException, ArrayIndexOutOfBoundsException { // body of the method }
(B) performDivision() throws ArithmeticException, ArrayIndexOutOfBoundsException
(C) performDivision() throws ArithmeticException} // body of the method }
(D) performDivision() throws ArithmeticException, ArrayIndexOutOfBoundsException { // body of the method }
Answer: (D) performDivision() throws ArithmeticException, ArrayIndexOutOfBoundsException { // body of the method }
Explanation: The correct syntax to declare that a method can throw multiple exceptions is throws followed by the exceptions.
Memory Tip: Use throws to declare multiple exceptions in a method header.
Question 77
Question: Which of the following is true about try/catch blocks in Java?
(I) All try/catch blocks must have a finally block.
(II) Barring the Java virtual machine from exiting, the finally block will always be executed.
(III) A try/catch block is limited to two or less catch blocks executed.
Options:
(A) (I) and (III)
(B) (I) only
(C) (III) only
(D) (I) and (II)
Answer: (D) (I) and (II)
Explanation:
- (I) False: A try/catch block does not necessarily need a finally block.
- (II) True: The finally block is always executed unless the JVM exits.
- (III) False: A try/catch block can have multiple catch blocks, not limited to two.
Memory Tip: Finally always executes unless the JVM exits.
Question 78
Question: What will be the output of the program?
“`java
public class Test {
public static void main(String[] args) {
try {
// Some code
} finally {
System.out.println(“Finally”);
}
}
}
**Options**:
(A) Finally
(B) Compilation fails
(C) The code runs with no output
(D) An exception is thrown at runtime
**Answer**: (A) Finally
**Explanation**: The **finally block** is guaranteed to execute, so the program will output "Finally".
**Memory Tip**: **Finally** always runs, regardless of exceptions.
---
### Question 79
**Question**: What is the output of this program?
java
class exception_handling {
public static void main(String args[]) {
try {
int a, b;
a = 5 / b;
b = 0;
System.out.print(” A”);
}
catch (ArithmeticException e) {
System.out.print(“B”);
}
}
}
“`
Options:
(A) A
(B) B
(C) Compilation error
(D) Runtime error
Answer: (B) B
Explanation: The code has a division by zero error, which triggers an ArithmeticException and prints “B”.
Memory Tip: If there’s an ArithmeticException, the catch block will handle it.
Question 80
Question: What happens if an exception is thrown but not caught?
Options:
(A) The program continues execution without any interruption
(B) The program terminates immediately
(C) The exception is passed to the calling method
(D) None of the above
Answer: (C) The exception is passed to the calling method
Explanation: If an exception is thrown but not caught in the current method, it is passed to the calling method for handling.
Memory Tip: Uncaught exceptions are passed up the call stack.
Question 81
Question: Which of the following exceptions is an unchecked exception in Java?
Options:
(A) IOException
(B) SQLException
(C) ArithmeticException
(D) FileNotFoundException
Answer: (C) ArithmeticException
Explanation: ArithmeticException is an unchecked exception because it inherits from RuntimeException.
Memory Tip: Unchecked exceptions are RuntimeExceptions.
Question 82
Question: What is the purpose of the throws
keyword in Java?
Options:
(A) To declare that a method can throw an exception
(B) To throw an exception from a method
(C) To catch an exception
(D) To handle exceptions
Answer: (A) To declare that a method can throw an exception
Explanation: The throws
keyword is used to declare exceptions that a method might throw.
Memory Tip: Use throws to declare exceptions in method headers.
Question 83
Question: Which of the following is a checked exception in Java?
Options:
(A) NullPointerException
(B) ArrayIndexOutOfBoundsException
(C) FileNotFoundException
(D) ArithmeticException
Answer: (C) FileNotFoundException
Explanation: FileNotFoundException is a checked exception because it must be handled or declared.
Memory Tip: Checked exceptions must be handled or declared using throws.
Question 84
Question: Which of the following methods is used to get the details of an exception in Java?
Options:
(A) getDetails()
(B) printStackTrace()
(C) getMessage()
(D) getCause()
Answer: (B) printStackTrace()
Explanation: The printStackTrace() method prints the stack trace, which provides details about the exception.
Memory Tip: Use printStackTrace() for detailed exception information.
Question 85
Question: What is the default behavior if an exception is not caught in Java?
Options:
(A) The program halts immediately
(B) The exception is automatically handled
(C) The exception is printed on the console
(D) The program continues without interruption
Answer: (A) The program halts immediately
Explanation: If an exception is not caught, the program will terminate and print an error message.
Memory Tip: Uncaught exceptions terminate the program.
Question 86
Question: Which of the following exception classes can be used for database-related errors?
Options:
(A) SQLException
(B) ArithmeticException
(C) IOException
(D) FileNotFoundException
Answer: (A) SQLException
Explanation: SQLException is used for errors that occur in database operations.
Memory Tip: Use SQLException for database-related issues.
Question 87
Question: Which of the following exceptions can be thrown by the parseInt()
method in Java?
Options:
(A) ArithmeticException
(B) NullPointerException
(C) NumberFormatException
(D) IOException
Answer: (C) NumberFormatException
Explanation: The parseInt() method throws a NumberFormatException if the string cannot be parsed as an integer.
Memory Tip: parseInt() throws NumberFormatException for invalid strings.
Question 88
Question: How can multiple exceptions be caught in a single catch block?
Options:
(A) By using multiple catch blocks
(B) By using a pipe symbol (|)
(C) By using the throws keyword
(D) By using a try-catch-finally block
Answer: (B) By using a pipe symbol (|)
Explanation: In Java 7 and later, you can catch multiple exceptions in a single catch block using the pipe symbol (|
).
Memory Tip: Use the pipe symbol to catch multiple exceptions in one block.
Question 89
Question: What is the purpose of the finally
block in Java exception handling?
Options:
(A) To handle exceptions
(B) To execute code that needs to run regardless of exceptions
(C) To throw an exception
(D) To log an exception
Answer: (B) To execute code that needs to run regardless of exceptions
Explanation: The finally block is used to ensure that certain code always executes, regardless of whether an exception was thrown or not.
Memory Tip: Finally ensures cleanup code always runs.
Question 90
Question: What is the result of the following code?java public class Test { public static void main(String[] args) { try { int a = 10 / 0; // Division by zero } catch (ArithmeticException e) { System.out.println("Caught an exception"); } finally { System.out.println("Finally block executed"); } } }
Options:
(A) Caught an exception
(B) Finally block executed
(C) Compilation error
(D) Runtime error
Answer: (A) Caught an exception, (B) Finally block executed
Explanation:
- The catch block handles the ArithmeticException and prints “Caught an exception.”
- The finally block will always execute and print “Finally block executed.”
Memory Tip: Catch handles the exception, and finally runs always.
Question 91
Question: Which of the following exceptions is thrown when a program attempts to access an array element using an index that is out of range?
Options:
(A) NullPointerException
(B) ArrayIndexOutOfBoundsException
(C) IndexOutOfBoundsException
(D) ArithmeticException
Answer: (B) ArrayIndexOutOfBoundsException
Explanation: An ArrayIndexOutOfBoundsException is thrown when an invalid index is used to access an element in an array.
Memory Tip: ArrayIndexOutOfBoundsException occurs due to an invalid index.
Question 92
Question: What type of exception is thrown when a program tries to divide a number by zero?
Options:
(A) ArithmeticException
(B) DivideByZeroException
(C) IllegalStateException
(D) NullPointerException
Answer: (A) ArithmeticException
Explanation: An ArithmeticException is thrown when an arithmetic operation, such as division by zero, is performed.
Memory Tip: ArithmeticException is thrown for illegal arithmetic operations.
Question 93
Question: What will be the output of the following code?
public class Test {
public static void main(String[] args) {
try {
String str = null;
str.length();
} catch (NullPointerException e) {
System.out.println("Exception caught");
} finally {
System.out.println("Finally block executed");
}
}
}
Options:
(A) Exception caught
(B) Finally block executed
(C) Compilation error
(D) Exception caught, Finally block executed
Answer: (D) Exception caught, Finally block executed
Explanation:
- NullPointerException occurs when attempting to invoke a method on a null object.
- The exception is caught, and the finally block is always executed.
Memory Tip: Finally always runs, even after an exception is caught.
Question 94
Question: Which of the following is NOT a method of the Throwable class?
Options:
(A) getMessage()
(B) printStackTrace()
(C) getCause()
(D) logException()
Answer: (D) logException()
Explanation: The Throwable class provides methods like getMessage(), printStackTrace(), and getCause(), but logException() is not a valid method.
Memory Tip: Throwable has standard methods for handling exceptions.
Question 95
Question: What is the result of the following code?
public class Test {
public static void main(String[] args) {
try {
throw new Exception("Test Exception");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
Options:
(A) Test Exception
(B) Compilation error
(C) Runtime exception
(D) No output
Answer: (A) Test Exception
Explanation: The exception is thrown using throw and caught in the catch block, printing the exception message.
Memory Tip: Use throw to manually throw an exception in Java.
Question 96
Question: What is the use of the finally
block?
Options:
(A) To catch exceptions
(B) To define methods that will be executed after a method returns
(C) To ensure that specific code runs regardless of exceptions
(D) To declare an exception
Answer: (C) To ensure that specific code runs regardless of exceptions
Explanation: The finally block ensures that code is executed after the try-catch block, no matter what happens.
Memory Tip: Finally block is for cleanup or closing resources.
Question 97
Question: Which of the following is a valid statement about exception handling in Java?
Options:
(A) A method can throw more than one exception using the throws
keyword
(B) The throw
keyword is used to declare exceptions
(C) A method can handle only one exception
(D) The finally
block can be omitted if there are no exceptions
Answer: (A) A method can throw more than one exception using the throws
keyword
Explanation: The throws
keyword allows multiple exceptions to be declared in the method signature, separated by commas.
Memory Tip: Use throws to declare multiple exceptions.
Question 98
Question: Which of the following will throw a FileNotFoundException?
Options:
(A) Opening a file in write mode
(B) Trying to read from a file that doesn’t exist
(C) Writing to a read-only file
(D) Reading from a file that is already opened by another program
Answer: (B) Trying to read from a file that doesn’t exist
Explanation: FileNotFoundException occurs when an attempt is made to open a file that does not exist.
Memory Tip: FileNotFoundException occurs during file opening.
Question 99
Question: What will be the output of the following code?
public class Test {
public static void main(String[] args) {
try {
String str = "Java";
int num = Integer.parseInt(str);
} catch (NumberFormatException e) {
System.out.println("Invalid number format");
}
}
}
Options:
(A) Invalid number format
(B) Compilation error
(C) Runtime exception
(D) No output
Answer: (A) Invalid number format
Explanation: The code attempts to convert a string to an integer, which will cause a NumberFormatException.
Memory Tip: NumberFormatException occurs when parsing an invalid number.
Question 100
Question: What will be the output of the following code?
public class Test {
public static void main(String[] args) {
try {
int a = 10 / 0;
} catch (ArithmeticException e) {
System.out.println("Division by zero");
} finally {
System.out.println("Finally block executed");
}
}
}
Options:
(A) Division by zero
(B) Finally block executed
(C) Division by zero, Finally block executed
(D) ArithmeticException
Answer: (C) Division by zero, Finally block executed
Explanation:
- ArithmeticException occurs due to division by zero, and it is caught in the catch block.
- The finally block is always executed.
Memory Tip: Finally block always executes, even if an exception is caught.