Java Programming - Language Fundamentals - Discussion

Discussion :: Language Fundamentals - Finding the output (Q.No.5)

5. 

What will be the output of the program ?

public class Test 
{
    public static void main(String [] args) 
    {
        signed int x = 10;
        for (int y=0; y<5; y++, x--)
            System.out.print(x + ", ");
    }
}

[A]. 10, 9, 8, 7, 6,
[B]. 9, 8, 7, 6, 5,
[C]. Compilation fails.
[D]. An exception is thrown at runtime.

Answer: Option C

Explanation:

The word "signed" is not a valid modifier keyword in the Java language. All number primitives in Java are signed. Hence the Compilation will fails.


Imran said: (Aug 27, 2010)  
If we remove "signed" then what will be the output ?

Sundar said: (Aug 27, 2010)  
Hi Imran,

I have tested the above program, and the output is given below.

C:\>javac Test.java
Test.java:5: not a statement
signed int x = 10;
^
Test.java:5: ';' expected
signed int x = 10;
^
2 errors

/*After removing the keyword 'signed' */

C:\>javac Test.java

C:\>java Test
10, 9, 8, 7, 6,

Hope you understand. Have a nice day!

Poonam said: (Sep 14, 2011)  
Thanks Sundar.

Santosh said: (Mar 3, 2014)  
If I am does not use the "signed", Is it prints the result as 9, 8, 7, 6, 5. Please give me reply. I am confused.

Anusha said: (Apr 18, 2015)  
System.out.print(x + ", ");

Is this format is correct?

Thanks in advance.

Rahaman said: (Apr 27, 2015)  
Yes if we don't use signed then the program will run but it does not print 9 to 5. It will print 10 to 6 because this loop is starting from 0. So the out will be 10, 9, 8, 7, 6.

Amit said: (Aug 23, 2016)  
Yes, if we remove 'signed' then it will print 10, 9, 8, 7, 6.

Because inside the loop, firstly 0 will assign to y (y=0), then the condition will check (y<5).
Then,
It print the value of x, and increase the value of y and decrease the value of x.

Nilam said: (Jan 4, 2017)  
Yes, if we remove 'signed' then it will print 10,9,8,7,6.

As because inside for loop we are using y=0 to <5 and x-- , hence it will print the value of x for 5 times but decreasing value of x.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.