Question

I am in an entry-level Java class and need assistance solving questions 20-25 for a homework assignment. Any help is appreciated. Thank you.

Q20 Read the code and answer the question 6 Points Read each of the following code snippets and determine if they are equival

i++; System.out.print(i); Snippet 2: for(int i=1; i<=9; i++) System.out.print(i); } 0 Yes O No Q20.3 Are these snippets equiv

O Yes O No Q21 Consider the following code 4 Points for(int i=0; i<4; i++) { for (int j=0; j<i; j++) { System.out.print(j); S

String 3 - What; System.out.println(functions (s) ); public static String functions (String in) { String s = Issues?+ in

Q23.2 What is the output of the following program? 5 Points int i = 1; while(i<=1) { mystery(i) System.out.println(); i++; En

Q24.1 What is the output of the previous code? 10 Points The program above prints 5 numbers. List them in the answer box belo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Question 20.1

In the first snippet we are printing numbers from 0 to 9 using a while loop. In the second snippet we are printing numbers from 0 to 9 again but with a for loop. Since the two snippets produce the same output, we can say that these are equivalent.

Question 20.2

In the first snippet, we are printing numbers from 1 to 9 because i is incremented before printing using a while loop. In the second snippet, we are printing numbers from 1 to 9 using a for loop. Since the two snippets produce the same output, we can say that these snippets are equivalent.

Question 20.3

In the first snippet we are printing numbers from 0 to 9 using a while loop. In the second snippet we are printing numbers from 0 to 9 using a for loop. Since the two snippets produce the same output, we can say that these snippets are equivalent.

Question 21.1

In the inner loop we print from 0 to i every time and then after this loop a new line is printed.

i is incrmeented from 0 to 3.

When i = 0, j = 0 but j should be less than i i.e. 0 and 0 is not less than 0, therefore, the inner loop won't execute and simply println().

Therefore, the output is :

0 (i = 1, j = 0; j < 1; j++)

01 (i = 2, j = 0; j < 2; j++)

012 (i = 3, j = 0; j < 3; j++)

Question 22.

When functionB() is called it first prints "Banana" and then calls functionG() which prints "Good". When functionG terminates, functionB() calls functionBr(). functionBr() prints "Bread" and then calls functionW() which stores "What" in s and calls functionS(s). In functionS(s) "Issues? "What" Issues?" is returned and printed in functionW.

Therefore the output of the given code is :

Banana

Good

Bread

Issues? What Issues?

Question 23.1

Mystery function prints "*" count number of times because in the base case it is >= 1 else return.

Question 23.2

In he while loop, mystery(1), mystery(2), mystery(3), mystery(4) is called and as explained above what mystery does, the output will be :

*

**

***

****

Question 24

The number is initialised with 42, first 42 is printed.

Then modifyInt(42) is called which increments the number and prints it i.e. prints 43 but after the function ends the number remains unchanged and we print 42.

Then an array is created with elements 1,11,111 and we print array[0] which is 1.

Then function modifyArray(array,0) is called which increments the value of at index which was 1 and now will be changed to 2 and we print it. Note the value is changed in this case because we incremented at psition i. Therefore, the output of the code is

42

43

42

1

2

Therefore, the 5 numbers printed are :

42 43 42 1 2 in the same order.

Question 25

total is initialised to 0, head is initialised to 0, tail = array.length-1 = 10-1 = 9

while(head < tail) // 0 < 9

Iteration 1 : array[tail] = array[9] = 10

array[head] = array[0] = 1

10-3 is not less than 5

head = 1

tail = 8

total = 0

Iteration 2 : array[tail] = array[8] = 9

array[head] = array[1] = 2

9-2 is not less than 5

head = 2

tail = 7

total = 0

Iteration 3 : array[tail] = array[7] = 8

array[head] = array[2] = 3

8-3 is not less than 5

head = 3

tail = 6

total = 0

Iteration 4 : array[tail] = array[6] = 7

array[head] = array[3] = 4

7-4 is less than 5

total = 0 + 1 = 1

head = 4

tail = 5

Iteration 5 :

array[tail] = array[5] = 6

array[head] = array[4] = 5

6-5 is less than 5

total = 1 + 1 = 2

head = 5

tail = 4

head is no more less than tail

Therefore, the numbers printed are :

0 0 0 0 1 2(The values of total for each iteration)

Please give an upvote if you liked my solution. Thank you :)

Add a comment
Know the answer?
Add Answer to:
I am in an entry-level Java class and need assistance solving questions 20-25 for a homework...
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
  • CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment....

    CS0007 Good day, I am in an entry-level Java class and need assistance completing an assignment. Below is the assignment criteria and the source code from the previous assignment to build from.  Any help would be appreciated, thank you in advance. Source Code from the previous assignment to build from shown below: Here we go! Let's start with me giving you some startup code: import java.io.*; import java.util.*; public class Project1 { // Random number generator. We will be using this...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Please answer both questions. Thanks Question 24 (1 point) What value would be returned if the...

    Please answer both questions. Thanks Question 24 (1 point) What value would be returned if the method mystery were called and passed the array values (1, 15, 37, 12) as its parameter? public static int mystery (int[] list) int x = 0; for (int i = 1; i < list.length; i++) { int y = list[i] - list [0]; if (y > x) x = Y } } return x; 29 35 1 36 Question 25 (1 point) What is...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • (a)How many times does the code snippet given below display "Hello"? int x = 1; while...

    (a)How many times does the code snippet given below display "Hello"? int x = 1; while (x != 15) {    System.out.println ("Hello");    x++; } (b)What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 5) {    sum = sum + i;    i++; } System.out.println("The value of sum is " + sum); Quie 2 What is the output of the following snipped code? public class Test {...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {...

    Draw a flowchart for this program public class InsertionSort {     public static void main(String a[]) {         int[] array = {7, 1, 3, 2, 42, 76, 9};         insertionSort(array);     }     public static int[] insertionSort(int[] input) {         int temp;         for (int i = 1; i < input.length; i++) {             for (int j = i; j > 0; j--) {                 if (input[j] < input[j - 1]) {                     temp = input[j];                     input[j] = input[j - 1];                     input[j - 1] = temp;                 }             }             display(input, i);...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • Help with a question in Java: What is the output from the following program? public class...

    Help with a question in Java: What is the output from the following program? public class Methods2 { public static void main(String[] args) { for (int i = 0; i < 3; i++) { for (int j = 0; j <= i; j++){ System.out.print(fun(i, j) + "\t"); } System.out.println(); } } static long fun(int n, int k) { long p = 1; while (k > 0){ p *= n; } return p; } }

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