Page 1 of 1
About Question enthuware.ocajp.i.v8.2.925 :
Posted: Sun Apr 30, 2017 8:09 am
by Aayushma
The question asks which of the following code "compiles without error"?
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();
//INSERT CODE HERE
}
}
Then why option d) Double d = al.get(al.length); is correct since it compiles fine.
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Sun Apr 30, 2017 1:50 pm
by admin
Are you sure it compiles fine? Did you read the explanation given with this option?
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Mon Oct 02, 2017 9:17 am
by JuergGogo
al.add(111); is not a valid option.
The add() is expecting Double, so autoboxing has to take place. Since al.add(111d); is working fine.
I don't understand:
< autoboxing will call new Double(), its constructor accepts double and String. Why al.add("111") doesn't work then?
< why the int 111 is't implicitely casted to double, then autoboxed to Double?
Thank you.
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Mon Oct 02, 2017 11:01 am
by admin
Because the rules of boxing are clearly defined in JLS (
https://docs.oracle.com/javase/specs/jl ... #jls-5.1.7 ) and it states that if you have an int value then it can only be autoboxed into an Integer. Converting an int to double and then boxing that double into a Double is not permitted in a single step.
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Thu Dec 14, 2017 4:01 pm
by Rinkesh
Can you give me an example of how the indexOf Method works?
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Thu Dec 14, 2017 4:03 pm
by Rinkesh
And one more thing a1.contains("string") will print Boolean value false right??
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Thu Dec 14, 2017 9:59 pm
by admin
The code given in the question itself illustrates the use of indexOf. Try adding a few elements in al and then see what it prints for al.indexOf(1.0);
>And one more thing a1.contains("string") will print Boolean value false right?
What happened when you tried it out?
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Fri Dec 15, 2017 9:11 pm
by Rinkesh
Code: Select all
import java.util.*;
public class TestClass {
public static void main(String[] args) throws Exception {
ArrayList<Double> al = new ArrayList<>();
al.add(123.0); al.add(114.0); al.add(138.0); al.add(153.0); al.add(168.0);
System.out.println(al.indexOf(1.0)); //Getting -1 here no matter how many elements I add
System.out.println(al.contains("string")); //This returns false as expected
}
}
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Fri Dec 15, 2017 9:17 pm
by admin
Well, the purpose of
indexOf is to find out if the passed object exists in the list. If so, it returns the index at which it exists otherwise it returns -1.
So why do you expect it to return anything other than -1 if your list doesn't contain 1.0?
Re: About Question enthuware.ocajp.i.v8.2.925 :
Posted: Sun Dec 17, 2017 7:40 am
by Rinkesh
Okay got it.Thanks!