Question

In: Computer Science

What output is produced by the following code fragment? int num = 0, max = 20;...

What output is produced by the following code fragment?

int num = 0, max = 20;

while (num < max)

{

System.out.println(num);

num += 4;

}

Solutions

Expert Solution

Output:

0

4

8

12

16

Explanation:

  • println() prints the value at new line every time it is called

Initially num = 0 and max = 20,

Iteration 1: num = 0, max = 20.

(num<max) ==> (0 < 20) which is true. So it goes inside while loop and prints the current max value i.e, 0. Then num+=4 means num = num +4 i.e, increments the num value by 4. So new num value = 0 + 4 = 4.

Iteration 2: num = 4, max = 20.

(num<max) ==> (4 < 20) which is true. So it goes inside while loop and prints the current max value i.e, 4. Then num+=4 means num = num +4 i.e, increments the num value by 4. So new num value = 4 + 4 = 8.

Iteration 3: num = 8, max = 20.

(num<max) ==> (8 < 20) which is true. So it goes inside while loop and prints the current max value i.e, 8. Then num+=4 means num = num +4 i.e, increments the num value by 4. So new num value = 8 + 4 = 12.

Iteration 4: num = 12, max = 20.

(num<max) ==> (12 < 20) which is true. So it goes inside while loop and prints the current max value i.e, 12. Then num+=4 means num = num +4 i.e, increments the num value by 4. So new num value = 12 + 4 = 16.

Iteration 5: num = 16, max = 20.

(num<max) ==> (16 < 20) which is true. So it goes inside while loop and prints the current max value i.e, 16. Then num+=4 means num = num +4 i.e, increments the num value by 4. So new num value = 16 + 4 = 20.

Iteration 6: num = 20, max = 20.

(num<max) ==> (20 < 20) which is false. So while loop terminates.


Related Solutions

Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
What will the following code segments print on the screen? int num = 3; switch (num){...
What will the following code segments print on the screen? int num = 3; switch (num){             case 1:                         System.out.println(“Spring”); case 2:                         System.out.println(“Summer”); case 3:                         System.out.println(“Autumn”); case 4:                         System.out.println(“Winter”); }
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; }
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 =...
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); }...
What’s the output of following code fragment: #include<stdio.h> #include<signal.h> void response (int sig_no) { printf("25"); } void response2 (int sig_no) { printf("43"); }     int main() {      int id = getpid();      signal(SIGUSR1, response); signal(SIGKILL, response2); for(int i=0; i<4; i++) { sleep(1); if (i % 3 == 0) { kill(id, SIGUSR1); } else { kill(id, SIGKILL); } } return 0; }
What is the output of the following C++ code? int* length; int* width; length = new...
What is the output of the following C++ code? int* length; int* width; length = new int; *length = 5; width = length; length = new int; *length = 2 * (*width); cout << *length << " " << *width << " " << (*length) * (*width) << endl;
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a =...
[after §3.22 − easy] String Processing : Consider the following code fragment: 1 int a = 20; 2 int b; 3 double x = 3.5; 4 String s = "All"; 5 char ch; 6 7 x += a; 8 x--; 9 a /= 4 - 1; 10 b = s.length(); 11 b += 4; 12 s += "is well"; 13 ch = s.charAt(b); 14 System.out.println("a = " + a + ", b = " + b); 15 System.out.println("x = "...
5C++ Questions 1. What will the following code print? num = 8; cout << --num <<...
5C++ Questions 1. What will the following code print? num = 8; cout << --num << " "; cout << num++ << " "; cout << num; 2.Since the while loop evaluates the condition before executing the statement(s), it is known as a(n)____ loop, whereas the do-while loop evaluates the condition after the statements have been executed, it is known as a(n)____ loop. 3.T/F While loops can only be used when a counter is decremented to zero. If the counter...
What is the output of the following piece of Java code? int id = 4; double...
What is the output of the following piece of Java code? int id = 4; double grossPay = 81.475; String jobType = "Intern"; System.out.printf("Id: %d, Type: %s, Amount:%.2f", id,grossPay,jobType); Select one: Error Id: 4, Type: Intern, Amount: $81.475 Id: 4, Type: $81.475, Amount: Intern Id: 4, Type: $81.48, Amount:Intern Id: 4, Type: Intern, Amount: $81.48
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT