Menu
Topics Index
...
`

Print formated statments


If you need explanation Read this topic

If you need Answer Take test on this topic

Print formated statments
What will be the output of the following program?
public class PrintStatment5
{
    public static void main(String args[])
    {
        int x = 5;
        System.out.printf("%d ", x++);
        System.out.printf("%d ", --x);
        System.out.printf("%d ", x--);
        System.out.printf("%d ", ++x);
        System.out.printf("%d ", x++);    
    }
}

A. 5 5 5 5 5
B. 5 4 3 3 3
C. 5 4 4 2 3
D. 5 4 4 4 4
E. Compilation Error or Runtime Error
Topic: Java Formatter

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans:A first it displays 5 then x=6 --x then displays 5 x=5 then 5  -- x=4 " 5"  the "5" then x=6

Posted by Indrasena Reddy    2014-06-18 13:00:56


Ans is d..in first sop as it is post increment the actual value will be printed then increments,,next sop decrements and then prints...Similarly the next operations are performed and the corresponding values printed..in post first value is used and operation performed and in pre first operation performed and that value is used..thus output is 5 4 4 4 4

Posted by Badam Swathi    2014-06-18 13:15:27


ans is A since the printing takes place in the form of increment and decrement operations like first the value is 5 and we r trying to display the x value by post increment i.e x++ so here the x value is displayed first and then incrementing is done on x variable i.e x=6 and pre drecrement is done so first decrement takes place then operartion is done i.e x=x-1...x=5 and display x=5 like that operations takes on....

Posted by Vicky Maduri    2014-06-18 14:36:33


Ans:A

Posted by Nausheen Fathima    2014-06-18 14:42:05


Congratulations Indrasena Reddy. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-19 07:25:01


© meritcampus 2019

All Rights Reserved.

Open In App