Java Questions & Answers - Integer and Floating Data Types

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

JAVA MCQS

Java Questions & Answers – Integer and Floating Data Types

This Section of our 1000+ Java MCQs focuses on Integer and Floating Datatypes of Java Programming

Language.

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

View Answer
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

View Answer
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

View Answer
4. An expression involving byte, int, and literal numbers is promoted to which of these?

a) int

b) long

c) byte

d) float

View Answer
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

View Answer
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

View Answer
Answer: c
Explanation: None.

7. What is the output of this program?

1. class average {
2. public static void main(String args[])
3. {
4. double num[] = {5.5, 10.1, 11, 12.8, 56.9, 2.5};
5. double result;
6. result = 0;
7. for (int i = 0; i < 6; ++i)
8. result = result + num[i];
9. System.out.print(result/6);
10.  
11. }
12. }

a) 16.34

b) 16.566666644

c) 16.46666666666667

d) 16.46666666666666

View Answer
Answer: c
Explanation: None.
output:
$ javac average.java
$ java average
16.46666666666667

8. What will be the output of these statement?


advertisement

1. class output {
2. public static void main(String args[])
3. {
4. double a, b,c;
5. a = 3.0/0;
6. b = 0/4.0;
7. c=0/0.0;
8.  
9. System.out.println(a);
10. System.out.println(b);
11. System.out.println(c);
12. }
13. }

a) Infinity

b) 0.0

c) NaN

d) all of the mentioned

View Answer
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 is the output of this program?

1. class increment {
2. public static void main(String args[])
3. {
4. int g = 3;
5. System.out.print(++g * 8);
6. }
7. }

a) 25

b) 24

c) 32

d) 33

View Answer
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 is the output of this program?

1. class area {
2. public static void main(String args[])
3. {
4. double r, pi, a;
5. r = 9.8;
6. pi = 3.14;
7. a = pi * r * r;
8. System.out.println(a);
9. }
10. }

a) 301.5656

b) 301

c) 301.56

d) 301.56560000

View Answer
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

View Answer
Answer: d
Explanation: Char occupies 16-bit in memory, so it supports 2^16 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

View Answer
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

View Answer
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

View Answer
Answer: d
Explanation: First 0 to 127 character set in Unicode are same as those of ISO-LATIN-1 and ASCII.
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’

View Answer
Answer: c
Explanation: Boolean can only be assigned true or false literals.

6. What is the output of this program?

1. class array_output {
2. public static void main(String args[])
3. {
4. char array_variable [] = new char[10];
5. for (int i = 0; i < 10; ++i) {
6. array_variable[i] = 'i';
7. System.out.print(array_variable[i] + "" );
8. i++;
9. }
10. }
11. }

a) i i i i i

b) 0 1 2 3 4

c) i j k l m

d) None of the mentioned

View Answer
Answer: a
Explanation: None.
output:
$ javac array_output.java
$ java array_output
i i i i i

7. What is the output of this program?

1. class mainclass {
2. public static void main(String args[])
3. {
4. char a = 'A';
5. a++;
6. System.out.print((int)a);
7. }
8. }

a) 66

b) 67

c) 65
d) 64

View Answer
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 is the output of this program?


advertisement

1. class mainclass {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. if (var1)
7. System.out.println(var1);
8. else
9. System.out.println(var2);
10. }
11. }

a) 0

b) 1

c) true

d) false

View Answer
Answer: c
Explanation: None.
output:
$ javac mainclass.java
$ java mainclass
true

9. What is the output of this program?

1. class booloperators {
2. public static void main(String args[])
3. {
4. boolean var1 = true;
5. boolean var2 = false;
6. System.out.println((var1 & var2));
7. }
8. }

a) 0

b) 1

c) true
d) false

View Answer
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 is the output of this program?

1. class asciicodes {
2. public static void main(String args[])
3. {
4. char var1 = 'A';
5. char var2 = 'a';
6. System.out.println((int)var1 + " " + (int)var2);
7. }
8. }

a) 162

b) 65 97

c) 67 95

d) 66 98

View Answer
Answer: b
Explanation: ASCII code for ‘A’ is 65 and for ‘a’ is 97.
output:
$ javac asciicodes.java
$ java asciicodes
65 97

You might also like