1answer.
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
RSB [31]
3 years ago
7

What will be the values of ans, x, and y after the following statements are executed? int ans = 35, x = 50, y =50; if ( x >=

y) { ans = x + 10; x -=y; } else { ans = y + 10; y += x; } A. ans = 60, x = 50, y =100 B. ans = 60, x =0, y =50 C. ans = 45, x = 50, y = 0 D. ans = 45, x = 50, y = 50
Computers and Technology
2 answers:
Nutka1998 [239]3 years ago
6 0

Answer:

B. ans = 60; x = 0; y = 50

Explanation:

<em>(i)First format the code properly as follows:</em>

int ans = 35, x = 50, y =50;

if ( x >= y) {

ans = x + 10;

x -=y;

} else {

ans = y + 10;

y += x;

}

<em>(ii) Taking the if statement of the code;</em>

The if statement block is executed if x is greater than or equal to y (x >= y).

Since x=50 and y=50, then the code continues to execute the statement in the block.

In the if block;

(a) the first line is

<em>ans = x + 10. </em>

Since x = 50, substituting for x gives

ans = 50 + 10 = 60

Therefore <em>ans = 60</em>

(b) the second line is

<em>x -= y</em>

This can be re-written as

x = x - y.

This means that the value of x - y is written back into x. Therefore since x = 50 and y = 50, substituting for x and y gives

x = 50 - 50 = 0

Therefore <em>x = 0</em>

(c) the value of y is not changed at all, therefore <em>y = 50</em>

<em />

<em>(iii) Taking the else statement of the code;</em>

Since the if statement of the code has been executed, the else statement is simply skipped.

Therefore the final values of ans, x, y are respectively 60, 0 and 50

Hatshy [7]3 years ago
4 0

Answer:

B. ans = 60, x = 0, y = 50

Step wise Explanation of the code chunk

  • The first statement assigns value 35 to ans variable, value of 50 to x variable and value of 50 to y variable.
  • Next is an if statement which checks if the value of x variable is greater than or equal to y
  • This condition is true as x = 50 and y = 50 So x = y
  • Now since the statement is true it will enter the if block of the code which is the instruction: ans = x + 10; x -=y; and ignores the else block.
  • This line changes the value of ans variable and assigns it the value obtained by adding 10 to the value of variable x.
  • we know that         x = 50

        So

                                      ans = x + 10

                                      ans = 50 + 10

                                          ans = 60

  • After this, comes another line x -=y; which means decrement the value of x by value of y

that means:

                                              x = x - y

                                              x = 50-50

                                                  x = 0

  • So the new value of ans is 60, new value of x is 0 and the value of y remains the same i.e. 50

Hence

                                                ans = 60

                                                    x  =  0

                                                    y  = 50

                                     

You might be interested in
You want to deploy a software package that's available to all users in the domain if they want to use it, but you don't want the
gulaghasi [49]

Answer:

Access installation through shared drive

Explanation:

4 0
3 years ago
What is computer software?​
Lady_Fox [76]

Answer:

Software is a collection of instructions that tell a computer how to work. This is in contrast to hardware, from which the system is built and actually performs the work.

3 0
2 years ago
Read 2 more answers
Which of the following best describes contextual design?
skelet666 [1.2K]

Answer:

B

Explanation:

3 0
2 years ago
Where ....................... the books that you borrowed? *
zzz [600]

Where ARE the books that you borrowed?

3 0
3 years ago
50 points please!!
MA_775_DIABLO [31]

Answer:

poopypoopypoopypoopy

6 0
3 years ago
Other questions:
  • Which is the last step in conducting a url research
    11·1 answer
  • Semiconductor memory is used mainly for primary storage even with its high cost. In another hand, the magnetic tape is the cheap
    6·1 answer
  • You're trying to improve an ad's perceived quality so it performs better during an ad auction. What change would have the least-
    15·1 answer
  • Consider the following code segment: ArrayList bulbs = new ArrayList(); bulbs.add(new Light()); bulbs.remove(0); bulbs.add(new L
    15·2 answers
  • To implement a small database, a database designer must know the "1" and the "M" sides of each relationship and whether the rela
    7·1 answer
  • Who found the first computer bug in 1947, and invented the concept of a compiler in 1952
    6·1 answer
  • Describe all the steps a router goes through while deciding where to send the a packet. Explain why a router would choose one ro
    7·1 answer
  • Join each of the follwing sentences using unless
    14·2 answers
  • c) If you are at foreign country visit, which banking card would you prefer to keep with you during visit (Debit Card or Credit
    10·1 answer
  • Documental acerca de los principales materiales que se emplean en la fabricación de medios técnicos utilizados en una oficina.
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!