answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Kay [80]
1 year ago
7

Consider the following two code segments, which are both intended to determine the longest of the three strings "pea", "pear", a

nd "pearl" that occur in String str. For example, if str has the value "the pear in the bowl", the code segments should both print "pear" and if str has the value "the pea and the pearl", the code segments should both print "pearl". Assume that str contains at least one instance of "pea".
I) if (str.indexOf("pea") >= 0)
{System.out.println("pea");}
else if (str.indexOf("pear") >= 0)
{System.out.println("pear");}
else if (str.indexOf("pearl") >= 0)
{System.out.println("pearl");}

II) if (str.indexOf("pearl") >= 0)
{System.out.println("pearl");}
else if (str.indexOf("pear") >= 0)
{System.out.println("pear");}
else if (str.indexOf("pea") >= 0)
{System.out.println("pea");}

Which of the following best describes the output produced by code segment I and code segment II?

a) Both code segment I and code segment II produce correct output for all values of str.
b) Neither code segment I nor code segment II produce correct output for all values of str.
c) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pear" but not "pearl".
d) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pearl".
e) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".
Computers and Technology
1 answer:
Nikolay [14]1 year ago
3 0

Answer:

e) Code segment II produces correct output for all values of str, but code segment I produces correct output only for values of str that contain "pea" but not "pear".

Explanation:

<em>if - elseif - else statements work in sequence in which they are written. </em>

  • <em> </em>In case <em>if() statement is true, </em>else if() and else statements will not get executed.
  • In case <em>else if() statement is true</em>, conditions in if() and else if() will be checked and else statement will not be executed.
  • In case <em>if() and else if() both are false</em>, else statement will be executed<em>.</em>

First, let us consider code segment I.

In this, first of all "pea" is checked in if() statement which will look for "pea" only in the String str. So, even if "pearl" or "pear" or "pea" is present in 'str' the result will be true and "pea" will get printed always.

After that there are else if() and else statements which will not get executed because if() statement was already true. As a result else if() and else statements will be skipped.

Now, let us consider code segment II.

In this, "pearl" is checked in if() condition, so it will result in desired output.

Executable code is attached hereby.

Correct option is (e).

Download java
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark"> java </span>
<span class="sg-text sg-text--link sg-text--bold sg-text--link-disabled sg-text--blue-dark"> java </span>
You might be interested in
Consider a single-platter disk with the following parameters: rotation speed: 7200 rpm; number of tracks on one side ofplatter:
horsena [70]

Answer:

Given Data:

Rotation Speed = 7200 rpm

No. of tracks on one side of platter = 30000

No. of sectors per track = 600

Seek time for every 100 track traversed = 1 ms

To find:

Average Seek Time.

Average Rotational Latency.

Transfer time for a sector.

Total Average time to satisfy a request.

Explanation:

a) As given, the disk head starts at track 0. At this point the seek time is 0.

Seek time is time to traverse from 0 to 29999 tracks (it makes 30000)

Average Seek Time is the time taken by the head to move from one track to another/2

29999 / 2 = 14999.5 ms

As the seek time is one ms for every hundred tracks traversed.  So the seek time for 29,999 tracks traversed is

14999.5 / 100 = 149.995 ms

b) The rotations per minute are 7200

1 min = 60 sec

7200 / 60 = 120 rotations / sec

Rotational delay is the inverses of this. So

1 / 120 = 0.00833 sec

          = 0.00833 * 100

          = 0.833 ms

So there is  1 rotation is at every 0.833 ms

Average Rotational latency is one half the amount of time taken by disk to make one revolution or complete 1 rotation.

So average rotational latency is: 1 / 2r

8.333 / 2 = 4.165 ms

c) No. of sectors per track = 600

Time for one disk rotation = 0.833 ms

So transfer time for a sector is: one disk revolution time / number of sectors

8.333 / 600 = 0.01388 ms = 13.88 μs

d)  Total average time to satisfy a request is calculated as :

Average seek time + Average rotational latency + Transfer time for a sector

= 149.99 ms + 4.165 ms + 0.01388 ms

= 154.168 ms

4 0
1 year ago
A ____ partition contains the data necessary to restore a hard drive back to its state at the time the computer was purchased an
Oxana [17]
The answer is back up
8 0
1 year ago
Read 2 more answers
3. Megan and her brother Marco have a side business where they shop at flea markets, garage sales, and estate
Neko [114]

Answer:

a

Explanation:

Megan doesn't have a registered business. She can't claim insurance

4 0
1 year ago
A competitive analysis of Google and Microsoft would look at their ________ . Both pay high salaries and offer great benefits. B
Yuki888 [10]

Answer:

personnel strategy

Explanation:

Competitive analysis is a process that involves relating the company to its environment. Competitive analysis helps identify the strengths and weaknesses of the company, as well as the opportunities and threats that affect it within its target market. This analysis is the basis on which the strategy will be designed, for this we must know or intuit as soon as possible:

The nature and success of the probable changes that the competitor may adopt.

The probable response of the competitor to the possible strategic movements that other companies may initiate.

The reaction and adaptation to the possible changes of the environment that may occur from the various competitors.

The competition is made up of companies that operate in the same market and perform the same function within the same group of customers regardless of the technology used for it. It is not, therefore, our competitor that manufactures a generic product like ours, but one that satisfies the same needs as we do with respect to the same target or consumer audience, for example, cinema can be the theme parks competition, since both are embedded in leisure.

4 0
1 year ago
Given a int variable named yesCount and another int variable named noCount and an int variable named response write the necessar
sergejj [24]

// This command takes input from the user.

Scanner input = new Scanner(System.in);

int response= input.nextInt();

// Making the variables required

int noCount =0 ;

int yesCount =0;

// Checking the response if it is 1 or 2 and reporting accordingly

if(response == 1 || response ==2){

 yesCount+=1;

 System.out.println("YES WAS RECORDED");

}

// Checking if the input is 3 or 4 then printing the required lines

else if (response == 3 || response ==4){

noCount+=1;

 System.out.println("NO WAS RECORDED");

}

// if the input is not valid than printing INVALID

else{

System.out.println("INVALID");

}

 

 

 

6 0
2 years ago
Other questions:
  • Which of the following best describes the concept behind Web 2.0
    5·1 answer
  • When seeking information on the internet about a variety of subjects the most useful place to look would be?
    13·2 answers
  • James is an employee at the packaging unit of a chocolate factory that has come up with a new concept of packaging and marketing
    11·2 answers
  • Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation
    6·1 answer
  • A company wants to publish Knowledge articles to its Customer Community. The articles should be organized for easy navigation by
    6·1 answer
  • Assume that machines A and B are on the same network 10.3.2.0/24. Machine A sends out spoofed packets, and Machine B tries to sn
    14·1 answer
  • For a class project, Jerome builds a simple circuit with a battery and three light bulbs. On his way to school, Jerome drops his
    9·1 answer
  • Suggest how the following requirements might be rewritten in a quantitative way. You may use any metrics you like to express the
    7·1 answer
  • How to code 2.9.5: Four colored triangles {Code HS}
    10·1 answer
  • When authenticating a user's password, the password supplied by the user is authenticated by comparing the ____ of the password
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!