Java is a multi-platform, object-oriented, and network-centric language that can be used as a platform in itself. It is a fast, secure, reliable programming language for coding everything from mobile apps and enterprise software to big data applications and server-side technologies.
Java is an extremely transferable programming language used across platforms and different types of devices, from smartphones to smart TVs. It's used for creating mobile and web apps, enterprise software, Internet of Things (IoT) devices, gaming, big data, distributed, and cloud-based applications among other types.
Java Interview Questions and Answers
core Java concepts, Object-Oriented Programming (OOP), multithreading, exception handling, design patterns, Java Collections, and more, that will surely help you to crack Java interviews.
Q.1) What is Encapsulation?
Answer 1: Encapsulation provides objects with the ability to hide their internal characteristics and behavior. Each object provides a number
of methods, which can be accessed by other objects and change its internal data. In Java, there are three access modifiers: public, private and protected. Each modifier imposes different access rights to other classes, either in the same or in external packages.
Some of the advantages of using encapsulation are listed below:
• The internal state of every objected is protected by hiding its attributes.
• It increases usability and maintenance of code, because the behavior of an object can be independently changed or extended.
• It improves modularity by preventing objects to interact with each other, in an undesired way.
You can refer to our tutorial here for more details and examples on encapsulation.
Q.2) What is Polymorphism?
Answer 2: Polymorphism is the ability of programming languages to present the same interface for differing underlying data types. A polymorphic type is a type whose operations can also be applied to values of some other type.
Q.3) What is Inheritance?
Answer 3: Inheritance provides an object with the ability to acquire the fields and methods of another class, called base class. Inheritance provides re-usability of code and can be used to add additional features to an existing class, without modifying it.
Q.4) What is Inheritance?
Answer 4: Abstraction is the process of separating ideas from specific instances and thus, develop classes in terms of their own functionality, instead of their implementation details. Java supports the creation and existence of abstract classes that expose interfaces, without including the actual implementation of all methods. The abstraction technique aims to separate the implementation details of a class from its behavior.
Q.5) Differences between Abstraction and Encapsulation ?
Answer 5: Abstraction and encapsulation are complementary concepts. On the one hand, abstraction focuses on the behavior of an object. On the other hand, encapsulation focuses on the implementation of an object’s behavior. Encapsulation is usually achieved by hiding information about the internal state of an object and thus, can be seen as a strategy used in order to provide abstraction.
Q.6) Can there be an abstract class with no abstract methods in it? Answer: Yes ?
Answer 6: Yes.
Q.7) Can an Interface be final?
Answer 7: No.
Q.8) Can an Interface have an inner class?
Answer 8: Yes.
Example
public interface abc { static int i=0;
void dd(); class a1 { a1() {
int j;
System.out.println(“in interfia”);
};
public static void main(String a1[])
{
System.out.println(“in interfia”);
}
}
}.
Q.9) Can there be an abstract class with no abstract methods in it?
Answer 9: Yes.
Q.10) Can an Interface be final?
Answer 10: No.
Q.11) Can we define private and protected modifiers for variables in interfaces?
Answer 11: select * from information_schema.tables.
Q.12)What is the query used to display all tables names in SQL Server (Query analyzer)?
Answer 12: Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in).
Q.13) What is Externalizable?
Answer 13: Only public and abstract modifiers are allowed for methods in interfaces.
Q.14) What modifiers are allowed for methods in an Interface?
Answer 14: Variables declared within a method are “local” variables. Variables declared within the class i.e not within any methods are “member” variables (global variables). Variables
declared within the class i.e not within any methods and are defined as “static” are class variables.
Q.15) What is a local member and a class variable?
Answer 15: There are 4 types of JDBC Drivers
Type 1: JDBC-ODBC Bridge Driver Type 2: Native API Partly Java Driver Type 3: Network protocol Driver Type 4: JDBC Net pure Java Driver.
Q.16) How many types of JDBC Drivers are present and what are they?
Answer 16: No.
Q.17) Can we implement an interface in a JSP?
Answer 17:ServletContext: Gives the information about the container PageContext: Gives the information about the Request.
Q.18) What is the difference between ServletContext and PageContext?
Answer 18: request.getRequestDispatcher(path): In order to create it we need to give the relative path of the resource context.getRequestDispatcher(path): In order to create it we need to give the absolute path of the resource.
Q.19) What is the difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?
Answer 19: Using <%jsp:param> tag.
Q.20) How to pass information from JSP to included JSP?
Answer 20: <%@ include> : Used to include static resources during translation time.
: Used to include dynamic content or static content during runtime.
Q.21) What is the difference between directive include and jsp include?
Answer 21: RequestDispatcher: server-side redirect with request and response objects. sendRedirect : Client-side redirect with new request and response objects.
Q.22) What is the difference between RequestDispatcher and sendRedirect?
Answer 22: Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.
Q.23) How does JSP handle runtime exceptions?
Answer 23:
Cookie mycook = new Cookie(“name”,”value”); response.addCookie(mycook);
Cookie killmycook = new Cookie(“mycook”,”value”).
Q.24) How do you delete a Cookie within a JSP?
killmycook.setMaxAge(0); killmycook.setPath(“/”); killmycook.addCookie(killmycook);
Answer 24: Method overloading in Java occurs when two or more methods in the same class have the exact same name, but different parameters. On the other hand, method overriding is defined as the case when a child class redefines the same method as a parent class.
Overridden methods must have the same name, argument list, and return type. The overriding method may not limit the access of the method it overrides.
Q.25) What is Function Overriding and Overloading in Java ?
Answer 25: A constructor gets invoked when a new object is created. Every class has a constructor. In case the programmer does not provide a constructor for a class, the Java
compiler (Javac) creates a default constructor for that class. The constructor overloading is similar to method overloading in Java. Different constructors can be created for a single class. Each constructor must have its own unique parameter list. Finally, Java does support copy constructors like C++, but the difference lies in the fact that Java doesn’t create a default copy constructor if you don’t write your own.
Q.26) What is a Constructor Constructor Overloading in Java and Copy-Constructor?
Answer 26: No, Java does not support multiple inheritance. Each class is able to extend only on one class, but is able to implement more than one interfaces. That means Multiple inheritance can be achieved using interface.
Q.27) Does Java support multiple inheritance ?
Answer 27: Java provides and supports the creation both of abstract classes and interfaces.
Both implementations share some common characteristics, but they differ in the following features:
• All methods in an interface are implicitly abstract. On the other hand, an abstract class may contain both abstract and nonabstract methods.
• A class may implement a number of Interfaces, but can extend only one abstract class.
• In order for a class to implement an interface, it must implement all its declared methods. However, a class may not implement all declared methods of an abstract class. Though, in this case, the sub-class must also be declared as abstract.
• Abstract classes can implement interfaces without even providing the implementation of interface methods.
• Variables declared in a Java interface is by default final. An abstract class may contain non- final variables.
• Members of a Java interface are public by default. A member of an abstract class can either be private, protected or public.
• An interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be invoked if it contains a main method.
Q.28) What is the difference between an Interface and an Abstract class ?
Answer 28: When an object is passed by value, this means that a copy of the object is passed. Thus, even if changes are made to that object, it doesn’t affect the original value. When an object is passed by reference, this means that the actual object is not passed, rather a
reference of the object is passed. Thus, any changes made by the external method, are also reflected in all places.
Q.29) What are pass by reference and pass by value ?
Answer 29:
Pass-by-reference: When a method is called, the method arguments reference the same variable in memory as the caller. Pass-by-value: When a method is called, the caller passes a copy of the argument variables to the method resulting in two values in memory.