Java Quizs Question & Answers – Integer and Floating Data Types

Q 1. An expression involving byte, int, and literal numbers is promoted to which of these?
A. long
B. int
C. byte
D. float

Show Answer Answer:-B. int
Explanation An expression involving bytes, ints, shorts, literal numbers, the entire expression is promoted to int before any calculation is done.

Q 2. 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

Show Answer Answer:-B. -3.4e+038
Explanation Range of float data type is -(3.4e38) To +(3.4e38)

Q 3. Which data type value is returned by all transcendental math functions?
A. double
B. float
C. int
D. long

Show Answer Answer:-A. double
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.

Q 4. What will be the output of the following Java code?

  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

Show Answer Answer:-C. 16.46666666666667
Explanation None. output:$ javac average.java $ java average 16.46666666666667

Q 5. What will be the output of the following Java statement?

  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. System.out.println(a);
  9. System.out.println(b);
  10. System.out.println(c);
  11. }
  12. }

A. Infinity
B. 0.0
C. NaN
D. all of the mentioned

Show Answer Answer:-D. all of the mentioned
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.

Q 6. What will be the output of the following Java code?

  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

Show Answer Answer:-C. 32
Explanation Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32. output:$ javac increment.java $ java increment 32

Q 7. What will be the output of the following Java code?

  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.56
B. 301
C. 301.5656
D. 301.56560000

Show Answer Answer:- C. 301.5656
Explanation None. output:$ javac area.java $ java area 301.5656

Q 8. 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

Show Answer Answer:-B. -32768 to 32767
Explanation Short occupies 16 bits in memory. Its range is from -32768 to 32767.

Q 9. 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

Show Answer Answer:-A. -128 to 127
Explanation Byte occupies 8 bits in memory. Its range is from -128 to 127.

Q 10. 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

Show Answer Answer:-D. All statements are correct
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.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page