Static and Non-Static Variables MCQ
What is a static variable in Java?
A. A variable that can be accessed only within a method
B. A variable that belongs to the class, not instances of the class
C. A variable that can only be initialized once
D. A variable that cannot be changed once assigned
What is a non-static (instance) variable in Java?
A. A variable that belongs to the class as a whole
B. A variable that belongs to a specific instance of a class
C. A variable that is shared among all instances of a class
D. A variable that must be declared as final
How many copies of a static variable are created when multiple objects of a class are instantiated?
A. One copy per object
B. One copy per thread
C. Only one copy, shared among all objects
D. A copy is created for each method call
How is a static variable accessed in Java?
A. Through an instance of the class
B. Using the this keyword
C. Using the class name
D. Using the super keyword
Can a non-static variable be accessed from a static method directly?
A. Yes, always
B. No, unless an object instance is created
C. Yes, but only if the variable is public
D. No, non-static variables cannot be accessed from static methods at all
What is the default value of a static variable if not explicitly initialized?
A. 0
B. null
C. It depends on the data type
D. It has no default value and must be initialized
What is the purpose of the static keyword in Java?
A. To make variables and methods instance-specific
B. To make variables and methods class-specific
C. To make variables and methods immutable
D. To prevent variables and methods from being inherited
Can a static variable be accessed within a non-static method?
A. No, it causes a compile-time error
B. Yes, directly
C. Yes, but only through the class name
D. No, it must be accessed through an object reference
Which of the following can be a characteristic of a static variable?
A. It can be overridden in subclasses
B. It can be initialized multiple times
C. It retains its value across all instances of the class
D. It is not shared among instances of the class
When is a static variable initialized in Java?
A. When an instance of the class is created
B. When the class is loaded into memory
C. When the first object of the class is created
D. When the static method is called
What happens if you declare a non-static variable as final?
A. The variable can be assigned a value only once
B. The variable cannot be accessed outside the class
C. The variable must be initialized at the time of declaration
D. The variable cannot be initialized
Can a static variable be final?
A. No, static variables cannot be final
B. Yes, but it must be initialized only once
C. Yes, and it must be initialized at the time of declaration
D. No, static variables cannot have access modifiers
Which of the following statements is true about non-static variables?
A. They are shared among all instances of the class
B. They can only be accessed through the class name
C. They are unique to each instance of the class
D. They must be declared as private
What is the scope of a static variable?
A. The entire class, and it is shared among all instances
B. The method in which it is declared
C. The block in which it is declared
D. The instance in which it is declared
Which of the following is NOT a valid way to declare a static variable in Java?
A. static int x;
B. public static int x;
C. private int static x;
D. protected static int x;
What is the default access modifier for a static variable if none is specified?
A. public
B. private
C. protected
D. package-private (default)
How can a non-static method access a static variable?
A. Using the this keyword
B. Using an instance of the class
C. Using the class name
D. It cannot access static variables
What happens if a static variable is modified by one instance of the class?
A. The change is reflected only in that instance
B. The change is reflected in all instances of the class
C. The static variable becomes final
D. The change is ignored
Which of the following is true about static and non-static variables?
A. Both are instance-specific
B. Both are class-specific
C. Static variables are class-specific, non-static variables are instance-specific
D. Non-static variables are class-specific, static variables are instance-specific
Can static variables be used in a static block?
A. No, static variables cannot be used in static blocks
B. Yes, but they must be final
C. Yes, and they are initialized in the static block if not already initialized
D. No, they can only be used in instance methods
Answers:
B. A variable that belongs to the class, not instances of the class
B. A variable that belongs to a specific instance of a class
C. Only one copy, shared among all objects
C. Using the class name
B. No, unless an object instance is created
C. It depends on the data type
B. To make variables and methods class-specific
B. Yes, directly
C. It retains its value across all instances of the class
B. When the class is loaded into memory
A. The variable can be assigned a value only once
C. Yes, and it must be initialized at the time of declaration
C. They are unique to each instance of the class
A. The entire class, and it is shared among all instances
C. private int static x;
D. package-private (default)
C. Using the class name
B. The change is reflected in all instances of the class
C. Static variables are class-specific, non-static variables are instance-specific
C. Yes, and they are initialized in the static block if not already initialized
No comments:
Post a Comment