Menu
Topics Index
...
`

Constructors2317


If you need explanation Read this topic

If you need Answer Take test on this topic

Constructors2317
What will be the output of the following program?
public class WrapperConstructors {
    public static void main(String[] args) {
        String value = "200";
        short s = 128;
        int i = 129;
        Short input = new Short(s);
        Short input1 = new Short(value);
        Integer data = new Integer(i);
        Integer data1 = new Integer(value);
        System.out.println(input + " " + input1);
        System.out.println(data + " " + data1);
    }
}

A. 128 200
129 200
B. 129 200
128 200
C. Compilation Error
D. Runtime Error
Topic: Creating Objects for Primitive Data Types (Byte, Short)

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.

Ans is B..here we have the 3 local variables value,s,i..next we are creating the instances for the Short class by passing the string and value...inside side Short class we have a suitable constructor to convert string to the short...so input value will be 200 and input1 value is 200

next we are creating the instances for the Integer class by passing the int value and String value similar to the Short class Integer class will have an constructor which can convert String to int using the parseInteger() method...data will contain 129 and data1 will assigned with 200

Posted by Uday Kumar    2015-01-21 15:29:19


ans a.in this program initially we assigned string  value with "200",short s with 128,int i with 129,then s of short object assigned with input,value with input 1,i is converted to integer object and assigned with data,value assigned with data1, then 1st sop prints input 128 and input1 200,2nd sop prints data 129,data1 200

Posted by Maheshwari Natarajan    2015-01-21 16:04:31


b.

Posted by Shubham Bansal    2015-01-22 03:28:46


Ans is Option A.... Execution starts from main() method here we are declaring a local variable "value" and initialized with the value 200 after that we are creating another short local variable "s" and initialized with the value 128 and after that we are declaring variable "i" and initialized with the value 129 after that we are creating object of Short class which is predefined wrapper class and we are initializing this variable "input" with the value 128 by passing "s" as parameter of constructor of class Short so input=128.... after  that we are creating another object of Short class and this time we are passing String value as a parameter to constructor of Short class, here inside this constructor String value will convert to Short value and it will assigned to the Short variable "input1" so the value of  input1=200....                                          After that we are creating Object for class Integer which is also predefined wrapper class and we are passing int value as a parameter of constructor of Class Integer so the variable data will be initialized with the value 129 so data=129.....  after that  again  we are creating Object of Class Integer and passing String value as a parameter to constructor of Class Integer inside this constructor we will conver String value to Integer value so the variable data1 initialized with 200 so data1=200.... after the we are printing input and input1 values as 128 ,200 respectively and data and data1 will print as 129 and 200 so Ans is Option A....    128 200                                                                                  129 200

Posted by Bhagi Bhagyasri    2015-01-22 04:22:24


This dose is now closed and the winners are Maheshwari Natarajan,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Tahir Abbas. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-22 08:32:48


© meritcampus 2019

All Rights Reserved.

Open In App