Core Java Interview Questions On Strings and Exceptions
Q -
Provide a code fragment consisting of a single statement showing how to use the
Integer wrapper class to convert a string containing digits to an integer and
store it in a variable of type int.
A -
See code fragment below
int num = new
Integer("3625").intValue();
Q -
Explain the difference between the capacity() method and the length() methods
of the StringBuffer class.
A -
The capacity() method returns the amount of space currently allocated for the
StringBuffer object. The
length()
method returns the amount of space used.
Q -
The following is a valid code fragment: True or False? If false, explain why.
StringBuffer str6 = new
StringBuffer("StringBuffer named str6".length());
A -
True.
Q -
Which of the following code fragments is the most efficient, first or second?
String str1 = "THIS STRING IS NAMED
str1";
String str1 = new String("THIS STRING IS
NAMED str1");
A -
The first code fragment is the most efficient.
System
Java
provides the System class which provides a platform-dependent interface between
your program and various system resources: True or False? If false, explain
why.
A -
False. Java provides the System class which provides a platform-independent
interface between your program and those resources.
Q -
You must instantiate an object of the System class in order to use it: True or
False? If false, explain why.
A -
False. You don't need to instantiate an object of the System class to use it,
because all of its variables and methods are class variables and methods.
Q -
The following code fragment can be used to instantiate an object of the System
class: True or False? If false, explain why.
System mySystemObject = new System();
A -
False. You cannot instantiate an object of the System class. It is a final
class, and all of its constructors are private.
Q -
What is the purpose of the write() method of the PrintStream class?
A -
The write() method is used to write bytes to the stream. You can use write() to
write data which is not intended to be interpreted as text (such as bit-mapped
graphics data).
Exceptions
Q -
The exception-handling capability of Java makes it possible for you to monitor
for exceptional conditions within your program, and to transfer control to special
exception-handling code which you design. List five keywords that are used for
this purpose.
A -
try, throw, catch, finally, and throws
Q -
All exceptions in Java are thrown by code that you write: True or False? If
false, explain why.
A -
False. There are situations where an exceptional condition automatically
transfers control to special exception-handling code which you write (cases
where you don't provide the code to throw the exception object).
Q -
When an exceptional condition causes an exception to be thrown, that exception
is an object derived, either directly, or indirectly from the class Exception:
True or False? If false, explain why.
A - False. When an exceptional condition
causes an exception to be thrown, that exception is an object derived, either
directly, or indirectly from the class Throwable.
0 comments:
Post a Comment