Question

In: Computer Science

The following is an infinite loop: for (int i = 0; i < 10; i++); System.out.println(i...

The following is an infinite loop:

for (int i = 0; i < 10; i++);
System.out.println(i + 4);

TRUE OR FALSE?

Solutions

Expert Solution

Answer: FALSE

Explanation:

Program:

Explanation:

Line 1: //Here taking class name as MyClass
Line 2: //Start of main()
Line 3: //for loop iteration
Line 4: //Printing the value of i+4
Line 5:// End of for loop
Line 6://End of main()
Line 7://End of MyClass class

Program:

public class MyClass {
public static void main(String args[]) {
for (int i = 0; i < 10; i++){
System.out.println(i+4);
}
}
}

Output:

Explanation:

Step 1:initially value of i=0
Checking the condition i<10 => 0<10 True
Print i+4 => Print 0+4 => Print 4
i = i++ => i = 4+1 => i = 5

Step 2: value of i=5
Checking the condition i<10 => 5<10 True
Print 5
i = i++ => i = 5+1 => i = 6

Step 3: value of i=6
Checking the condition i<10 => 6<10 True
Print 6
i = i++ => i = 6+1 => i = 7

Step 4: value of i=7
Checking the condition i<10 => 7<10 True
Print 7
i = i++ => i = 7+1 => i = 8

Step 5:value of i=8
Checking the condition i<10 => 8<10 True
Print 8
i = i++ => i = 8+1 => i = 9

Step 6:value of i=9
Checking the condition i<10 => 9<10 True
Print 9
i = i++ => i = 9+1 => i = 10


Related Solutions

(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else {...
int a = 3; int b = -2; if((a>0)&&(b>0)){ if (a>b) { System.out.println("A"); } else { System.out.println("B"); } } else if ((b<0)||(a<0)) { System.out.println("C"); } else { System.out.println("D"); }
(True or False) The following is an infinite loop. int main() { bool flag = false;...
(True or False) The following is an infinite loop. int main() { bool flag = false; int num = 0; while(!flag); { cin >> num; if(num == -1) { flag = true; } } return 0; }
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10;...
#include <iostream> using namespace std; int main() { int even=0,odd=0,sum=0,sum2=0,largest,smallest; int num,i; for ( i=1; i<=10; i++){ cout << " Enter " << i << " number: "; cin >> num; if ( num%2==0){ even++; sum+=num; } else { odd++; sum2+=num; if(num>largest){ largest = num; } if(num<largest) { smallest = num; } } cout << " The sum of even number is : " << sum << endl; cout << " The total-count of even number is : " <<...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) {...
14. How many times does "#" print? for(int i = 0; i < 10; ++i) { if(i == 2 || i==4 || i==6) { continue; } cout << "#"; }
How many times will the following while loop iterate? int i = 1; while (i <...
How many times will the following while loop iterate? int i = 1; while (i < 5) {     i = i + 1;     System.out.println(“Hello!”); } Group of answer choices 4 0 5 It will iterate infinitely
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i...
public void printQueue(DoublyLinkedQueue<Integer> Q){ int len = Q.size(); int k = 0; for (int i=0; i < len; ++i){ k = Q.dequeue(); Q.enqueue(k);    System.out.println(Q.dequeue()); } } What is the complexity of this code? Select one: a. O(N), N = k b. Q(1) c. Q(N2), N = Q.size() d. O(M), M = Q.size() e. None of the alternatives is correct. which one?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT