In: Computer Science
2. Consider these declarations: ArrayList stringList = new ArrayList(); String ch = “ ”; Integer intOb = new Integer(5);
For the following statements, indicate VALID or INVALID (invalid if the statement causes an error)
A. strList.add(ch); _______________
B. strList.add(new String(“handy andy”); ______________
C. strList.add(intOb.toString()); ____________
D. strList.add(ch + 8); _________________
E. strList.add(intOb + 8); ____________
/* Please consider all the comments for the reason and explaination */
ArrayList strList = new ArrayList(); //This is object creation of ArrayList of java collection,
//Since there is not type declaration of type in arraylist, this arraylist object is capable to keep all type of data
// Although warning will be given as 'unchecked or unsafe operations.'
String ch = “ ”; // This is string assignment
Integer intOb = new Integer(5); // This is an integer object creation
// So for below, this is result,
//Please note that, ArrayList object name is stringList,
// So either rename above stringList as strList or all below strList should be renamed as stringList
// I have renamed above object as strList
// Since the current ArrayList object 'strList' has no specific defined type,
// So it can contain all type of data or object
// Hence all below are valid and will run without error and hence
A. strList.add(ch); -->Valid
B. strList.add(new String(“handy andy”); -->Valid
C. strList.add(intOb.toString()); -->Valid
D. strList.add(ch + 8); -->Valid
E. strList.add(intOb + 8); -->Valid
/* That's all*/