Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

January 9, 2010

Adobe interview Ques (4th Jan 10)

public class GreatQues {

public static void main(String[] args) {

int a = 10;
int b =20;

System.out.println("Sum of the numbers : " + a + b);
//System.out.println("Diff of the numbers : " + a - b); // (Line 1) Compile time error
System.out.println("Product of the numbers : " + a * b);
System.out.println("Division of the numbers : " + a / b);

short x = 127;
short y = 140;
//short z = x + y; // Compile time error (Line 2)
System.out.println(127 + 140);


}

}

Output :

Sum of the numbers : 1020
Product of the numbers : 200
Division of the numbers : 0
267


Explanation :

When we write sysout ( String + some integer), the compiler treats that interger as a String only and just appends the value to the String.
- operator is not overloaded in String, therefore, the Line 1 gives compile time error saying : - operator is undefined for arguments of type String and int.

Line 2 gives compile time error because any mathematical operation on two shorts / ints / bytes always gives int as result.

1 comment:

arvind said...

could you post other questions as well