Java Tricky Programs

Java Tricky Programs


1) What will be the output ?


public class MainTest
{
public static void main(String[] args)
{
String s = "One"+5*2+"Two"+"Three"+3+4+"Four"+"Five"+5;
System.out.println(s);
}
}


Ans :

Output : One10TwoThree34FourFive5

Description : Multiplication is priority operation will done first 5*2 and then all string concatination will perform.


2) What will be the output ?

public class TestMath
{
    public static void main(String[] args)
    {
        int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15;

        System.out.println(i);
    }
}

?
Ans :

Output : 75

Description : Simple math logic will apply here.

Simplify using brackets :

(10 +) + 11 - (- 12)+ (+ 13)- (- 14)+ (+ 15);


Apply Math Rules :

10 + 11 + 12 +13 + 14 + 15 =75




3) Can you instantiate this class

public class IntegerTest { IntegerTest a=new IntegerTest(); }

Ans : Yes

No comments:

Post a Comment