Showing posts with label Literals. Show all posts

What is the output of this program ?

class Literal2
{
public static void main(String[] args)
{
int i=0786;
System.out.println(i);
}
}
/*
Output:- CE[Integer number too large]
Explanation:- In octal number, only allowed digits are 0 to 7.
*/

Read more »

What is the output of this program?

class Literal1
{
public static void main(String[] args)
{
int i=012;
System.out.println(i);
}
}
/*
Output:- 10
Explanation:- A decimal number prefixed with zero(0) is called octal literal. If we print
any literal value that will print in decimal format.
*/

Read more »

What is the output of this program?

class Literal27
{
public static void main(String[] args)
{
int i=1__2___3____4;
System.out.println(i);
}
}
/*
Output:- 1234
Explanation:- We can write any number of underscore between the digits.
*/

Read more »

What is the output of this program?

class Literal26
{
public static void main(String[] args)
{
char c=6_5;
double d=1_26_520.72_25;
System.out.println(c);
System.out.println(d);
}
}
/*
Output:- A 126520.7225
Explanation:- Underscore can be used in any literals except boolean literal.
*/
Read more »

What is the output of this program?

class Literal25
{
public static void main(String[] args)
{
int i=_1_23_456;
System.out.println(i);
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- From 1.7 version of java we can underscore(_) symbol in numeric literals but underscore must be in between the digits.
*/

Read more »

What is the output of this program?

class Literal24
{
public static void main(String[] args)
{
int i=1_23_456;
System.out.println(i);
}
}
/*
Output:- 123456
Explanation:- From 1.7 version of java we can underscore(_) symbol in numeric literals.
At the time of compilation these underscore symbol will be removed automatically. The main
advantage of this approach is readability of the code will be improved.
*/
Read more »

What is the output of this program?

class Literal23
{
public static void main(String[] args)
{
char c=0b1000001;
System.out.println(c);
}
}
/*
Output:- A
Explanation:- Until java 1.6 version we can specify literal value only for decimal,octal,hexadecimal
literals but from 1.7 version onwards we can specify literal value even in binary form also.
but literal value should be prefixed with "0b" or "0B" and allowed digits are only 0 and 1.
*/
Read more »

What is the output of this program?

class Literal22
{
public static void main(String[] args)
{
char c1='\n';
char c2='\t';
System.out.println(c1);
System.out.println(c2);
}
}
/*
Output:- new line printed nothing output
Explanation:- Every escape character Java is valid char literal.
There are 8 escape characters[\n,\t,\r,\b,\f,\',\",\\] in Java.
*/
Read more »

What is the output of this program?

class Literal21
{
public static void main(String[] args)
{
char c=\u0041;
System.out.println(c);
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- UNICODE literal must be enclosed within single quote.
*/
Read more »

What is the output of this program?

class Literal20
{
public static void main(String[] args)
{
char c='\u0041';
System.out.println(c);
}
}
/*
Output:- A
Explanation:- We can assign a char literal in UNICODE representation which is
nothing but '\ uXXXX' where XXXX is hexadecimal number.
*/

Read more »

What is the output of this program?

class Literal19
{
public static void main(String[] args)
{
char c=65536;
System.out.println(c);
}
}
/*
Output:- CE[possible loss of precision]
Explanation:- The allowed range in char data type is 0 to 65535
*/
Read more »

What is the output of this program?

class Literal18
{
public static void main(String[] args)
{
char c1=65;
char c2=0102;
char c3=0x43;
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
}
}
/*
Output:- A B C
Explanation:- We can assign a  integral, octal, hexadecimal literal into
char data type which represents unicode value of that character.
*/

Read more »

What is the output of this program?

class Literal17
{
public static void main(String[] args)
{
char c='ab';
System.out.println(c);
}
}
/*
Output:- CE[unclosed character literal-2, not a statement]
Explanation:- In char literal only one character is acceptable within single quote.
*/

Read more »

What is the output of this program?

class Literal16
{
public static void main(String[] args)
{
char c="a";
System.out.println(c);
}
}
/*
Output:- CE[incompatible types]
Explanation:- String literal cannot assigned into a char data type.
*/

Read more »

What is the output of this program?

class Literal15
{
public static void main(String[] args)
{
char c=a;
System.out.println(c);
}
}
/*
Output:- CE[Cannot find symbol]
Explanation:- If we are trying to assign a char literal then it must be enclosed within single quotes.
*/

Read more »

What is the output of this program?

class Literal14
{
public static void main(String[] args)
{
char c='a';
System.out.println(c);
}
}
/*
Output:- a
Explanation:- We can represent a char literal as a single character within single quotes.
*/

Read more »

What is the output of this program?

class Literal13
{
public static void main(String[] args)
{
boolean b="true";
System.out.println(b);
}
}
/*
Output:- CE[incompatible types]
Explanation:- The only allowed value for boolean data type are "true" and "false" and it must
be in lower case letter. If we are trying to assign except these two values then
it will rise an error of incompatible types.
*/

Read more »

What is the output of this program?

class Literal12
{
public static void main(String[] args)
{
boolean b=True;
System.out.println(b);
}
}
/*
Output:- CE[cannot find symbol]
Explanation:- The only allowed value for boolean data type are "true" and "false" and it must
be in lower case letter.

*/

Read more »

What is the output of this program?

class Literal11
{
public static void main(String[] args)
{
float d=1.2e3;
System.out.println(d);
}
}
/*
Output:- CE[possible loss of precision]
Explanation:- We can also specify floating point literal in exponential form(scientific notation).
And this notation is also treated as double literal. And double literal cannot assigned
directly to the float data type.

*/
Read more »

What is the output of this program?

class Literal10
{
public static void main(String[] args)
{
double d=1.2e3;
System.out.println(d);
}
}
/*
Output:- 1200.0
Explanation:- We can also specify floating point literal in exponential form(scientific notation).
And this notation is also treated as double literal.

*/

Read more »