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...
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
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)
In Java, write the method public static void insertUnique(List l, T e), user of the ADT...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is...
Consider a class ‘A’ with a method public static void show( ). A class ‘Derived’ is the subclass of ‘A’. Is it possible to define the same method public static void show( ) in the Derived class. Why give reasons? Is it run time polymorphism? (0.5 Marks) Is it possible to define the method public void show( ) (non static method) in the Derived class. What will happen and why give reasons?
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20,...
public class Lab1 { public static void main(String[] args) { int array [] = {10, 20, 31, 40, 55, 60, 65525}; System.out.println(findPattern(array)); } private static int findPattern(int[] arr) { for (int i = 0; i < arr.length - 2; i++) { int sum = 0; for (int j = i; j < i + 2; j++) { sum += Math.abs(arr[j] - arr[j + 1]); } if (sum == 20) return i; } return -1; } } QUESTION: Modify the given...
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) { }
Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must...
Re-do Problem 2, but now implement the following method: public static void deleteEvenNumbers() This method must delete all even numbers in the chain pointed to by aList, while preserving the original of the remaining numbers. For example, if aList is this list: 3, 9, 2, 10, 5, 6; the list becomes after calling the method as follows: 3, 9, 5. useing netbeans
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT