Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

(TEST CORRECTIONS JAVA PROGRAMMING: WHY IS THIS THE CORRECT ANSWER AND WHAT IS THE CORRECT ANSWER FOR C-E) a) Which of the following correctly gives

(TEST CORRECTIONS JAVA PROGRAMMING: WHY IS THIS THE CORRECT ANSWER AND WHAT IS THE CORRECT ANSWER FOR C-E)

a) Which of the following correctly gives random numbers between -10 and 10 inclusively? Note: Math.random() creates a random number of [0,1).

Selected Answer:

int n = (int)(Math.random() * 10)-20;

Correct Answer:

int n = (int)(Math.random() * 21)-10;

---------------------------------------------

b) How many times is the println statement executed? for (int i = 0; i < 7; i++) for (int j = 0; j < i; j++) System.out.println(i * j);

Selected Answer: 7

Corrected Answer: 21

--------------------------------------------------------------------------

c) What is output? Convert the following loop into a for loop.

int sum = 10;

int i = 2;

while (i <= 6){

sum += 2 * i;

i++;

}

System.out.println(sum);

Your answer format: (no more than 6 lines)

Ouput: ________________

______________________ // Converted code

______________________

______________________

______________________

_______________________

Selected Answer:

for ( int i = 2; i < 7; i++)

for (int sum = 10; sum< i; sum += 2*i)

}

System.out.println(sum);

-----------------------------------------

d)

Write output produced by the following code.

for (int i = 1; i <= 5; i++) { for (int j = 6; j >= i; j--) System.out.print(" * "); System.out.println(" ! "); }

Selected Answer:

! * * * * * *

! * * * * *

! * * * *

! * * *

! * *

*

----------------------------------

e) Identify and correct the errors in the following method to computer the sum of two integers:

int sum (int a, int b); {

int result;

result = x + y;

}

Selected Answer:

int sum ( int a, int b) {

int result;

result = x + y;

}

public int method1( int x ) { if ( x <= 10 ) x += 1; else x -= 1; return x; }

-----------------------------------------------------------------

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students explore these related Databases questions