Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet51/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   47   48   49   50   51   52   53   54   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

What is a RuntimeException?


Answer:
The java.lang.RuntimeException is a sub–class of the
java.lang.Exception class. The RuntimeException and
its sub–classes are also known as unchecked exceptions. This is
because the compiler does not force you to handle such
exceptions. So even if code that might potentially throw an
unchecked exception is written, the compiler does not cause an
error. Some examples of RuntimeExceptions are
ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException.



131.
132.
CHAPTER
12
String Handling
What is a string literal? Give an example.
Answer:
A string literal is a set of characters enclosed in double quotes. It
can be assigned to a String variable with the help of the
assignment operator (=). The following line of code shows that
the string literal “I’m a literal” is assigned to the string variable
called ‘name’.
String name = “I’m a literal”;
What will be the output of the following code? Explain
why.
class StringDemo {
public static void main (String strArgs[]) {
String strVar = “Welcome”;
strVar.concat(“ to Java”);
System.out.println(strVar);
}
}
Answer:
This code prints the following output:
Welcome


133.
134.
Java strings are immutable objects so unless explicitly assigned,
the string’s value does not change. The method concat ()
simply appends the value to Java to strVar but does not assign
the appended String back to the variable strVar.
In order for the String Welcome to Java to be printed, the code
needs to be modified as follows:
strVar = strVar.concat(“ to Java”);

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   47   48   49   50   51   52   53   54   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling