Question

1.The following statement gets an element from position 4 in an array named myArray: x =...

1.The following statement gets an element from position 4 in an array named myArray:

x = myArray[4];

What is the equivalent operation using an array list named list.?

A x = list.get();
B x = list.get(4);
C x = list.get[4];
D x = list[4];

2.Consider the following code snippet:

ArrayList<Integer> num1 = new ArrayList<Integer>();
int data;
Scanner in = new Scanner(System.in);
for (int i = 0; i < 5; i++)
{
   data = in.nextInt();
   num1.add(data);
   if (data == 0 && num1.size() > 3)
   {
      num1.remove(num1.size() - 1);
   }
}
System.out.println("size is : " + num1.size());

What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input?

A size is : 1
B size is : 0
C size is : 2
D size is : 4

3.What is the output of the following code snippet?

public static int check(ArrayList<Integer> listData)
{
   int sum = 0;
   for (int i = 0; i < listData.size(); i++)
   {
      sum = sum + listData.get(i);
   }
   return sum;
}
public static void main(String[] args)
{
   ArrayList<Integer> vdata = new ArrayList<Integer>();
   int rsum;
   for (int cnt = 0; cnt < 3; cnt++)
   {
      vdata.add(cnt + 1);
   }
   rsum = check(vdata);
   System.out.println(rsum);
}
A 2
B 4
C 3
D 6

4.What is the output of the following code snippet?

ArrayList<Integer> num;
num.add(4);
System.out.println(num.size());
A 4
B Error because num is not initialized
C 0
D 1

5.Consider the following code snippet:

ArrayList<Integer> arrList = new ArrayList<Integer>();
for (int i = 0; i < arrList.size(); i++)
{
    arrList.add(i + 3);
}

What value is stored in the element of the array list at index 0? Think carefully about this.

A 0
B 3
C 6
D None

6. Which statement is true about the code snippet below?

ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Jerry");
ArrayList<String> friends = names;
friends.add("Harry");
A The final size of names is 2; the final size offriends is 3.
B The final size of names is 3; the final size offriends is 2.
C compilation error
D The final size of names is 3; the final size of friends is 3.

7. Which statement is true about the code snippet below?

ArrayList<String> names = new ArrayList<String>();
names.add("John");
names.add("Jerry");
ArrayList<String> friends = new ArrayList<String>(names);
friends.add("Harry");
A The final size of names is 3; the final size offriends is 3.
B The final size of names is 3; the final size offriends is 2.
C The final size of names is 2; the final size of friends is 3.
D compilation error

8. Which statement is true about the code snippet below?

import java.util.*;
public class Test
{
    public static void main(String[] args) 
    {
        Scanner in = new Scanner(System.in);
        ArrayList inputs = new ArrayList();
        int ind = 0;
        System.out.print("Enter some numbers: ");
        while (in.hasNextDouble())
        {
            inputs.set(ind, in.nextDouble());
            ind++;
        }
        
        System.out.println(ind);
    }
}
A The array list is full after 100 numbers are entered.
B The code adds all input numbers to the array list.
C The code has a run-time error.
D The code has compile-time error.

9. Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList?

A:

int max = aList[0]; 
for (int count = 1; count < aList.size();count++)
{
   if (aList.get(count) > max)
   {
      max = aList.get(count);
   }
}
B:
int max = 0;
for (int count = 1; count < aList.size(); count++)
{
   if (aList.get(count) > max)
   {
      max = aList.get(count);
   }
}
C:
int max = aList.get(0);
for (int count = 1; count < aList.size(); count++)
{
   if (aList.get(count) > max)
   {
      max = aList.get(count);
   }
}
D:
if (aList.size() > 0)
{
   int max = aList.get(0);
   for (int count = 1; count < aList.size();count++)
   {
      if (aList.get(count) > max)
      {
         max = aList.get(count);
      }
   }
}

10. What will be printed by the statements below?

ArrayList<String> names = new ArrayList<String>();
names.add("Annie");
names.add("Bob");
names.add("Charles");
names.set(2, "Doug");
names.add(0, "Evelyn");
System.out.print (names);
    
A [Annie, Bob, Charles, Doug, Evelyn]
B [Evelyn, Annie, Doug, Charles]
C [Evelyn, Doug, Charles]
D [Evelyn, Annie, Bob, Doug]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1) x = list.get(4);

2) size is : 4

3) size is : 4

4) D 6

5) B. 3

6) D. The final size of names is 3; the final size of friends is 3.

7) c. The final size of names is 2; the final size of friends is 3.

8)
The code adds all input numbers to the array list.

9) D

10) D [Evelyn, Annie, Bob, Doug]


Thanks, PLEASE UPVOTE if helpful


