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
Write a static method named countLastDigits that accepts an array of integers as a parameter and examines its elements to determ
Crazy boy [7]

Answer:

See explaination for the program code

Explanation:

code:

public static int[] countLastDigits(int[] list) {

int[] count = new int[10];

for (int i = 0; i < list.length; i++) {

int digit = list[i] % 10;

count[digit]++;

}

return count;

}

8 0
2 years ago
A signal travels from point A to point B. At point A, the signal power is 100 W. At point B, the power is 90 W. What is the atte
11Alexandr11 [23.1K]

Answer:

Attenuation = 0.458\ db

Explanation:

Given

Power at point A = 100W

Power at point B = 90W

Required

Determine the attenuation in decibels

Attenuation is calculated using the following formula

Attenuation = 10Log_{10}\frac{P_s}{P_d}

Where P_s = Power\ Input and P_d  = Power\ output

P_s = 100W

P_d = 90W

Substitute these values in the given formula

Attenuation = 10Log_{10}\frac{P_s}{P_d}

Attenuation = 10Log_{10}\frac{100}{90}

Attenuation = 10 * 0.04575749056

Attenuation = 0.4575749056

Attenuation = 0.458\ db <em>(Approximated)</em>

7 0
2 years ago
A blood bank maintains two tables - DONOR, with information about people who are willing to donate blood and ACCEPTOR, with info
Kipish [7]

Answer:

The sql query is given below.

Since we need to count of males and females for a particular blood group, we put xxx for the blood group.

SELECT COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "M%") as Male_Donors,

COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "F%") as Female_Donors

FROM DONOR

WHERE BG = xxx;

Explanation:

The clauses in the query are as follows.

1. SELECT: all the columns required in the output are put in this clause.

2. FROM JOIN ON: the table(s) from which the above columns are taken are put in this clause.

3. WHERE: any condition required to filter the output is put in this clause.

The query is explained below.

1. Find the number of male donors. Number of anything can be found using COUNT() function. A query is required since gender is included in deciding the type of donor.

2. The query is defined to find number of male donors as follows.

COUNT( SELECT DID FROM DONOR WHERE GENDER LIKE "M%"; )

3. In the previous query, LIKE operator is used since it is not defined what value is stored for male donors.

4. Similarly, the query to find the number of female donors is formed.

COUNT( SELECT DID FROM DONOR WHERE GENDER LIKE "F%"; )

5. Next, the final query is formed as follows.

SELECT: both COUNT() functions will come here.

FROM: table name

WHERE: specific blood group will be put here

GROUP BY: this clause is optional and is not used in this query.

HAVING: this clause is optional and is not used in this query.

6. The query after putting all clauses is shown below.

SELECT COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "M%"),

COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "F%")

FROM DONOR

WHERE BG = xxx;

7. Alias is used in the above query for each column to get the final query.

SELECT COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "M%") as Male_Donors, COUNT(SELECT DID FROM DONOR WHERE GENDER LIKE "F%") as Female_Donors

FROM DONOR

WHERE BG = xxx;

7 0
2 years ago
This subsystem defines the logical structure of the database by using a data dictionary or schema. multiple choice data manipula
zubka84 [21]
Data manipulation subsystem<span>provides tools for maintaining and analyzing data
</span>Application generation subsystem<span>provides tools to create data entry forms and specialized programming languages that combine with common and widely used programming languages
</span>Data administration subsystem<span>helps supervise the overall database; including maintenance of security, supplying disaster recovery support, and observing the overall performance of database operations</span>
7 0
2 years ago
Read 2 more answers
Recall that TCP can be enhanced with SSL to provide process-to-process security services, including encryption. Does SSL operate
Irina-Kira [14]

Answer:

SSL has operate in Application Layer to encrypt the information from user and  pass it to the TCP. To enhance TCP security developer can integrate the code of SSL with application layer.

Explanation:

SSL certificate is used to enhance the security of user data and encrypt the information of passwords, data transfer and other information to make it secure. This certificate is  installed at application layer to make all the information secure and send it to the Transmission Control Protocol (TCP) to send it further. The TCP can be enhanced more by adding SSL code to the application Layer of the Network.

4 0
2 years ago
Other questions:
  • Which statement regarding dialogues in multiplayer games is true? Dialogues are based only on players’ actions.
    7·2 answers
  • Problem statement: Using loop, write a program that will ask the user to enter a character for left or right. Then, the user wil
    13·1 answer
  • What name are input devices, output devices, and auxiliary storage devices collectively known?
    9·1 answer
  • You have installed a point-to-point connection using wireless bridges and Omni directional antennas between two buildings. The t
    9·1 answer
  • What are the advantages and disadvantages of using a very large memory cell size, say, W = 64 instead of the standard size W = 8
    15·1 answer
  • Write the printItem() method for the base class. Sample output for below program:
    8·1 answer
  • Nicole wants to create a database to collect information about videos in her video rental store. She would like to use the datab
    9·1 answer
  • Mile markers appear as _____ green signs.
    6·2 answers
  • Write a MATLAB function named lin_spaced_vector with two inputs and one return value. The first input will be a single real numb
    7·1 answer
  • 2. BIOS is contained in ROM and not in RAM. Can you guess why?​
    15·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!