OOPs Questions Flashcards

1
Q

Four main principles of OOPS Concepts?

A

Inheritance
Polymorphism
Data Encapsulation
Abstraction

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is inheritance?

A

The process by which one class acquires the properties and functionalities of another class is called inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Does Java support Multiple Inheritance?

A

When a class extends more than one classes then it is called multiple inheritance. Java doesn’t support multiple inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Polymorphism?

A

Polymorphism is the ability of an object to take many forms. The most common use of polymorphism in OOPs is to have more than one method with the same name in a single class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the types of Polymorphism?

A

static polymorphism and dynamic polymorphism

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is method overriding in Java?

A

When a sub class (child class) overrides the method of super class(parent class) then it is called overriding. To override a method, the signature of method in child class must match with the method signature in parent class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can we override a static method?

A

No, we cannot override a static method in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is method overloading?

A

When a class has more than one methods with the same name but different number, sequence or types of arguments then it is known as method overloading

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Does Java support operator overloading?

A

Operator overloading is not supported in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can we overload a method by just changing the return type and without changing the signature of method?

A

No, We cannot do this. To overload a method, the method signature must be different, return type doesn’t play any role in method overloading.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Is it possible to overload main() method of a class?

A

Yes, we can overload main() method in Java.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the difference between method overloading and method overriding?

A

Overloading happens at compile-time while Overriding happens at runtime: The binding of overloaded method call to its definition has happens at compile-time however binding of overridden method call to its definition happens at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is static and dynamic binding in Java?

A

Binding refers to the linking of method call to its body. A binding that happens at compile time is known as static binding while binding at runtime is known as dynamic binding.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is Encapsulation?

A

Wrapping of the data and code together is known as encapsulation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is an abstract class in Java?

A

An abstract class is a class which can’t be instantiated (we cannot create the object of abstract class), we can only extend such classes. It provides the generalised form that will be shared by all of its subclasses, leaving it to each subclass to fill in the details. We can achieve partial abstraction using abstract classes, to achieve full abstraction we use interfaces.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Interface in java?

A

An interface is used for achieving full abstraction. A class implements an interface, thereby inheriting the abstract methods of the interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the difference between abstract class and interface?

A

1) abstract class can have abstract and non-abstract methods. An interface can only have abstract methods.
2) An abstract class can have static methods but an interface cannot have static methods.
3) abstract class can have constructors but an interface cannot have constructors.

18
Q

Name the access modifiers that can be applied to the inner classes?

A

public ,private , abstract, final, protected.

19
Q

What is a constructor in Java?

A

Constructor is used for creating an instance of a class, they are invoked when an instance of class gets created. Constructor name and class name should be same and it doesn’t have a return type.

20
Q

Can we inherit the constructors?

A

No, we cannot inherit constructors.

21
Q

Can we mark constructors final?

A

No, Constructor cannot be declared final.

22
Q

What is a default constructor?

A

Constructors with no arguments are known as default constructors, when you don’t declare any constructor in a class, compiler creates a default one automatically.

23
Q

What is a parameterized constructor?

A

Constructor with arguments are known as parameterized constructors.

24
Q

Can a constructor call another constructor?

A
Yes. A constructor can call the another constructor of same class using this keyword. For e.g. this() calls the default constructor.
Note: this() must be the first statement in the calling constructor.
25
Q

Can a constructor call the constructor of parent class?

A

Yes. In fact it happens by default. A child class constructor always calls the parent class constructor. However we can still call it using super keyword. For e.g. super() can be used for calling super class default constructor.

Note: super() must be the first statement in a constructor.

26
Q

THIS keyword?

A

The this keyword is a reference to the current object.

27
Q

Can this keyword be assigned null value?

A

No, this keyword cannot have null values assigned to it.

28
Q

Explain ways to pass the arguments in Java?

A

In java, arguments can be passed as call by value – Java only supports call by value, there is no concept of call by reference in Java.

29
Q

What is static variable in java?

A

Static variables are also known as class level variables. A static variable is same for all the objects of that particular class in which it is declared.

30
Q

What is static block?

A

A static block gets executed at the time of class loading. They are used for initializing static variables.

31
Q

What is a static method?

A

Static methods can be called directly without creating the instance (Object) of the class. A static method can access all the static variables of a class directly but it cannot access non-static variables without creating instance of class.

32
Q

Explain super keyword in Java?

A

super keyword references to the parent class. There are several uses of super keyword:

It can be used to call the superclass(Parent class) constructor.
It can be used to access a method of the superclass that has been hidden by subclass (Calling parent class version, In case of method overriding).
To call the constructor of parent class.
33
Q

Use of final keyword in Java?

A

Final methods – These methods cannot be overridden by any other method.
Final variable – Constants, the value of these variable can’t be changed, its fixed.
Final class – Such classes cannot be inherited by other classes. These type of classes will be used when application required security or someone don’t want that particular class

34
Q

What is a Object class?

A

This is a special class defined by java; all other classes are subclasses of object class. Object class is superclass of all other classes.

objectClone ()
boolean equals(Object obj)
finalize()
toString ()

35
Q

objectClone ()

A

to creates a new object that is same as the object being cloned.

36
Q

boolean equals(Object obj)

A

determines whether one object is equal to another.

37
Q

finalize()

A

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

38
Q

toString ()

A

Returns a string representation of the object.

39
Q

What are Packages in Java?

A

A Package can be defined as a grouping of related types (classes, interfaces, enumerations and annotations).

40
Q

What is the difference between import java.util.Date and java.util.* ?

A

The star form (java.util.* ) includes all the classes of that package and that may increase the compilation time – especially if you import several packages. However it doesn’t have any effect run-time performance

41
Q

Garbage collection in java?

A

Since objects are dynamically allocated by using the new operator, java handles the de-allocation of the memory automatically, when no references to an object exist for a long time. This whole process is called garbage collection. The whole purpose of Garbage collection is efficient memory management.

42
Q

Use of finalize() method in java?

A

finalize() method is used to free the allocated resource.