Java Variables and Data Types Quiz Questions

Welcome to the Java variables and data types quiz collection! This quiz set contains 30 multiple-choice questions based on Java variables and data types.

This quiz is designed for beginners as well as students preparing for exams, interviews, and coding tests. Each question includes four multiple-choice options, the correct answer, and an explanation to help you understand the concept clearly.

Whether you are a school or college student or a coding beginner, this quiz will help you test your basic Java knowledge and improve your understanding step by step. There is no time limit. At the end of the quiz, you will get the scorecard, which will show the performance in the quiz.

Let’s start the quiz and check how well you know the variables and data types of Java programming!

Which of the following is a valid variable name in Java?
A. 2myVar
B. _myVar
C. my-Var
D. my Var
_myVar
Java identifiers can start with a letter, underscore (_), or dollar sign ($), but NOT a digit or hyphen. Spaces are never allowed. _myVar is perfectly valid.
What is the default value of a boolean instance variable in Java?
A. true
B. false
C. 0
D. null
false
Instance (class-level) boolean variables are initialized to false by default in Java. Local boolean variables have no default and must be explicitly initialized before use.
Which data type is used to store a single character in Java?
A. String
B. char
C. byte
D. short
char
char is a 16-bit Unicode character type in Java (range: ‘\u0000’ to ‘\uffff’). String stores sequences of characters, not a single one.
How many bytes does a long data type occupy in Java?
A. 2
B. 4
C. 6
D. 8
8
long is a 64-bit signed integer, occupying 8 bytes. Its range is −2⁶³ to 2⁶³−1. Compare: int = 4 bytes, short = 2 bytes, byte = 1 byte.
Which keyword is used to declare a constant in Java?
A. const
B. final
C. static
D. constant
final
final makes a variable’s value unchangeable after initialization. Java does not have a ‘const’ keyword. The static final together creates a class-level constant.
What is the range of the byte data type in Java?
A. 0 to 255
B. -128 to 127
C. -256 to 255
D. 0 to 127
-128 to 127
byte is an 8-bit signed integer. Its range is −2⁷ (−128) to 2⁷−1 (127). Note: Java’s byte is signed unlike C/C++ where it can be unsigned.
Which of these is not a primitive data type in Java?
A. int
B. boolean
C. String
D. double
String
String is a class (reference type) in Java, not a primitive data type. The eight primitive data types are: byte, short, int, long, float, double, char, boolean.
What will ‘int x;’ produce if accessed inside a method without initialization?
A. 0
B. null
C. Garbage value
D. Compile-time error
Compile-time error
Local variables in Java are not given default values. Accessing an uninitialized local variable causes a compile-time error: “variable x might not have been initialized”.
Which data type should you use to store the value 3.14 with high precision?
A. float
B. double
C. int
D. long
double
double provides 64-bit IEEE 754 double-precision (~15-16 significant digits). float is 32-bit (~6-7 digits). For most decimal precision tasks, double is preferred.
What is the size of a char in Java?
A. 1 byte
B. 2 bytes
C. 4 bytes
D. Depends on OS
2 bytes
Java char is always 2 bytes (16 bits) regardless of platform, because Java uses Unicode (UTF-16) encoding. This distinguishes it from C/C++ where char is typically 1 byte.
What is the output of System.out.println(10 / 3);
A. 3.33
B. 3.0
C. 3
D. Compile error
3
Integer division in Java truncates the decimal part. 10/3 = 3 (not 3.33). To get a decimal result, at least one operand must be a float/double: 10.0/3 → 3.3333…
Which statement correctly declares and initializes a long variable?
A. long x = 9999999999;
B. long x = 9999999999L;
C. long x = (long)9999999999;
D. Both B and C
Both B and C
Integer literals exceeding int range (> 2,147,483,647) must use the ‘L’ suffix or explicit cast. Option A causes a compile error. Options B and C both work correctly.
Which of the following declaration is invalid in Java?
A. var x = 10;
B. var name = “Java”;
C. var value;
D. var price = 10.5;
var value;
var requires initialization during declaration. Without an initializer, the compiler cannot infer the type, resulting in a compile error.
What is the output of the following code?

public class Main {
    public static void main(String[] args) {
        System.out.println('A' + 1);
    }
}
A. A1
B. B
C. 66
D. Compile error
66
char ‘A’ has ASCII/Unicode value 65. When added to int 1, the char is widened to int, resulting in 65+1 = 66. To get ‘B’, you’d need: System.out.println((char)(‘A’ + 1));
Which of the following will cause a loss of data?
A. int to long
B. float to double
C. double to int
D. byte to int
double to int
Narrowing conversion (double to int) truncates the fractional part, causing data loss. The other options are widening conversions which preserve data.
What is the default value of an int instance variable in Java?
A. null
B. -1
C. 0
D. Undefined
0
Instance variables of numeric types default to 0 (int, byte, short, long → 0; float/double → 0.0). This is different from local variables which have no default.
What does the ‘var’ keyword do in Java?
A. Creates a variable of type Object
B. Enables local variable type inference
C. Declares a global variable
D. Creates a nullable variable
Enables local variable type inference
The keyword ‘var’ enables local-variable type inference. The compiler infers the type from the right-hand side: var list = new ArrayList<String>();
What is the output of the following program?

