Menu
Topics Index
...
`

Classes


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes
Compare the following program?
public class CrossCheck1 {
    public static void main(String args[]) {
        short s = 10;
        int a = s;
        s += 10;
        System.out.println(s + "," + a);
    }
}

public class CrossCheck2 {
    public static void main(String args[]) {
        short s = 10;
        int a = s;
        s = s + 10;
        System.out.println(s + "," + a);
    }
}

A. Both CrossCheck1 and CrossCheck2 produce same output.
B. CorssCheck1 does not compile, but CorssCheck2 will compile.
C. CorssCheck2 does not compile, but CorssCheck1 will compile.
D. Both CorssCheck1 and CorssCheck2 compile, but they produce different output.
E. Both CorssCheck1 and CorssCheck2 does not compile.
Topic: Arithmetic Compound Assignment Operators In Java

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.

A is the answer because s+=10 and s=s+10 both r same

Posted by Yeshu Yeshwanthi    2013-08-30 09:14:06


ans is optn A - both produce same output but execution of s+=10 is more faster than s=s+10

Posted by Vamsi Krishna Bulusu    2013-08-30 09:14:07


ans should be E as both does not compile..short+10 (int) must give int..so typecasting is needed..

Posted by Pramod Jain    2013-08-30 09:16:58


Answer is C : 1st program compiles though its not typecasted since here in s+=10,s is taken as short only ! whr as in 2nd progrm dats nt d case s=s+10; here s is requird to b type casted ! thus error it throws !

Posted by Shaik Nyamath    2013-08-30 11:14:37


C is the correct answer as in the first case there is no need of type casting where as in second case int value cannot be assigned to shord we need to explicitly typecast it

Posted by Harish Reddy Bitla    2013-08-30 14:01:12


Congratulations Harish Reddy. You are this dose winner. Please send us your mobile details in a private message and we will process the recharge asap.

Posted by Merit Campus    2013-08-31 10:40:39


© meritcampus 2019

All Rights Reserved.

Open In App