Quiz 5 - Loops
What is the output of the following fragment? int i = 1 int j = 1 while (i<5) { i++; j = j*2; } System.out.println(j); Sellected Answer: 4 Correct answer 16
...
Which of the following expression yields an integer between 0 and 100, inclusive? Selected Answer: Correct (int)(Math.random() * 101) Correct Answer: Correct (int)(Math.random() * 101)
...
Analyze the following code. int count = 0; while (count <100){ //Point A// System.out.println("Welcome to Java"); count++ //Point B// } //Point C// Correct Answer(s) count < 100 is always true at Point A count < 100 is always false at Point C
...
Do the following two statements in (I) and (II) result in the same value in sum? (I) int sum=0; for (int i=0; i<10; ++i){ sum+=i } (II) int sum=0; for (int=0; i<10; ++i){ sum+=i } Correct answer Yes
...
How many times will the following code print "Welcome to Java"? int count = 0; while (count<10){ System.out.println("Welcome to Java"); count++; } Correct Answer 10
...
How many times will the following code print "Welcome to Java"? int count=0; do { System.out.println("Welcome to Java"); } while (count ++<10); Selected Answer 9 Correct Answer 11
...
In a for statement, if the continuation condition is blank, the condition is assumed to be ________. Selected Answer: Correct true Correct Answer: Correct true
...
What is the number of iterations in the following loop? for (i =1) ; (i <=n; i++) { //iteration } Correct Answer: n
...
What is the output for y? int y=0 for (int i=0; i<10; i++){ y +=i; } System.out.println(y); Correct Answer 45
...
Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + ... + 99/100? A double sum= 0; for( int i=1; i<=99; i++){ sum = i/ (i + 1); } System.out.println("The sum is" +sum); B double sum=0; for(int i=1;i<99; i++){ sum+= i/ (i+1); } System.out.println("Sum is" +sum); D double sum=0; for (int i=1; i<=00; i++){ sum+=i / (i+1.0); } System.out.println("Sum is" +sum); C double sum=0; for (int =1; i<=99; i++){ sum+= 1.0*i / (i + 1); } System.out.println("Sum is" +sum); E double sum=0; for(int i=1; i<99; i++){ sum+= i /( i + 1); } System.out.println("Sum is" +sum); Correct Answer(s) CD
...
Which of the following loops prints "Welcome to Java" 10 times? A for (int count=1; count<=10; count++) { System.out.println("Welcome to Java"); } B for (int count=0; count<10;count ++) { System.out.println("Welcome to Java"); } C for (int count=1; count<10; count++) { System.out.println("Welcome to Java"); } D for (int count=0; count<=10; count++) { System.out.println("Welcome to Java"); } Correct Answer AB
...
Which of the following loops produces the output? 1 2 3 4 1 2 3 1 (I) for (int i = 5; i>0; i--){ for (int j = 1; j <5 j++) System.out.print(j + " "); System.out.println(); } (II) for (int i=1; i < 5; i++){ for (int j=1; j<i; j++); System.out.print(j + " "); System.out.println(); } (III) int i = 0; while (i < 5) { for (int j= 1; j<i; j++); System.out.print(j + " "); System.out.println(); i++ } (IV) int i = 5 while (i<0){ for (int j=1; j<i; j++) System.out.println(); i--; } Selected Answers (I) (IV) Correct answers (I) (IV)
...
Will the following program terminate? int balance=10; while (true) { if (balance <9) break; balance=balance -9; } Selected Answer: Correct Yes Correct Answer: Correct Yes
...
You can always write a program without using break or continue in a loop. Selected Answer: Correct true Correct Answer: Correct true
...
What is the printout after the following loop terminates? int number= 25 int i; boolean isPrime=true for (i=2; i<number; i++){ if( number % i ==0){ isPrime=false;break; } } System.out.println("i is " + i + " isPrime is " + isPrime); Selected Answer Correct i is 5 isPrime is false Correct Answer i is 5 isPrime is false
....
Will the following program terminate? int balance =10; while(true) { if (balance<9) continue; balance = balance -9; } Selected Answer: Incorrect Yes Correct Answer: Correct No
.......