0% found this document useful (0 votes)
16 views

Java Accenture Questions

Uploaded by

jahedaigonb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Java Accenture Questions

Uploaded by

jahedaigonb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Java Interview questions:

1) 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).

2) 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.

3) What is checked and unchecked exception?

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

4) What are the reasons behind the occurrence of an exception?

Following are the reasons behind the occurrence of an exception:

o Accessing a file, which does not exist

o Dividing a variable by zero

o Inserting an element in the array outside the range

o If throw statement occurs

o Abnormal execution condition captured by JVM

5) 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:

o Object

o Class

o Inheritance

o Polymorphism

o Abstraction

o Encapsulation

6) Explain the basic features of OOPs.

Following are the basic features of OOPs:

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 Parent class (Superclass): A class which got inherited by another class is


termed as parent class or Base class.

o 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.

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.

7) 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:

o The Class is a logical entity whereas Object is a physical quantity.

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.

8) 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.

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.

Example for encapsulation:

1. class EncapsulationEg{

2. private String empname;

3. private int empage;

4. private int empid;

5.

6. public String getEmpName() //getter method

7. {
8. return empname;

9. }

10. public int getEmpAge()

11. {

12. return empage;

13. }

14. public int getEmpId()

15. {

16. return empid;

17. }

18. public void setEmpName(String setvalue) //setter methods

19. {

20. empname=setvalue;

21. }

22. public void setEmpAge(int setvalue){

23. empage=setvalue;

24. }

25. public void setEmpId(int setvalue){

26. empid=setvalue;

27. }

28.

29. }

30. public class TestEncapsulation{

31. public static void main(String args[]){

32.

33. EncapsulationEg en= new EncapsulationEg();

34. en.setEmpName("Alvin");

35. en.setEmpAge(22);

36. en.setEmpId(12568);
37. System.out.println("Employee Name: " +en.getEmpAge());

38. System.out.println("Employee Age: " +en.getEmpAge());

39. System.out.println("Employee ID: " +en.getEmpId());

40. }

41. }

Output:

Employee Name: 22

Employee Age: 22

Employee ID: 12568

9) 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.

Syntax:

1. Return-type method_name()

2. {

3. // Code to be executed

4. method_name(); // same name of calling method }

10) How can you differentiate between C, C++, and Java?

There are the following differences between the C, C++, and Java language.

Index C language C++ Java

1. C language is a C++ is an object-oriented Java is also an object-oriented language


procedural language. language. (not pure as it also supports primitive data
types).

1. C language is platform C++ is platform dependent. Java is platform independent language.


dependent.
1. C language supports C++ language also Java does not support pointers.
pointers. supports pointers.

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.

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

1. Runtime Polymorphism

2. Compile-time polymorphism

Here we will discuss runtime polymorphism.

Runtime Polymorphism- We can achieve runtime Polymorphism by method overriding in


Java. And method overriding is a process of overriding a method in the subclass which is
having the same signature as that of in superclass.

1. class A{ //Superclass

2. void name()

3. {

4. System.out.println("this is student of Superclass");

5. }

6. }

7.

8. class Student extends A //Subclass

9. {

10. void name(){ // method Override with same signature(runtime polymorphism)

11. System.out.println("this is student of subclass");

12. }

13. public static void main (String[] args) {


14. A a= new A(); // refrence of A class

15. A b= new Student(); // refrence of student class

16.

17. a.name();

18. b.name();

19. }

20. }

Output:

this is student of Superclass

this is student of subclass

12) How can you differentiate between method overloading and method overriding?

No. Method overloading 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.

2. It can be accessed within a class. Method overriding requires two classes to be


accessed which having IS-A relationship.

3. Return type may be changed or may remain same Return type should be the same for both methods.
with different parameters

4. Method overloading is a concept of compile-time Method overriding is a concept of method


polymorphism. overriding.

5. e.g. class A{ e.g. class A {


void m1() void m1(){
{// codes.......} // code............}
Void m1 (int a) {//code.........} }
Class B extends A{
Void m1(){
// code...........}
13) 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.

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 cannot be instantiated in Java.

o The Interface contains methods which are public and abstract (by default).

o A class can implement an interface.

o For declaring an interface, we use the keyword interface.

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:

1. public class A implements C, D {

2. Code

3. }

15) Explain inheritance in Java? How can it be achieved?

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.

o Inheritance increases the code reusability.


o Inheritance is an important feature of OOP concept.

o Inheritance is also a representation of the IS-A relationship

There are two terms used in inheritance:

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.

The Syntax of java inheritance:

1. Class A extends B // Here A represents subclass and B represent Superclass

2. {

3. // Code

4. }

16) Can we use multiple inheritance in Java? Explain with reason?

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.

17) 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.

18) What is the significance of "static" keyword?

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.

19) 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:

o Java.util.Collection

o Java.util.Map

20) What is List interface in collections?

List interface is an interface in Java Collection Framework. List interface extends the
Collection interface.

o It is an ordered collection of objects.

o It contains duplicate elements.

o It also allows random access of elements.

Syntax:

1. public interface List<E> extends Collection<E>

21) 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:

1. protected Object clone() throws CloneNotSupportedException

22) 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.

23) 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:

o A Collection is an interface in java and Collections is a class of collection framework.

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.

24) 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.

1. interface InterfaceA {

2. default public void m1() { System.out.println("This is interface A!"); }

3.

4. }

5. interface InterfaceB {

6. default public void m1(){ System.out.println("This is interface B!"); } //same signature


as interface InterfaceA?

7.

8. }

9.

10. public class Simple implements InterfaceA, InterfaceB {

11. public static void main(String args[]) {

12. Simple s1= new Simple();

13. s1.m1(); // It will give error..

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 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.

o An Abstract class can have only methods without body or can have methods with
some implementation.

o The Abstract class cannot be instantiated

o It's not necessary that an abstract class should have an abstract method.

Syntax:

1. abstract class Student{

2. }

26) 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.

27) 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:

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.

You might also like