Q: Variables used in a switch statement can be used with which datatypes?
A: Variables used in a switch statement can only be a byte, short, int, or char.
Q: When parseInt() method can be used?
A: This method is used to get the primitive data type of a certain String.
Q: Why is String class considered immutable?
A: The String class is immutable, so that once it is created a
String object cannot be changed. Since String is immutable it can
safely be shared between many threads ,which is considered very
important for multithreaded programming.
Q: Why is StringBuffer called mutable?
A: The String class is considered as immutable, so that once
it is created a String object cannot be changed. If there is a necessity
to make alot of modifications to Strings of characters then
StringBuffer should be used.
Q: What is the difference between StringBuffer and StringBuilder class?
A: Use StringBuilder whenever possible because it is faster
than StringBuffer. But, if thread safety is necessary then use
StringBuffer objects.
Q: Which package is used for pattern matching with regular expressions?
A: java.util.regex package is used for this purpose.
Q: java.util.regex consists of which classes?
A: java.util.regex consists of three classes: Pattern class, Matcher class and PatternSyntaxException class.
Q: What is finalize() method?
A: It is possible to define a method that will be called just
before an object’s final destruction by the garbage collector. This
method is called finalize( ), and it can be used to ensure that an
object terminates cleanly.
Q: What is an Exception?
A: An exception is a problem that arises during the execution
of a program. Exceptions are caught by handlers positioned along the
thread’s method invocation stack.
Q: What do you mean by Checked Exceptions?
A: It is an exception that is typically a user error or a
problem that cannot be foreseen by the programmer. For example, if a
file is to be opened, but the file cannot be found, an exception occurs.
These exceptions cannot simply be ignored at the time of compilation.
Q: Explain Runtime Exceptions?
A: It is an exception that occurs that probably could have
been avoided by the programmer. As opposed to checked exceptions,
runtime exceptions are ignored at the time of compliation.
Q: Which are the two subclasses under Exception class?
A: The Exception class has two main subclasses : IOException class and RuntimeException Class.
Q: When throws keyword is used?
A: If a method does not handle a checked exception, the method
must declare it using the throwskeyword. The throws keyword appears at
the end of a method’s signature.
Q: When throw keyword is used?
A: An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.
Q: How finally used under Exception Handling?
A: The finally keyword is used to create a block of code that
follows a try block. A finally block of code always executes, whether or
not an exception has occurred.
Q: What things should be kept in mind while creating your own exceptions in Java?
A: While creating your own exception:
- All exceptions must be a child of Throwable.
- If you want to write a checked exception that is automatically enforced by the Handle or Declare Rule, you need to extend the Exception class.
- You want to write a runtime exception, you need to extend the RuntimeException class.
For more Visit: http://www.quontrasolutions.com/blog/category/java
No comments:
Post a Comment