public class Main {
    public static void main(String[] args) {
        System.out.println(0.1 + 0.2 == 0.3);
    }
}
A. true
B. false
C. Compile error
D. Runtime exception
false
Floating-point arithmetic uses binary representation, causing precision errors. 0.1 + 0.2 = 0.30000000000000004, not 0.3.
What is the scope of a variable declared inside a for-loop?
A. Entire class
B. Entire method
C. Only within the for-loop block
D. Accessible after the loop
Only within the for-loop block
A variable declared inside a for-loop is scoped only to that loop block. Attempting to access it after the loop causes a compile-time error.
Which of the following is true about static variables?
A. Each object gets its own copy.
B. They are stored on the heap.
C. They belong to the class, shared across all instances.
D. They cannot be final.
They belong to the class, shared across all instances.
Static variables belong to the class, not individual objects. All instances share a single copy. They are stored in the Method Area (part of heap in modern JVMs).
Can the keyword ‘var’ be used to declare an instance variable in a class?
A. Yes, since Java 10
B. Yes, but only for primitive fields
C. No, ‘var’ is restricted to local variables only
D. Yes, if the field is also static
No, ‘var’ is restricted to local variables only
The keyword ‘var’ is used for local variable declarations inside methods, constructors, and initializer blocks. You cannot use it with instance fields, method parameters, constructor parameters, return types, or catch parameters.
What will be the output of the following code?

public class Main {
    public static void main(String[] args) {
        var var = 10;
        System.out.println(var);
    }
}
A. Error
B. 10
C. null
D. undefined
10
var is not a reserved keyword; it is a reserved type name. Therefore, using ‘var’ as a variable name is perfectly valid in Java, and the program prints 10.
Which data type should be used for precise financial calculations?
A. float
B. double
C. BigDecimal
D. long
BigDecimal
BigDecimal provides accurate decimal arithmetic. Both float and double are prone to floating-point precision errors, making them unsuitable for financial calculations.
What happens when you execute the code below?

public class Main {
    public static void main(String[] args) {
        byte b = 127;
        b++;
        System.out.println(b);
    }
}
A. b = 128
B. b = -128
C. ArithmeticException at runtime
D. Compile error
b = -128
byte range is -128 to 127. After 127, the value wraps to -128 (two’s complement overflow). The ++ operator on byte does b = (byte)(b + 1). No exception is thrown.
Which of the following is an invalid use of the ‘var’ keyword?
A. var x = 42;
B. var list = new ArrayList<>();
C. var name;
D. var map = new HashMap<String, Integer>();
var name;
The keyword ‘var’ requires an initializer so the compiler can infer the type. ‘var name;’ without initialization is a compile error.
Which of the following is allowed with the var keyword?
A. var a = 10, b = 20;
B. var[] arr = new int[5];
C. var message = “Hello”;
D. var x = null;
var message = “Hello”;
Other declarations are invalid. Java cannot infer the type from null (option D). Multiple declarations in one line (option A) and array bracket notation (option B) are also not allowed with var.
What happens when the following program executes?

public class Main {
    public static void main(String[] args) {
        short s1 = 10;
        short s2 = 20;
        short s3 = s1 + s2;
        System.out.println(s3);
    }
}
A. s3 = 30
B. Compile error: int cannot be converted to short
C. Runtime ArithmeticException
D. s3 is auto-cast to short
Compile error: int cannot be converted to short
Arithmetic on short/byte produces an int result. s1 + s2 is of type int. Assigning int to short requires an explicit cast: short s3 = (short)(s1 + s2); Java’s type promotion rules apply to all arithmetic expressions.
Which is true about: float f = 3.14;
A. Compiles fine, f = 3.14
B. Compile error: 3.14 is a double literal
C. Runtime error
D. f is truncated to 3.1
Compile error: 3.14 is a double literal
Decimal literals are double by default. 3.14 is a double, and assigning to float is a narrowing conversion requiring explicit cast or ‘f’ suffix. Fix: float f = 3.14f; OR float f = (float)3.14;
Which of the following compiles WITHOUT error? (assuming int a = 5;)
A. byte b = a;
B. byte b = 5;
C. byte b = 128;
D. byte b = a + 1;
byte b = 5;
byte b = 5; → OK: 5 is a compile-time constant within range. byte b = a; → ERROR: ‘a’ is a variable, requires explicit cast. byte b = 128; → ERROR: 128 is out of byte range (-128 to 127). byte b = a + 1; → ERROR: int expression requires explicit cast.
What is the expected output of the following code?

public class Main {
    public static void main(String[] args) {
        System.out.println("5" + 3 + 2);
    }
}
A. 10
B. 532
C. 55
D. 523
532
String concatenation is evaluated left-to-right. “5” + 3 = “53” (String + int = String). “53” + 2 = “532”. If the expression were 3 + 2 + “5”, it would be 5 + “5” = “55”. Operator associativity is the key!

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.