Top 15 Java interview Questions & Answers - Part 1 | textpound

  Top 15 Java Interview Questions & Answers



Top 15 Java interview Questions & Answers




1. What is a Constructor in java ?


Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.


2. What is platform?



A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform.


3.  Is String a data type in java?

String is not a primitive data type in java. When a string is created in java, it's actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object.






4. Why does Java not support pointers?



The 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.


5. How many Types of java constructors ?


There are two types of constructors:
Default constructor
Parameterized constructor






types of java constructors
types of constructors






6. What is this in java?


In java, this is a reference variable that refers to the current object



class User {

int userId;

String userName;



User(String userName, int UserId){

this.userId = i;

this.userName = n;

}

void display(){System.out.println(“user name is ”+userName+” Id is ”+userId);}



public static void main(String args[]){

User obj1 = new User (“david”,333);

User obj2= new User ("hook”,121);

obj1.display();

obj2.display();

}

}

In the above example, parameter and instance variables are same that is why we are using this keyword to distinguish between local variable and instance variable.

7. Can variables be used in Java without initialization?


In Java, if a variable is used in a code without prior initialization by a valid value, program doesn't compile and gives an error as no default value is assigned to variables in Java. 

 
8.What is the purpose of default constructor?


Default constructor provides the default values to the object like 0, null etc. depending on the type.
Example of default constructor

class User {

User (){System.out.println("default constructor is called ");}

public static void main(String args[]){

User a=new User ();

}



9. Why Java is platform independent?


Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

10. Is constructor inherited?


No, constructor is not inherited.


11.  What is method overloading?


When a class has more than one methods with the same name but different number, sequence or types of arguments then it is known as method overloading. 

12. What is Inheritance?


Inheritance is a mechanism in which one object inherit all the properties and behaviour of another object of another class.
Syntax of Java Inheritance

 class Subclass-name extends Superclass-name

{

 //methods and fields

 }




The extends keyword indicates that you are making a new class that derives from an existing class.A class that is inherited is called a super or parent class. The new class is called a subclass or child class.

 class Teachers{
 int salary=25000;

 }

 class Bonus extends Techer {

 int bonus=5000;

 public static void main(String args[]){

 Bonus p=new Bonus();

 System.out.println("Techer salary is:"+p.salary);

       System.out.println("Bonus of Techer is:"+p.bonus);

 }

 }



 In the above example, Bonus class inherit all the properties of Teacher class.



13. Does Java support operator overloading?




Operator overloading is not supported in Java.



14. Why does Java not support pointers?




The 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.








Post a Comment

1 Comments