Java OOP Flashcards

1
Q

Describe what ‘Public’ means in a user defined method

A

public - access modifier. It means the method can be accessed from anywhere.

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

Describe what ‘Static’ means in a user defined method

A

Static means that the method can be accessed without any objects.

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

Describe what ‘Void’ means in a user defined method

A

Void means that the method does not return any value

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

Describe the complete syntax of a method

A
modifier static returnType nameOfMethod (parameters) {
    // method body
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are some advantages of using methods?

A

The main advantage is code reusability. We can write a method once, and use it multiple times. We do not have to rewrite the entire code each time. Think of it as, “write once, reuse multiple times

Methods make code more readable and easier to debug

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

What is a constructor?

A

In Java, every class has its constructor that is invoked automatically when an object of the class is created. A constructor is similar to a method but in actual, it is not a method.

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

What are the three types of constructors in Java?

A

No-Arg Constructor
Default Constructor
Parameterized Constructor

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

Describe a No-Arg Constructor

A

A Java constructor may or may not have any parameters (arguments). If a constructor does not accept any parameters, it is known as a no-arg constructor. For example,

private Constructor( ) {
   // body of constructor
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Describe a default constructor

A

If you do not create any constructors, the Java compiler will automatically create a no-argument constructor during run-time. This constructor is known as the default constructor. The default constructor initializes any uninitialized instance variables with default values

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

Describe parametrized constructor

A

Similar to methods, we can pass parameters to a constructor. Such constructors are known as a parameterized constructor. For example,

private Constructor (arg1, arg2, ..., argn) {
    // constructor body
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is constructor overloading?

A

In constructor overloading, there are two or more constructors with different parameters

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

Important Notes about Constructors

A

Constructors are invoked implicitly when you instantiate objects.

The two rules for creating a constructor are:
The name of the constructor should be the same as that of class.
A Java constructor must not have a return type.

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

What are access modifiers?

A

In Java, access modifiers are used to set the accessibility (visibility) of classes, interfaces, variables, methods, constructors, data members, and the setter methods. For example,

class Animal {
    public void method1() {...}

private void method2() {…}
}

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

There are four access modifiers keywords in Java

A

Default
Private
Protected
Public

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

What is a private access modifier

A

When variables and methods are declared private, they cannot be accessed outside of the class

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

What is a protected access modifier

A

When methods and data members are declared protected, we can access them within the same package as well as from subclasses

17
Q

What is a public access modifier

A

When methods, variables, classes, and so on are declared public, then we can access them from anywhere. The public access modifier has no scope restriction

18
Q

Describe ‘this’ keyword

A

In Java, this keyword is used to refer to the current object inside a method or a constructor

19
Q

What is the final keyword used for in Java

A

In Java, the final keyword is used to denote constants. It can be used with variables, methods, and classes

20
Q

What is a recursive function?

A

In Java, a method that calls itself is known as a recursive method. And, this process is known as recursion

21
Q

What is inheritance?

A

Inheritance is one of the key features of OOP (Object-oriented Programming) that allows us to define a new class from an existing class

22
Q

Describe ‘is-a’ relationship

A

Inheritance is an is-a relationship. We use inheritance only if an is-a relationship is present between the two classes.

Here are some examples:

A car is a vehicle.
Orange is a fruit.
A surgeon is a doctor.
A dog is an animal.

23
Q

Types of inheritance

A

There are five types of inheritance.

Single inheritance - Class B extends from class A only.

Multilevel inheritance - Class B extends from class A; then class C extends from class B.

Hierarchical inheritance - Class A acts as the superclass for classes B, C, and D.

Multiple inheritance - Class C extends from interfaces A and B.

Hybrid inheritance - Mix of two or more types of inheritance.

24
Q

Describe abstraction

A

Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user

Think — a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button

25
Q

Describe Encapsulation

A

Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions — called methods

26
Q

Describe Polymorphism

A

Polymorphism is a object oriented programming feature that allows us to perform a single action in different ways

27
Q

What is an interface?

A

An interface is a blueprint of a class, which can be declared by using interface keyword. Interfaces can contain only constants and abstract methods (methods with only signatures no body).Like abstract classes, Interfaces cannot be instantiated, they can only be implemented by classes or extended by other interfaces. Interface is a common way to achieve full abstraction in Java

28
Q

What is abstraction?

A

Hiding internal details and showing functionality is known as abstraction. For example phone call, we don’t know the internal processing

29
Q

What are the four principles of object oriented programming?

A

The four principles of OOP are

Encapsulation
Abstraction
Inheritance
Polymorphism

30
Q

In terms of inheritance, what is the effect of keeping a constructor private?

A

It has direct implications for inheritance, since a subclass calls its parents constructor. The class A can be inherited, but only by its own or its parents inner classes

31
Q

In Java, does the finally block get executed if we insert a return statement inside the try block of a try-catch-fianlly?

A

Yes, it will get executed. The finally block get executed when the try block exits.