Java MCQ on Data Types, Variables and Arrays
Java MCQ on Data Types, Variables and Arrays |
1
What is the range of short data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
==================================
Answer: b
Explanation: Short occupies 16 bits in memory
Its range is from -32768 to 32767
2
What is the range of byte data type in Java?
a) -128 to 127
b) -32768 to 32767
c) -2147483648 to 2147483647
d) None of the mentioned
==================================
Answer: a
Explanation: Byte occupies 8 bits in memory
Its range is from -128 to 127
3
Which of the following are legal lines of Java code?
Subscribe Now: Java Newsletter | Important Subjects Newsletters
1
int w = (int)888
8;
2
byte x = (byte)100L;
3
long y = (byte)100;
4
byte z = (byte)100L;
a) 1 and 2
b) 2 and 3
c) 3 and 4
d) All statements are correct
==================================
Answer: d
Explanation: Statements (1), (2), (3), and (4) are correct
(1) is correct because when a floating-point number (a double in this case) is cast to an int, it simply loses the digits after the decimal
(2) and (4) are correct because a long can be cast into a byte
If the long is over 127, it loses its most significant (leftmost) bits
(3) actually works, even though a cast is not necessary, because a long can store a byte
Participate in Java Programming Certification Contest of the Month Now!
4
An expression involving byte, int, and literal numbers is promoted to which of these?
a) int
b) long
c) byte
d) float
==================================
Answer: a
Explanation: An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any calculation is done
5
Which of these literals can be contained in float data type variable?
a) -1
7e+308
b) -3
4e+038
c) +1
7e+308
d) -3
4e+050
==================================
Answer: b
Explanation: Range of float data type is -(3
4e38) To +(3
4e38)
6
Which data type value is returned by all transcendental math functions?
a) int
b) float
c) double
d) long
==================================
Answer: c
Explanation: None
7
What will be the output of the following Java code?
class average {
public static void main(String args[])
{
double num[] = {5
5, 10
1, 11, 12
8, 56
9, 2
5};
double result;
result = 0;
for (int i = 0; i < 6; ++i)
result = result + num[i];
System
out
print(result/6);
}
}
a) 16
34
b) 16
566666644
c) 16
46666666666667
d) 16
46666666666666
==================================
Answer: c
Explanation: None
output:
$ javac average
java
$ java average
16
46666666666667
8
What will be the output of the following Java statement?
class output {
public static void main(String args[])
{
double a, b,c;
a = 3
0/0;
b = 0/4
0;
c=0/0
0;
System
out
println(a);
System
out
println(b);
System
out
println(c);
}
}
a) Infinity
b) 0
0
c) NaN
d) all of the mentioned
==================================
Answer: d
Explanation: For floating point literals, we have constant value to represent (10/0
0) infinity either positive or negative and also have NaN (not a number for undefined like 0/0
0), but for the integral type, we don’t have any constant that’s why we get an arithmetic exception
9
What will be the output of the following Java code?
class increment {
public static void main(String args[])
{
int g = 3;
System
out
print(++g * 8);
}
}
a) 25
b) 24
c) 32
d) 33
==================================
Answer: c
Explanation: Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32
output:
$ javac increment
java
$ java increment
32
10
What will be the output of the following Java code?
class area {
public static void main(String args[])
{
double r, pi, a;
r = 9
8;
pi = 3
14;
a = pi * r * r;
System
out
println(a);
}
}
a) 301
5656
b) 301
c) 301
56
d) 301
56560000
==================================
Answer: a
Explanation: None
output:
$ javac area
java
$ java area
301
5656
1
What is the numerical range of a char data type in Java?
a) -128 to 127
b) 0 to 256
c) 0 to 32767
d) 0 to 65535
==================================
Answer: d
Explanation: Char occupies 16-bit in memory, so it supports 216 i:e from 0 to 65535
2
Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned
==================================
Answer: c
Explanation: Unicode defines fully international character set that can represent all the characters found in all human languages
Its range is from 0 to 65536
3
Which of these values can a boolean variable contain?
a) True & False
b) 0 & 1
c) Any integer value
d) true
==================================
Answer: a
Explanation: Boolean variable can contain only one of two possible values, true and false
4
Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1
==================================
Answer: d
Explanation: First 0 to 127 character set in Unicode are same as those of ISO-LATIN-1 and ASCII
Sanfoundry Certification Contest of the Month is Live
100+ Subjects
Participate Now!
5
Which one is a valid declaration of a boolean?
a) boolean b1 = 1;
b) boolean b2 = ‘false’;
c) boolean b3 = false;
d) boolean b4 = ‘true’
==================================
Answer: c
Explanation: Boolean can only be assigned true or false literals
6
What will be the output of the following Java program?
Check this: Programming MCQs | Java Books
class array_output {
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = 'i';
System
out
print(array_variable[i] + "" );
i++;
}
}
}
a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned
==================================
Answer: a
Explanation: None
output:
$ javac array_output
java
$ java array_output
i i i i i
7
What will be the output of the following Java program?
class mainclass {
public static void main(String args[])
{
char a = 'A';
a++;
System
out
print((int)a);
}
}
a) 66
b) 67
c) 65
d) 64
==================================
Answer: a
Explanation: ASCII value of ‘A’ is 65, on using ++ operator character value increments by one
output:
$ javac mainclass
java
$ java mainclass
66
8
What will be the output of the following Java program?
class mainclass {
public static void main(String args[])
{
boolean var1 = true;
boolean var2 = false;
if (var1)
System
out
println(var1);
else
System
out
println(var2);
}
}
a) 0
b) 1
c) true
d) false
==================================
Answer: c
Explanation: None
output:
$ javac mainclass
java
$ java mainclass
true
9
What will be the output of the following Java code?
class booloperators {
public static void main(String args[])
{
boolean var1 = true;
boolean var2 = false;
System
out
println((var1 & var2));
}
}
a) 0
b) 1
c) true
d) false
==================================
Answer: d
Explanation: boolean ‘&’ operator always returns true or false
var1 is defined true and var2 is defined false hence their ‘&’ operator result is false
output:
$ javac booloperators
java
$ java booloperators
false
10
What will be the output of the following Java code?
class asciicodes {
public static void main(String args[])
{
char var1 = 'A';
char var2 = 'a';
System
out
println((int)var1 + " " + (int)var2);
}
}
a) 162
b) 65 97
c) 67 95
d) 66 98
==================================
Answer: b
Explanation: ASCII code for ‘A’ is 65 and for ‘a’ is 97
output:
$ javac asciicodes
java
$ java asciicodes
65 97
1
How to format date from one form to another?
a) SimpleDateFormat
b) DateFormat
c) SimpleFormat
d) DateConverter
==================================
Answer: a
Explanation: SimpleDateFormat can be used as
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-mm-dd'T'hh:MM:ss");
String nowStr = sdf
format(now);
System
out
println("Current Date: " + );
2
How to convert Date object to String?
a)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf
parse(new Date());
Note: Join free Sanfoundry classes at Telegram or Youtube
b)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf
format(new Date());
c)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date()
parse();
Take Java Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
d)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date()
format();
==================================
Answer: b
Explanation: SimpleDateFormat takes a string containing pattern
sdf
format converts the Date object to String
3
How to convert a String to a Date object?
a)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf
parse(new Date());
b)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
sdf
format(new Date());
c)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date()
parse();
d)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");
new Date()
format();
==================================
Answer: a
Explanation: SimpleDateFormat takes a string containing pattern
sdf
parse converts the String to Date object
4
Is SimpleDateFormat thread safe?
a) True
b) False
==================================
Answer: b
Explanation: SimpleDateFormat is not thread safe
In the multithreaded environment, we need to manage threads explicitly
5
How to identify if a timezone is eligible for DayLight Saving?
a) useDaylightTime() of Time class
b) useDaylightTime() of Date class
c) useDaylightTime() of TimeZone class
d) useDaylightTime() of DateTime class
==================================
Answer: c
Explanation: public abstract boolean useDaylightTime() is provided in TimeZone class
6
What is the replacement of joda time library in java 8?
a) java
time (JSR-310)
b) java
date (JSR-310)
c) java
joda
d) java
jodaTime
==================================
Answer: a
Explanation: In java 8, we are asked to migrate to java
time (JSR-310) which is a core part of the JDK which replaces joda library project
7
How is Date stored in database?
a) java
sql
Date
b) java
util
Date
c) java
sql
DateTime
d) java
util
DateTime
==================================
Answer: a
Explanation: java
sql
Date is the datatype of Date stored in database
8
What does LocalTime represent?
a) Date without time
b) Time without Date
c) Date and Time
d) Date and Time with timezone
==================================
Answer: b
Explanation: LocalTime of joda library represents time without date
9
How to get difference between two dates?
a) long diffInMilli = java
time
Duration
between(dateTime1, dateTime2)
toMillis();
b) long diffInMilli = java
time
difference(dateTime1, dateTime2)
toMillis();
c) Date diffInMilli = java
time
Duration
between(dateTime1, dateTime2)
toMillis();
d) Time diffInMilli = java
time
Duration
between(dateTime1, dateTime2)
toMillis();
==================================
Answer: a
Explanation: Java 8 provides a method called between which provides Duration between two times
10
How to get UTC time?
a) Time
getUTC();
b) Date
getUTC();
c) Instant
now();
d) TimeZone
getUTC();
==================================
Answer: c
Explanation: In java 8, Instant
now() provides current time in UTC/GMT
1
Which of these is long data type literal?
a) 0x99fffL
b) ABCDEFG
c) 0x99fffa
d) 99671246
==================================
Answer: a
Explanation: Data type long literals are appended by an upper or lowercase L
0x99fffL is hexadecimal long literal
2
Which of these can be returned by the operator &?
a) Integer
b) Boolean
c) Character
d) Integer or Boolean
==================================
Answer: d
Explanation: We can use binary ampersand operator on integers/chars (and it returns an integer) or on booleans (and it returns a boolean)
3
Literals in java must be appended by which of these?
a) L
b) l
c) D
d) L and I
==================================
Answer: d
Explanation: Data type long literals are appended by an upper or lowercase L
Note: Join free Sanfoundry classes at Telegram or Youtube
4
Literal can be of which of these data types?
a) integer
b) float
c) boolean
d) all of the mentioned
==================================
Answer: d
Explanation: None
5
Which of these can not be used for a variable name in Java?
a) identifier
b) keyword
c) identifier & keyword
d) none of the mentioned
==================================
Answer: b
Explanation: Keywords are specially reserved words which can not be used for naming a user defined variable, example: class, int, for etc
Take Java Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
6
What will be the output of the following Java program?
class evaluate
{
public static void main(String args[])
{
int a[] = {1,2,3,4,5};
int d[] = a;
int sum = 0;
for (int j = 0; j < 3; ++j)
sum += (a[j] * d[j + 1]) + (a[j + 1] * d[j]);
System
out
println(sum);
}
}
a) 38
b) 39
c) 40
d) 41
==================================
Answer: c
Explanation: None
output:
$ javac evaluate
java
$ java evaluate
40
7
What will be the output of the following Java program?
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i) {
array_variable[i] = i/2;
array_variable[i]++;
System
out
print(array_variable[i] + " ");
i++;
}
}
}
a) 0 2 4 6 8
b) 1 2 3 4 5
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
==================================
Answer: b
Explanation: When an array is declared using new operator then all of its elements are initialized to 0 automatically
for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop
output:
$ javac array_output
java
$ java array_output
1 2 3 4 5
8
What will be the output of the following Java program?
class variable_scope
{
public static void main(String args[])
{
int x;
x = 5;
{
int y = 6;
System
out
print(x + " " + y);
}
System
out
println(x + " " + y);
}
}
a) 5 6 5 6
b) 5 6 5
c) Runtime error
d) Compilation error
==================================
Answer: d
Explanation: Second print statement doesn’t have access to y , scope y was limited to the block defined after initialization of x
output:
$ javac variable_scope
java
Exception in thread "main" java
lang
Error: Unresolved compilation problem: y cannot be resolved to a variable
9
Which of these is an incorrect string literal?
a) “Hello World”
b) “Hello\nWorld”
c) “\”Hello World\””
d)
"Hello
world"
==================================
Answer: d
Explanation: All string literals must begin and end in the same line
10
What will be the output of the following Java program?
class dynamic_initialization
{
public static void main(String args[])
{
double a, b;
a = 3
0;
b = 4
0;
double c = Math
sqrt(a * a + b * b);
System
out
println(c);
}
}
a) 5
0
b) 25
0
c) 7
0
d) Compilation Error
==================================
Answer: a
Explanation: Variable c has been dynamically initialized to square root of a * a + b * b, during run time
1
Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type
b) The destination type is larger than source type
c) The destination type can be larger or smaller than source type
d) None of the mentioned
==================================
Answer: b
Explanation: None
2
What is the prototype of the default constructor of this Java class?
public class prototype { }
a) prototype( )
b) prototype(void)
c) public prototype(void)
d) public prototype( )
==================================
Answer: d
Explanation: None
Subscribe Now: Java Newsletter | Important Subjects Newsletters
3
What will be the error in the following Java code?
Become Top Ranker in Java Programming Now!
byte b = 50;
b = b * 50;
a) b cannot contain value 100, limited by its range
b) * operator has converted b * 50 into int, which can not be converted to byte without casting
c) b cannot contain value 50
d) No error in this code
==================================
Answer: b
Explanation: While evaluating an expression containing int, bytes or shorts, the whole expression is converted to int then evaluated and the result is also of type int
4
If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?
a) long
b) int
c) double
d) float
==================================
Answer: c
Explanation: If any operand is double the result of an expression is double
5
What is Truncation is Java?
a) Floating-point value assigned to an integer type
b) Integer value assigned to floating type
c) Floating-point value assigned to an Floating type
d) Integer value assigned to floating type
==================================
Answer: a
Explanation: None
6
What will be the output of the following Java code?
class char_increment
{
public static void main(String args[])
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
System
out
println(c1 + " " + c2);
}
}
a) E U
b) U E
c) V E
d) U F
==================================
Answer: a
Explanation: Operator ++ increments the value of character by 1
c1 and c2 are given values D and 84, when we use ++ operator their values increments by 1, c1 and c2 becomes E and U respectively
output:
$ javac char_increment
java
$ java char_increment
E U
7
What will be the output of the following Java code?
class conversion
{
public static void main(String args[])
{
double a = 295
04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System
out
println(c + " " + d);
}
}
a) 38 43
b) 39 44
c) 295 300
d) 295
04 300
==================================
Answer: b
Explanation: Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable
b contains 300 which is larger than byte’s range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44
output:
$ javac conversion
java
$ java conversion
39 44
8
What will be the output of the following Java code?
class A
{
final public int calculate(int a, int b) { return 1; }
}
class B extends A
{
public int calculate(int a, int b) { return 2; }
}
public class output
{
public static void main(String args[])
{
B object = new B();
System
out
print("b is " + b
calculate(0, 1));
}
}
a) b is : 2
b) b is : 1
c) Compilation Error
d) An exception is thrown at runtime
==================================
Answer: c
Explanation: The code does not compile because the method calculate() in class A is final and so cannot be overridden by method of class b
9
What will be the output of the following Java program, if we run as “java main_arguments 1 2 3”?
class main_arguments
{
public static void main(String [] args)
{
String [][] argument = new String[2][2];
int x;
argument[0] = args;
x = argument[0]
length;
for (int y = 0; y < x; y++)
System
out
print(" " + argument[0][y]);
}
}
a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3
==================================
Answer: d
Explanation: In argument[0] = args;, the reference variable arg[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements
Output:
$ javac main_arguments
java
$ java main_arguments
1 2 3
10
What will be the output of the following Java program?
class c
{
public void main( String[] args )
{
System
out
println( "Hello" + args[0] );
}
}
a) Hello c
b) Hello
c) Hello world
d) Runtime Error
==================================
Answer: d
Explanation: A runtime error will occur owning to the main method of the code fragment not being declared static
1
Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc
==================================
Answer: c
Explanation: Operator new allocates a block of memory specified by the size of an array, and gives the reference of memory allocated to the array variable
2
Which of these is an incorrect array declaration?
a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
==================================
Answer: d
Explanation: Operator new must be succeeded by array type and array size
3
What will be the output of the following Java code?
Note: Join free Sanfoundry classes at Telegram or Youtube
int arr[] = new int [5];
System
out
print(arr);
a) 0
b) value stored in arr[0]
c) 00000
d) Class name@ hashcode in hexadecimal form
==================================
Answer: d
Explanation: If we trying to print any reference variable internally, toString() will be called which is implemented to return the String in following form:
classname@hashcode in hexadecimal form
4
Which of these is an incorrect Statement?
a) It is necessary to use new operator to initialize an array
b) Array can be initialized using comma separated expressions surrounded by curly braces
c) Array can be initialized when they are declared
d) None of the mentioned
==================================
Answer: a
Explanation: Array can be initialized using both new and comma separated expressions surrounded by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};
Take Java Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
5
Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned
==================================
Answer: a
Explanation: None
6
What will be the output of the following Java code?
class array_output
{
public static void main(String args[])
{
int array_variable [] = new int[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = i;
System
out
print(array_variable[i] + " ");
i++;
}
}
}
a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10
==================================
Answer: a
Explanation: When an array is declared using new operator then all of its elements are initialized to 0 automatically
for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop
output:
$ javac array_output
java
$ java array_output
0 2 4 6 8
7
What will be the output of the following Java code?
class multidimention_array
{
public static void main(String args[])
{
int arr[][] = new int[3][];
arr[0] = new int[1];
arr[1] = new int[2];
arr[2] = new int[3];
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
arr[i][j] = j + 1;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < i + 1; ++j)
sum + = arr[i][j];
System
out
print(sum);
}
}
a) 11
b) 10
c) 13
d) 14
==================================
Answer: b
Explanation: arr[][] is a 2D array, array has been allotted memory in parts
1st row contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements
each element of array is given i + j value in loop
sum contains addition of all the elements of the array
output:
$ javac multidimention_array
java
$ java multidimention_array
10
8
What will be the output of the following Java code?
class evaluate
{
public static void main(String args[])
{
int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
int n = 6;
n = arr[arr[n] / 2];
System
out
println(arr[n] / 2);
}
}
a) 3
b) 0
c) 6
d) 1
==================================
Answer: d
Explanation: Array arr contains 10 elements
n contains 6 thus in next line n is given value 3 printing arr[3]/2 i:e 3/2 = 1 because of int Value, by int values there is no rest
If this values would be float the result would be 1
5
output:
$ javac evaluate
java
$ java evaluate
1
9
What will be the output of the following Java code?
class array_output
{
public static void main(String args[])
{
char array_variable [] = new char[10];
for (int i = 0; i < 10; ++i)
{
array_variable[i] = 'i';
System
out
print(array_variable[i] + "");
}
}
}
a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i
==================================
Answer: d
Explanation: None
output:
$ javac array_output
java
$ java array_output
i i i i i i i i i i
10
What will be the output of the following Java code?
class array_output
{
public static void main(String args[])
{
int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
int sum = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3 ; ++j)
sum = sum + array_variable[i][j];
System
out
print(sum / 5);
}
}
a) 8
b) 9
c) 10
d) 11
==================================
Answer: b
1
What is the order of variables in Enum?
a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method
==================================
Answer: a
Explanation: The compareTo() method is implemented to order the variable in ascending order
2
Can we create an instance of Enum outside of Enum itself?
a) True
b) False
==================================
Answer: b
Explanation: Enum does not have a public constructor
3
What will be the output of the following Java code?
Subscribe Now: Java Newsletter | Important Subjects Newsletters
enum Season
{
WINTER, SPRING, SUMMER, FALL
};
System
out
println(Season
WINTER
ordinal());
a) 0
b) 1
c) 2
d) 3
==================================
Answer: a
Explanation: ordinal() method provides number to the variables defined in Enum
Get Free Certificate of Merit in Java Programming Now!
4
If we try to add Enum constants to a TreeSet, what sorting order will it use?
a) Sorted in the order of declaration of Enums
b) Sorted in alphabetical order of Enums
c) Sorted based on order() method
d) Sorted in descending order of names of Enums
==================================
Answer: a
Explanation: Tree Set will sort the values in the order in which Enum constants are declared
5
What will be the output of the following Java code snippet?
class A
{
}
enum Enums extends A
{
ABC, BCD, CDE, DEF;
}
a) Runtime Error
b) Compilation Error
c) It runs successfully
d) EnumNotDefined Exception
==================================
Answer: b
Explanation: Enum types cannot extend class
6
What will be the output of the following Java code snippet?
enum Levels
{
private TOP,
public MEDIUM,
protected BOTTOM;
}
a) Runtime Error
b) EnumNotDefined Exception
c) It runs successfully
d) Compilation Error
==================================
Answer: d
Explanation: Enum cannot have any modifiers
They are public, static and final by default
7
What will be the output of the following Java code snippet?
enum Enums
{
A, B, C;
private Enums()
{
System
out
println(10);
}
}
public class MainClass
{
public static void main(String[] args)
{
Enum en = Enums
B;
}
}
a)
10
10
10
b) Compilation Error
c)
10
10
d) Runtime Exception
==================================
Answer: a
Explanation: The constructor of Enums is called which prints 10
8
Which method returns the elements of Enum class?
a) getEnums()
b) getEnumConstants()
c) getEnumList()
d) getEnum()
==================================
Answer: b
Explanation: getEnumConstants() returns the elements of this enum class or null if this Class object does not represent an enum type
9
Which class does all the Enums extend?
a) Object
b) Enums
c) Enum
d) EnumClass
==================================
Answer: c
Explanation: All enums implicitly extend java
lang
Enum
Since Java does not support multiple inheritance, an enum cannot extend anything else
10
Are enums are type-safe?
a) True
b) False
==================================
Answer: a
Explanation: Enums are type-safe as they have own name-space
1
Which of the following is the advantage of BigDecimal over double?
a) Syntax
b) Memory usage
c) Garbage creation
d) Precision
==================================
Answer: d
Explanation: BigDecimal has unnatural syntax, needs more memory and creates a great amount of garbage
But it has a high precision which is useful for some calculations like money
2
Which of the below data type doesn’t support overloaded methods for +,-,* and /?
a) int
b) float
c) double
d) BigDecimal
==================================
Answer: d
Explanation: int, float, double provide overloaded methods for +,-,* and /
BigDecimal does not provide these overloaded methods
3
What will be the output of the following Java code snippet?
Note: Join free Sanfoundry classes at Telegram or Youtube
double a = 0
02;
double b = 0
03;
double c = b - a;
System
out
println(c);
BigDecimal _a = new BigDecimal("0
02");
BigDecimal _b = new BigDecimal("0
03");
BigDecimal _c = b
subtract(_a);
System
out
println(_c);
a)
0
009999999999999998
0
01
Take Java Programming Tests Now!
b)
0
01
0
009999999999999998
c)
0
01
0
01
d)
0
009999999999999998
0
009999999999999998
==================================
Answer: a
Explanation: BigDecimal provides more precision as compared to double
Double is faster in terms of performance as compared to BigDecimal
4
What is the base of BigDecimal data type?
a) Base 2
b) Base 8
c) Base 10
d) Base e
==================================
Answer: c
Explanation: A BigDecimal is n*10^scale where n is an arbitrary large signed integer
Scale can be thought of as the number of digits to move the decimal point to left or right
5
What is the limitation of toString() method of BigDecimal?
a) There is no limitation
b) toString returns null
c) toString returns the number in expanded form
d) toString uses scientific notation
==================================
Answer: d
Explanation: toString() of BigDecimal uses scientific notation to represent numbers known as canonical representation
We must use toPlainString() to avoid scientific notation
6
Which of the following is not provided by BigDecimal?
a) scale manipulation
b) + operator
c) rounding
d) hashing
==================================
Answer: b
Explanation: toBigInteger() converts BigDecimal to a BigInteger
toBigIntegerExact() converts this BigDecimal to a BigInteger by checking for lost information
7
BigDecimal is a part of which package?
a) java
lang
b) java
math
c) java
util
d) java
io
==================================
Answer: b
Explanation: BigDecimal is a part of java
math
This package provides various classes for storing numbers and mathematical operations
8
What is BigDecimal
ONE?
a) wrong statement
b) custom defined statement
c) static variable with value 1 on scale 10
d) static variable with value 1 on scale 0
==================================
Answer: d
Explanation: BigDecimal
ONE is a static variable of BigDecimal class with value 1 on scale 0
9
Which class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal?
a) MathContext
b) MathLib
c) BigLib
d) BigContext
==================================
Answer: a
Explanation: MathContext class is a library of functions to perform arithmetic operations of BigInteger and BigDecimal