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

Java Questions

The document discusses various Java concepts like memory areas, constructors, static variables, inheritance, abstraction, interfaces, strings, collections and more. Memory areas include heap, stack, class area. Constructors initialize objects. Static variables/methods belong to the class. Inheritance allows classes to acquire properties of other classes. Abstraction hides implementation details. Interfaces provide method signatures without implementation.

Uploaded by

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

Java Questions

The document discusses various Java concepts like memory areas, constructors, static variables, inheritance, abstraction, interfaces, strings, collections and more. Memory areas include heap, stack, class area. Constructors initialize objects. Static variables/methods belong to the class. Inheritance allows classes to acquire properties of other classes. Abstraction hides implementation details. Interfaces provide method signatures without implementation.

Uploaded by

Think Box
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

2) How many types of memory areas are allocated by JVM?

Many types:
1. Class(Method) Area
2. Heap
3. Stack
4. Program Counter Register
5. Native Method Stack

15) What is constructor?


o Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of
object creation.

16) What is the purpose of default constructor?


o The default constructor provides the default values to the objects. The java compiler creates a default
constructor only if there is no constructor in the class

17) Does constructor return any value?


Ans:yes, that is current instance (You cannot use return type yet it returns a value)

19) Can you make a constructor final?


No, constructor can't be final.

20) What is static variable?


o static variable is used to refer the common property of all objects (that is not unique for each object) e.g.
company name of employees,college name of students etc.
o static variable gets memory only once in class area at the time of class loading.

21) What is static method?


o A static method belongs to the class rather than object of a class.
o A static method can be invoked without the need for creating an instance of a class.
o static method can access static data member and can change the value of it.

28)What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviour of another object of
another class. It represents IS-A relationship. It is used for Code Resusability and Method Overriding.

29) Which class is the superclass for every class.


Object class.

37) What is method overloading?


If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It
increases the readability of the program.

40) What is method overriding:


If a subclass provides a specific implementation of a method that is already provided by its parent class, it is
known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the
method
41) Can we override static method?
No, you can't override the static method because they are the part of class not object.

42) Why we cannot override static method?


It is because the static method is the part of class and it is bound with class whereas instance method is bound
with object and static gets memory in class area and instance gets memory in heap.

47) What is final variable?


If you make any variable as final, you cannot change the value of final variable(It will be constant

48) What is final method?


Final methods can't be overriden

49) What is final class?


Final class can't be inherited

56) What is abstraction?


Abstraction is a process of hiding the implementation details and showing only functionality to the user.
Abstraction lets you focus on what the object does instead of how it does it.

57) What is the difference between abstraction and encapsulation?


Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.

58) What is abstract class?


A class that is declared as abstract is known as abstract class. It needs to be extended and its method
implemented. It cannot be instantiated.

61) Is it possible to instantiate the abstract class?


No, abstract class can never be instantiated.

62) What is interface?


Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully
abstraction and multiple inheritance.

64) Can an Interface be final?


No, because its implementation is provided by another class.

66) What is difference between abstract class and interface?


Abstract class Interface

1)An abstract class can have method body (non-abstract methods). Interface have only abstract methods.

2)An abstract class can have instance variables. An interface cannot have instance varia

3)An abstract class can have constructor. Interface cannot have constructor.

4)An abstract class can have static methods. Interface cannot have static methods.

5)You can extends one abstract class. You can implement multiple interfaces.
87) How many objects will be created in the following code?
1. String s1="Welcome";
2. String s2="Welcome";
3. String s3="Welcome";
Only one object.

90) What is the basic difference between string and stringbuffer


object?
String is an immutable object. StringBuffer is a mutable object.

91) What is the difference between StringBuffer and StringBuilder ?


StringBuffer is synchronized whereas StringBuilder is not synchronized.

119) What is the purpose of finalize() method?


finalize() method is invoked just before the object is garbage collected.It is used to perform cleanup processing.

152)What is singleton class?


Singleton class means that any given time only one instance of the class is present, in one JVM.

5) What is difference between wait() and sleep() method?

wait() sleep()

1) The wait() method is defined in Object class. The sleep() method is defined in Thread class.

2) wait() method releases the lock. The sleep() method doesn't releases the lock.
24. What is the importance of hashCode() and equals() methods ? In Java, a HashMap uses
the hashCode and equals methods to determine the index of the key-value pair and to detect duplicates. More
specifically, the hashCodemethod is used in order to determine where the specified key will be stored. Since different
keys may produce the same hash value, the equals method is used, in order to determine whether the specified key
actually exists in the collection or not. Therefore, the implementation of both methods is crucial to the accuracy and
efficiency of the HashMap.

26. What is difference between Array and ArrayList ? When will you use Array over ArrayList?
The Arrayand ArrayList classes differ on the following features:
 Arrays can contain primitive or objects, while an ArrayList can contain only objects.
 Arrays have fixed size, while an ArrayList is dynamic.
 An ArrayListprovides more methods and features, such as addAll, removeAll, iterator, etc.
 For a list of primitive data types, the collections use autoboxing to reduce the coding effort. However, this
approach makes them slower when working on fixed size primitive data types.

27. What is difference between ArrayList and LinkedList ? Both the ArrayList and LinkedList classes
implement the List interface, but they differ on the following features:
 An ArrayList is an index based data structure backed by an Array. It provides random access to its elements with a
performance equal to O(1). On the other hand, a LinkedList stores its data as list of elements and every element
is linked to its previous and next element. In this case, the search operation for an element has execution time
equal to O(n).
 The Insertion, addition and removal operations of an element are faster in a LinkedList compared to an ArrayList,
because there is no need of resizing an array or updating the index when an element is added in some arbitrary
position inside the collection.
 A LinkedList consumes more memory than an ArrayList, because every node in a LinkedList stores two references,
one for its previous element and one for its next element.
Check also our article ArrayList vs. LinkedList.

You might also like