Question

In: Computer Science

Consider this code: "int v = 20; --v; System.out.println(v++);". What value is printed, what value is...

Consider this code: "int v = 20; --v; System.out.println(v++);". What value is printed, what value is v left with?

20 is printed, v ends up with 19
19 is printed, v ends up with 20
20 is printed, v ends up with 20
19 is printed, v ends up with 19
cannot determine what is printed, v ends up with 20

Solutions

Expert Solution

*************Summary***************

The answer for above question is given below and also the explanation.....

I have also provided a program with output for reference...showing the correct answer as output..

I hope it works for you :) !!!!!!!!!!!!!!!!!!!

**************Answer****************

The answer for above question is 19 is printed, v ends up with 20 ....

************Explanation**************

1. int v=20;

v is intialized to 20 so value of v=20

2. --v;

as the - - signs are before v...so it pre decrement operation...that means the value is decremented first and then rest of the statement is executed...so the value of v will be 19...v=19

3. System.out.println(v++);

as the ++ signs are after variable v..so it is post increment operation......that means first the statement is executed with current value of v ....and then the it increments the value of v....

1. so when executing system.out.println(v++) ..it uses the current value of v i.e v=19..so it prints 19 ........

2. And when statement has done executing ..then it increments the value of v ...so the latest value of v becomes 20.....

So from 1 and 2

19 is printed because it executes statement first with current value of v

and v ends up with 20 because it increments value of v after execution of that statement

*****************Program*******************

import java.util.Scanner;

public class Main
{
  public static void main(String args[])
  { 
    int v=20;    //inutializing v to 20
    
    --v;    //pre decrementing value of v...v will be 19
    
    System.out.println(v++);    
    //as there is post increment operator (++ operator after variable v).
    //..so it will print current value of v i.e. 19 using the system.out.println
    //and then after the printing of value of v
    //it will increment value of v ..so value of will be 20 after execution 
      
    //to check the value with which v ends up
    System.out.println("V ends up to with "+v);
  }
}

*****************Output*********************


Related Solutions

JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
Consider the code below. How many times the value of x is printed? (In other words,...
Consider the code below. How many times the value of x is printed? (In other words, how many times the loop is executed?) public static void main(String args[]) {       int x = 20;       while( x > 10 ) {          System.out.println( x );          x--;       }    } Consider the code below. What needs to be placed instead of ??? so that loop is run exactly 7 times? public static void main(String args[]) {       int x = 7;       while( x > ??? )...
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"); }
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next;...
In c++: Code Challenge Consider the following code: struct ListNode { int value; struct ListNode *next; }; ListNode *head; // List head pointer Assume that a linked list has been created and head points to the first node. Write code that traverses the list displaying the contents of each node’s value member Now write the code that destroys the linked list
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++)...
// 1. System.out.println("1."); int max = 5; for (int n = 1; n <= max; n++) { System.out.println(n); } System.out.println(); // 2. System.out.println("2."); int total = 25; for (int number = 1; number <= (total / 2); number++) { total = total - number; System.out.println(total + " " + number); } System.out.println(); // 3. System.out.println("3."); for (int i = 1; i <= 2; i++) { for (int j = 1; j <= 3; j++) { for (int k = 1;...
QUESTION 5 What is printed? int[] aArray = {1, 2, 3, 4}; int[] bArray = {5,...
QUESTION 5 What is printed? int[] aArray = {1, 2, 3, 4}; int[] bArray = {5, 6, 7, 8}; bArray = aArray; System.out.print(bArray[2]); 1 points    QUESTION 6 What is printed? public static void main(String[] args) { int[] aArray = { 1, 2, 3, 4 }; System.out.print(aArray[2]); int i = aMeth(aArray); System.out.print(i + "" + aArray[2]); } public static int aMeth(int[] iArray) { for (int i = 0; i < iArray.length; i++) { iArray[i]++; } int[] bArray = { 5,...
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; }
Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x =...
Find the output for the following statements. System.out.println(3%2+1+(3/2)); char ch = 69; System.out.println(ch); int x = 30; int y = 4; double z = x/y; System.out.println(z);
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
V 1 Consider a 20​-year bond with a face value of $ 1 comma 000 that...
V 1 Consider a 20​-year bond with a face value of $ 1 comma 000 that has a coupon rate of 5.8 %, with semiannual payments. a. What is the coupon payment for this​ bond? b. Draw the cash flows for the bond on a timeline. a. What is the coupon payment for this​ bond? The coupon payment for this bond is $ Nothing. ​ (Round to the nearest​cent.) b. Draw the cash flows for the bond on a timeline....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT