Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Java Interview Questions and answers

1. What are JVM, JRE, and JDK?

Java Virtual Machine (JVM) is part of JDK and JRE. It is responsible for the execution of Java applications.

JRE (Java Runtime Environment) – provides the minimum requirements for Java application execution, includes Java Virtual Machine, core classes, and some supporting files.

The Java Development Kit (JDK) is an environment used for developing Java applications, consists of Java Runtime Environment, interpreter, compiler, archiver, documentation generator, and more tools for Java development.

2. What Java Access Modifiers do you know?

Private – defines an access level within the class. You cannot access it from outside the class.

Default – defines an access level only within the package. You cannot access it from outside the package. Without specifying any access level, it will be the default.

Protected- defines the access level within the package and outside the package through child class. Without a child’s class, it cannot be accessed from another package.

Public – defines access from everywhere. You can access it from within the class, outside the class, within the package, and outside the package.

3. What are the default values of byte, int, short primitive data types in Java?

The default values of byte, int, short primitive data types is 0.

4. What are the default values of float and double datatype in Java?

The default value of float is 0.0f and of double is 0.0d.

5. How to get a primitive data type of a certain String?

Wrapper classes have methods to get numeric values from String. For example:

 int x =Integer.parseInt(“9”);

double c = Double.parseDouble(“5”);

6. What does it mean when a field or method is static?

We can access static fields and methods without an instance of the class.

Static methods don’t have access to the fields of a class instance, they cannot access keyword this (it refers to the current instance of a class). 

7. What does it mean when a variable is final?

The value of Variables declared as final can’t be modified. Therefore, you must initialize a final variable during creation. The keyword final is for constants. If the final field is a reference you cannot be re-bound it to a reference of another object, but the object pointed by that reference can be changed.

8.What are wrapper classes?

Wrapper classes are Java primitives converted into the reference types (objects). Every primitive data type has a corresponding wrapper (int – Integer, float – Float, boolean-Boolean, char – Character). Wrapper classes “wrap” the primitive data type inside an object.  

9. What if a method is final?

You cannot override a final method in a subclass. A subclass can call all the final methods of a superclass but cannot override it. As a workaround, the method can be overloaded.

10. What if a class is final?

You cannot create a subclass of a final class. That keyword prevents inherites.

11. What is a constructor in Java? What types of constructors do you know?

A constructor is a method used to initialize an object. It has the same name as its class and returns the instance of the class. But we don’t need to write any return type for constructors.

Constructor without arguments is called Default Constructor. 

It is created by default if no other constructor is defined by the user. It initializes the instance variables with the default values. Another kind of constructor of a Parameterized Constructor. A class can have as much parameterized constructors as a developer needs. We use parameterized constructors for the initialization of the instance variables with the provided values.

12. What are Anonymous classes? What do you know about them?

Anonymous class is a class that does not have a name, inner class. We do definition and instantiation for them in one single statement. An anonymous class has to implement just a single interface or to extend just a single class. An anonymous class doesn’t have a constructor, because it does not have any name.  

13. What kind of variables can be in a class?

There are local variables, instance variables, and class variables.

14. What is a Local Variable?

Local Variables are variables defined inside methods, constructors or blocks. They are declared and initialized within that aria and destroyed outside the aria.

15. What is an Instance Variable?

Variables inside a class but outside a method are called instance variables. They are created when the class is loaded.

16. What is a Class Variable?

These are variables declared inside a class, outside a method, with the static keyword.

17. What are abstract classes in Java?

In Java language,  an abstract class is a class that cannot be instantiated and may or may not contain abstract methods. Both abstract classes and methods are marked with the abstract keyword.

18. Can nonabstract classes contain abstract methods?

No. Nonabstract classes cannot contain abstract methods.

19. What are the interfaces in Java?

An interface is one of the ways to achieve abstraction in Java language. It can have fields method declarations and methods, but cannot be instantiated. 

20. What are functional interfaces?

Functional interfaces are any interfaces that contain just one abstract method. Its implementation can be a lambda expression. Default and static methods do not count as abstract. 

21. What is the best type of loop to iterate over arrays and Collection objects?

The for-each loop designed for iterating over arrays and Collection objects. 

22. What are the errors?

Errors are scenarios that cannot be handled in the application. It’s not possible to anticipate them and recover from them. As an example can be a hardware failure, JVM crash, or the out of memory error.

23. What two main groups of exceptions do you know?

Java language allows working with checked and unchecked Exceptions Exceptions.

24. What are the checked exceptions?

Checked Exception is a scenario that can be anticipated and handled in an application. For example, FileNotFoundException. We need to catch this exception and provide message for debugging purposes.

26. What are unchecked exceptions?

Runtime Exceptions are unchecked exceptions. They are caused by bad programming. For example, trying to retrieve a value from an object that is null. We will get NullPointerException. Or another scenario, we can get an element from an array by index without checking the length of the array. We can get ArrayIndexOutOfBoundException at runtime.

27. What is the purpose of keyword throws?

If your method does not handle a checked exception, you must add throws keyword and a particular exception(exceptions) to its signature. It shows that we still need to handle that checked exception in the place where the particular method is called.

28. When keyword throw is used?

A developer can throw his own exception or rethrow an exception that was just caught, by using the throw keyword.

29. How to use keyword finally in Exception Handling?

With the finally keyword we can create a block of code that will be always executed. It fits perfectly for try-catch-finally structure. Block finally will be executed even an exception occurs.

30. What is garbage collection in Java?

The garbage collector frees the memory. It cleans objects that are no longer used in an application.

31. What are immutable objects?

An immutable object can’t be changed after creation. For example, an instance of String.

32. What is a method overloading?

Overloading happens to methods with the same name. But valuable for them to have different parameters  list(number, types, order) – a different signature.

33. What is a method overriding?

We can override a method of a superclass in its subclass. Both methods have to have the same name and exactly the same number, order and type of parameters, and same return type.

34. What is the thread?

A thread is a subprocess that a separate path of execution by running in a different stack frame. Multiple threads can exist in one process. Threads share the process resources, but still, they execute independently.

35. What are the two ways to create a thread in Java?

There are two ways to create a thread:

  • By extending the Thread class
  • By implementing the Runnable interface.

36. What is the daemon threads?

The daemon thread is a thread with low priority. It provides the support and services to the user threads on a background.

37. What is synchronization?

Synchronization is the way to handle threads work on any shared resource to prevent consistency problems and thread interference.

38. What is deadlock?

Deadlock is a blocking of a set of threads that are competing for a set of resources. A thread can make progress, but it does not mean that there is not a deadlock somewhere else.

39. What is race-condition?

A Race condition problem occurs in applications that have multithreading. It is when various threads are simultaneously accessing a shared resource at the same time. The use of synchronization avoids the Race condition.

40. What is JDBC?

The Java Database Connectivity is a free open-source application programming interface. It is a standard Java API specifying interfaces for connectivity between the Java applications and a wide range of databases. The JDBC contains methods to connect to a database server, send queries to create a table, update, delete, insert records in a database,  retrieve and process the results obtained from a database.

The post Java Interview Questions and answers appeared first on H2kinfosys Blog.



This post first appeared on It Online Training Courses, please read the originial post: here

Share the post

Java Interview Questions and answers

×

Subscribe to It Online Training Courses

Get updates delivered right to your inbox!

Thank you for your subscription

×