Menu
Topics Index
...
`

Creating Literals


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating Literals
What will be the output of the following program?
public class DemoOnLiteral
{
    public static void main(String args[])
    {
        char ch = '\\';
        System.out.print(ch + "Hello India" + ch);
    }
}

A. \\Hello India\\
B. \Hello India\
C. Hello India
D. Compilation Error
E. Runtime Error
Topic: Character Literals 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 b.here we have the char with literal'\\' here we are using  the print the \...\ is used to print the escape sequences(\n,\t etc) here we use the \ to print \ ..so ch will have the \ and next we are appending to the hello india string

Posted by Uday Kumar    2015-01-23 04:45:22


Ans is Option B... Execution starts from main() method here we are creating a character variable "ch" and which is initialized with escape value backslash(\) the representation of escape value is "\\" after that we have println stmt here ch value will print as \ and HelloIndia string is appending to this character ch so the output is \Hello India\....

Posted by Bhagi Bhagyasri    2015-01-23 05:36:46


b

Posted by Abhishek Kumar    2015-01-23 05:46:00


ans is b.

Posted by Shubham Bansal    2015-01-23 06:46:09


ANS IS B (\Hello India\)

Above program uses the concept of "ESCAPE SEQUENCE".

Escape sequences are special Token in java which are used to perform different operations in System.out.println() or System.out.print()."\" is used with another symbols to print as special symbols are not part of aplhabets.

Folowing are some of the Escape sequences :

\ is used for escape sequences in programming languages.

1)\n -prints a newline
2)\ -prints a backslash(\)
3)\" -prints Quotes(")
4)\t -prints a tabulator
5)\b- moves the cursor one back

--- In above program char ch is defined with "\\" i.e. backslash.In system.out.println ch is printed i.e. \ then "HELLO" then again "\"

OUTPUT=\Hello India\

Posted by Mânïshå Mùlchåndânï    2015-01-23 13:47:29


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Mânïshå Mùlchåndânï,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Abhishek Kumar. 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-26 06:51:16


© meritcampus 2019

All Rights Reserved.

Open In App