Java Interview Questions With Answers – Part 8: Java Classes

[1][2][3][4][5][6][7][8] – Here’s part 8 of some Java Interview Questions. The level of questions is suitable for beginners as well advanced core Java programmers. This part covers Java Classes.

java-classes-interview-questions

The series includes more than 200 questions, so stay tuned for more.

Q101. What are the variables defined in a class called?

A101. The variables defined in a class are called instance variables.

Q102. How can you define members of a class?

A102. Members are the methods and variables defined within a class.

Q103. Are these two objects or instance variables?

class Ranking {

           int rank;
int points;

}

A103. The above two, rank and points are instance variables defined by class Ranking.

Q104. Define object?

A104. Object is an instance of a class.

Q105. How to create an object? Create an object and name it rkobj1. Class name is Ranking.

class Ranking {

           int rank;

int points;

}

A105. Here’s how you can create an object named rkobj1 for the class Ranking shown above,

Ranking rkobj1 = new Ranking();

As shown above, rkobj1 is an instance of class Ranking.

Q106. What does the following class and object creation means in terms of instance variables?

class Ranking {

           int rank;

int points;

}

Ranking rkobj1 = new Ranking();

A106. Here’s what you can evaluate,

class name: Ranking
rank: instance variable
points: instance variable
rkobj1: object of class Ranking

The Ranking object rkobj1 contains its own copies of the instance variables rank and points.

Q107. How can you access the instance variables in the class given in Q106?

A107. You can easily access the instance variables using the dot (.) operator. For example,

// the following assigns the value 2 to the copy of instance variable rank

rkobj1.rank = 2;

// the following assigns the value 90 to the copy of instance variable points

rkobj1.points = 90;

Q108. What is the another way of writing the following statement to declare an object,

            Ranking rkobj1 = new Ranking();

A108. Another way of declaring an object is shown below,

            Ranking rkobj1;
rkobj1 = new Ranking();

Q109. Can we assign an object like the following statements?

            Ranking rkobj1 = new Ranking();
Ranking rkobj2 = rkobj1;

A109. Yes, the statement allows rkobj2 refer to the same object as rkobj1

Q110. Write a code snippet to create a method and call it with an object?

A110.

public class Ranking {

    int rank;

    int points;

    void GetInfo(int r, int p){  //method

        rank=r;

        points=p;

    }

    void ShowInfo()

    {

    System.out.println(“Rank: ” +rank+” Points: “+points);

}

public static void main(String args[]){

    Ranking s1=new Ranking();

    Ranking s2=new Ranking();

    s1.GetInfo(1, 100);

    s2.GetInfo(2,150);

    s1.ShowInfo();

    s2.ShowInfo();

}

}

[1][2][3][4][5][6][7][8]

Comments

comments

thegenericwhiz@gmail.com'

GW Editorial Staff

Editorial Staff at Generic Whiz.

Leave a Reply

Your email address will not be published. Required fields are marked *