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


What will be the output when the following code is


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

What will be the output when the following code is
executed?
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(“Core ”);
stringBuffer.append(“Java”);
System.out.println(“The resultant StringBuffer Value is–
“+stringBuffer);
Answer:
On executing the code above, it will print the following output:
The resultant StringBuffer Value is: Core Java
The StringBuffer.append() method appends the specified
value to the value in the StringBuffer.
What will be the output of the following code snippet?
String xValue = “xyz”;
String yValue =
xValue.concat(“qrs”).toUpperCase().replace(‘Z’, ‘c’);
System.out.println(yValue);


135.
a.
b.
c.
d.
Answer:
The code above prints the following output:
XYcQRS
First, xValue is concatenated with the value qrs and so the
value becomes xyzqrs. Then, the value is converted to
uppercase and so, the value becomes XYZQRS. Then, the
character Z is replaced with c and so, the value becomes
ZYcQRS.
What are the various ways of assigning a string literal to a
String variable?
Answer:
Following are the various ways of assigning a string literal to a
string variable:
String strValue1 = “String Literal 1”;
String strValue2 = new String(“String Literal
2”);
String strValue3 = new String(); strValue3 =
“String Literal 3”;
String strValue4 = strValue3;
Approach a creates the String variable strValue1 by assigning a
String literal to it. Approach b creates the variable strValue2 by
using the String constructor that accepts a String value.
Approach c creates the variable strValue3 using the default
String constructor and later assigns a value to it. Approach d
creates the variable strValue4 by assigning another String
variable to it.



Download 1.52 Mb.

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




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