Question

In: Computer Science

How many asterisks does the following code fragment print? a = 0 while a < 100:...

  1. How many asterisks does the following code fragment print?

a = 0

while a < 100:

b = 0

while b<55:

   print('*', end='')

   b += 1

  print()

a += 1

Solutions

Expert Solution

The answer is 110 asterisks.

Here I will attach the given code indentation:

Code:-

a = 0
while a < 100:
b = 0
while b<55:
print('*', end='')
b += 1
print()
a += 1

Output:-

Reason:-

Here we can see that there is a nested while loop. In the first loop the condition satisfies when variable a is lesser then 100 then it enters the second loop if the b value is < 55. So both the conditions are true hence it will be in the inner loop.

So the inner loop executes until b value is <55 then at that time value will also be 55. Then it comes to first while loop there a=55 and it's lesser than 100 so it goes inner way. there b became 0 and executes till b value =55.

So a value will get incremented in the inner loop and it reaches 110 and B variable reaches 55. So now both condition of loop fails and the program will end. So because of two loops, the number of asterisks print is 110 as loop iterates for 110 times and prints the 100 astrisks.

***Any doubt please feel free to comment...Please Upvote...Thank You...***


Related Solutions

Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that...
Consider the following fragment of C code: for (i=0; i<100; i++) { A[i]=B[i]+C; } Assume that A and B are arrays of 64-bit integers, and C and i are 64-bit integers. Assume that all data values and their addresses are kept in memory (at addresses 1000, 3000, 5000, and 7000 for A, B, C, and i, respectively) except when they are operated on. Assume that values in registers are lost between iterations of the loop. Assume all addresses and words...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) {...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) { if(i == 2 || i==4 || i==6) { continue; } cout << "#"; }
What output is produced by the following code fragment? int num = 0, max = 20;...
What output is produced by the following code fragment? int num = 0, max = 20; while (num < max) { System.out.println(num); num += 4; }
How many times should the following C code print "Example" and draw a process graph #include...
How many times should the following C code print "Example" and draw a process graph #include <stdio.h> #include <sys/types.h> #include <unistd.h> void try() { fork(); printf("Example\n"); fork(); return; } int main() { try(); fork(); printf("Example\n"); exit(0); }
Consider the following code segment:    count = 1    while count <= 10:       print(count,...
Consider the following code segment:    count = 1    while count <= 10:       print(count, end=" ") Which of the following describes the error in this code? The loop control variable is not properly initialized. The comparison points the wrong way. The loop is infinite. The loop is off by 1. Does this code contain an error? If so, what line is the error on? 0: ans = input("Yes/No? ") 1: if ans == "Yes": 2: print("Confirmed!") 3: else...
What would be the output of the following? autocorrectisawful = 4 while (autocorrectisawful > 0): print(autocorrectisawful)...
What would be the output of the following? autocorrectisawful = 4 while (autocorrectisawful > 0): print(autocorrectisawful) if (autocorrectisawful == 2): break autocorrectisawful -= 1
Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code. int counter; int x = 5; int y = 6; for (counter =10; counter >0;counter--) IF(X==Y) Y = Y + 1 ; ELSE Y = Y + 2} Fill in the blanks in the following code: MOV__________ ;loop counter into r0-ten times round the loop MOV__________ ;Value of y loaded into r1 MOV__________ ;Value of x...
Read the following code:     x = 1     while(x < 26)         print(x)         x = x + 1...
Read the following code:     x = 1     while(x < 26)         print(x)         x = x + 1 There is an error in the while loop. What should be fixed? Add a colon to the end of the statement Begin the statement with the keyword count Change the parentheses around the test condition to quotation marks Use quotation marks around the relational operator Question 2(Multiple Choice Worth 5 points) (03.03 LC) Python uses __________ to determine whether the condition of a while...
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i...
C++ Visual Studios How many times will "!" print? int i = -5 while(-5 <= i <= 0) { cout << "!"; --i; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT