Interview Question for Software Engineer / Developers


Country: United States




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

- the indexes need to be from 0 to 2, regardless of Java flavor.
- prior to 1.5, Java did not have autoboxing, so you'd need to change ll[1].add(1); to ll[1].add(new Integer(1)); This is because LinkedList can only accommodate objects, and a primitive is not an object, so you need to use a wrapper type. Starting with Java 1.5, the conversion is made automatically.
- Java 1.5 and later may warn about using LinkedList without a generic type parameter (e.g. LinkedList<Object>)

- Anonymous August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

The problem is that while defining linkedlist, we are not enforcing any type. The problem will come while retrieving the elements from the linkedlist. Since, the type of elements are different, ClassCastException will be thrown if we mistakenly do something like.
(int)ll[1].get(1). Because, get(1) stores "red" and we cannot cast it to int.

Java 1.5 gives us a means of enforcing strict types on linkedlist at compile time through generics.

- Chander Shivdasani August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

LinkedList[] ll = new LinkedList[3];
        ll[1] = new LinkedList();
        ll[1].add(new Integer(1));
        ll[1].add(1);
        ll[1].add("red");
        System.out.println(ll[1].get(2));

This code works perfectly fine ... If we do not write the 2nd line ....then Null pointer exception will come. The bound should be between 0 to 2. And autoboxing is also a problem as explained above .....

- abc August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

I will just compile and run it and tell you.

- Anonymous August 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

generic are the new feature that has been added to the JDK 1.5

LinkedList <Object> ll = new LinkedList<Object>();//not an array to use [ ] and not mandatory to specify the size
li.add(1);//no indication of the indices for a Linked list
li.add("red");
li.add("Blue");
li.add(1.2);

- Balu August 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

you can't add primitives to collections classes before 1.5.

- gopalkrishna2 September 07, 2012 | Flag


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