Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CIT-111
- Chapter 8 Loops
- Ex 8.1:
- public class WhileLoopExample {
- public static void main(String[] args){
- int num = 0;
- System.out.println("Let's count to 10!");
- while(num < 10){
- num = num + 1;
- System.out.println("Number: " + num);
- }
- System.out.println("We have counted to 10! Hurray! ");
- }
- }
- What is the output when the above code is executed?
- EX:8.2
- public class ForLoopExample {
- public static void main(String[] args) {
- for(;;)
- System.out.println("Hello");
- }
- }
- What is the output when the above code segment is executed?
- A. compile error; no output
- B. run time error; no output
- C. no error; no output
- D. HelloHelloHello
- E. Hello (infinite times)
- EX:8.3
- public class ForLoopExample {
- public static void main(String[] args) {
- int y = 1;
- for (int x=0; y<10; x++)
- System.out.println(x);
- }
- }
- How many times will the above loop execute?
- A. 1
- B. 0
- C. 9
- D. 10
- E. infinite
- EX:8.4
- Re-write the below for loop as a while loop.
- public class LoopClass{
- public static void main(String[] args){
- for(int x=0; x < 20; x++){
- System.out.println(x);
- }
- }
- }
- EX:8.5
- public class WhileLoopExample
- {
- public static void main(String args[])
- {
- System.out.println("Let's count to 10!");
- public void sillyLoop( int i ){
- while ( i != 0 ){
- i-- ;
- }
- }
- }
- }
- This has the potential to be an infinite loop. True/False?
- EX:8.6
- public void printGrid()
- {
- for ( int row = 0 ; row < height ; row++ ) //line 1
- {
- // PRINT a row
- for ( int col = 0 ; col < width ; col++ ) //line 2
- {
- System.out.print( "*" ) ; //line 3
- }
- // PRINT newline
- System.out.println( "" ) ; //line 4
- }
- }
- Show the trace from the above example where width is 3 and height is 2.
- EX:8.7
- n = 0;
- while (n < 5) {
- n++;
- System.out.println(n + " ") ;
- }
- What is the ouput of the above code?
- A. 0 0 0 0 ... forever
- B. 1 2 3 4
- C. 1 2 3 4 5
- D. 0 1 2 3 4
- E. 0 1 2 3 4 5
- EX:8.8
- The logical expression associated with the while loop is evaluated ________________.
- a. before any of the statements in the loop
- b. after any of the statements in the loop
- c. after the first statement in the loop
- d. before the last statement in the loop
- e. right in the middle of the loop
- EX:8.9
- 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.
- EX:8.10
- 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.
- EX:8.11
- What is the output?
- for(int i=1; i<15; i=i+3){
- System.out.print(i);
- }
- A. 15
- B. 1471013
- C. 47101316
- D. 147101516
- EX:8.12
- What represents the condition in the for statement?
- for(int j=11; j>=2; j=j-2)
- EX:8.13
- What is the last value of m that gets printed out?
- for(int m = 30; m > 0; m = m - 4){
- System.out.println(m);
- }
- EX:8.14
- What is the output?
- int total=0;
- for(int s=1; s<15; s++){
- total=total+s;
- }
- System.out.println(total);
- EX 8.15
- What is the output?
- for(int m=30; m>0; m=m-4){
- System.out.println(m);
- }
- EX 8.16
- Show the exact output produced by the following main() routine:
- public static void main(String[] args) {
- int x,y;
- x = 5;
- y = 1;
- while (x > 0) {
- x = x - 1;
- y = y * x;
- System.out.println(y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement