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] |
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
1.The following statement gets an element from position 4 in an array named myArray: x =...
Please, explain clearly each line of code with your answers. Question 1 Consider the following code snippet: int ctr = 0; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr; ctr = ctr + i; } cout << myarray[2]; What is the output of the code snippet? Question 2 Consider the following code snippet: int cnt = 0; int numarray[2][3]; for (int i = 0; i < 3; i++) { for (int j =...
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 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 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 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;...
PLease! Need help in java!!!!!! I'm trying to put int x[]= {3, 6, 4, 1 } into an ArrayList,sort in ascending order and return it. public ArrayList<Integer> lookOut( int x[]) { ArrayList<Integer> list = new ArrayList<Integer>(); for(int i =0; i < x.length; i++){ list.add(x[i]); Collections.sort(list); } return list; } My test case: public void lookOut() { int x[] = {...
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 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 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...
Please help me finish my code. Before the final pause in the main function, create an ArrayList of another type such as double, char, short, bool, float, etc. Your choice. Add five data items to your array as we did for the int and string array lists. Print them out with a for loop as we did before. Print out the count and capacity of your new array list. Source.cpp #include #include #include #include "ArrayList.h" using namespace std; /// Entry...