Java Interview Guide : 200+ Interview Questions and Answers - Static variables and methods

Java Interview Guide : 200+ Interview Questions and Answers - Static variables and methods

Assessment

Interactive Video

Information Technology (IT), Architecture

University

Hard

Created by

Quizizz Content

FREE Resource

The video tutorial explains static variables and methods, highlighting their class-level nature. Static variables are shared across all instances of a class, unlike instance variables which are unique to each object. The tutorial uses a Cricketer class example to demonstrate how static variables work. It also covers static methods, which can only access class-level variables, and emphasizes the importance of using class names to access static members. The video concludes with a summary and encourages viewers to experiment with examples to deepen their understanding.

Read more

7 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the primary characteristic of static variables?

They are unique to each instance of a class.

They are shared across all instances of a class.

They can only be accessed within the method they are declared.

They are automatically initialized to zero.

2.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

In the Cricketer class example, what happens to the static variable 'count' when a new cricketer is created?

It is decremented by one.

It remains unchanged.

It is reset to zero.

It is incremented by one.

3.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is the effect of modifying a static variable in one object?

It affects only that object.

It affects all instances of the class.

It causes a runtime error.

It resets the variable to its default value.

4.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

Why can't static methods access instance variables?

Because static methods are not part of any instance.

Because instance variables are private by default.

Because static methods are only accessible within the same class.

Because instance variables are not initialized.

5.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What happens if you try to declare a local variable as static?

It will be ignored by the compiler.

It will be initialized to zero.

It will cause a compilation error.

It will be shared across all instances.

6.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

What is a recommended practice when accessing static variables?

Use the instance name to access them.

Use the class name to access them.

Access them only within static methods.

Avoid using static variables altogether.

7.

MULTIPLE CHOICE QUESTION

30 sec • 1 pt

How can you access a static method?

Only within the same package.

Directly using the class name.

Only through a subclass.

Only through an instance of the class.