Java Programming - Language Fundamentals - Discussion

Discussion Forum : 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 + ", ");
    }
}
10, 9, 8, 7, 6,
9, 8, 7, 6, 5,
Compilation fails.
An exception is thrown at runtime.
Answer: Option
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.

Discussion:
8 comments Page 1 of 1.

Sundar said:   1 decade ago
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!
(1)

Amit said:   8 years ago
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.
(1)

Nilam said:   7 years ago
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.

Rahaman said:   9 years ago
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.

Santosh said:   1 decade ago
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:   9 years ago
System.out.print(x + ", ");

Is this format is correct?

Thanks in advance.
(1)

Imran said:   1 decade ago
If we remove "signed" then what will be the output ?

Poonam said:   1 decade ago
Thanks Sundar.

Post your comments here:

Your comments will be displayed after verification.