Question

What is the output of the following fragment? please explain. C language int i = 1;...

What is the output of the following fragment?

please explain.

C language
int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
printf(“%d”, j);
(a) 4
(b) 8
(c) 16
(d) 32

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int i = 1;
int j = 1;
while (i < 5) {
    i++;
    j = j * 2;
}
printf(“%d”, j);


Let's trace through all iterations of while loop
i = 1
    i is increased to 2
    j = j*2 = 1*2 = 2
i = 2
    i is increased to 3
    j = j*2 = 2*2 = 4
i = 3
    i is increased to 4
    j = j*2 = 4*2 = 8
i = 4
    i is increased to 5
    j = j*2 = 8*2 = 16
i = 5
    loop exits because i < 5 is false.
so, final value of j is 16
so, it prints 16

Answer: (c) 16


Add a comment
Know the answer?
Add Answer to:
What is the output of the following fragment? please explain. C language int i = 1;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 20) What is the output of the following segment of C code: int avg(int n, int*...

    20) What is the output of the following segment of C code: int avg(int n, int* a); int main () {             int array[4]={1,0,6,9};             printf("%d", avg(4, array)+ 1);             system("pause");             return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...

  • What is the output of the following code fragment? int i = 1; while( i <=...

    What is the output of the following code fragment? int i = 1; while( i <= 5 ) if(i == 2 || i == 4) System.out.println(i + ":" + " is an even index) System.out.println("i: " + i); i++;

  • ( C prog) write an output for the following code fragment int a[]= {1, 2, 3,...

    ( C prog) write an output for the following code fragment int a[]= {1, 2, 3, 4, 5, 6}: int *ip; int i; ip=a; ip +=3; *ip= *ip * 3; for (i=0; i <6; ++i) printf("%d," a[i]);

  • 1.)What would be the output of the following programs: (a) main( ) { int i =...

    1.)What would be the output of the following programs: (a) main( ) { int i = 5, j = 2 ; junk ( i, j ) ; printf ( "\n%d %d", i, j ) ; } junk ( int i, int j ) { i = i * i ; j = j * j ; } (b) main( ) { int i = 5, j = 2 ; junk ( &i, &j ) ; printf ( "\n%d %d", i,...

  • Write the output from the following code fragment: for (int i = 1; I < 7;...

    Write the output from the following code fragment: for (int i = 1; I < 7; ++i) { switch (i) { case 1: printf ("\n%d Banana", i); case 2: printf ("\n%d Kumquat", i);

  • QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2,...

    QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2, 3};         int *p = a;         int **r = &p;         printf("%p %p", *r, a); A. Different memory addresses printed B. 1 2 C. Same memory address printed twice D. 1 1 2 points    QUESTION 10 What will be the output of following code snippet? int arr[4] = {1, 2, 3, 4};         int *p;         p = arr + 3;         *p = 5;         printf("%d\n", arr[3]); A....

  • in C language please! 9. 14 points Show what the following C code will print. const...

    in C language please! 9. 14 points Show what the following C code will print. const int MAX_I_COUNT = 4; const int MAX_J_COUNT = 15; int i, j; for( i = 1; i <MAX_I_COUNT; i++) { for(j = 2; j <MAX_J_COUNT; j += 4 ) printf("%d", j + i); puts("\n-----" ); puts( "I'm Done!" ); 10. 14 points What would be printed? int bean = 5, cheese = 3; int *p = &bean; *p += bean; ++cheese; *p += cheese;...

  • PART A: Predict how many lines of output will be displayed by the following program fragment:...

    PART A: Predict how many lines of output will be displayed by the following program fragment: i = 0 ​​ do { ​​for (j = 0; j < 4; j = j + 1) ​​printf("%d\n", i + j); ​​i = i + 1; ​​} while (i < 5); PART B: Then write either a C or a C++ program to verify your prediction. ​​ I know it will output 20 lines, the full C++ code is what I need.

  • should be in C language What is the output of the following segment of C code:...

    should be in C language What is the output of the following segment of C code: void CapsLetter (char* x, charl); int main() { char* text; text="This is some sample text."; CapsLetter(text, 'e'); printf("%s", text); return 0; سی void Caps Letter (char* x, char 1) { int i=0; while (x[i]!='\0') { if (x[i]==1) x[i]=1-32; } 1. This is som sample text. 2. THIS IS SOME SAMPLE TEXT. 3. This is some sample text. 4. this is some sample text. What...

  • Please try to explain the answers as well. It will be highly appreciated. Output of the...

    Please try to explain the answers as well. It will be highly appreciated. Output of the following expression: 5 * 4 % 2 = ? Output of a loop: Example: What will be the output of the Java code (Assume all variables are properly declared.) num = 10; while (num <= 32)                 num = num + 5; System.out.println(num); What will be the output of the Java code (Assume all variables are properly declared.) public class test {   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT