Site icon Narayana Tutorial

Classes and Objects interview questions

Classes and Objects interview questions

Classes and Objects interview questions

We know a class is a model for creating objects. this means the properties and actions of the objects are written in the class. Properties are represented by variables and actions of the objects are represented by methods. So a class contains variables and methods. The same variables and methods are also available in the objects because they are created from the class.

In this post, I am going to sharing important Classes and Objects interview questions.

1) What is hash code?

A) Hash Code is a unique identification number allotted to the objects by the JVM. This hash code number is alos called reference number which is created based on the location of the object in memory, and is unique for all objects, except for string objects.

2) How can you find the hash code of an object?

A) The hashcode() method of Object class in java.lang package is useful to find the hash code of an object.

3) What are the default values of the data types.

A) Data Type Default Value

byte–0

short–0

int–0

long–0

float–0.0

double–0.0

char — a space

String — null

any class type — null

boolean — false

4) Access Modifiers

A) An access specifier is a key word that specifies how to access the members of a class or a class itself.

We can use access specifiers before a class and its members. There are four access specifiers available in java.

1. private

private members of a class are not accessible any where outside the class. They are accessible only whithin the class by the methods of that class

2. public

public members of a class are accessible every where outside the class. so any other program can read them and use them.

3. protected

protected members of a class are accessible outside the class, but generally, within the same directory.