ITBots.in
ITBots.in
  • Home
  • About
  • Jobs
  • Events
  • Courses
  • Exams
  • Blog
  • Recruiter
  • Pricing
Sign In
Applicant Recruiter/Advertiser

Java Interview Questions With Answers (For freshers)

Go back
  • Sumasri Sumasri
  • Sep 22, 2022
Share on Facebook Share on Twitter Share on LinkedIn Share to Whatsapp
Report an Issue   
Java Interview Questions With Answers (For freshers)

Java is a computer programming language and platform. As one of the most popular programming languages, it offers a wide variety of job opportunities.
If you are a fresher and preparing for a Java interview then please keep on reading. We have prepared a collection of questions to be asked in the interview which can help you with the preparation.
 

1. Why is Java a platform-independent language?

Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems. The only condition to run that byte code is for the machine to have a runtime environment (JRE) installed in it
 

2. Why is Java not a pure object-oriented language?

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

 

3. What is the minimum requirement to run the java program from JVM JDK and JRE?

 JDK, because it includes JRE and some other files too.

 

4. Can java be said to be the complete object-oriented programming language?

It is not wrong if we claim that java is the complete object-oriented programming language. Because Everything in Java is under the classes. And we can access that by creating the objects. But also if we say that java is not a completely object-oriented programming language because it has the support of primitive data types like int, float, char, boolean, double, etc.

Now for the question: Is java a completely object-oriented programming language? We can say that - Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don't directly belong to the Integer classes.

 

5. How is Java different from C++?

C++ is only a  compiled language, whereas Java is compiled as well as an interpreted language.
Java programs are machine-independent whereas a c++ program can run only in the machine in which it is compiled. 
C++ allows users to use pointers in the program. Whereas java doesn’t allow it. Java internally uses pointers. 
C++ supports the concept of Multiple inheritances whereas Java doesn't support this. And it is due to avoiding the complexity of name ambiguity that causes the diamond problem.

 

6. Pointers are used in C/ C++. Why does Java not make use of pointers?

Pointers are quite complicated and unsafe to use by beginner programmers. Java focuses on code simplicity, and the usage of pointers can make it challenging. Pointer utilization can also cause potential errors. Moreover, security is also compromised if pointers are used because the users can directly access memory with the help of pointers.

Thus, a certain level of abstraction is furnished by not including pointers in Java. Moreover, the usage of pointers can make the procedure of garbage collection quite slow and erroneous. Java makes use of references as these cannot be manipulated, unlike pointers.

 

7. Does the constructor return any value?

The constructor doesn’t return any value as it does not have any return type.

 

8.  Can we make a constructor final?

  •  Final means no changes be made once initialized so to do use the final keyword with the constructor could cause a problem.

  •  The constructor cannot be final because the constructor cannot be inherited and in the constructor, values are initialized to the variable so this variable changes every time.

  •  A constructor can’t be inherited so can’t be overridden, so there is no use in making the constructor final.

 

9. What is static in java?

Static is a non-access modifier in java which is applicable for the following

a. Blocks

b. Variables

c. Method

d. Nested classes

 

10. Can we override a static method?

  • No, we cannot override a static method java.

  • If a derived class defined a static method with the same signature as the static method in the base class, the method in the derived class hides the method in the base class.

 

11. Inheritance in java?

  • The process by which one class acquires the properties ( data members ) and functionalities ( Method ) of another class is called inheritance.

  • An object which acquired the properties of another object is called inheritance properties means code & data/method & fields.

  • Type of Inheritance.

a. Single Inheritance

b. Multilevel Inheritance

c. Hierarchical Inheritance

d. Hybrid Inheritance

 

12. Why multiple inheritance is not possible in java?

  •  Java does not support multiple inheritances.

  •  Suppose C is the child class extending from both parent class A and parent class B with the same method defined in them. Then child class cannot understand which class method to call. So there is confusion here which leads to ambiguity and leads to compile time error.

  •  This is the reason java does not support multiple inheritances

  •  Java does not support multiple inheritances. For this java has an interface

 

13.  Difference between abstract class and inheritance

Abstraction Class Interface

  •  A class which is declared with the abstract keyword is known as an abstract class in java The interface in java is a mechanism to achieve abstraction, It can’t have a method body

  •  An abstract class can have abstract and non-abstract method Interface can have only abstract methods

  •  An abstract class can have final, non-final, static and not-static variables Interface has only static and final variable

  •  An abstract class may contain non-final variables Variables declared in a java interface are by default final

  •  An abstract class can be extended using the keyword “extends” Interface can be implemented using the keyword “implements”

  •  A java abstract class can have class members like private, protected, etc Member of a java interface are public by default

  • Abstract class can extend another java class and implement multiple java interface

  • Interface can extend another java interface only

 

14.  What do you understand by an instance variable and a local variable?

Instance variables are those variables that are accessible by all the methods in the class. They are declared outside the methods and inside the class. These variables describe the properties of an object and remain bound to it at any cost.

All the objects of the class will have their copy of the variables for utilization. If any modification is done on these variables, then only that instance will be impacted by it, and all other class instances continue to remain unaffected.

Example:

class Athlete {
public String athleteName;
public double athleteSpeed;
public int athleteAge;
}

Local variables are those variables present within a block, function, or constructor and can be accessed only inside them. The utilization of the variable is restricted to the block scope. Whenever a local variable is declared inside a method, the other class methods don’t have any knowledge about the local variable.

Example:

public void athlete() {
String athleteName;
double athleteSpeed;
int athleteAge;
}

 

15. What are the default values assigned to variables and instances in java?

  • There are no default values assigned to the variables in java. We need to initialize the value before using it. Otherwise, it will throw a compilation error of (Variable might not be initialized). 

  • But for instance, if we create the object, then the default value will be initialized by the default constructor depending on the data type. 

  • If it is a reference, then it will be assigned to null. 

  • If it is numeric, then it will assign to 0.

  • If it is a boolean, then it will be assigned to false. Etc.

 

16. What do you mean by data encapsulation?

  • Data Encapsulation is an Object-Oriented Programming concept of hiding the data attributes and their behaviours in a single unit.

  • It helps developers to follow modularity while developing software by ensuring that each object is independent of other objects by having its own methods, attributes, and functionalities.

  • It is used for the security of the private properties of an object and hence serves the purpose of data hiding.

 

17.  How is an infinite loop declared in Java?

Infinite loops are those loops that run infinitely without any breaking conditions. Some examples of consciously declaring infinite loop is:

  • Using For Loop:
for (;;)
{
   // Business logic
   // Any break logic
}
  • Using while loop:
while(true){
   // Business logic
   // Any break logic
}
  • Using do-while loop:
do{
   // Business logic
   // Any break logic
}while(true);

 

18. Define Copy constructor in java.

Copy Constructor is the constructor used when we want to initialize the value to the new object from the old object of the same class. 

class InterviewBit{
   String department;
   String service;
   InterviewBit(InterviewBit ib){
       this.departments = ib.departments;
       this.services = ib.services;
   }
}
Here we are initializing the new object value from the old object value in the constructor. Although, this can also be achieved with the help of object cloning.

 

20. Explain the use of the final keyword in variable, method and class.

In Java, the final keyword is used as defining something as constant /final and represents the non-access modifier.

  • final variable:
    • When a variable is declared as final in Java, the value can’t be modified once it has been assigned.
    • If any value has not been assigned to that variable, then it can be assigned only by the constructor of the class.
  • final method:
    • A method declared as final cannot be overridden by its children's classes.
    • A constructor cannot be marked as final because whenever a class is inherited, the constructors are not inherited. Hence, marking it final doesn't make sense. Java throws compilation error saying - modifier final not allowed here
  • final class:
    • No classes can be inherited from the class declared as final. But that final class can extend other classes for its usage.

 

21. Why is the main method static in Java?

The main method is always static because static members are those methods that belong to the classes, not to an individual object. So if the main method will not be static then for every object, It is available. And that is not acceptable by JVM. JVM calls the main method based on the class name itself. Not by creating the object.

Because there must be only 1 main method in the java program as the execution starts from the main method. So for this reason the main method is static. 

 

22. What is a ClassLoader?

Java Classloader is the program that belongs to JRE (Java Runtime Environment). The task of ClassLoader is to load the required classes and interfaces to the JVM when required.

 

23. How can we implement any code before the main method?

When we need to implement any statements even before object creations at load time of class, we use a static block of code in the class. Any kind of statements which is inside this static block of code gets implemented once at the time of loading the class even before object creation in the main method.

 

24. What are the two methods of executing multi-threading in Java?

Multi-threaded applications are built in Java in any of the following two ways:

  • With Java.Lang.Runnable Interface. Classes execute this interface to authorize multi-threading. There occurs a Run() method in the interface that is executed.

  • Through writing a class which expands Java.Lang.Thread class.

 

25. How can garbage collection be done in Java?

If an object is not referenced anymore in Java, the garbage collection takes place and the object has been eradicated automatically. Java calls either Runtime.gc method or System.gc() method for automatic garbage collection.

 

26. What is the difference between Vector and an array?

Vectors are said to be dynamic in nature and hold the data of various data types while an array gathers data of a similar primitive type that is static in nature.

 

27. In java why strings are known as Immutable?

String objects are known as immutable in java once the value is assigned to a string, it cannot be changed. If it is changed, a new object is created.

 

28. Define multi-threading

It is one of the significant and frequently asked Java Interview Questions

Multi-threading is said to be a programming concept to generate multiple tasks within a single program in a concurrent manner. Threads share a stack of the same process and run in parallel. It helps in the performance enhancement of any program.

 

29. Why is the Runnable Interface used in Java?

A runnable interface has been used in java for executing multi-threaded applications. Java.Lang.A runnable interface is executed by a class to support multi-threading.

 

30. Does main() method in java return any data?

The main() method cannot return back any data and thus, it is always declared with a void return type in Java.

 

 

 

×
  • Home
  • About
  • Jobs
  • Events
  • Courses
  • Exams
  • Blog
  • Recruiter
  • Pricing

  Applicant

  • Login
  • Register
  • Forgot Password

* By proceeding you agree to our Privacy Policy and Terms of Use
*
*
*
*
Password should contain one upper case,one lower case,one number and one special character with 8-30 characters.

* By proceeding you agree to our Privacy Policy and Terms of Use

  Advertiser

  • Login
  • Register
  • Forgot your password

* By proceeding you agree to our Privacy Policy and Terms of Use
*
*
*
*
Password should contain one upper case,one lower case,one number and one special character with 8-30 characters.

* By proceeding you agree to our Privacy Policy and Terms of Use
  • about
  • privacy
  • terms of use
  • careers
  • contact us
  • sitemap

© 2021 Botmatrix Services Private Limited. All Rights Reserved.