Java Data types MCQ Questions and Answers

Java Data types MCQ Questions and Answers Here provide Multiple Choice Questions with Answers on Java Data Types. in this post, you can learn and practice java mcq questions of java integer data type, floating data type, character data type, boolean data type, enum data type etc.

Java Data types MCQ Questions and Answers

Integer and Floating Data Types MCQ

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?

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.

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: Only double data type value is returned by all transcendental math functions. Transcendental math functions don’t return int or long. They return double instead of float as double has larger range.

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

Character and Boolean Data Types MCQ

11. 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.

12. 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.

13. 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.

14. 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.

15. 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.

16. What will be the output of the following Java program?

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

17. 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

18. 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

19. 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. It returns true when both the values are true and false otherwise. Since, var1 is defined true and var2 is defined false hence their ‘&’ operator result is false.
OUTPUT:

$ javac booloperators.java
$ java booloperators
false

20. 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

Java Datatypes Enum MCQ

21. What is the order of variables in Enum?

a) Ascending order
b) Descending order
c) Random order
d) Depends on the order() method
Answer: d
Explanation: The order of appearance of variables in Enum is their natural order (the natural order means the order in which they are declared inside Enum type). However, the compareTo() method is implemented to order the variable in ascending order.

22. Can we create an instance of Enum outside of Enum itself?

a) True
b) False
Answer: b
Explanation: No, instances of Enum cannot be created outside of Enum boundary, because Enum does not have a public constructor.

23. What will be the output of the following Java code?

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.

24. 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.

25. 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.

26. 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.

27. 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.

28. 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.

29. Are enums are type-safe?
a) True
b) False
Answer: a
Explanation: Enums are type-safe as they have own name-space.

Be the first to comment

Leave a Reply

Your email address will not be published.


*