Java Accenture Questions
Java Accenture Questions
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).
o 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
o 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.
E.g. ArithmeticException
o Object
o Class
o Inheritance
o Polymorphism
o Abstraction
o Encapsulation
o 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.
o 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.
o Inheritance - Inheritance is a process by which one class can have all properties of
other class. Inheritance increases the code reusability. There are two terms used
o Child class (Subclass): Class which inherits other class, called as Child class
or derived class.
o Abstraction: If we show only functionality and hide the explanations or details then
this process is called as Abstraction. For achieving the abstraction, we use two ways
in Java
o Abstract class
o Interface
o 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.
The class and object both are the features of OOPs concepts. The basic differences between
both features are given below:
o Class does not occupy memory at the time of creation whereas Object occupied
space in memory when it is created.
o 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.
o A Class is like a factory which generates object and object are the instances of the
class.
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.
o 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.
o One of the examples of the fully encapsulated class is Java Bean class.
o Encapsulation keeps its data hide from other class hence it is also called as data-
hiding.
1. class EncapsulationEg{
5.
7. {
8. return empname;
9. }
11. {
13. }
15. {
17. }
19. {
20. empname=setvalue;
21. }
23. empage=setvalue;
24. }
26. empid=setvalue;
27. }
28.
29. }
32.
34. en.setEmpName("Alvin");
35. en.setEmpAge(22);
36. en.setEmpId(12568);
37. System.out.println("Employee Name: " +en.getEmpAge());
40. }
41. }
Output:
Employee Name: 22
Employee Age: 22
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.
Syntax:
1. Return-type method_name()
2. {
3. // Code to be executed
There are the following differences between the C, C++, and Java language.
1. We cannot create our own In C++ language also, we In the Java language, we can create our
package in C language cannot create our package. package and can specify the classes.
1. In C, there is no any In C++, we can use multiple Java does not support multiple
concept of inheritance. inheritance. inheritance.
1. Runtime Polymorphism
2. Compile-time polymorphism
1. class A{ //Superclass
2. void name()
3. {
5. }
6. }
7.
9. {
12. }
16.
17. a.name();
18. b.name();
19. }
20. }
Output:
12) How can you differentiate between method overloading and method overriding?
1. The process of calling two methods having the The process of calling two methods, one in the
same name with different parameters is called subclass and other in the superclass, having the
method overloading (in the same class) same signature is called as method overriding.
3. Return type may be changed or may remain same Return type should be the same for both methods.
with different parameters
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.
14) 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.
o The Interface contains methods which are public and abstract (by default).
Syntax:
1. interface Interface_Name{
2. //Methods
3. }
We can implement multiple interfaces in one class and parent interfaces can be declared
using a comma(,) operator.
Syntax:
2. Code
3. }
o Inheritance in Java is a process by which one class can have all properties of other
class. That means one class inherits all the behaviour of the other class.
1. Child class (Subclass): Class which inherits other class, called a Child class or
derived class.
2. Parent class (Superclass): A class which got inherited by another class is termed as
parent class or Base class.
2. {
3. // Code
4. }
No, we cannot use multiple inheritance in java as it creates ambiguity and diamond problem
in the program. To overcome this problem, we can use interface in Java.
Let suppose class A inherits the two parent class B and C in which a method with the same
name is present in both the classes and hence when we try to override that method it will
create confusion for the compiler and will give the compilation error. Therefore, Java doesn?t
support multiple inheritance.
We can access private members of the class by using public getters and setters from outside
the class in Java.
o Static keyword in Java is a non-access modifier which can be used with the block,
variable, methods, and nested classes.
o Static Keywords are the part of the class, and it does not belong to the instance of the
class.
o We use static keyword in java with variables, block, and method for achieving memory
management.
o Java static property can be shared by all the objects.
o For accessing the static members, we don't need to create the instance of the class.
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:
o Java.util.Collection
o Java.util.Map
List interface is an interface in Java Collection Framework. List interface extends the
Collection interface.
Syntax:
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.
We cannot insert duplicate elements in Set. If we add a duplicate element, then the output
will only show the unique elements.
o 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.
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.
1. interface InterfaceA {
3.
4. }
5. interface InterfaceB {
7.
8. }
9.
14. }}
Error: Simple.java:10: error: class Simple inherits unrelated defaults for m1() from types
InterfaceA and InterfaceB
25) What is an abstract class in Java?
o An Abstract class can have only methods without body or can have methods with
some implementation.
o It's not necessary that an abstract class should have an abstract method.
Syntax:
2. }
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.
The Collection and Array both are responsible for storing the data, but the fundamental
differences between both are given below:
o Arrays are always of fixed size, we cannot change its size at runtime, but In Collection,
size can be changed dynamically.
o Arrays can only store homogeneous or similar type objects, but in Collection, both
homogeneous and heterogeneous objects can be stored.
o Arrays cannot provide the "ready-made" methods for user requirements as sorting,
searching, etc. but Collection includes readymade methods to use.
o Arrays are good in performance as compare to Collection but Array take more space
in memory in comparison to Collection.