Advertisement
Mary_Martha

Untitled

Apr 20th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. CIT-111
  2. Chapter 8 Loops
  3.  
  4. Ex 8.1:
  5. public class WhileLoopExample {
  6. public static void main(String[] args){
  7. int num = 0;
  8. System.out.println("Let's count to 10!");
  9. while(num < 10){
  10. num = num + 1;
  11. System.out.println("Number: " + num);
  12. }
  13. System.out.println("We have counted to 10! Hurray! ");
  14. }
  15. }
  16.  
  17. What is the output when the above code is executed?
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. EX:8.2
  28. public class ForLoopExample {
  29.  
  30. public static void main(String[] args) {
  31. for(;;)
  32. System.out.println("Hello");
  33. }
  34. }
  35. What is the output when the above code segment is executed?
  36.  
  37. A. compile error; no output
  38. B. run time error; no output
  39. C. no error; no output
  40. D. HelloHelloHello
  41. E. Hello (infinite times)
  42.  
  43.  
  44.  
  45. EX:8.3
  46. public class ForLoopExample {
  47.  
  48. public static void main(String[] args) {
  49. int y = 1;
  50. for (int x=0; y<10; x++)
  51. System.out.println(x);
  52. }
  53. }
  54. How many times will the above loop execute?
  55.  
  56. A. 1
  57. B. 0
  58. C. 9
  59. D. 10
  60. E. infinite
  61.  
  62.  
  63.  
  64. EX:8.4
  65. Re-write the below for loop as a while loop.
  66. public class LoopClass{
  67. public static void main(String[] args){
  68. for(int x=0; x < 20; x++){
  69. System.out.println(x);
  70. }
  71. }
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. EX:8.5
  86. public class WhileLoopExample
  87. {
  88. public static void main(String args[])
  89. {
  90. System.out.println("Let's count to 10!");
  91. public void sillyLoop( int i ){
  92. while ( i != 0 ){
  93. i-- ;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. This has the potential to be an infinite loop. True/False?
  100.  
  101.  
  102.  
  103.  
  104. EX:8.6
  105. public void printGrid()
  106. {
  107. for ( int row = 0 ; row < height ; row++ ) //line 1
  108. {
  109. // PRINT a row
  110. for ( int col = 0 ; col < width ; col++ ) //line 2
  111. {
  112. System.out.print( "*" ) ; //line 3
  113. }
  114. // PRINT newline
  115. System.out.println( "" ) ; //line 4
  116. }
  117. }
  118. Show the trace from the above example where width is 3 and height is 2.
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137. EX:8.7
  138. n = 0;
  139. while (n < 5) {
  140. n++;
  141. System.out.println(n + " ") ;
  142. }
  143. What is the ouput of the above code?
  144.  
  145. A. 0 0 0 0 ... forever
  146. B. 1 2 3 4
  147. C. 1 2 3 4 5
  148. D. 0 1 2 3 4
  149. E. 0 1 2 3 4 5
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156. EX:8.8
  157. The logical expression associated with the while loop is evaluated ________________.
  158.  
  159. a. before any of the statements in the loop
  160. b. after any of the statements in the loop
  161. c. after the first statement in the loop
  162. d. before the last statement in the loop
  163. e. right in the middle of the loop
  164.  
  165.  
  166.  
  167.  
  168. EX:8.9
  169. Write a Java code segment using the while loop to find the sum of the even integers 2,4,6,8,...,500. Display the resulting sum. Be sure to give code to declare and initialize variables used.
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187. EX:8.10
  188. Write a for loop that will print out all the multiples of 3 from 3 to 36, that is: 3 6 9 12 15 18 21 24 27 30 33 36.
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. EX:8.11
  209. What is the output?
  210. for(int i=1; i<15; i=i+3){
  211. System.out.print(i);
  212. }
  213. A. 15
  214. B. 1471013
  215. C. 47101316
  216. D. 147101516
  217.  
  218.  
  219.  
  220.  
  221. EX:8.12
  222. What represents the condition in the for statement?
  223. for(int j=11; j>=2; j=j-2)
  224.  
  225.  
  226.  
  227.  
  228. EX:8.13
  229. What is the last value of m that gets printed out?
  230. for(int m = 30; m > 0; m = m - 4){
  231. System.out.println(m);
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238. EX:8.14
  239. What is the output?
  240. int total=0;
  241. for(int s=1; s<15; s++){
  242. total=total+s;
  243. }
  244. System.out.println(total);
  245.  
  246.  
  247.  
  248.  
  249.  
  250. EX 8.15
  251. What is the output?
  252. for(int m=30; m>0; m=m-4){
  253. System.out.println(m);
  254. }
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. EX 8.16
  267. Show the exact output produced by the following main() routine:
  268.  
  269. public static void main(String[] args) {
  270. int x,y;
  271. x = 5;
  272. y = 1;
  273. while (x > 0) {
  274. x = x - 1;
  275. y = y * x;
  276. System.out.println(y);
  277. }
  278. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement