ITP Software

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Execute this body 5 times: { print "Hello World" } [NO OUTPUT] Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

Hello World Hello World Hello World Hello World Hello World

What is the output of this program? constant boolean _TRUE_, no _TRUE_ = False no = _TRUE_ print no "yes" "no" True False Undefined, variable was not initialized.

False

What would these print? print !True print "!True" print "yes" print yes print "y" != "Y"

False !True yes ERROR, name wasn't declared. True

Execute this body 1 time: { print "Hello World" } [NO OUTPUT] Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

Hello World

Execute the body 100,842 times: boolean b = false; { b = !b // Repeat 100842 times. } print b [NO OUTPUT] False True maybe

False

What is the value of the loop counter when this counter algorithm STOPS. int i = 10; i > 9; i--; (start the loop counter at 10, hop by -1 while counter is greater than 9) -1 0 9 10 12

9

For this program, choose the best initial value that should be assigned to the word variable. (Fill in that blank!) String word = ______ print "Please spell a 3 letter word!" for(int i = 0;i < 3; i++) { char letter = input(); word = word + letter; } print "Your word is: " + word "" // Also known as the empty String. "Your word is: " "Your word is: Cat" NULL

"" // Also known as the empty String.

What is the output of this program? String no = "input()" print no True False "no" "input()" Undefined. I don't know for certain what the input value will be.

"input()"

What is the output of this program? String yes yes = "no" print yes "yes" "no" True False Undefined, variable was not initialized.

"no"

What is the output of this program? String yes yes = "no" print "yes" "yes" "no" True False Undefined, variable was not initialized.

"yes"

for(String s = ""; s != "aaaaa"; s = s + "a") { print "(" + s + ")" if(s == "aa") { s = s + "a" } } // Note: s = s + "a" is executed here. <No output> <Empty String> a aa aaa aaaa () (a) (aa) (aaaa) () (a) (aa) (aaa) (aaaa) (aaaaa)

() (a) (aa) (aaaa)

Execute the body 4 times: boolean a = true; boolean b = true; { print "(" + a + ", " + b + ")" b = !b if(b) { a = !a } } [NO OUTPUT] (true, true) (true, false) (false, true) (false, false) (true, true) (false, true) (true, false) (false, false) (true, true) (true, false) (false, true) (false, false) (true, true)

(true, true) (true, false) (false, true) (false, false)

What is the value of the loop counter when this counter algorithm STOPS. int i = 10; i >= 0; i--; (start the loop counter at 10, hop by -1 while counter is greater than or equal to 0) -1 0 9 10 12

-1

What is the value of the loop counter when this counter algorithm STOPS. int i = 0; i > 9; i = i + 3 (start the loop counter at 0, hop by +3 while counter is greater than 9) -1 0 9 10 12

0

What is the value of the loop counter when this counter algorithm STOPS. int i = 10; i > 0; i--; (start the loop counter at 10, hop by -1 while counter is greater than 0) -1 0 9 10 12

0

Execute the body 1,000,000,000 times: (Or until the user wouldn't notice a difference...) float limit = 3.14 { limit = limit / 2.0 } print limit 3.14 1.57 .785 .3925 0.00000000000000...

0.00000000000000...

For this program, choose the best initial value that should be assigned to the counter variable. (Fill in that blank!) int counter = ______ print "List of all 2 digit positive integers: " while(counter <= 99) { print counter; counter++; } 0 2 10 MIN_INTEGER MAX_INTEGER

10

How many iterations occur until this counter algorithm STOPS int i = 9; i >= 0; i--; (start the loop counter at 9, hop by -1 while counter is greater than or equal to 0) -Here are some other ways of phrasing this question: How many times is the loop guard evaluated to true? How many 'hops' occur, i.e. how many times is the i-- statement executed? See the example on the section panel to the left for an example of a counter algorithm that has 5 iterations. -1 0 9 10 12

10

How many times is the loop body executed in the following code int i = 1; while(i < 11) { i++; // This statement is run exactly // once each time the body is run, // since it is inside of the body. } 11 10 9 5

10

In question 1 above, what is the value of i (the loop counter) at the end of the loop (when the loop guard evaluated to false?) You could also think of this as the value for i you would put on an end of execution memory diagram for this program. 0 1 5 6 9 10 11 12

11

Execute the body 9,000,000 times: int counter = 0 { counter = counter + 2 if(counter > 13) { counter = counter - 2 } } print counter 12 13 14 18000000

12

What is the value of the loop counter when this counter algorithm STOPS. int i = 0; i < 10; i = i + 3 (start the loop counter at 0, hop by +3 while counter is less than 10) -1 0 9 10 12

12

How many iterations occur until this counter algorithm STOPS? int i = 0; i < 10; i = i + 3 (start the loop counter at 0, hop by +3 while counter is less than 10) Here are some other ways of phrasing this question: How many times is the loop guard evaluated to true? How many 'hops' occur, i.e. how many times is the i-- statement executed? 1 2 3 4 9 10 12

4

Execute the body 56 times: int counter = 1 { counter++ } print counter 1 56 57 113

57

What is the value of the loop counter when this counter algorithm STOPS. int i = 0; i < 9; i = i + 3 (start the loop counter at 0, hop by +3 while counter is less than 9) -1 0 9 10 12

9

What is the value of the loop counter when this counter algorithm STOPS. int i = 10; i > 9; i = i + 3 (start the loop counter at 10, hop by +3 while counter is greater than 9) -1 0 9 10 12 A very large negative number close to the minimum integer value. It is achieved due to integer overflow and surprisingly stops the computation, because its value is less than 9. (It takes many iterations to get there, but eventually this is what ultimately might happen. If it didn't then this loop never stops and gets stuck on the MAXIMUM integer forever!)

A very large negative number close to the minimum integer value. It is achieved due to integer overflow and surprisingly stops the computation, because its value is less than 9. (It takes many iterations to get there, but eventually this is what ultimately might happen. If it didn't then this loop never stops and gets stuck on the MAXIMUM integer forever!)

Please select the counter algorithm that has the fewest iterations. int x = 0; x <= 9; x++ int i = 0; i < 10; i++ int i = 1; i < 11; i++ int i = 1010; i < 1020; i++ int i = 9; i >= 0; i-- int i = 9; i > -1; i-- Actually, they all iterate 10 times and work equally well for doing something 10 times as long as the actual values of the loop counter don't matter. For the sake of other programmers though, it would be best to use the one that seems the most readable to you.

Actually, they all iterate 10 times and work equally well for doing something 10 times as long as the actual values of the loop counter don't matter. For the sake of other programmers though, it would be best to use the one that seems the most readable to you.

Every variable needs to be assigned an initial value before it is used in expressions on the right side of assignment statements. The same is true for loop counters and iterative result variables. Please check EACH and EVERY fact that is true about these values. All iterative algorithms compute a solution. This solution often is a value stored in a variable that is improved each iteration. At the beginning of the algorithm that variable needs to be assigned an initial value, also known as 'the first guess' or the 'empty value' if it wants to initialize the variable while abstaining as much as possible from guessing wrong in a way that can't be improved. If an iterative algorithm is computing an integer value, then 0 will always be an appropriate initial value for its variable. The empty value for a process using the OR operator is false, because (false OR x) is logically equivalent to (x). false doesn't affect the result. An algorithm that processes a list of input data can initialize its result variable to the first input if that is a reasonable initial guess or the answer to the problem if the input sequence was only of length 1. Programmers who use this approach often do a bit loop gymnastics either duplicating the code body for the initial value, using a do - while loop, rather than a while loop, starting their counting algorithms at 1, rather than 0, etc. Essentially, they may need to write a little bit more code than if they used the empty value approach. Empty Value Example: int sum = 0; int n = input(); for(int i = 0; i < n; i++) { sum = sum + input(); } print sum; // prints 0 when n is 0 or less. Guess the 1st Value Example int n = input(); int sum = input(); // Hopefully N >= 1 ... // Otherwise this CRASHES. for(int i = 1; i < n; i++) { sum = sum + input(); } print sum;

All iterative algorithms compute a solution. This solution often is a value stored in a variable that is improved each iteration. At the beginning of the algorithm that variable needs to be assigned an initial value, also known as 'the first guess' or the 'empty value' if it wants to initialize the variable while abstaining as much as possible from guessing wrong in a way that can't be improved. The empty value for a process using the OR operator is false, because (false OR x) is logically equivalent to (x). false doesn't affect the result. An algorithm that processes a list of input data can initialize its result variable to the first input if that is a reasonable initial guess or the answer to the problem if the input sequence was only of length 1. Programmers who use this approach often do a bit loop gymnastics either duplicating the code body for the initial value, using a do - while loop, rather than a while loop, starting their counting algorithms at 1, rather than 0, etc. Essentially, they may need to write a little bit more code than if they used the empty value approach. Empty Value Example: int sum = 0; int n = input(); for(int i = 0; i < n; i++) { sum = sum + input(); } print sum; // prints 0 when n is 0 or less. Guess the 1st Value Example int n = input(); int sum = input(); // Hopefully N >= 1 ... // Otherwise this CRASHES. for(int i = 1; i < n; i++) { sum = sum + input(); } print sum;

What is the output of this program? boolean no = input() no = False AND no print no True False "no" Undefined. I don't know for certain what the input value will be.

False

What is the output of this program? boolean yes boolean no yes = True no = !False print !yes != !no "yes" "no" True False Undefined, variable was not initialized.

False

What is the output of this program? constant boolean _FALSE_ _FALSE_ = True print False == True Note: Underscore '_' is a legal character for use in names. "yes" "no" True False Undefined, variable was not initialized.

False

Please match each term with its best definition from available in the Definite Loop Infinite Loop Indefinite Loop Finite Loop An iterative process whose execution properties, such as how many times it will loop, can be predicted before the loop even starts. For loops were designed for this purpose. An iterative process whose scope can't be predicted, such as an operating system running the computer until the user closes it. The time the user closes the computer can't be perfectly predicted by the computer. While loops are often used for this purpose. Like all number data understood by a computer, these iterative processes don't go on forever. Their loop guard is eventually invalidated and they halt. An iterative process that does go on forever, at least from a software perspective. It is not possible for their loop guard to be invalidated in any possible running of the program. (Loops forever for any instance of the software's problem.) The loop will end when the program is terminated externally, the computer runs out of a resource such as available memory, or the computer grows so old that its hardware fails.

Definite Loop - Correct match: An iterative process whose execution properties, such as how many times it will loop, can be predicted before the loop even starts. For loops were designed for this purpose. Indefinite Loop - Correct match: An iterative process whose scope can't be predicted, such as an operating system running the computer until the user closes it. The time the user closes the computer can't be perfectly predicted by the computer. While loops are often used for this purpose. Finite Loop - Correct match: Like all number data understood by a computer, these iterative processes don't go on forever. Their loop guard is eventually invalidated and they halt. Infinite Loop - Correct match: An iterative process that does go on forever, at least from a software perspective. It is not possible for their loop guard to be invalidated in any possible running of the program. (Loops forever for any instance of the software's problem.) The loop will end when the program is terminated externally, the computer runs out of a resource such as available memory, or the computer grows so old that its hardware fails.

What is the output of this program? String yes String no print yes AND no True False Undefined, variables were not initialized. Error, logic operators can't be used with String data.

Error, logic operators can't be used with String data.

for(boolean b = false; b == false; b = !b) { print b } Out: False Out: True Out: false, true Out: true, false

Out: False

Execute the body 6 times: (Reminder: % is the modulus operator.) int index = 0; { print (index % 3) ; } Out: 0, 1, 2, 3, 4, 5 Out: 0, 0, 0, 1, 1, 1 Out: 0, 1, 2, 3, 1, 2 Out: 0, 1, 2, 0, 1, 2

Out: 0, 1, 2, 0, 1, 2

Execute the body 6 times: (Reminder: % is the modulus operator.) int index = 0; { print index; index = (index +1 ) %3 ; } Out: 0, 1, 2, 3, 4, 5 Out: 0, 0, 0, 1, 1, 1 Out: 0, 1, 2, 3, 1, 2 Out: 0, 1, 2, 0, 1, 2

Out: 0, 1, 2, 0, 1, 2

for(int i = 3; i < 8; i++) { print i } Out: 0, 1, 2, 3, 4, 5, 6, 7 Out: 2, 3, 4, 5, 6, 7, 8 Out: 3, 4, 5, 6, 7 Out: 3, 5, 7, 9

Out: 3, 4, 5, 6, 7

Select 5 of these steps that are part of writing an effective iterative algorithm. Prepare for iteration by assigning initial values to all variables that are declared outside of the loop, but will be accessed within the loop. halfbake the solution Write a loop body that, when executed under certain 'invariant' conditions, improves the state of the variables while preserving its invariant, and making forward progress towards termination. If needed after the loop, calculate the final desired values based on the loop variables and non-iterative expressions. Attach a loop statement to the body and if needed add 'non-work' statements to the body such as those needed for a counter algorithm implemented using a while loop. Use a stopping condition to control how much improvement / iteration is enough. Most useful loops have some way of giving up / timing out / finishing their job. You won't wait for a bus forever and neither should you loops. Even those programs that will wait for the bus forever, routinely take a break and check for new orders or if the situation has changed. make an educated guess that is far better than the correct answer would have been. just solve the problem directly without a loop. Selected Answer - Incorrect Iterate forever until the solution is absolutely perfect in every way. Who needs good enough, when we can have perfect! Allow the program to move on to the next task. Think about how your iterative algorithm fits into and will be used in your larger program. ask the user to solve the problem for you

Prepare for iteration by assigning initial values to all variables that are declared outside of the loop, but will be accessed within the loop. Write a loop body that, when executed under certain 'invariant' conditions, improves the state of the variables while preserving its invariant, and making forward progress towards termination. If needed after the loop, calculate the final desired values based on the loop variables and non-iterative expressions. Attach a loop statement to the body and if needed add 'non-work' statements to the body such as those needed for a counter algorithm implemented using a while loop. Use a stopping condition to control how much improvement / iteration is enough. Most useful loops have some way of giving up / timing out / finishing their job. You won't wait for a bus forever and neither should you loops. Even those programs that will wait for the bus forever, routinely take a break and check for new orders or if the situation has changed. Allow the program to move on to the next task. Think about how your iterative algorithm fits into and will be used in your larger program.

Please select each of the three overarching flow control structures used in Structured Programming Sequence (Block / Body) Structure Indecisive Structure Garbage Collector Structure Recapitulation (Repeating a second time) Structure Logical Structure Obliteration Structure Selection (decision / if-then-else) Structure Tunnel Structure Repetition (loop) Structure

Sequence (Block / Body) Structure Selection (decision / if-then-else) Structure Repetition (loop) Structure

// Goal: print Hello World 5 times. int counter = 0; while(counter < 5) { String message = "Hello World" counter++ } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body doesn't do any work.

// Goal: print Hello World 5 times. int counter = 0; while(counter < 5) { counter ++ } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body both doesn't do any work and doesn't make any forward progress.

The body doesn't do any work.

// Goal: print Hello World 5 times. int counter = 0; while(counter < 5) { counter++ } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body doesn't do any work.

// Goal: print Hello World 5 times. do { int counter = 0 print "Hello World" counter++ }while(counter < 5); The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body fails to make forward progress.

// Goal: print Hello World 5 times. int counter = 0; while(counter < 5) { print "Hello World" } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body fails to make forward progress.

// Goal: print Hello World 5 times. String message = "" int counter = 0; while(counter < 5) { message = message + "Hello" + "World; print message counter++ } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body is not repeatable.

// Goal: print Hello World 5 times. int counter = 0; while(counter < 5) { String message = ""message = message + "Hello" + "World; print message counter++ } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body is well built.

// Goal: print Hello World 5 times. int counter = 0; while(counter < 5) { print "Hello World" counter++ } The body doesn't do any work. The body fails to make forward progress. The body is not repeatable. The body is well built.

The body is well built.

Execute the body 99 times: boolean b = false; { b = !b // Repeat 99 times. } print b [NO OUTPUT] False True maybe

True

What is the output of this program? boolean no = input() no = True OR no print no True False "no" Undefined. I don't know for certain what the input value will be.

True

What is the output of this program? boolean yes boolean no yes = True no = !False print yes == no "yes" "no" True False Undefined, variable was not initialized.

True

What is the output of this program? boolean yes yes = True print yes True False Undefined, variable was not initialized. Error, relational operators can't be used with Boolean data.

True

What is the output of this program? String yes print yes "yes" "no" True False Undefined, variable was not initialized.

Undefined, variable was not initialized.

What is the output of this program? String yes yes == "yes" print yes "yes" "no" True False Undefined, variable was not initialized.

Undefined, variable was not initialized.

What is the output of this program? boolean yes yes = (yes == True) print yes "yes" "no" True False Undefined, variable was not initialized.

Undefined, variable was not initialized.

What is the output of this program? boolean yes yes == True print yes "yes" "no" True False Undefined, variable was not initialized.

Undefined, variable was not initialized.

What is the output of this program? boolean yes boolean no print yes AND no True False Undefined, variables were not initialized. Error, logic operators can't be used with String data.

Undefined, variables were not initialized.

What is the output of this program? boolean no = input() no = False OR no print no True False "no" Undefined. I don't know for certain what the input value will be.

Undefined. I don't know for certain what the input value will be.

What is the output of this program? boolean no = input() no = True AND no print no True False "no" Undefined. I don't know for certain what the input value will be.

Undefined. I don't know for certain what the input value will be.

What is the output of this program? boolean no = input() print no True False "no" "input()" Undefined. I don't know for certain what the input value will be.

Undefined. I don't know for certain what the input value will be.

For this while loop, please order these conditionals from weakest to strongest. Fact: There are at least 256 different character values. Assume that c < 'f' only evaluates to false for roughly half of them. // Fact: 'a' < 'b' < 'c' < 'd' < 'e' < etc. char c = 'a' while(c < 'f') { print c; c++; // increase c's character code by 1: // 'a' becomes 'b', 'b' becomes 'c', etc. } Weakest (fewest false states) c != 'f' c != 'f' && c != 'g' c < 'f' ('a' <= c && c <= 'e') || ('A' <= c && c <= 'E') 'a' <= c && c <= 'e' Strongest (most false states.)

Weakest (fewest false states) c != 'f' c != 'f' && c != 'g' c < 'f' ('a' <= c && c <= 'e') || ('A' <= c && c <= 'E') 'a' <= c && c <= 'e' Strongest (most false states.)

Execute this body 0 times: { print "Hello World" } [NO OUTPUT] Hello World Hello World Hello World Hello World Hello World Hello World Hello World Hello World

[NO OUTPUT]

Execute the body 2 times: char a = 'a' char b = 'b' char c = 'c' char d = 'd' char e = 'e' { e = d d = c c = b b = a } print "" + a + b + c + d + e aabcd eabcd aaabc deabc

aaabc

This program is written in two ways. Select the while or the do-while form that is simpler. boolean firstTime = false; boolean a = true; boolean b = true; while(firstTime || !a || !b) { print "(a: " + a + ", b: " + b + ")" b = !b; if(b) { a = !a; } firstTime = false; } boolean a = true; boolean b = true; do { print "(a: " + a + ", b: " + b + ")" b = !b; if(b) { a = !a; } }while(!a || !b);

boolean a = true; boolean b = true; do { print "(a: " + a + ", b: " + b + ")" b = !b; if(b) { a = !a; } }while(!a || !b);

Which of these indefinite loops could stop? Check any and all finite loops. Leave infinite loops unselected. boolean done = fals while(!done) { print "Are you done?" print input(); } boolean done = false while(!done) { print "Are you done?" done = input(); } for(int i = 0; i >= 0; i++) { i = input(); } for(int i = 0; i >= MIN_INTEGER_VALUE; i++) { i = input(); } // A program to help me learn something new about// the numeric range of integers on any machine.// Should be near 2 ** 32 - 1 on 32 bit machines.// Should be near 2 ** 64 - 1 on 64 bit machines.int highest_int = 0; while(highest_int < highest_int + 1) { highest_int++;}print "I found the MAX integer: " + highest_int // A program to help me learn something new about // the numeric range of floating point numbers. float small = 1.0; int bit_estimate = 0; while(small != small/2) { small = small/2; bit_estimate++; } print msg = "Smallest float?: 2 to the power of -"; print msg + bit_estimate // Bit estimate results on my machine on 11.4.2020. // 2**(-150) for floats and // 2**(-1075) for doubles.

boolean done = false while(!done) { print "Are you done?" done = input(); } for(int i = 0; i >= 0; i++) { i = input(); } // A program to help me learn something new about// the numeric range of integers on any machine.// Should be near 2 ** 32 - 1 on 32 bit machines.// Should be near 2 ** 64 - 1 on 64 bit machines.int highest_int = 0; while(highest_int < highest_int + 1) { highest_int++;}print "I found the MAX integer: " + highest_int // A program to help me learn something new about // the numeric range of floating point numbers. float small = 1.0; int bit_estimate = 0; while(small != small/2) { small = small/2; bit_estimate++; } print msg = "Smallest float?: 2 to the power of -"; print msg + bit_estimate // Bit estimate results on my machine on 11.4.2020. // 2**(-150) for floats and // 2**(-1075) for doubles.

For this program, choose the best initial value that should be assigned to the done variable. (Fill in that blank!) boolean done = _______ while(done == false) { print "Are you done yet?" done = input(); } true false maybe not really

false

For this program, choose the best initial value that should be assigned to the happily_ever_after variable. (Fill in that blank!) boolean happily_ever_after = _________ for(int i = 0; i < 3; i++) { print "In movie #" + i + "did they find true love?" happily_ever_after = happily_ever_after || input(); } if(happily_ever_after) { print "And they lived happily ever after!" } else { print "Sadly this fairy tale ended tragically." } // Logical Note: If someone lives happily ever after, there is // no way to stop living happily as it lasts forever! true false maybe not really

false

This program is written in two ways. Select the while or the do-while form that is simpler. int start = 5; int lucky = start; print "Number: " + lucky lucky = (lucky + 1) % 10; while(lucky != start) { print "Number: " + lucky lucky = lucky + 1; if(lucky == 10) { lucky = 0; } } int start = 5; int lucky = start; do { print "Number: " + lucky lucky = (lucky + 1) % 10 }while(lucky != start);

int start = 5; int lucky = start; do { print "Number: " + lucky lucky = (lucky + 1) % 10 }while(lucky != start);

Which of these definite loops will stop? Check any and all finite loops. Leave infinite loops unselected. while(true) { print "Iteration!"; }; while(false) { print "Iteration!"; } int n = UserInput(); for(int i = 0; i < n; i++) { print "Iteration!"; } for(int i = 0; i < 10; i++) { print "Iteration"; if(i == 6) { i = i - 1; } } // This is definite if know what hardware you'll be using.// Indefinite otherwise. int signed_number = 0; while(signed_number >= 0) { print "Iteration"; signed_number++; // increase signed_number by 1// Overflows to negative number if its// now greater than MAX_INTEGER_VALUE.} int i = 1; while(i < 10) { print "Iteration!" i = i + 2 i = i % 10; } for(int i = 0; 0 <= i || i < 10; i++) { print "Iteration!"; }

while(false) { print "Iteration!"; } int n = UserInput(); for(int i = 0; i < n; i++) { print "Iteration!"; } // This is definite if know what hardware you'll be using.// Indefinite otherwise. int signed_number = 0; while(signed_number >= 0) { print "Iteration"; signed_number++; // increase signed_number by 1// Overflows to negative number if its// now greater than MAX_INTEGER_VALUE.}


Set pelajaran terkait

Chapter 30: Abdominal and Genitourinary Injuries Practice Questions

View Set

CNA Skills Assist w/ use of bedpan

View Set

Ch 43: Assessment and Management of Patients with Hepatic Disorders

View Set

Network Defense Essentials (NDE)

View Set