Add a comment
Know the answer?
Add Answer to:
1.The following statement gets an element from position 4 in an array named myArray: x =...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • We want to create and then initialize an array named A to contain the integers: 4,...

    We want to create and then initialize an array named A to contain the integers: 4, 5, 6, 7. There are multiple ways this can be done. From the list below, select all of the correct ways to create and initialize A. int size = 4; int [] A = new int [size]; A[0] = 4; A[1] = 5; A[2] = 6; A[3] = 7; int [] A = new int [4];       A[0] = 4;       A[1] = 5;...

  • Array lists are objects that, like arrays, provide you the ability to store items sequentially and...

    Array lists are objects that, like arrays, provide you the ability to store items sequentially and recall items by index. Working with array lists involves invoking ArrayList methods, so we will need to develop some basic skills. Let's start with the code below: The main method imports java.utii.ArrayList and creates an ArrayList that can hold strings by using the new command along with the ArrayList default constructor. It also prints out the ArrayList and, when it does, we see that...

  • C# 1.) Assume an integer array named bArray was dimensioned to store 10 rows and 2...

    C# 1.) Assume an integer array named bArray was dimensioned to store 10 rows and 2 columns. Use a variable named element to reference individual cells. Fill in the blanks below for the foreach loop which might be used to display the contents of the array. foreach(Blank 1, Blank 2, Blank 3, Blank 4) . 2.) In order to place a new element in an ArrayList use the ___________ method. 3.) With the following declaration: int [ , ] ctn...

  • Could you please add Round Robin CPU scheduling algorithm with quantum 10 in this code? Scheduling.java...

    Could you please add Round Robin CPU scheduling algorithm with quantum 10 in this code? Scheduling.java import java.util.ArrayList; import java.util.Random; import java.util.Scanner; public class Scheduling { public static void main(String[] args) { ArrayList<Proc> pr = new ArrayList<Proc>(); ArrayList<Proc> pr1 = new ArrayList<Proc>(); ArrayList<Proc> pr2 = new ArrayList<Proc>(); ArrayList<Proc> pr3 = new ArrayList<Proc>();    int count=0; Random ran = new Random(); boolean unique=false; int num=0; for(int i=0;i<=5;i++){ unique=false; Proc p1=new Proc(); while(!unique){ num=ran.nextInt(10) + 1; if(i==0) break; for(int j=0;j<i;j++){ if(num==pr.get(j).id){ unique=false;...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the...

    please edit my java code perferably on jgrasp version 50.0 , wont complie correctly :( the program is supposed to read from my input file and do the following        1) print out the original data in the file without doing anything to it.        2) an ordered list of all students names (last names) alphabetically and the average GPA of all student togther . 3) freshman list in alphabeticalorder by last name with the average GPA of the freshmen...

  • I want to sort my array by pushing the findMax number to the end of the...

    I want to sort my array by pushing the findMax number to the end of the list with this idea The Idea of sort the arraylist: (assume that the arraylist has n elements)Search the n elements of the arraylist for the maximum value then put this maximum value at the last position of the arraylist. Hence at index n-1. Move all other elements forward. Search the first n- 1 elements of the arraylist for the maximum value then put this...

  • using the code above my instructor just want me to delete name from a list of...

    using the code above my instructor just want me to delete name from a list of students name. the function needs to be called delete_name. It's in c++. please no changing the code above just adding. this is pointer and dynamic array. // This is chapter 9 Pointers and Dynamic array #include< iostream> #include<string> using namespace std; //Gv //function declaration string* add_name(string*,int&); void display_names (string*,int); //main int main() //local var int size-3; string *students_names-new string[size]; //code cout<<"Enter "<<size< students names:"<<endl;...

  • Java question Q1) Use the following code snippet which generates a random sized array with random...

    Java question Q1) Use the following code snippet which generates a random sized array with random contents to complete the following problems: public int [] createRandomArray() {         int size = (int) (Math.random() * 10) + 1;         int[] array = new int [size];         for (int i = 0; i < array.length; i++) {             array[i] = (int) (Math.random() * 10 ) + 1;         }         return array;     } Assignment...

  • PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class...

    PLEASE COMPLETE THE CODES. package javaprogram; import java.io.PrintStream; import java.util.ArrayList; import java.util.Scanner; /** * Movie class definition. * * @author David Brown * @version 2019-01-22 */ public class Movie implements Comparable { // Constants public static final int FIRST_YEAR = 1888; public static final String[] GENRES = { "science fiction", "fantasy", "drama", "romance", "comedy", "zombie", "action", "historical", "horror", "war" }; public static final int MAX_RATING = 10; public static final int MIN_RATING = 0; /** * Converts a string of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT