Top 150+ Java Interview Questions and Answers

    58 Votes

37. What is "this" reference in java?

To refer the current object, “this” keyword is used. When we want to pass the instance of the current object to a method in another object, we pass the current instance as shown below.
In case, a variable of an object and parameter name of a method in the object are same, we use "this" keyword to avoid ambiguity as shown below.
String name;
public void method1(String name){;
The "this" keyword included with parenthesis, i.e. this() with or without parameters is used to call another constructor. The default constructor for the Car class can be redefined as below
public Car(){

The this() can be called only from a constructor and must be the first statement in the constructor.

38. What are the uses of Java packages?

"To organize related set of classes and interfaces, a package is used. It also defines the scope of a class. An application consists of thousands of classes and interfaces, and therefore, it is very important to organize them logically into packages. By definition, package is a grouping of related types providing access protection and name space management." Packages enable you to organize the class files provided by Java.

39. What is the significance of import keyword?

To access variables and methods of a class(Car) in another class(Person), we need to include the Class "Car" in Class "Person". This is done using "import" keyword. The import keyword followed by fully qualified class name is to be placed before the application class definition.

import classname ;
// Class Definition.
Ex: import Java.util.Date;
// Class Definition
Above code tell JRE to import all the classes in a package. All the classes in Java.lang package are included implicitly.

40. What is user defined package in java?

When you write a Java program, you create many classes. You can organize these classes by creating your own packages. The packages that you create are called user-defined packages. A user-defined package contains one or more classes that can be imported in a Java program.

package land.vehicle;
public class Car
String brand;
String color;
int wheels;

41. How to compile a Java Source file ?

From the command prompt, Go to the location in which Java file is located. Enter command “javac” followed by Java source file name. For example, To compile person class, type command “javac” . Class files will be generated in the same location

42. How to compile all the files in a Folder?

javac *.java”

43. How to place compiled Java file in a different location ?

To compile Java classes and place all the files in a different location use command “javac -d ../Java/All/*.java

44. How to run a compiled  Java file ?

Go to the location in which Java file is located from command prompt. Enter command “java” followed by the class name. For example, to execute Employee class, type command

java Employee

45. Why Information hiding is used and how Information hiding is implemented in Java ?

In order to avoid direct access of an attribute in a class, attributes are defined as private and use getter and setter methods to restrict the attributes access in a class. An example is given below
public class Person {
    private String FirstName;

    public String getFirstName() 
        return FirstName;
    public void setFirstName(String FirstName) 
        this.FirstName = FirstName;

46. What is a Constructor ?

The default constructor is called automatically, when an object of that class is created. A constructor has the same name as the that of the class.

A constructor always returns objects of the class, hence there is no return type specified in it's definition. A constructor is most often defined with the accessibility modifier “public” so that every program can create an instance but is not mandatory. A constructor is provided to initialize the instance variables in the class when it is called.

If no constructor is provided, a default constructor is used to create instances of the class. A default Constructor initializes the instance variables with default values of the data types. If at least one constructor is provided, the default constructor is not provided by the JVM. A class can have multiple constructors.

class Car {
String brand;
String color;
public Car() {
public Car(String b, String c, int w, int s, int g)
void accelerate(){}
void brake(){}
Page 4 of 14

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers
Java Thread Interview Questions and Answers