Literals in Java MCQ Questions and Answers
Table of Contents
Study and learn Literals in Java questions and answers on Literals or Constants. Literals are of type Integer (Decimal, Octal, Hexadecimal and Binary), Real Number (float, double), Character and String. Attend job interviews easily with these MCQs.
Literals in Java
Like any other programming language, Java too supports Data constants or Literals. A literal is simply a constant of data we assign to a variable of a particular data type.
Literals or Constants in java
There are 5 types of Literals or constants in Java language.
- Integer Literals
- Floating Point Literals
- Boolean Literals
- Character Literals
- String Literals
JDK 7 introduced two new features namely Binary Literals and Underscores in number literals.
Underscore in number constants or literals for Readability
You can embed underscore symbols ( _ ) as part of a number constant to increase readability. You should not add Underscore at the beginning or end. Also, you can not put Underscore before or after a DOT or Decimal Point in floating point or double numbers. You can add more than one underscore symbols consecutively. Underscore symbols are removed before processing.
Examples are as below.
int a = 123_456_7; int b = 23_456_56789_1; long c = 24___56__789_12L; int d = 0xef_a_b; //Hexadecimal constant int e = 0b1011_0111; //binary constant int f = 01234_5678; //Octal constant float pp = 34__456.12___23_4; //floating point constant double qq = 455_456.126_234; //double constant
1. Integer Literals
When initializing byte, short, int and long data type variables, we use integer literals. For long data, you can use lower case ‘l’ or upper case ‘L’ at the end if you want.
Integer literals or constants of 4 types.
- Decimal Literal
- Octal Literal
- Hexadecimal Literal
- Binary Literals
A. Decimal Literal
Decimal literals can have digits from 0 to 9. There is no need to prefix the number to specify explicitly.
Example:
int a = 01245621; long b = 987_654_321_0L; short c = 3456; byte bt = 123;
B. Octal Literal
Octal literals can have digits from 0 to 7. You should Prefix the number with a ZERO ‘0’.
Example:
int a = 0345; int b = 0456_789; short c = 0802; //Error. 8 is out of range of Octal. byte bt = 110;
C. Hexadecimal Literal
Hexadecimal literals can have digits from 0 to 9, a, b, c, d, e, f, A, B, C, D, E, F. You should Prefix the number with a ‘0x‘ or ‘0X‘.
Example:
int a = 0x345; int b = 0X456_7__89; long ab = 0xffeeL;
D. Binary Literal
Binary literals can have digits from 0 or 1. You should Prefix the number with a ‘0b‘ or ‘0B‘. JDK 7 supports this literal.
Example:
int a = 0b1011_1010; int b = 0B10__10__11_01; long ab = 0b1101_1100_0101_1111L;
2. Floating Point Literals
Floating point data types namely float and double variables can hold floating point literals. For floats ‘f’ or ‘F’ may be postfixed at the end. For double, you may append ‘d’ or ‘D’ at the end of a number. By default, all real numbers are of ‘double’ data type.
To specify exponents ‘e’ or ‘E’ is used. Exponent represents 10 power some number say 10 power 2 ( = 100). So E is called Power of Ten Exponent
To specify Two power of some number, JDK 7 introduced an exponent which represents 2 instead of 10. The symbol used is ‘p’ or ‘P’. So, P is called Power of Two Exponent. This P format can be used only with Hexadecimal floating point numbers.
Literals in Java
Example:
float a = 123_456.789_0987F; float b = 123456.78678f; float c = 12e23; double d = 345E567d; // E notation double e = 345E-567; double f = 345E+567; float g = 0x23.4567P4; //Hexadecimal float double h = 0X5678p-10; //P notation
3. Boolean Literals
Boolean literal can have only two possible values true or false. TRUE or FALSE are illegal to use. Capitalization is not allowed. Also, 1 is not equal to true and 0 is not equal to false.
Java Literals
Example:
boolean a = false; boolean b = true; boolean c = 0; //error boolean d = -123; //error if( a ) { } // Works if( 1 ) { } // Error. if( TRUE ) { } // Error. if( false ) { } //Works.
4. Character Literals
As Java language supports 16 bit type characters, the number of characters that can be represented has been increased to 65536. You can specify a single character as a character literal to a character variable. To specify a character literal, you need to surround the character with Single Quotes (‘ ‘).
Example:
char a='a'; char b = 'A'; char c = 'ab'; //Error
You can also specify Octal character literal in \ddd notation. ‘d’ is the digit from 0 to 7.
Example:
char a = '\123'; char b = '\567'; char c = '345'; //Error
You can also specify Hexadecimal character literal in \udddd notation. ‘d’ value can be from 0 to 9 and a to f or A to F. These are also called Unicode literals in Java.
Example:
char a = '\uaef0'; char b = '\ua123'; char c = "\u1234"; //Error. Double Quote
5. String Literals
String literal is a group of characters, numbers and symbols surrounded by double quotes. In C language, a string is a character array. In Java language, String is an Object. It has methods to calculate length, reverse string, replace characters and more.
Literals in Java
Example:
String a = "Hello Java"; String b = "Hello\nJava"; //you can use new line String c = "Hello" + " Java"; //Concatenation
Having an understanding of literals improves your coding. You can write better java programs after practising these basics.
[WpProQuiz 54]
Literals in Java MCQ (Important)
1) What is the suffix used to represent a floating point number in Java?
A) r or R
B) f or F
C) fl or FL
D) None of the above
Answer [=] B
Explanation:
float a = 1.345f; float b = 567.345678F;
2) What is the precision after decimal points offered by a float data type in Java?
A) 3 digits
B) 6 digits
C) 10 digits
D) 15 digits
Answer [=] B
Explanation:
float interest = 24.123456f;
3) A real number literal for floating point literal with a missing f or F suffix represents which data type?
A) double
B) float
C) long
D) int
Answer [=] A
Explanation:
float a = 1.23; //error //can not convert from double to float float b = 1.23F // works double c = 1.567; //works
4) What is the suffix used to convert an int literal to long literals in Java?
A) 0l or 0L
B) l or L
C) i or I
D) 0x or 0X
Answer [=] B
Explanation:
int a = 987654; //works int b = 9876543210; //Out of range error long c = 9876543210;//Out of range error long d = 9876543210L; //works
5) A character literal in Java is enclosed within a pair of ___?
A) Square brackets
B) Double Quotes
C) Single Quotes
D) Exclamations
Answer [=] C
Explanation:
char ch='A'; char ch2 = 'b';
6) Which version of Java started offering unsigned int and unsigned long to represent very long numbers?
A) JDK 5
B) JDK 6
C) JDK 7
D) JDK 8
Answer [=] D
Explanation:
You have to use Object version of int and long namely Integer and Long to avail the feature. Using pri-mitive data types, you can not create an un-signed int or un-signed long.
7) Choose a correct statement about Java literal types.
A) Decimal literal uses Base 10 number system.
B) Binary literal uses Base 2 number system.
C) Octal literal uses Base 8 number system.
D) All the above
Answer [=] D
Explanation:
Hexadecimal literal uses Base 16 number system.
8) A String literal in Java is surrounded by a pair of _____?
A) Braces
B) Single Quotes
C) Double Quotes
D) Exclamations
Answer [=] C
Explanation:
String name = "JAVA HERO";
9) Which among the following is not a pri-mitive data type in Java?
A) char
B) String
C) byte
D) short
Answer [=] B
Explanation:
A string is a Class that can handle a string of characters or a group of characters. If the name of the type starts with an Uppercase letter, it is a Class. So it is non-primitive.
10) Which version of Java introduced Hexadecimal floating point numbers?
A) JDK 5
B) JDK 6
C) JDK 7
D) JDK 8
Answer [=] C
Explanation:
float a = 0x3.1p0f; // 3.0625 //3 x p0 = 3 x 2^0 = 3 //(0.1)/16 = 0.0625
11) What are the two floating point notations in Java language?
A) Exponential e or E (10^a)
B) Exponential p or P (2^a)
C) A and B
D) None of the above
Answer [=] C
12) Choose the correct implementation of floating point literals in the exponential form in Java.
A) float a = 12.0e2f; //1200.0
B) float a = 100.0e-2f; // 1.0
C) float a = 123.456e-21f; //1.23456E-19
D) All the above
Answer [=] D
13) Choose the correct usage of boolean literal in the options below.
A) boolean b= false;
B) boolean b= 2<4; //2<4 is true;
C) if(true) { System.out.println(“HELLO”); }
D) All the above
Answer [=] D
14)
What is the output of this Java snippet? int a = 0b111; System.out.println(a);
A) 111
B) 7
C) 8
D) Compiler error
Answer [=] B
Explanation:
1x2^2 + 1x2^1 + 1x2^0 1x4 + 1x2 + 1 4 + 2 + 1
15) Choose the wrong Java code statement below.
A) boolean a = false;
B) boolean a = (5>6)||(4>3);
C) boolean a = 1;
D) boolean a = 4>3?true:false;
Answer [=] C
Explanation:
You can not assign an integer value to a boolean data type. Java does not convert integers to boolean true or false.
16) Choose the wrong Java code statement below.
A) char a =’a’;
B) char a =”ab”;
C) char a =97;
D) char a =’\u0123′;
Answer [=] B
Explanation:
A character variable can hold only one letter that can be represented by UTF-16 Unicode internally. Use only single quotes.
String str = "ab"; //works
17) Choose the wrong representation of a character literal in Octal notation in Java.
A) char ch=’\65′;
B) char ch=’\142′;
C) char ch=’\065′;
D) char ch=’142′;
Answer [=] D
Explanation:
char ch='\142'; //works char ch2 = '\97';//9 is not Octal digit
18)
A Unicode character literal in Java is surrounded by a pair of ___? literal = \ua123
A) Single Quotes
B) Double Quotes
C) Exclamations
D) Backslashes
Answer [=] A
Explanation:
char ch='\ua123';
19) What is the default boolean literal assigned to a boolean variable in Java?
A) true
B) false
C) undefined
D) None of the above
Answer [=] B
Explanation:
Default values are assigned only to the instance variables.
20) What is the default character literal value assigned to a char variable in Java?
A) ‘a’
B) ‘0’
C) ‘\u0000’
D) 0
Answer [=] C
Important MCQ
1) What is Literal in Java?
A) Literal is the value that is given or assigned to a variable.
B) Literal is a data type
C) Literal is similar to String
D) None of the above
Answer [=] A
Explanation:
Examples: 123, 45.67f, ‘C’, “abc”, false
2) What are the types of Literals available in Java language?
A) Integer and Float
B) Character and String
C) Boolean
D) All the above
Answer [=] D
Explanation:
Literals are Data assigned to Pri-mitive data type variables.
3) What are the types of Integer Literals in Java?
A) Decimal Literals
B) Octal and Hexadecimal Literals
C) Binary Literals
D) All the above
Answer [=] D
Explanation:
JDK 7 introduced binary literals to easily set individual bits of a number.
4) Choose correct examples of decimal literals in Java.
A) int a = 12345;
B) int a = 12_3__5;
C) long a = 987____654_3__21L;
D) All the above
Answer [=] D
Explanation:
To represent big numbers, simply append letter ‘l’ or ‘L’ to the number to make it a long integer. This avoids compiler errors saying “out of range”
5) An Octal number is Java is represented with a leading ____?
A) O (Alphabet)
B) 0 (ZERO)
C) 0x
D) 0X
Answer [=] B
Explanation:
Eg. int a=0765;
6) Choose correct ranges for Decimal, Octal and Hexadecimal numbers in Java?
A) Decimal: 0 to 9
B) Octal: 0 to 7
C) Hexadecimal: 0 to 9 and A to F / a to f
D) All the above
Answer [=] D
7) Choose the correct example of Octal Literal in Java?
A) short = 0564;
B) int = 076__45_2;
C) int = 0______11;
D) All the above
Answer [=] D
Explanation:
int = 0______11; // 8^1 * 1 + 8^0 * 1 = 9
8) What is the prefix used to represent Hexadecimal numbers in Java?
A) 0x
B) 0X
C) A and B
D) None of the above
Answer [=] D
Explanation:
int a=0xFEB5; int b=0X9876__45;
9) Choose correct examples of Hexadecimal literals in Java?
A) long a = 0X987654321L;
B) int a = 0x76FE____23;
C) byte b = 0X0__________F;
D) All the above
Answer [=] D
10) Binary literals in Java are introduced with which version of Java?
A) JDK 5
B) JDK 6
C) JDK 6
D) JDK 8
Answer [=] C
11) Underscore symbols in literal numbers are introduced with which version of Java?
A) JDK 5
B) JDK 6
C) JDK 7
D) JDK 8
Answer [=] C
12) What is the prefix used to represent Binary literals in Java?
A) b or B
B) 0b or 0B
C) xB or xb
D) ob or oB
Answer [=] B
Explanation:
ZERO B or ZERO b byte a = 0b00001111; //15 in decimal
13) What is the correct representation of using Binary literals in Java?
A) int a = 0b1010;
B) int a = 0B1011_1010;
C) int a = 0B0______________1;
D) All the above
Answer [=] D
Explanation:
int a = 0B0______________1; //decimal=1 int b = 0b1010; //decimal=10
14) What is the compiler error for improperly using Underscores ( _ ) in literals in Java?
A) Underscores are out of range
B) IllegalUnderscoresException
C) Underscores have to be located within digits
D) Too many Underscores
Answer [=] C
Explanation:
Underscore symbols cannot be used at the beginning and the end of digits of a number.
15) Choose a correct rule for using Underscores in literals of Java language.
A) Underscores cannot come at the end of a number or next to the last digit of a number.
B) Underscores cannot come at the beginning of a number or before the first digit of a number.
C) Underscores cannot come before or after a decimal point in real numbers like float and double.
D) All the above
Answer [=] D
Explanation:
Also, there is no limit on the number of underscores between digits.
16) What is the maximum number of Underscore characters that can be used in between the digits of a numeric literal in Java?
A) 8
B) 16
C) 32
D) No Limit
Answer [=] D
Explanation:
Theoretically, there is no limit on the number of underscores.
17) Java uses UTF-16 Unicode format to represent characters. What is UTF?
A) Universal Transcript Format
B) Universal Transformation Format
C) Universal Technology Format
D) None of the above
Answer [=] B
Explanation:
Unicode contains 65535 characters.
18) What is the name given to character literals in Java that start with a Back Slash character?
A) Back Slash Sequence
B) Slash Sequence
C) Escape Sequence
D) Extended Sequence
Answer [=] C
Explanation:
\b = backspace \n = new line \\ = backslash
19) What is the literal in Java that can be used to test whether an Object of any type is alive or not?
A) alive
B) liveof
C) null
D) 0
Answer [=] C
Explanation:
String a; if(a==null) System.out.println("Object destroyed");
20) What is the common UTF standard used on the Websites for information exchange?
A) UTF 16
B) UTF 8
C) UTF 32
D) None of the above
Answer [=] B
Explanation:
UTF 16 character encoding standard used by Java language is used only by the Windows internal system and JavaScript Library. Unix, Linux and MacOS use UTF-8 encoding standard.