Created
October 20, 2015 21:16
-
-
Save leonardofaria/a963265846e5d8a4eb71 to your computer and use it in GitHub Desktop.
WMDD 4820 - Quiz 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 1. How many times are the following loop bodies repeated? | |
// What is the printout of each loop? (3 points) | |
// a. | |
var i = 1; | |
while (i < 10) | |
if (i % 2 == 0) | |
document.write( i + "<br />"); | |
// Answer: infinity loop. i will be never incremented. | |
// b. | |
var i = 1; | |
while (i < 10) | |
if (i % 2 == 0) | |
document.write( i++ + "<br />"); | |
// Answer: infinity loop. i will be never incremented. | |
// c. | |
var i = 1; | |
while (i < 10) | |
if (i++ % 2 == 0) | |
document.write( i ); | |
// Answer: | |
// 3 | |
// 5 | |
// 7 | |
// 9 | |
// ----------------------------------------------------------------------------- | |
// 2. Will the following programs terminate? If so, give the output. (4 points) | |
// a. | |
var balance = 49; | |
while (true) | |
{ | |
if (balance < 9) | |
break; | |
balance = balance - 9; | |
} | |
document.write ( "Balance is " + balance ); | |
// Answer: Balance is 4 | |
// b. | |
var balance = 49; | |
while (true) | |
{ | |
if (balance < 9) | |
continue; | |
balance = balance - 9; | |
} | |
document.write ( "Balance is " + balance ); | |
// ----------------------------------------------------------------------------- | |
// 3. Show the output of the following code segments. (6 points) | |
// a. | |
var i = 0; | |
while (i < 4) | |
{ | |
for (var j = i; j > 1; j--) | |
document.write ( j ); | |
document.write( "****" + "<br/>" ); | |
i++; | |
} | |
// Answer: | |
// **** | |
// **** | |
// 2**** | |
// 32**** | |
// b. | |
var i = 4; | |
while (i >= 1) | |
{ | |
var num = 1; | |
for (var j = 1; j <= i; j++) | |
{ | |
document.write( num + "xxx" ); | |
num *= 2; | |
} | |
document.write( "<br/>" ); | |
i--; | |
} | |
// Answer: | |
// 1xxx2xxx4xxx8xxx | |
// 1xxx2xxx4xxx | |
// 1xxx2xxx | |
// 1xxx | |
// c. | |
var row = 4; | |
while ( row >= 1 ) { | |
column = 1; | |
while ( column <= 5 ) { | |
document.write ( row % 2 ? "<" : ">" ); | |
++column; | |
} | |
--row; | |
document.write("<br/>"); | |
} | |
// Answer | |
// >>>>> | |
// <<<<< | |
// >>>>> | |
// <<<<< | |
// ----------------------------------------------------------------------------- | |
// 4. How many times is the print statement executed? (2 points) | |
for (var i = 0; i < 10; i++) | |
for (var j = 0; j < i; j++) | |
document.write( i * j + "<br/>"); | |
// Answer: 45 times |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment