Saturday, August 3, 2024

Static and Non-Static Methods MCQ

Static and Non-Static Methods MCQ

What is a static method in Java?


A. A method that can only be accessed within an instance of a class

B. A method that belongs to the class rather than any object instance

C. A method that cannot access static variables

D. A method that can only be accessed through an object

What is a non-static (instance) method in Java?


A. A method that belongs to the class rather than any object

B. A method that belongs to a specific instance of a class

C. A method that cannot be overridden

D. A method that can only access other non-static methods

How can a static method be 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 static method access instance variables directly?


A. Yes, always

B. No, it can only access static variables

C. Yes, but only if the method is final

D. Yes, but only if the method is private

Can a non-static method access static variables directly?


A. No, it causes a compile-time error

B. Yes, but only through an instance of the class

C. Yes, directly without any restrictions

D. No, non-static methods cannot access static variables

Which of the following is true about static methods?


A. They can access non-static variables and methods directly

B. They are called using an object reference

C. They belong to the class rather than any specific instance

D. They can be overridden in subclasses

When is a static method typically used?


A. When a method needs to modify instance-specific data

B. When a method performs operations not dependent on instance variables

C. When a method is frequently overridden in subclasses

D. When a method must be synchronized across instances

Can static methods be overridden in Java?


A. Yes, they can be overridden like instance methods

B. No, they are hidden, not overridden

C. Yes, but only if they are public

D. No, static methods cannot be hidden

Which of the following is a correct statement about non-static methods?


A. They can only be called from static methods

B. They can access static and instance variables

C. They cannot be synchronized

D. They belong to the class rather than the object

How does the this keyword function within a non-static method?


A. Refers to the class itself

B. Refers to the current object instance

C. Refers to the parent class

D. Refers to a static variable

What is the purpose of the static keyword in method declaration?


A. To indicate the method belongs to the class, not instances

B. To make the method synchronized

C. To allow the method to be overridden in subclasses

D. To prevent the method from accessing instance variables

What is the default access modifier for a static method if none is specified?


A. public

B. private

C. protected

D. package-private (default)

Can a static method access this or super keywords?


A. Yes, this can be used in static methods

B. Yes, super can be used in static methods

C. No, neither this nor super can be used in static methods

D. Yes, both this and super can be used in static methods

Which of the following can a static method NOT do?


A. Access static variables

B. Call other static methods

C. Access instance variables

D. Access static methods in other classes

Can a non-static method be called from a static method?


A. Yes, directly

B. Yes, but only through an instance of the class

C. No, it causes a compile-time error

D. Yes, but only if the non-static method is public

Which of the following correctly distinguishes static and non-static methods?


A. Static methods can access instance variables, non-static methods cannot

B. Non-static methods can access both static and instance variables, static methods can only access static variables

C. Static methods belong to the object, non-static methods belong to the class

D. Non-static methods cannot be synchronized, while static methods can

When is a static method called?


A. When an instance of the class is created

B. When the class is loaded into memory

C. When an instance method of the class is called

D. When the class is unloaded from memory

Can static methods be inherited?


A. No, static methods are not inherited

B. Yes, and they can be overridden

C. Yes, and they are hidden, not overridden

D. Yes, but they cannot be hidden

What is the result of trying to override a static method in a subclass?


A. The subclass method will hide the superclass method

B. A compile-time error occurs

C. The subclass method overrides the superclass method

D. The subclass method is ignored

Which of the following is a valid declaration of a static method in Java?


A. public void myMethod() {}

B. static public void myMethod() {}

C. private static void myMethod() {}

D. static void myMethod() {}

Answers:

B. A method that belongs to the class rather than any object instance

B. A method that belongs to a specific instance of a class

C. Using the class name

B. No, it can only access static variables

C. Yes, directly without any restrictions

C. They belong to the class rather than any specific instance

B. When a method performs operations not dependent on instance variables

B. No, they are hidden, not overridden

B. They can access static and instance variables

B. Refers to the current object instance

A. To indicate the method belongs to the class, not instances

D. package-private (default)

C. No, neither this nor super can be used in static methods

C. Access instance variables

B. Yes, but only through an instance of the class

B. Non-static methods can access both static and instance variables, static methods can only access static variables

B. When the class is loaded into memory

C. Yes, and they are hidden, not overridden

A. The subclass method will hide the superclass method

C. private static void myMethod() {}

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...