Interview Question


Country: Bangladesh
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
5
of 5 vote

Strings in Java are immutable objects. This means that they can't be modified once created, and so the only way for their contents to be removed from memory is when they are garbage collected, at which point the memory once used for them will be freed and can eventually be overwritten with other data.

The problem with garbage collection is that it doesn't happen at any kind of guaranteed interval. The strings may persist in memory for a long time, and if a process crashes during this time, the contents of the string may end up in a memory dump or the like. With a character array, you can read the password, finish working with it as soon as you can, and then immediately zero out the array, for example via Arrays.fill (passwordArray, (char)0).

Even this isn't fully secure, because it's just reducing the window of opportunity for someone's password to show up in a memory dump somewhere, not eliminating the core problem. You should try to always minimize the need to store any passwords at all and try to store cryptographically strong hashes of passwords instead when possible.

- eugene.yarovoi November 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Because Strings r stored in Constant pool even when the object dies..so any hacker can exploit this vulnerability..

- JavaRanch November 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Strings in Java are immutable objects. This means that they can't be modified once created, and so the only way for their contents to be removed from memory is when they are garbage collected, at which point the memory once used for them will be freed and can eventually be overwritten with other data.

The problem with garbage collection is that it doesn't happen at any kind of guaranteed interval. The strings may persist in memory for a long time, and if a process crashes during this time, the contents of the string may end up in a memory dump or the like. With a character array, you can read the password, finish working with it as soon as you can, and then immediately zero out the array, for example via Arrays.fill (passwordArray, (char)0).

Even this isn't fully secure, because it's just reducing the window of opportunity for someone's password to show up in a memory dump somewhere, not eliminating the core problem. You should try to always minimize the need to store any passwords at all and try to store cryptographically strong hashes of passwords instead when possible.

- Anonymous April 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Java encodes String with certain charset. It's ok to keep the encoded password String on the same machine. But if that password String is going to be sent to another machine through network or to be used by other programs, there comes the encoding problem. The default charset is determined during virtual-machine startup and typically depends upon the locale and charset of the underlying operating system.

- USTChucat November 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

class Abc
{
public static void main(String arr[])
{
}
}

- Anonymous February 24, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More