Tuesday, August 30, 2005

Java Questions

This post is a Work-In-Progress and I am going to post here, commonly asked Java Questions and their Answers. I am requesting all members to atleast post their questions, as comments.

2nd Sept. 2005
Questions by Vijay Jadhav
(a) Write Java code to remove the trailing spaces in a string, without using the trim() method.
Answer provided by Pushpendra Bharambe
public class TrimString
{
public static void main(String[] args)
{
String str = "Hello World ";

System.out.println("Length of str = " + str.length());
while (str.endsWith(" "))
{
int k = str.lastIndexOf(" ");
str=str.substring(0, k);
}
System.out.println("Length of new str = " + str.length());
}
}
What if we wanted to remove all spaces from a Java String?
Answer provided by Vishal Dedaniya
public class RemAllSpaces
{
public static void main(String[] args)
{
String s=" ab bc cvnf v ", s1;
int x;
char[] c;
c = s.toCharArray();
for(int i = 0; i < c.length; i++)
{
if(c[i] != ' ')
{
x=0;
System.out.print(c[i]);
}
}
}
}
(b) Write Java code to insert a character at an index, in an array of characters.
Answer awaited.

31st Aug. 2005
Question by Medha Shewale
(a) Why do we need to declare a specific package name for all the beans we intend to use in JSP pages?
Answer to Medha's question above.

Questions by Rahul Kulkarni
(a) Why should we override the equals() method of the Object class?
(b) If we override the doGet() and doPost() method of a Servlet class, which one is called first, by the container?
Answers to Rahul's questions (a) and (b) above.

Update: GeekInterview is an Open Database where you can share interview questions, comment/answer any questions without any registration just by providing name and optional email address.
Technorati Tags:
Blogs linking to this article

0 Comments:

Post a Comment

<< Home