Static Block MCQs
What is a static block in Java?
A. A block of code that is executed only once when an object is created
B. A block of code that is executed before the main method
C. A block of code that initializes instance variables
D. A block of code that cannot be used for initialization
When is a static block executed in Java?
A. When an object of the class is created
B. Before any static or non-static methods are called
C. After the main method is called
D. When the static method is called for the first time
Can a class have multiple static blocks in Java?
A. No, only one static block is allowed
B. Yes, but only one will be executed
C. Yes, and they are executed in the order they are defined
D. No, static blocks cannot be defined more than once
Which of the following statements is true about static blocks?
A. They can access instance variables directly
B. They can access static variables and methods directly
C. They can only be executed if an object is created
D. They are executed after constructors
What is the purpose of a static block in Java?
A. To initialize instance variables
B. To perform operations that need to be done once, before the class is used
C. To declare static methods
D. To override methods from a superclass
Can static blocks throw exceptions in Java?
A. No, static blocks cannot throw exceptions
B. Yes, but only unchecked exceptions
C. Yes, they can throw checked and unchecked exceptions
D. Yes, but only checked exceptions
Can static blocks be used to initialize static variables?
A. No, static variables must be initialized in constructors
B. Yes, static blocks are often used for this purpose
C. No, static variables cannot be initialized in static blocks
D. Yes, but only for final static variables
What happens if there is an exception in a static block?
A. The program continues execution with a warning
B. The program terminates with an ExceptionInInitializerError
C. The static block is ignored, and the program continues
D. The exception is caught automatically, and execution continues
Which of the following is true regarding the execution order of static blocks?
A. They are executed in the order of their declaration
B. They are executed randomly
C. They are executed after instance methods
D. They are executed just before garbage collection
What is the output of the following code snippet?
java
Copy code
class Test {
static int a;
static {
a = 10;
System.out.println("Static block executed");
}
public static void main(String[] args) {
System.out.println("Main method executed");
}
}
A. Main method executed
B. Static block executed
C. Static block executed\nMain method executed
D. Main method executed\nStatic block executed
Can a static block access static methods of the same class?
A. No, static methods cannot be accessed from static blocks
B. Yes, but only if the method is private
C. Yes, directly without creating an instance
D. Yes, but only through an instance of the class
Is it possible to have a return statement in a static block?
A. Yes, and it returns control to the JVM
B. No, return statements are not allowed in static blocks
C. Yes, but only in the last static block
D. Yes, but it must return a value
Can a static block access static variables from other classes?
A. No, static blocks cannot access static variables from other classes
B. Yes, but only if they are declared as final
C. Yes, if they are accessible in the current context
D. Yes, but only if they are private
What is the primary difference between a static block and a static method?
A. A static block can access instance variables, while a static method cannot
B. A static block can execute code, while a static method cannot
C. A static block is executed when the class is loaded, while a static method must be called explicitly
D. A static block cannot throw exceptions, while a static method can
Can static blocks be synchronized in Java?
A. No, static blocks cannot be synchronized
B. Yes, using the synchronized keyword
C. Yes, but only if they access instance variables
D. No, synchronization is only for methods
Which of the following is NOT a valid use of a static block in Java?
A. Initializing static variables
B. Executing static methods
C. Creating objects of the class
D. Logging configuration settings
Can a static block call an instance method of the same class?
A. Yes, directly
B. No, instance methods cannot be called from static blocks
C. Yes, but only through an object instance
D. No, it will cause a compile-time error
How is the execution of multiple static blocks in a class handled?
A. Only the first static block is executed
B. The last static block overrides the others
C. They are executed in the order they appear in the source code
D. The static blocks are ignored
What is the use of a static block in a class that contains only static variables?
A. To provide a constructor for the class
B. To initialize the static variables
C. To override the static variables
D. To access instance variables
What will happen if a class has a static block that initializes a static variable, and the same variable is also initialized at the point of declaration?
A. The variable will have the value assigned at the point of declaration
B. The variable will have the value assigned in the static block
C. The variable will have a compile-time error
D. The value from the static block will overwrite the value assigned at the point of declaration
Answers:
B. A block of code that is executed before the main method
B. Before any static or non-static methods are called
C. Yes, and they are executed in the order they are defined
B. They can access static variables and methods directly
B. To perform operations that need to be done once, before the class is used
C. Yes, they can throw checked and unchecked exceptions
B. Yes, static blocks are often used for this purpose
B. The program terminates with an ExceptionInInitializerError
A. They are executed in the order of their declaration
C. Static block executed\nMain method executed
C. Yes, directly without creating an instance
B. No, return statements are not allowed in static blocks
C. Yes, if they are accessible in the current context
C. A static block is executed when the class is loaded, while a static method must be called explicitly
B. Yes, using the synchronized keyword
C. Creating objects of the class
C. Yes, but only through an object instance
C. They are executed in the order they appear in the source code
B. To initialize the static variables
D. The value from the static block will overwrite the value assigned at the point of declaration
No comments:
Post a Comment