JAVA OOPS CODING AND PROGRAMMING INTERVIEW QUESTIONS AND ANSWERS

 

 

1.Q. Write a Java program to accept three numbers and find the largest of the three using polymorphism concept of method overloading.

 

 

 

Solution:

 

public  class  LargestNumber{

  //Method  to  find  the  largest  of  three  integers  using  method  overloading

   public  int   findLargestNum( int a , int  b , int  c ) {

     int    largest=a;

     if(b > largest )  {

              largest=b;

    }

else  if (c>largest){

       largest=c;

       }

  return   largest;

      }

 

//Method   to  find  the  largest of  three  double  numbers    using  method  overloading

 

    public   double    findLargest (  double  a , double b ,  double  c ) {

        double    largest=a;

        if(b>largest){

           largest=b;

          }

     else  if( c>  largest ) {

          largest=c;

          }

     return  largest;

    }

 

public  static  void  main ( String  args[] ) {

  LargestNumber     lar=  new  LargestNumber();

    int  num1=65 ,  num2=23,  num3=45;

    System.out.println(“Largest  Integer  number is:” + lar.findLargest(num1,num2,num3));

 

  //Accepting  three  double  numbers

     double  num4=65.5,  num5= 23.6,  num6=45.8;

    System.out.println(“Largest  double  number  is:”+lar.findLargest(num4,num5,num6));

}

  }

  

    

 

      

 

 

OUTPUT:

 

Largest integer  number is :  65

Largest  double  number  is:  65.5

 

 

 

 

 

2.Q.  Create  a  class  Student  with    overloaded  constructors  to  initialize  student   details  based  on  different  combination  of  arguments.  Ensure  that  the  constructors  support  the  creation  of  objects  in  various  ways.

 

 

 

Solution:

class    Student{

private  String  sname;

private   int   rollno;

private  String   address;

//Constructor  to  initialize   only  student  name

public   Student(String  sname) {

       this.sname=sname;

     }

//constructor  to  imitialize  student  name  and   roll  no  of  student

public  Student(String  sname , int  rollno){

   this.sname=sname;

   this.rollno=rollno;

    }

//constructor  to  initialize  student  name , roll no  and  address  of  the  student

   public  Student(String  sname , int  rollno , String address){

this.sname=sname;

 this.rollno=rollno;

this.address=address;

}

//Method  to  display   Student  details

public  void  displayDetails() {

      System.out.println(“Student     name”+sname);

 if(rollno!=0){

System.out.println(“Roll no is :” +rollno);

   }

if(address!=null){

 System.out.println(“Address  is :”+address);

    }

}

 

//Main  method  to  test  Student  class

 public  static  void  main(String  args[])  {

  Student    stu1=new  Student(“joy”);

  stu1.displayDetails();

  Student   stu2= new  Student(“joy”, 189);

  stu2.displayDetails();

  Student  stu3= new  Student(“felix”,134,”new york”);

   stu3.displayDetails();

   }

}

 

OUTPUT:

Student  name :  joy

Student  name: joy

Roll no is :  189

Student  name : felix

Roll no  is :  134

Address  is :  new  york

 

Leave a Comment

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