Core java interview Questions You'll Most Likely Be Asked (Second Edition)


Download 1.52 Mb.
Pdf ko'rish
bet30/171
Sana07.01.2023
Hajmi1.52 Mb.
#1081886
1   ...   26   27   28   29   30   31   32   33   ...   171
Bog'liq
Core JAVA Interview Questions Youll M...

076.





077.
}
This code creates a class called Book. It has a constructor that
initializes the name and author fields. The this keyword is
used to refer to the current object’s copies of the name and
author fields. This helps in distinguishing between the instance
fields and the variables passed in. The this keyword can also
be used with methods. So, the doSomethingElse method
invokes the doSomething method via the this keyword.
Explain the differences between a class and an object.
Answer:
There are several differences between a class and an object.
These are as follows:
A class acts like a template and defines a new data type. An
object on the other hand is a value of that data type.
A class does not have any memory allocated to it. An object
has memory allocated to it.
A class is a logical entity, an object has physical existence.
A class is declared using the class keyword, an object is
created using the new keyword.
A class does not have a state, an object has state.
Explain the new keyword with a code sample.
Answer:
The new keyword is used to allocate memory for an object. It
needs to be followed by a call to the constructor of the method.
The following code snippet demonstrates the new keyword–
Person person1 = new Person();


078.
Here, Person is the name of the class, person1 is an object of
type Person. The new keyword is followed by the constructor for
the Person class. This allocates memory for the person1
object. The new keyword is only required to be specified with
objects and not for primitive types like int, double, etc.
What will be the output of the following code snippet?
public class MyClass2 {
private int field1 = 0;
private static int field2 = 0;
public static void main(String[] args) {
MyClass2 object1 = new MyClass2();
object1.field1++; //Line 1
object1.field2++; //Line 2
MyClass2 object2 = new MyClass2();
object2.field1++; //Line 3
object2.field2++; //Line 4
System.out.println(object2.field1); //Line 5
System.out.println(object2.field2); //Line 6
}
}
Answer:
This code will print the following output:
1
2
Here, the class MyClass2 has 2 fields, field1 and field2.
field1 is an instance field while field2 is a static field. When
it comes to instance fields, each object of a class has its own


079.
copy of instance fields. So object1 and object2 will have their
own copies of field1 and Lines 1 and 3 will cause this copy of
the field to be incremented. However, in the case of static fields,
there is only one copy of the static fields corresponding to all the
objects of the class. So, there is only one copy of field2
corresponding to object1 and object2. So, lines 2 and 4 will
increment this same copy and cause Line 6 to print the value 2.

Download 1.52 Mb.

Do'stlaringiz bilan baham:
1   ...   26   27   28   29   30   31   32   33   ...   171




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling