50 Java MCQs: Understanding Data Types and Variables
This set of 50 multiple-choice questions covers the basics of data types and variables in Java. Each question is designed to test…
50 Java MCQs: Understanding Data Types and Variables
This set of 50 multiple-choice questions covers the basics of data types and variables in Java. Each question is designed to test fundamental understanding, with answers and explanations provided to aid learning.

1. What is the default value of a boolean variable in Java?
A) true
B) false
C) 0
D) null
Answer: B) false
Explanation: In Java, the default value of a boolean variable is false.
2. Which of the following is not a primitive data type in Java?
A) int
B) float
C) String
D) char
Answer: C) String
Explanation: String is a reference type, while int, float, and char are primitive data types.
3. Which data type is used to store a character in Java?
A) String
B) char
C) Character
D) byte
Answer: B) char
Explanation: The char data type is used to store a single character in Java.
4. What is the size of an int data type in Java?
A) 8 bits
B) 16 bits
C) 32 bits
D) 64 bits
Answer: C) 32 bits
Explanation: An int data type in Java occupies 32 bits (4 bytes) of memory.
5. Which of the following data types can hold the largest range of values?
A) byte
B) short
C) int
D) long
Answer: D) long
Explanation: The long data type can hold the largest range of values among the given options, as it is 64 bits (8 bytes).
6. What is the default value of a char variable in Java?
A) 0
B) null
C) ''
D) '\u0000'
Answer: D) '\u0000'
Explanation: The default value of a char variable is the null character '\u0000'.
7. Which of the following declarations is correct for a float variable?
A) float f = 5.5;
B) float f = 5.5f;
C) float f = (float)5.5;
D) Both B and C
Answer: D) Both B and C
Explanation: float literals must have an f or F suffix or be explicitly cast to float.
8. How many bytes does a double data type occupy in Java?
A) 4
B) 8
C) 16
D) 32
Answer: B) 8
Explanation: A double data type in Java occupies 8 bytes (64 bits) of memory.
9. What will be the output of the following code?
int a = 10;
int b = 20;
System.out.println(a + b);
A) 10
B) 20
C) 30
D) null
Answer: C) 30
Explanation: The code adds a and b, resulting in 30.
10. Which of the following is the correct way to declare a variable in Java?
A) int 1number;
B) int number-1;
C) int number1;
D) int number 1;
Answer: C) int number1;
Explanation: Variable names must begin with a letter and cannot contain special characters like - or 1.
11. What is the value of byte variable b after executing the following code?
byte b = 10;
b = b + 5;
A) 10
B) 15
C) Compilation error
D) null
Answer: C) Compilation error
Explanation: The expression b + 5 results in an int, which cannot be directly assigned to a byte without explicit casting.
12. Which of the following is a valid declaration of an array of int in Java?
A) int arr[];
B) int arr = new int[5];
C) int arr[5];
D) Both A and B
Answer: D) Both A and B
Explanation: Option A is a valid declaration, and option B is a valid initialization. Option C is incorrect syntax in Java.
13. What will be the output of the following code?
char c = 'A';
System.out.println(c + 1);
A) A
B) B
C) 66
D) 65
Answer: C) 66
Explanation: The character 'A' has an ASCII value of 65; adding 1 results in 66.
14. What is the range of a short data type in Java?
A) -128 to 127
B) -32,768 to 32,767
C) -2,147,483,648 to 2,147,483,647
D) 0 to 255
Answer: B) -32,768 to 32,767
Explanation: The short data type in Java has a range of -32,768 to 32,767.
15. Which of the following keywords is used to declare a constant variable in Java?
A) static
B) const
C) final
D) constant
Answer: C) final
Explanation: The final keyword is used to declare a constant variable in Java.
16. What will be the output of the following code?
int x = 5;
int y = 2;
System.out.println(x / y);
A) 2
B) 2.5
C) 2.0
D) 3
Answer: A) 2
Explanation: Integer division results in an integer value; therefore, 5 / 2 yields 2.
17. How do you declare a variable that can hold a decimal number in Java?
A) decimal
B) double
C) float
D) Both B and C
Answer: D) Both B and C
Explanation: Both double and float data types can hold decimal numbers in Java.
18. What is the result of the following operation?
int a = 7;
int b = 3;
int c = a % b;
A) 1
B) 2
C) 3
D) 7
Answer: A) 1
Explanation: The modulus operator % returns the remainder of the division, which is 1 in this case.
19. Which of the following is a valid way to declare a String variable in Java?
A) String name;
B) String name = "John";
C) String name = new String("John");
D) All of the above
Answer: D) All of the above
Explanation: All provided options are valid ways to declare a String variable in Java.
20. What is the maximum value that can be stored in a byte variable?
A) 127
B) 255
C) 128
D) 64
Answer: A) 127
Explanation: The byte data type has a maximum value of 127, as it is signed and ranges from -128 to 127.
21. Which of the following statements is true regarding Java primitive data types?
A) They can be null.
B) They are immutable.
C) They do not inherit from the Object class.
D) They are reference types.
Answer: C) They do not inherit from the Object class.
Explanation: Primitive data types do not inherit from Object and are not reference types.
22. What will happen if you try to assign a value to a final variable after it has been initialized?
A) The value will be updated.
B) A compilation error will occur.
C) It will throw a runtime exception.
D) Nothing happens.
Answer: B) A compilation error will occur.
Explanation: A final variable cannot be reassigned once it has been initialized.
23. What is the result of the following code snippet?
int a = 10;
int b = a++;
System.out.println(b);
A) 10
B) 11
C) 12
D) 0
Answer: A) 10
Explanation: The post-increment operator ++ returns the original value before incrementing, so b is assigned 10.
24. What is the output of the following code?
double d = 10 / 4;
System.out.println(d);
A) 2.5
B) 2
C) 2.0
D) 10.0
Answer: B) 2
Explanation: The division is performed with integers, so it results in an integer value 2.
25. Which of the following is a correct declaration of a multi-dimensional array?
A) int arr[][];
B) int arr[5][5];
C) int[] arr[];
D) Both A and C
Answer: D) Both A and C
Explanation: Both A and C are valid ways to declare a multi-dimensional array in Java.
26. What is the default value of a long variable in Java?
A) 0
B) null
C) 0L
D) 0.0
Answer: C) 0L
Explanation: The default value of a long variable is 0L.
27. What is the output of the following code?
int a = 2;
int b = 3;
int c = a * b + b;
System.out.println(c);
A) 5
B) 6
C) 9
D) 10
Answer: C) 9
Explanation: The expression evaluates to 2 * 3 + 3, which equals 9.
28. Which data type would you use to store a very large integer value?
A) int
B) long
C) byte
D) BigInteger
Answer: D) BigInteger
Explanation: For very large integer values that exceed the limits of long, BigInteger from java.math is used.
29. What will be the output of the following code?
String str = "Hello" + " World";
System.out.println(str);
A) Hello
B) World
C) Hello World
D) HelloWorld
Answer: C) Hello World
Explanation: String concatenation combines the two strings into "Hello World".
30. How can you explicitly convert a double to an int in Java?
A) int x = (int) doubleValue;
B) int x = doubleValue;
C) int x = doubleValue.toInt();
D) int x = Double.valueOf(doubleValue);
Answer: A) int x = (int) doubleValue;
Explanation: You can explicitly convert a double to an int using type casting.
31. What will be the output of the following code?
boolean flag = true;
System.out.println(flag ? "Yes" : "No");
A) Yes
B) No
C) true
D) false
Answer: A) Yes
Explanation: The ternary operator evaluates to "Yes" when flag is true.
32. What is the value of the following expression?
int x = 10;
int y = 3;
int z = x / y;
A) 3.0
B) 3
C) 3.33
D) 10
Answer: B) 3
Explanation: Integer division results in an integer value; thus, 10 / 3 equals 3.
33. Which of the following is a valid way to initialize a char variable?
A) char c = 'A';
B) char c = "A";
C) char c = new char('A');
D) All of the above
Answer: A) char c = 'A';
Explanation: The correct way to initialize a char variable is by using single quotes.
34. What will be the output of the following code?
String str = null;
System.out.println(str);
A) null
B) ""
C) 0
D) Compilation error
Answer: A) null
Explanation: The output will be null, as the variable str is explicitly assigned a null value.
35. What is the output of the following code snippet?
int a = 5;
System.out.println(a++);
System.out.println(a);
A) 5 6
B) 6 5
C) 5 5
D) 6 6
Answer: A) 5 6
Explanation: The post-increment operator returns the original value before incrementing, so the first print outputs 5, and the second print outputs 6.
36. What is the result of the following code?
byte a = 10;
byte b = 20;
byte c = a + b;
A) 30
B) Compilation error
C) byte c = 30;
D) 0
Answer: B) Compilation error
Explanation: The expression a + b results in an int, which cannot be assigned directly to a byte without explicit casting.
37. Which of the following methods can be used to convert a String to an int?
A) Integer.parseInt(string)
B) string.toInt()
C) Integer.valueOf(string)
D) Both A and C
Answer: D) Both A and C
Explanation: Both Integer.parseInt(string) and Integer.valueOf(string) can convert a String to an int.
38. What is the result of this expression?
double result = 10 / 3;
A) 3.0
B) 3.3333
C) 3
D) Compilation error
Answer: C) 3
Explanation: Since both 10 and 3 are integers, the result of the division is also an integer.
39. What will be the output of the following code?
float f = 1.5f;
System.out.println(f + 1);
A) 1.5
B) 2.5
C) 2
D) 3
Answer: B) 2.5
Explanation: The float value 1.5f added to 1 results in 2.5.
40. How do you declare a variable of type Integer in Java?
A) int num;
B) Integer num;
C) Number num;
D) Integer num = null;
Answer: B) Integer num;
Explanation: Integer is a wrapper class for the primitive int data type.
41. Which of the following statements is true about the float data type?
A) It is always 32 bits.
B) It cannot store decimal values.
C) It has a higher precision than double.
D) It is used for character storage.
Answer: A) It is always 32 bits.
Explanation: The float data type is consistently 32 bits in Java.
42. What will be the output of the following code?
byte b = 1;
b = b + 1;
System.out.println(b);
A) 1
B) 2
C) Compilation error
D) 0
Answer: C) Compilation error
Explanation: The expression b + 1 results in an int, which cannot be directly assigned to a byte.
43. Which of the following statements is incorrect?
A) int a = 10;
B) char b = 'A';
C) boolean c = "true";
D) double d = 10.5;
Answer: C) boolean c = "true";
Explanation: A boolean variable cannot be assigned a String value.
44. What will be the output of the following code?
String str = "Java";
System.out.println(str.length());
A) 4
B) 5
C) 3
D) 0
Answer: A) 4
Explanation: The length of the string "Java" is 4.
45. How do you declare an array of double in Java?
A) double arr[];
B) double[] arr;
C) double arr = new double[5];
D) Both A and B
Answer: D) Both A and B
Explanation: Both are valid ways to declare an array of double in Java.
46. Which of the following will result in a runtime exception?
A) Dividing by zero. B) Accessing an out-of-bounds array index. C) Type casting an incompatible type. D) All of the above. Answer: D) All of the above. Explanation: Each of these actions can lead to a runtime exception.
47. What is the maximum value of an int in Java?
A) 2147483647
B) 4294967295
C) 32767
D) 1000000
Answer: A) 2147483647
Explanation: The maximum value for an int in Java is 2147483647 (2^31 - 1).
48. Which of the following statements is true?
A) A char can be assigned a numeric value.
B) String is a primitive data type.
C) A double can store very large integers.
D) An int can hold a decimal value.
Answer: A) A char can be assigned a numeric value.
Explanation: Characters in Java can also represent integer values based on their ASCII codes.
49. What will be the output of the following code?
int a = 5;
int b = 10;
int c = a < b ? a : b;
System.out.println(c);
A) 5
B) 10
C) 0
D) 15
Answer: A) 5
Explanation: The ternary operator selects a because 5 < 10 is true.
50. Which of the following data types is used to represent true/false values in Java?
A) int
B) boolean
C) char
D) String
Answer: B) boolean
Explanation: The boolean data type is specifically used to represent true/false values in Java.
메타데이터
- post_id
- 5815420d81b7
- slug
- 50-java-mcqs-understanding-data-types-and-variables-5815420d81b7
- url
- https://medium.com/@azizmarzouki/50-java-mcqs-understanding-data-types-and-variables-5815420d81b7
- canonical_url
- https://medium.com/@azizmarzouki/50-java-mcqs-understanding-data-types-and-variables-5815420d81b7
- author_url
- https://medium.com/@azizmarzouki
- status
- ok
- fetched_at
- 2026-07-29 00:11:46