Question

In: Computer Science

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
4: print("Not Confirmed!")

line 2

line 3

line 1

No error, this code is fine.

What is the output of the following code:

x = 10
  while(x>0):
      print(x-1)

9,9,9,...(forever)

9,8,7,6,5,4,3,2,1

10,9,8,7,6,5,4,3,2,1,0

9,8,7,6,5,4,3,2,1,0

In Python, how do you know that you have reached the end of the if-statement block?

A comment #

}

The code is less indented than the previous line

;

Solutions

Expert Solution

ANSWER FIRST QUESTION:-

count = 1
while count<=10:
    print(count,end=" ")

So the error in the above code is that it is an infinite loop.

What is an infinite loop?

In simple words, it is a non- terminating loop. So the statements inside the loop will keep on executing endlessly. In the above example, it will keep printing 1 on the output window. This is because the loop will keep on executing until the value of the loop is less than equal to 10. And here count is 1 and there is nothing inside the loop that is changing its value.

ANSWER SECOND QUESTION:-

0: ans = input("Yes/No? ")
1: if ans == "Yes":
2: print("Confirmed!")
3: else
4: print("Not Confirmed!")

In the above code, the error is in the third line. There is a syntax error here. unlike other programming languages like C, C++, and Java the if-else statement ends with a :(colon) in python.

So the correct code will look like this:-

ans=input("Yes/No?")
if ans=="Yes":
    print ("Confirmed")
else: #This is the error that was fixed
    print ("Not confirmed")

ANSWER TO THE THIRD QUESTION:-

x = 10
while(x>0):
    print(x-1)

It will print 9,9,9,9,9,9(Forever). Again, it is a case of an infinite loop. and x is 10 and the statement inside while i.e

print(x-1) will keep on printing 9 forever because the value of x will always remain greater than 0 as we don't have any update statement or expression inside the loop to change its value.

In Python, how do you know that you have reached the end of the if-statement block?

ANSWER- Unlike languages like C, C++, and Java Python works on indentation. So the end of the if-else will be determined by its indentation.

E.g:-

x = 5
if(x==5):
    print "Hi, I am five"
    print "Have a nice day" #end of if
x=x+1
    

So, here the indentation of the if is

print "Hi, I am five"
print "Have a nice day"

That means it will end after these lines.

The line x=x+1 is not a part of if block.

HAPPY LEARNING


Related Solutions

Bash scripting return output: Determine the output this code 1. #!/bin/bash Valid = true count=1 while...
Bash scripting return output: Determine the output this code 1. #!/bin/bash Valid = true count=1 while [ $valid ] do echo $count if [ $count -eq 5 ]; then break fi ((count++)) done 2. #!/bin/bash num=100 for x in $(seq 1 5) do temp=$((num+x)) if [ $temp -1e 103]; then echo $temp else echo "Too Big" fi done 3.#!/bin/bash num=5 for x in $(seq 1 5) do echo $((num*x)) done 4. #!/bin/bash for x in $(seq 1 10) do echo...
Write assembly code for the following machine code. Assume that the segment is placed starting at...
Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
a. Write machine code for the following assembly code. Assume that the segment is placed starting...
a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.
Fill in the blanks of the following segment of code, so that the output would be 1 3 4.int count = 0;do{++ count;if (count == 2)Blank;cout << count << " ";} while (count <= Blank);cout << endl;
Question 1: What does the following code print to the console when the main method is...
Question 1: What does the following code print to the console when the main method is run? public static void referenceMystery(int x, int[] a) { x = 3; a[0] = 4; System.out.println(x+" "+a[0]); } public static void main(String[] args) { int x = 1, y = 2; int[] a = new int[1]; int[] b = new int[1]; referenceMystery(y, b); } 1. x a[0] 2. 1 3. 2 0 4. 3 4 Question 2: What does the following code print out...
21. What is the output of the following segment of code if 7 is input by...
21. What is the output of the following segment of code if 7 is input by the user when asked to enter a number?____________ int num; int total = 0; cout << "Enter a number from 1 to 10: "; cin >> num; switch (num) { case 1: case 2: total = 5; case 3: total = 10; case 7: total = total + 3; case 8: total = total + 6; default: total = total + 4; } cout...
Trace the following code segment 1. use the red numbers under each statement to show the...
Trace the following code segment 1. use the red numbers under each statement to show the order that you execute the code by. For example: (1) means int p=3; (2) means p<10; (3) means p+=3; (1)(2)(3) means you execute int p=3 then p<10 then P+=3; (2)(1)(3) means you execute p<10 then int p =3 then P+=3; 2. show its output for (int p = 3 ; p <10; p += 3 )    (1) (2) (3)          {    for...
Trace the following code segment 1. use the red numbers under each statement to show the...
Trace the following code segment 1. use the red numbers under each statement to show the order that you execute the code by. For example: (1) means int p=3; (2) means p<10; (3) means p+=3; (1)(2)(3) means you execute int p=3 then p<10 then P+=3; (2)(1)(3) means you execute p<10 then int p =3 then P+=3; 2. show its output: int x = 30,  num = 100;     (1)    while ( x > 0) (2)           { cout   <<   endl                          <<  “ x...
Consider the following segment table: Segment Base Length 0 219 600 1 2300 14 2 90...
Consider the following segment table: Segment Base Length 0 219 600 1 2300 14 2 90 100 3 1327 580 4 1952 96 What are the physical addresses for the following logical addresses? 0,430 1,10 2,500 3,400 4,112
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT