Saturday, August 3, 2024

Instance Variables MCQ

 Instance Variables MCQ

What is an instance variable in Java?


A. A variable defined inside a method

B. A variable defined outside of any method, with the static keyword

C. A variable defined inside a class but outside any method, without the static keyword

D. A variable defined within a loop

Which of the following is true about instance variables?


A. They are shared among all instances of a class

B. Each instance of a class has its own copy of the instance variables

C. They must be declared as final

D. They can only be accessed by static methods

When is the memory allocated for an instance variable?


A. When the class is loaded

B. When the method is called

C. When the object is created

D. When the program starts

Can instance variables have access modifiers?


A. No, they cannot have access modifiers

B. Yes, they can be public, protected, private, or default

C. Yes, but they can only be public or private

D. No, they must be default access

Which of the following is an example of an instance variable?


A. public static int count = 0;

B. private int age = 25;

C. final int MAX_VALUE = 100;

D. int x = 5; (inside a method)

What is the default value of an uninitialized instance variable of type int?


A. 0

B. null

C. NaN

D. -1

How can instance variables be accessed within a class?


A. Using the class name

B. Using the object reference

C. Using the this keyword

D. Both B and C

What is the scope of an instance variable?


A. Within the method where it is declared

B. Within the class, including methods and constructors

C. Across all classes

D. None of the above

Can instance variables be initialized within a constructor?


A. Yes

B. No

C. Only if they are final

D. Only if they are static

What happens if an instance variable is not initialized explicitly?


A. It will have a garbage value

B. It will not be accessible

C. It will be initialized to a default value

D. It will cause a compile-time error

Can instance variables be accessed by static methods?


A. Yes, directly

B. Yes, using an object reference

C. No, static methods cannot access instance variables

D. Only if the instance variable is final

How are instance variables stored in memory?


A. In the heap

B. In the stack

C. In the method area

D. In the registers

Which of the following statements about instance variables is correct?


A. They can be accessed without creating an object of the class

B. They are initialized only once when the class is loaded

C. They are not shared among objects of the class

D. They cannot be marked as final

Can an instance variable be a constant?


A. Yes, if it is declared with final

B. No, only static variables can be constants

C. Yes, if it is declared with static

D. No, instance variables cannot be constants

What is the main difference between instance variables and local variables?


A. Instance variables are stored in the stack, local variables in the heap

B. Instance variables have class-wide scope, local variables have method-wide scope

C. Instance variables can be static, local variables cannot

D. There is no difference

How can you differentiate instance variables from local variables in a method when they have the same name?


A. By using the this keyword for instance variables

B. By using the class name for local variables

C. By using super keyword for instance variables

D. You cannot differentiate them

If an instance variable is declared but not initialized, what will its value be if accessed?


A. It will have a garbage value

B. It will cause a runtime error

C. It will have the default value for its type

D. It will cause a compile-time error

What access modifier allows an instance variable to be accessible only within its class?


A. public

B. protected

C. private

D. default (package-private)

Can instance variables be final?


A. No, instance variables cannot be final

B. Yes, but they must be initialized at the time of declaration or in the constructor

C. Yes, but they can only be initialized in the constructor

D. Yes, but they can be modified later

Which keyword is used to refer to instance variables when there is a naming conflict with local variables?


A. super

B. class

C. this

D. instance

Answers:

C. A variable defined inside a class but outside any method, without the static keyword

B. Each instance of a class has its own copy of the instance variables

C. When the object is created

B. Yes, they can be public, protected, private, or default

B. private int age = 25;

A. 0

D. Both B and C

B. Within the class, including methods and constructors

A. Yes

C. It will be initialized to a default value

B. Yes, using an object reference

A. In the heap

C. They are not shared among objects of the class

A. Yes, if it is declared with final

B. Instance variables have class-wide scope, local variables have method-wide scope

A. By using the this keyword for instance variables

C. It will have the default value for its type

C. private

B. Yes, but they must be initialized at the time of declaration or in the constructor

C. this

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...