How do you discover that you have an infinite loop in your code?1.
# The code does not compile.
# The code compiles and runs but gives the wrong result.
# The code runs forever.
# The code compiles, but there is a run-time error.
code runs forever
1/18
Term | Definition |
---|---|
How do you discover that you have an infinite loop in your code?1. # The code does not compile. # The code compiles and runs but gives the wrong result. # The code runs forever. # The code compiles, but there is a run-time error. | code runs forever |
What is the output of this code sequence? (The user successively enters 3, 5, and –1.) System.out.print( "Enter an int > " ); int i = scan.nextInt( ); while ( i != -1 ) { System.out.println( "Hello" ); System.out.print( "Enter an int > " ); i = scan.nextInt( ); } | Hello Hello (end) |
6. What is the output of this code sequence? (The user successively enters 3, 5, and –1.) int i = 0; while ( i != -1 ) { System.out.println( "Hello" ); System.out.print( "Enter an int > " ); i = scan.nextInt( ); } | Hello Hello Hello (end) |
What is the output of this code sequence? (The user successively enters 3, 5, and –1.)7. System.out.print( "Enter an int > " ); int i = scan.nextInt( ); while ( i != -1 ) { System.out.print( "Enter an int > " ); i = scan.nextInt( ); System.out.println( "Hello" ); } | Hello Hello (end) |
What are the values of i and sum after this code sequence is executed? int sum = 0; int i = 17; while ( i % 10 != 0 ) { sum += i; i++; } | sum = 54 i = 20 |
What are the values of i and sum after this code sequence is executed? int i = 0; int sum = 0; while ( i != 7 ) { sum += i; i++; } | sum = 21 i = 7 |
This while loop generates random integers between 3 and 7 until a 5 is generated and prints all the random integers, excluding 5. Random random = new Random( ); int i = random.nextInt( 5 ) + 3; | while (i != 5) { System.out.println(i); int i = random.nextInt(5) + 3; } |
This loop takes an integer input from the user, then prompts for additional integers and prints all integers that are greater than or equal to the original input until the user enters 20, which is not printed. System.out.print( "Enter a starting integer > " ); int start = scan.nextInt( ); // your code goes here | int num = 0; while ( != 20) { if (num >= start) System.out.println(num + " is greater than " + start); System.out.print( "Enter a new integer > " ); num = scan.nextInt( ); } |
This loop takes integer values as input from the user and finds the sum of those integers until the user types in the value –1 (which is not added). System.out.print( "Enter an integer value, " + "enter -1 to stop > " ); int value = scan.nextInt( ); // your code goes here | int value = scan.nextInt( ); int total = 0; while (value != -1) { System.out.println("Add: "); total += value; value = scan.nextInt(); } System.out.println("Total: " + total); |
This loop calculates the sum of the first four positive multiples of 7 using a loop (the sum will be equal to 7 + 14 + 21 + 28 = 70). int sum = 0; int countMultiplesOf7 = 0; int count = 1; // your code goes here | while (countMultiplesOf7 != 4) { if (count % 7 == 0) { countMultiplesOf7++; sum += count; } count++; } |
This loop takes words as input from the user and concatenates them until the user types in the word “end” (which is not concatenated). The code then outputs the concatenated String sentence = ""; String word; // your code goes here while ( ! word.equals( "end" ) ) { // and your code goes here } System.out.println( "The sentence is " + sentence ); | System.out.println("Enter a word: "); word = scan.next(); sentence = sentence + " " + word; |
This loop reads integers from a file (already associated with the Scanner object reference file) and computes the sum. We don’t know how many integers are in the file. int sum = 0; // your code goes here | while (file.hasNext()) { num = scan.nextInt(); sum += num; } |
Where is the problem with this code sequence (although this code sequence does compile)? int i = 0; while ( i < 3 ) System.out.println( "Hello" ); | There is no change to the i var, so this will just go forever |
Where is the error in this code sequence that is supposed to read and echo integers until the user enters –1? int num; while ( num != -1 ) { System.out.print( "Enter an integer > " ); num = scan.nextInt( ); System.out.println( num ) } | didn't give num a value before it was looked at by the while's condition |
You coded the following in the class int i = 0; while ( i < 3 ) { System.out.println( "Hello" ); i-- } The code compiles but never terminates. Explain what the problem is and how to fix it. | The i will always be smaller because it is being --, try adding instead |
Write a program that expects a word containing the @ character as an input. If the word does not contain an @ character, then your program should keep prompting the user for a word. When the user types in a word containing an @ character, the program should simply print the word and terminate. | import java.util.Scanner; public class App { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Type a word: "); String word = scan.next(); int local = word.indexOf('@'); while (local > -2) { if (local > -1) { System.out.print(word); local = -3; } else { System.out.print("Type a word: "); word = scan.next(); local = word.indexOf('@'); } } } } |
Write a program that reads double values from a file named input.txt and outputs the average. | import java.io.File; import java.io.IOException; import java.util.Scanner; public class App { public static void main(String[] args) throws IOException { File input = new File("C:\\input.txt"); Scanner scan = new Scanner(input); int total = 0; double sum = 0; while (scan.hasNextDouble()) { sum += scan.nextDouble(); total++; } System.out.print(sum / total); } } |
Discuss how you would detect whether you have an infinite loop in your code. | I would check to see if there is any point where the vars in the condition change, if they can change, then at somepoint things will probably invalidate the while loop, if not, then its going to go forever. |