Static Vs Instance
6.Which keyword is used to define class-level members?
-
A. final
-
B. static
-
C. public
-
D. const
static
static
7.What will be the output?
class Demo {
static String message = "Hello";
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
d1.message = "Hi";
System.out.println(d2.message);
}
}
-
A. Hi
-
B. Hello
-
C. null
-
D. Compilation error
Hi
Hi
8.Which of the following statements is false?
-
A. Static variables are initialized when the class is loaded.
-
B. Static methods can use this to refer to current object.
-
C. Static methods can be called without an object.
-
D. Static variables are shared among all objects of a class.
Static methods can use this to refer to current object.
Static methods can use this to refer to current object.
9.Static blocks are executed:
-
A. When an object is created
-
B. At the end of the program
-
C. When the class is loaded into memory
-
D. When JVM exits
When the class is loaded into memory
When the class is loaded into memory
10.Output?
class A {
static int x = 10;
static {
x += 5;
}
public static void main(String[] args) {
System.out.println(x);
}
}
-
A. 10
-
B. 15
-
C. Compilation error
-
D. 5
15
15