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


Write a code sample that reverses a String without using a


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

Write a code sample that reverses a String without using a
loop.
Answer:
There are several ways that you can reverse a String without
using a loop. One of the ways is to use the reverse method on
the StringBuilder class. The following code demonstrates this:
StringBuilder sBuilder = new StringBuilder(“Core Java”);
sBuilder.reverse();
System.out.println(sBuilder);
This code creates a StringBuilder instance with the String Core
Java and then invokes the reverse() method on it.
So, this code prints the following output:
avaJ eroC
The StringBuffer class also has a method called reverse()
which can be used to reverse a String.
Write a code sample that demonstrates how you can
convert a String to uppercase.
Answer:


140.
a.
b.
c.
The String class has a method called toUpperCase(). This can
be used to convert a String to uppercase. The following code
demonstrates this:
String myStr = “Hello World”;
myStr = myStr.toUpperCase();
System.out.println(myStr);
This code creates a String called myStr with the value Hello
World. It invokes the toUpperCase() method on this String
object. So, this code prints the following output:
HELLO WORLD
Explain how you can convert an Integer to a String.
Answer:
There are several ways in which you can convert an Integer to a
String. These are as follows:
Directly enclosing the Integer in quotes: You can enclose the
number in quotes and assign to a String variable as follows:
String strNum = “10”;
Using String.valueOf: The String class has a valueOf
method. You can use this to convert an Integer to a String as
shown below:
String strNum = String.valueOf(10);
Using the Integer.toString method: The Integer class
has a toString method. This can also be used to convert
an Integer to a String as shown below:


String numStr2 = new Integer(10).toString();




Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   50   51   52   53   54   55   56   57   ...   171




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