Question

In: Computer Science

Assume you call the method below with an array list that contains (2,3,4,5,1)   public static void...

Assume you call the method below with an array list that contains (2,3,4,5,1)  

public static void m3(ArrayList list1)

{

       if (list1.size() ==1)

  System.out.println(list1.get(0));

       else

              if (list1.size()%2==0)

    System.out.println(list1.remove(0));

              else

                  System.out.println(list1.remove(list1.size()-1));  

  

      

}

What will be the output?

Assume you call the method below with an array list that contains (2,3,4,5,1)  

public static void m3(ArrayList list1)

{

       if (list1.size() ==1)

  System.out.println(list1.get(0));

       else

              if (list1.size()%2==0)

    System.out.println(list1.remove(0));

              else

                  System.out.println(list1.remove(list1.size()-1));  

  

      

}

What will be the output?

Solutions

Expert Solution

Answer- 1 will be printed.

Explanation

(i) in this program we have a method m3 that take ArrayList as a argument .   

(ii) in the first if statement we check the size of our ArrayList that

  is 5 initially so if size will be equals to 1 then condition is true but now our case condition is false.

If(5==1)

So condition is false.

(iii) now condition is false so else block will be executed , and in the else block have a another if statement and we checked a condition that is if size of list is modulo by 2 is equals to 0 then our condition will be true  but here out condition it is failed. because modulus is 1.

If(5%2==0)-> it becomes

If(1==0) so condition is false

(iv) so now else block will be executed- in this else block we have a print statement and inside print statement we called remove method through object list1. and inside remove method we called list1.size() method that return size of list.so size of list is 5 and now we minus the size of list by 1

  so (5-1=4).

list.(remove(4));

  remove method remove the value of 4th index so in our case the value of 4th index is 1 so 1 will be our output.

Note:

Remove method is used to remove the element present at the specified position in the list.


Related Solutions

Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};   ...
class Main { public static void main(String[] args) {        int[] array = {1,2,3,4,5};        //Complexity Analysis //Instructions: Print the time complexity of method Q1_3 with respect to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //TODO }    public static void Q1_3(int[] array){ int count = 0; for(int i = 0; i < array.length; i++){ for(int j = i; j < array.length;...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions:...
class Main { public static void main(String[] args) { int[] array = {1,2,3,4,5}; //Complexity Analysis //Instructions: Print to n=Size of input array. For example, if the complexity of the //algorithm is Big O nlogn, add the following code where specified: System.out.println("O(nlogn)"); //code here } public static void (int[] array) { int count = 0; for(int i = 0; i < array.length; i++) { for(int j = i; j < array.length; j++) { for(int k = j; k < array.length; k++)...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its...
Create a method:         Public Static boolean isSubArray489(int[] intArray) This method has an array of integers as its input parameter, we'll say that a SubArray 9,8,7is that three integers, 9,8,7values appearing decreasing order one by one in the array. Return true if the array does contain any SubArray “987” Notice that any 3 integers that presenting a decreasing by one order must return True. (987, 876,765,654, 543, 432,321, 210 are all True) Call this method in your main function, and pass in...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare...
CODE: C# using System; public static class Lab6 { public static void Main() { // declare variables int hrsWrked; double ratePay, taxRate, grossPay, netPay=0; string lastName; // enter the employee's last name Console.Write("Enter the last name of the employee => "); lastName = Console.ReadLine(); // enter (and validate) the number of hours worked (positive number) do { Console.Write("Enter the number of hours worked (> 0) => "); hrsWrked = Convert.ToInt32(Console.ReadLine()); } while (hrsWrked < 0); // enter (and validate) the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT