Static Vs Instance
1.What is a static variable in Java?
-
A. A variable that belongs to the instance of the class.
-
B. A variable that is local to a method.
-
C. A variable shared across all instances of a class.
-
D. A constant variable.
A variable shared across all instances of a class.
A variable shared across all instances of a class.
2.What is true about static methods?
-
A. They can access instance variables directly.
-
B. They can only be accessed using object reference.
-
C. They belong to the class, not objects.
-
D. They can use this keyword.
They belong to the class, not objects.
They belong to the class, not objects.
3.Which of the following can be accessed from a static context?
-
A. Instance variables
-
B. Static variables
-
C. Instance methods
-
D. this keyword
Static variables
Static variables
4.What is the output of the following code?
class Test {
static int count = 0;
Test() {
count++;
}
public static void main(String[] args) {
new Test();
new Test();
System.out.println(Test.count);
}
}
-
A. 1
-
B. 2
-
C. 0
-
D. Compilation error
2
2
5.Can static methods be overridden?
-
A. Yes
-
B. No
-
C. Only in interfaces
-
D. Only final static methods
No
No