Accenture Java Interview Questions
1. What is OOP concept?
OOP stands for Object-Oriented Programming. Object-Oriented Programming is a coding practice
which works with objects and class. Java is one of the programming languages which is based on
these concepts. The basic OOP concepts are:
- Object
- Class
- Inheritance
- Polymorphism
- Abstraction
- Encapsulation
2. Explain the basic features of OOPs?
- Object: An object is a physical entity which has a state and behaviour. It occupies
space in memory. It is a sample of a class. Object helps to access the methods and variables
in the program.
- Class A Class is "collection of objects." A class is a logical entity, which does not
take any space. A class includes all the data and methods which shows the behaviour of an
object.
- Inheritance Inheritance is a process by which one class can have all properties of
other class. Inheritance increases the code reusability.
- Polymorphism: Polymorphism is a method of performing "a single task in different
ways." Polymorphism enables a programmer to use methods or operators in different ways. In
Java, we use method overloading and overriding to obtain the polymorphism.
- Abstraction: If we show only functionality and hide the explanations or details then
this process is called as Abstraction.
- Encapsulation: Encapsulation is a process of enclosing the data and code together to
form a single unit. It makes the data safer within the code for any modification. For
achieving the encapsulation, we can declare the data variables of class as private.
3. Why is Java called platform independent?
Java is platform independent that means we can execute our code in any operating system either it
is mac, Window or Linux. Java is Platform independent because when we write code, then its
compiler converts it into bytecode and this bytecode can be executed on any platform (JDK should
be installed in that OS).
4. Differentiate between class and object.
The class and object both are the features of OOPs concepts. The basic differences between both
features are given below:
- The Class is a logical entity whereas Object is a physical quantity.
- Class does not occupy memory at the time of creation whereas Object occupied space in memory
when it is created.
- For declaring a class, we use a 'class' keyword followed by a class name, whereas we can
create the object using the 'new' keyword in Java.
- A Class is like a factory which generates object and object are the instances of the class.
5. What is encapsulation in Java?
Encapsulation is a process of enclosing the data and code together to form a single unit. The
best example to understand the encapsulation is a capsule which contains the medicine in it.
- If we declare all the data members of the class as private, then it is called a fully
encapsulated class in Java, and then we can use getter and setter method to access it.
- One of the examples of the fully encapsulated class is Java Bean class.
- Encapsulation keeps its data hide from other class hence it is also called as data-hiding.
6. What is Recursion and recursive function in Java?
Recursion is a process of calling a method by itself continuously till not get termination point.
A method which calls itself is called as a recursive method.
Return-type method_name()
{
// Code to be executed
method_name(); // same name of calling method }
7. What do you understand by Exception Handling?
Exception handling is a process of handling exceptions occurs during the execution of a program.
Due to the occurrence of exception, execution of programs get halted, so it is very important to
handle these exceptions so that program can be executed smoothly. We can handle the exceptions
by using five keywords: try, catch, throw, throws, and finally.
8. What is checked and unchecked exception?
- Checked exception: If the exception occurs or checked at compile time during the
execution
of a program, it is called as the checked exception. We should handle these exceptions using
try-catch block or using throws keyword.
E.g., if someone tries to read a file which is not present then it will throw a checked
exception at compile time FileNotFoundException
- Unchecked exceptions: If the exception is not checked at compile time and occurred at
runtime then this type of exception is called an unchecked exception. This type of
exceptions occur due to an error in the logic of the code. If we do not handle this type of
exception then also compiler will not give a compilation error.
9. What are the reasons behind the occurrence of an exception?
- Accessing a file, which does not exist
- Dividing a variable by zero
- Inserting an element in the array outside the range
- If throw statement occurs
- Abnormal execution condition captured by JVM
10. What is the difference between abstract classes and interfaces?
An abstract class allows creating functionality that subclasses can implement or override. An
interface only allows defining functionality, not implementing it and whereas a class can extend
only one abstract class, it can take advantage of multiple interfaces.
11. What do you understand by runtime polymorphism?
Polymorphism is a method of performing "a single task in different ways". Polymorphism is of
two types
- Runtime Polymorphism
- Compile-time polymorphism
12. What are the keyword "super" and "this" in Java?
- super keyword: "super" is a keyword in Java which is used to give reference to the object of
parent class. "super" keyword cannot be used as an identifier as it is reserved keyword in
Java.
- this Keyword: "this" keyword in Java is used to refer to the object of the current class.
The 'this' keyword cannot be used as an identifier as it is a reserved keyword in Java.
13. What is an interface in Java? Can we implement multiple interfaces in one class?
Interface in Java is a way to achieve the abstraction. The Interface is like a class but not
exactly as Interface also can have methods and variable as the class does but Interface only
contain method signature does not have the body.
- The Interface cannot be instantiated in Java.
- The Interface contains methods which are public and abstract (by default).
- For declaring an interface, we use the keyword interface.
Syntax:
interface Interface_Name
{
//Methods
}
14.What can we do if we want to access a private member of a class?
We can access private members of the class by using public getters and setters from outside the
class in Java.
15. What is the significance of "static" keyword?
- Static keyword in Java is a non-access modifier which can be used with the block, variable,
methods, and nested classes.
- Static Keywords are the part of the class, and it does not belong to the instance of the
class.
- We use static keyword in java with variables, block, and method for achieving memory
management
- Java static property can be shared by all the objects.
16. What is a copy constructor in Java?
Copy constructor is a special type of constructor that creates an object using another object of
the same Java class. It returns a duplicate copy of an existing object of the class. We can
assign a value to the final field but the same cannot be done while using the clone() method.
17. What is "Collection Framework" in Java?
Collection Framework in Java is an architecture for storing the classes, and interfaces and
manipulating the data in the form of objects. There are two main interfaces in Collection
Framework that are:
- Java.util.Collection
- Java.util.Map
18. What is List interface in collections?
List interface is an interface in Java Collection Framework. List interface extends the
Collection interface.
- It is an ordered collection of objects.
- It contains duplicate elements.
- It also allows random access of elements.
Syntax:
public interface List extends Collection
19. What do you understand by object cloning?
Object cloning is a mechanism of creating the same copy of an object. For object cloning, we can
use clone() method of the Object class. The class must implement the java.lang.Clonable
interface, whose clone we want to create otherwise it will throw an exception.
Syntax of clone() method:
protected Object clone() throws CloneNotSupportedException
20. Can we insert duplicate values in Set?
We cannot insert duplicate elements in Set. If we add a duplicate element, then the output will
only show the unique elements.
21. What is the difference between Collections, and Collection in Java?
Collection and collections both are the part of Java Collection Framework, but the primary
differences between both are given below:
- A Collection is an interface in java and Collections is a class of collection framework.
- The Collection interface provides the methods that can be used for data structure whereas
Collections class provides the static methods which can be used for various operation on a
collection.
22. What is "Diamond problem" in Java? How can it be removed?
The Diamond problem occurs in multiple inheritance, but Java does not allow multiple inheritance.
In case of Java, it can occur with interfaces. When we implement two interfaces which are having
methods with the same signature then it creates ambiguity for the compiler, and it gives compile
time error. Its structure looks like diamond so it is called as Diamond problem.
E.g. Let's take an example which will show the diamond problem.
interface InterfaceA {
default public void m1() { System.out.println("This is interface A!"); }
}
interface InterfaceB {
default public void m1(){ System.out.println("This is interface B!"); } //same signature as
interface InterfaceA
}
public class Simple implements InterfaceA, InterfaceB {
public static void main(String args[]) {
Simple s1= new Simple();
s1.m1(); // It will give error..
}}
23. What is an abstract class in Java?
- An Abstract class is used to achieve abstraction in Java. If we use the keyword "abstract"
with the class name, then it is called as an abstract class.
- An Abstract class can have only methods without body or can have methods with some
implementation.
- The Abstract class cannot be instantiated
- It's not necessary that an abstract class should have an abstract method.
24. What is deadlock condition in multithreading?
A Deadlock condition occurs in the case of multithreading. It is a condition when one thread is
waiting for an object lock, which is already acquired by another thread and the second thread is
waiting for lock object which is taken by the first thread, so this is called deadlock condition
in Java.
25. Differentiate between Collection and array.
The Collection and Array both are responsible for storing the data, but the fundamental differences between both are given below:
- Arrays are always of fixed size, we cannot change its size at runtime, but In Collection, size can be changed dynamically.
- Arrays can only store homogeneous or similar type objects, but in Collection, both homogeneous and heterogeneous objects can be stored.
- Arrays cannot provide the "ready-made" methods for user requirements as sorting, searching, etc. but Collection includes readymade methods to use.
- Arrays are good in performance as compare to Collection but Array take more space in memory in comparison to Collection.