Our Blog

BY : developer 0 comment

Selected Java Interview Questions Part – 1

1) What is constructor?

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

eg:

class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}

void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,”Karan”);
Student4 s2 = new Student4(222,”Aryan”);
s1.display();
s2.display();
}
}

Output:

111 Karan
222 Aryan

2) What is Inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

Syntax:

class Subclass-name extends Superclass-name
{
//methods and fields
}

Eg:

In this example Programmer is the subclass and Employee is the superclass. Relationship between two classes is Programmer IS-A Employee. It means that Programmer is a type of Employee.

class Employee{
float salary=40000;
}

class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println(“Programmer salary is:”+p.salary);
System.out.println(“Bonus of Programmer is:”+p.bonus);
}
}

Output:

Programmer salary is: 40000.0
Bonus of Programmer is: 10000

3) Why multiple inheritance is not supported in java?

To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.

Consider a scenario where A, B and C are three classes. The C class inherits A and B classes. If A and B classes have same method and you call it from child class object, there will be ambiguity to call method of A or B class.

Since compile time errors are better than runtime errors, java renders compile time error if you inherit 2 classes. So whether you have same method or different, there will be compile time error now.

class A{
void msg(){System.out.println(“Hello”);}
}

class B{
void msg(){System.out.println(“Welcome”);}
}

class C extends A,B{//suppose if it were
public static void main(String args[]){
C obj=new C();
obj.msg();//Now which msg() method would be invoked?
}
}

Output:

Compile time error

4) Why Java does not support pointers?

Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe (unsecured) and complex to understand. Usage of pointers leads to illegal access of data. It shows the exact address of the data and using this address any modifications can be made to the information stored which leads to lack of security. Java is a secured and robust language. This is why pointers was removed from java. There are reference variables in java which act in a similar ways like pointer, that is, holding the address of an object.

5) What is super in java?

It is a keyword that refers to the immediate parent class object.

Eg:

//without using super keyword

class Vehicle{
int speed=50;
}

class Bike3 extends Vehicle{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}

public static void main(String args[]){
Bike3 b=new Bike3();
b.display();
}
}

Output: 100

//example of super keyword

class Vehicle{
int speed=50;
}

class Bike4 extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}

public static void main(String args[]){
Bike4 b=new Bike4();
b.display();
}
}

Output:

50

6) What is method overloading?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.

In this example, we have created two overloaded methods, first sum method performs addition of two numbers and second sum method performs addition of three numbers.

class Calculation{
void sum(int a,int b){System.out.println(a+b);}
void sum(int a,int b,int c){System.out.println(a+b+c);}
public static void main(String args[]){
Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);
}
}

Output:

30

40

In this example, we have created two overloaded methods that differ in data type. The first sum method receives two integer arguments and second sum method receives two double arguments.

class Calculation2{
void sum(int a,int b){System.out.println(a+b);}
void sum(double a,double b){System.out.println(a+b);}
public static void main(String args[]){
Calculation2 obj=new Calculation2();
obj.sum(10.5,10.5);
obj.sum(20,20);
}
}

Output:

21.0

40

7) Can we overload main() method?

Yes, You can have many main() methods in a class by overloading the main method.

Eg:

class Overloading1{
public static void main(int a){
System.out.println(a);
}

public static void main(String args[]){
System.out.println(“main() method invoked”);
main(10);
}
}

Output:

main() method invoked

10

For IPSR MEDIA:
Rudit Mathews | Java + Android Student ( IT FINISHING SCHOOL) | IPSR INTERNATIONAL CENTRE, KOCHI ( Edappally)

List of Authors

Leave a Reply

Your email address will not be published.

Tags

#aintegrateddigitlmarketing##ansibleautomates#AWS#blog#cicd#Container#DO180#DO280#ipsronlinetraining#kubernetes#OpenShift#OpenShiftTraining#pythonindemand##redhatautomation#redhatcertification#redhatcertification #redhatlinux #redhatsystemadministration #ansibleautomates #containers #kubernetes #RHCSA #RHCE #DO180 #DO280 #ipsr #ipsronlinetraining #openshift#RedHatLearningSubscription#redhatlinux#RedHatOpenShift#redhatsystemadministration#RedHatTraining#RHCE#RHCSA#RHLS#RHLSPremium#tiktokanalyticsandroidansiblearticleArtificial IntelligenceASP.NETaws online trainingbacklinkboot campcareercareer advancementcareer opportunitycertificationcloudclougcontainerscybersecurityCyberSecurityCertificationData Analysts in 2024data analyticsdata analytics certificationdata analytics trainingdemandDev-OpsDevopDevOpsDigital marketingdigital marketing courseDigital Marketing Salary in IndiaExam resultsfiles typesForrester ResearchindiaInfluencer MarketingInstagraminterview questionsinterview quetioninterview techniqueIOTipsrITit careerIT Finishing schoolsIT jobsIT proffessionalsITFSjavajob interviewjob opportunitiesjob opportunitiessjobskeywordKMEA Collegelearn python onlinelink buildingLinuxlinux online trainingLinux System Administrationmachine learningMastering DevOpsnetworkingonline python trainingopen sourcephytonplacementsPrivate Cloudpythonpython certificationpython certification coursePython in 2024python trainingRankingsRed HatRed Hat Academyred hat linuxRed Hat Linux TrainingredhatresultsRHCARHCE certificationrolessocial media marketing online coursesoftwarestudent poststudents postsuccess storiestablueThreads by Instagramtraining