Question

In: Computer Science

2. Consider these declarations: ArrayList stringList = new ArrayList(); String ch = “ ”; Integer intOb...

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); ____________

Solutions

Expert Solution

/* 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*/

​​​​​​​


Related Solutions

JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");           &
Java: Determine the ouotput of this code ArrayList<String>list=new ArrayList<String>();             list.add("Namath");             list.add("Sauer");             list.add("Maynard");             list.add("Namath");             list.add("Boozer");             list.add("Snell");             list.add("Namath");             list.add("Atkinson");             list.add("Lammonds");             list.add("Dockery");             list.add("Darnold");             list.remove(2);             list.set(2, "Brady");             list.remove(2);             list.set(4,"Unitas");             list.add(1,"Lamomica");             list.add(3,"Hanratty");             list.remove("Namath");             list.remove(list.size()-1);             list.remove(2);             list.set(7, "Riggins");             Iterator iter = list.iterator();           while (iter.hasNext())         {   System.out.print(iter.next() + " ");                         }                     } }
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the...
Method: ArrayList<Integer> diff(ArrayList<Integer> list1, ArrayList<Integer> list2) diff() method accepts two ArrayLists of Integer and returns the union of elements in two lists. For example: list1 contains elements [1, 2, 3, 4, 5] list2 contains elements [3, 4, 5, 6, 7] Diff() method should return an array list with elements [1, 2, 3, 4, 5, 6, 7].
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list =...
import java.util.*; class Main { static ArrayList<String> list; public static List<String> createList(ArrayList<String> arrayList) { list = arrayList; return list; } public static void printList(ArrayList<String> arrayList) { System.out.println("Printing in 4 ways\n"); // 1 System.out.println(arrayList); //2 for(String s:arrayList) System.out.print(s+" "); System.out.println(); //3 System.out.println(Arrays.deepToString(list.toArray())); //4 for(int i=0;i<arrayList.size();i++) System.out.print(arrayList.get(i)+" "); System.out.println(); } public static void filterList(ArrayList<String> arrayList) { System.out.println("Filtered in 2 ways\n"); ArrayList<String> copyArrayList = arrayList; //1 for(int i=0;i<arrayList.size();i++) { if(arrayList.get(i).contains("chuck")) { arrayList.remove(i); i--; } } System.out.println(arrayList); //2 copyArrayList.removeIf(str -> str.contains("chunk")); System.out.println(copyArrayList); }   ...
Consider the following schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string)...
Consider the following schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The key fields are underlined, and the domain of each field is listed after the field name. Therefore sid is the key for Suppliers, pid is the key for Parts, and sid and pid together form the key for Catalog. The Catalog relation lists the prices charged for parts by Suppliers. WRITE THE FOLLOWING QUERIES IN RELATIONAL ALGEBRA...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer)...
Question (3) Consider the following relations: Student(snum: integer, sname: string, major: string, level: string, age: integer) Class(name: string, meets at: string, room: string, fid: integer) Enrolled(snum: integer, cname: string) Faculty(fid: integer, fname: string, deptid: integer) The meaning of these relations is straightforward; for example, Enrolled has one record per student-class pair such that the student is enrolled in the class. Write the following queries in Oracle SQL. No duplicates should be printed in any of the answers. i) (2 points)...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the...
(Java Programming Problem) Build two ArrayList lists from the Integer and String type arrays, populate the lists with repeats (see example below). Write a generic method (removeDuplicates( ….. )) to remove those duplicate items and returns an ArrayList<E> list without duplicates. Original Integer List: [14, 24, 14, 42, 24, 25, 25, 23] No-Duplicate List: [14, 24, 42, 25, 23] Same generic method for the name list Original List: [Mike, Lara, Jenny, Lara, Jared, Jonny, Lindsey, Mike, Jared] No-Duplicate List: [Mike,...
Determine the outputs of the code: String[]array= {"Namath","Sauer","Namath","Namath","Snell","Snell","Namath","Namath","Namath", "Maynard","Namath","Namath"};        ArrayList<String>list=new ArrayList<String>(); &nbs
Determine the outputs of the code: String[]array= {"Namath","Sauer","Namath","Namath","Snell","Snell","Namath","Namath","Namath", "Maynard","Namath","Namath"};        ArrayList<String>list=new ArrayList<String>();        for(int i=0;i<array.length;i++)        {            list.add(array[i]);        }        System.out.println("OUTPUT 1");        System.out.println(list);        System.out.println("OUTPUT 2");        for(int i=0;i<list.size();i++)        {            if(list.get(i).equals("Namath"))            {                System.out.println(i+"\t"+ list.remove(i));            }        }        System.out.println("OUTPUT 3");        for(String s:list)        {           ...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname:...
INRO TO DATABASES Consider the following Schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL using join, nested queries and set operators. 1. Find names of suppliers who supply every red or green part. 2. Find the sids of suppliers who supply every red part or supply every green part. 3. Find sids...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT