Topics Index

Java online tutorial2220

If you need explanation Read this topic

If you need Answer Take test on this topic

Java online tutorial2220
What will be the output of the following program?
public class Campus {
    public static void main(String[] args) {
        char data[]={'M','E','R','I','T',' ','C','A','M','P','U','S'};
        String output = String.valueOf(data);
        String str = data;

C. Compilation Error
D. Runtime Error
Topic: Data Conversion Using valueOf 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.

Ans is C

here we have the char array with the some elements and we are passing that array to the valueOf() which returns the string for that output will assigned with the "MERITCAMPUS"....valueOf() will takes the different type pf arguments..booleans,int,etc..
next we are assigning the char array directly to the str so here compiler will raise an error saying incompatable types..we cant copy one array into string direclt we should use valueOf() or String() Constructors..

Posted by Uday Kumar    2015-01-23 14:19:57

ANS IS C (Compilation Error)

There is one Errors in Program:

error: incompatible types: char[] cannot be converted to String
        String s=d;

--Firstly,Array of characters is declared with characters of meritcampus.

--Then That array of characters is concerted into String using following Static method.

--->static String valueOf(char[] data)
Returns the string representation of the char array argument.

--Another version of above method.

-->static String valueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.

--After that that character array is assigned directly to String Variable which is incompatable.Character array can nit be directly  assigned to String.Above method is used to convert char array into string.

So,There is a comiplation Error stating Incompatible.

Posted by Mânïshå Mùlchåndânï    2015-01-23 14:39:53

ans is C  execution starts with main() here data array is initialized of type char .output of type string is declared as string.valueOf(data) it returns  returns the string representation of the char array argument. str of type string is directly assigned to the char type data here an error is raised  here array char array cannot be directly copied into it leads to compilation error

Posted by Ramya Reddy Addulla    2015-01-23 14:42:23

c. is ans

Posted by Shubham Bansal    2015-01-23 15:54:45

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï, Ramya Reddy Addulla,  for 'Best Comment' and Mânïshå Mùlchåndânï for the 'Popular Comment'. The 'lucky liker' is ?????????? ?????. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-26 07:03:30

© meritcampus 2019

All Rights Reserved.

Open In App