Final Variables MCQ
What is a final variable in Java?
A. A variable that can be changed multiple times
B. A variable that can only be initialized once
C. A variable that must be declared as static
D. A variable that is automatically garbage collected
When must a final variable be initialized in Java?
A. At the time of declaration
B. In the constructor
C. In an initializer block
D. Any of the above
Can a final variable be assigned a value more than once?
A. Yes, at runtime
B. Yes, at compile-time
C. No, it can only be assigned once
D. Yes, but only if it is static
Which of the following is a valid way to declare a final variable?
A. final int x = 5;
B. int final x = 5;
C. final static int x;
D. static int final x = 5;
Can final variables be left uninitialized?
A. Yes, but they must be initialized in the constructor
B. No, they must be initialized at the time of declaration
C. Yes, but they must be initialized before the object is garbage collected
D. Yes, but they must be initialized in a static block
What happens if you try to change the value of a final variable?
A. It will cause a compile-time error
B. It will change the value at runtime
C. It will throw a runtime exception
D. The change will be ignored
Can a final variable be a class member?
A. No, only local variables can be final
B. Yes, both class members and local variables can be final
C. Yes, but only if they are static
D. No, final variables cannot be members
Which of the following statements is true about final variables?
A. They must be static
B. They cannot be accessed from static methods
C. They cannot be changed once initialized
D. They must be declared public
Can a final variable be initialized in a method?
A. Yes, if the method is static
B. No, it must be initialized at the time of declaration or in the constructor
C. Yes, but only if the variable is declared within the method
D. No, final variables cannot be used in methods
What is the correct way to define a constant value using a final variable?
A. final int MAX_VALUE = 100;
B. static final int MAX_VALUE = 100;
C. final static int MAX_VALUE;
D. int final MAX_VALUE = 100;
Can final variables be declared inside a loop?
A. Yes, but they must be initialized within the loop
B. No, final variables cannot be declared inside loops
C. Yes, but they cannot be changed within the loop
D. No, final variables must be declared outside the loop
What is the significance of a final variable in a multi-threaded environment?
A. It ensures that the variable cannot be shared among threads
B. It ensures that the variable's value cannot be changed by any thread
C. It prevents the variable from being visible to other threads
D. It makes the variable thread-local
Can a final variable reference a mutable object?
A. No, it must reference an immutable object
B. Yes, but the reference cannot be changed
C. No, it must reference a primitive type
D. Yes, and the object's state can be modified
Which of the following is NOT allowed with a final variable?
A. Declaring it as static
B. Initializing it in a constructor
C. Assigning it a value multiple times
D. Using it as a method parameter
Can a final variable be a method parameter?
A. Yes, and its value cannot be changed within the method
B. No, method parameters cannot be final
C. Yes, but only in static methods
D. No, because method parameters are inherently non-final
What does it mean if a final variable is declared inside a method?
A. It can be changed within the method
B. It cannot be accessed outside the method
C. It must be initialized immediately
D. It must be assigned a value before the method exits
Can a final variable be inherited in a subclass?
A. Yes, and it can be overridden
B. Yes, but it cannot be modified
C. No, final variables cannot be inherited
D. Yes, and it must be reinitialized
What is the default value of a final variable if not initialized?
A. 0
B. null
C. It depends on the type
D. It has no default value and must be explicitly initialized
What is the purpose of using the final keyword with a variable?
A. To indicate that the variable can be changed
B. To prevent the variable from being garbage collected
C. To ensure that the variable cannot be modified after initialization
D. To make the variable visible to all threads
Can a final variable be part of an interface?
A. No, interfaces cannot contain variables
B. Yes, and it must be initialized in the interface
C. Yes, and it must be initialized with a value
D. No, final variables can only be in classes
Answers:
B. A variable that can only be initialized once
D. Any of the above
C. No, it can only be assigned once
A. final int x = 5;
A. Yes, but they must be initialized in the constructor
A. It will cause a compile-time error
B. Yes, both class members and local variables can be final
C. They cannot be changed once initialized
C. Yes, but only if the variable is declared within the method
B. static final int MAX_VALUE = 100;
A. Yes, but they must be initialized within the loop
B. It ensures that the variable's value cannot be changed by any thread
B. Yes, but the reference cannot be changed
C. Assigning it a value multiple times
A. Yes, and its value cannot be changed within the method
D. It must be assigned a value before the method exits
B. Yes, but it cannot be modified
D. It has no default value and must be explicitly initialized
C. To ensure that the variable cannot be modified after initialization
C. Yes, and it must be initialized with a value
No comments:
Post a Comment