Java Flashcards

1
Q

What is OOP?

A

Object-Oriented Programming. a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object, Classes, Inheritance, Polymorphism, Abstraction, Encapsulation.

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

What is an object?

A

Any entity that has a state and behavior. An instance of a class. It contains an address and takes up some space in memory. Can communicate without knowing the details of each other’s data or code. The only necessary thing is the type of message accepted and the type of response returned by the objects.

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

what is a class?

A

In the context of Java, classes are templates that are used to create objects, and to define object data types and methods. Blueprint from which you can create an individual object. Does not consume any space.

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

Advantages of OOP over Prodcedure Oriented Programming.

A
  • Makes development and maintenance easier whereas in a procedure-oriented programming language it is not easy to manage if code grows as project size increases.
  • Provides data hiding whereas in a procedure-oriented programming language a global data can be accessed from anywhere.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Functional Programming?

A

using functions to program.

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

What is a constructor?

A

a block of codes similar to the method. It is called when an instance of the object is created, and memory is allocated for the object. A special type of method which is used to initialize the object.

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

When is a constructor called?

A

When an object is created, compiler makes sure that constructors for all of its subobjects (its member and inherited objects) are called. Without parameters, these constructors are called automatically, otherwise, parameterized constructors can be called using initializer list.

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

Rules to remember while creating a constructor.

A
  • Constructor name must be the same as its class name
  • A Constructor must have no explicit return type
  • A Java constructor cannot be abstract, static, final and synchronized
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Default Constructor?

A

a no-args constructor that the Java compiler inserts on your behalf; it contains a default call to super(); which is the default behavior. If you implement any constructor then you no longer receive a default constructor.

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

What is a Parameterized Constructor?

A

A Constructor which has a specific number of parameters. Used to provide different values to the distinct objects. Can have any number of parameters in the constructor.

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

What is Constructor Overloading?

A

a technique of having more than one constructor with different parameter lists.

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

Constructor:
- A Constructor is used to initialize the state of an object.
- A Constructor must not have a return type.
- The Constructor is invoked implicitly.
- The Java compiler provides a default constructor if you don’t have any constructor in a class.
- The Constructor name must be same as the class name.

A

Method:
- A method is used to expose the behavior of an object.
- A method must have a return type.
- A method is invoked explicitly.
- The method is not provided by the complier in any case.
- The method name may or may not be same as class name.

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

What is Inheritance?

A

A mechanism in which one object acquires all the properties and behaviours of a parent object. New classes can be built on top of existing classes. Can inherit existing class and reuse methods and fields of the parent class. Parent-child relationship.

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

Advantages of Inheritance

A
  • For Method Overriding (so runtime polymorphism can be achieved).
  • For code Resuability.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Terms used in Inheritance?

A
  • Class: A class is a group of objects which has common properties. It is a template or blueprint from which objects are created.
  • Subclass/Child Class: Subclass is a class which inherits the other class. It is also called a derived class, extended class, or child class.
  • Super Class/Parent Class: Superclass is the class from where a subclass inherits the features. Its also called a base class or a parent class.
  • Reusability: reuse fields and methods of the existing class when you create a new class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Three types of Inheritance in Java:

A

Single, multilevel, and hierarchical.

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

Why is multiple inheritance not supported in Java?

A

To reduce the complexity and simplify the language, multiple inheritance is not supported in Java. To reduce ambiguity when inheriting.

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

What is Polymorphism?

A

the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.s. Any Java object that can pass more than one IS-A test is considered to be polymorphic. all Java Objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

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

What is Method Overloading?

A

If a class has multiple methods having same name but different in parameters.

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

Different ways to overload a method?

A
  • By changing number of arguments.
  • By changing the data type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Usage and rules of Method Overriding:

A

Usage;
- Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.
- Method overriding is used for runtime polymorphism.
Rules:
- The method must have the same name as in the parent class.
- The method must have the same parameter as in the parent class.
- There must be an IS-A relationship (inheritance).

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

Method Overloading Vs Method Overriding

A

Method Overloading:
- Method overloading is used to increase the readability of the program.
- Method overloading is performed within class.
- In case of method overloading, parameter must be different.
- Method overloading is the example of compile time polymorphism.
- In Java, method overloading cant be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter.

Method Overriding:
- Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
- Method overriding occurs in two classes that have IS-A (inheritance) relationship.
- In case of method overriding, parameter must be same.
- Method overriding is the example of runtime polymorphism.
- Return type must be same or covariant in method overriding.

23
Q

What is Encapsulation?

A

Encapsulation is a process of wrapping code and data together into a single unit. Can use setter and getter methods to get data. Java Bean class is an example of fully encapsulated class.

24
Q

Advantages of Encapsulation:

A
  • By providing only a setter and getter method, you can make the class read-only or write-only. In other words, you can skip the getter and setter methods.
  • It provides you the control over the data.
  • A way to achieve data hiding in Java because other class will not be able to access the data through the private data members.
  • Easy to test.
25
Q

Two types of access modifiers:

A
  • Access modifiers
  • non-access modifiers
26
Q

4 types of java access modifiers:

A
  • private
  • default
  • protected
  • public
27
Q

What is Abstraction?

A

Abstraction is a process of hiding the implementation details and showing only functionality of the user.

28
Q

What is Generalization?

A

the process of extracting shared characteristics from two or more classes, and combining them into a generalized superclass. Shared characteristics can be attributes, associations, or methods.

29
Q

What is Specialization?

A

Creating new subclasses from an existing class. If it turns out that certain attributes, associations, or methods only apply to some of the objects of the class, a subclass can be created.

30
Q

Two ways to achieve abstraction?

A
  • Abstract class (0 to 100%)
  • Interface (100%)
31
Q

What is Abstract Class?

A
  • must be declared with an abstract keyword
  • It can have abstract and non-abstract methods
  • It cannot be instantiated
  • It can have constructors and static methods also
  • It can have final methods which will force the subclass not to change the body of the method
32
Q

What is Java?

A

a general-purpose computer-programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.

33
Q

What is a Switch statement?

A

Allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
- Can only be integers, convertible integers (byte, short, char), strings and enums.
- You can have any number of case statements within a switch.
- The value for a case must be the same data type as the variable in the switch and it must be constant or a literal.
- Can have optional default case, which appears at the end of the switch. Case for performing task when none of the cases is true.

34
Q

“this” keyword:

A
  • this can be used to refer current class instance variable.
  • this can be used to refer current class instance variable.
  • this can be used to invoke current class constructor.
35
Q

“new” keyword:

A
  • instantiates a class by dynamically allocating memory for a new object and returning a reference to that memory. All class objects must be dynamically allocated.
36
Q

“super” keyword:

A
  • reference variable which is used to refer immediate parent class object. can refer to parent class instance variable or invoke immediate parent class method. super() can be used to invoke immediate parent class constructor.
37
Q

“static” keyword:

A

used mainly for memory management. Can be variable, method, block, nested class. Static variables can be used to refer to the common property of all objects and gets memory only once in the class area at the time of class loading. Makes program memory efficient.

38
Q

“final” keyword:

A

used to restrict the user. Can be variable, method, class.

39
Q

What is a package in Java?

A

a group of similar types of classes, interfaces and sub-packages.

40
Q

Advantages of packages

A
  • used to categorize the classes and interfaces so that they can be easily maintained.
  • provides access protection.
  • removes naming collision.
41
Q

Accessing packages from other packages

A
  • Using packagename.*
    -> all the classes and interfaces of this package will be accessible but not subpackages.
  • Using packagename.classname
    -> only declared class of this package will be accessible.
  • Using fully qualified name
    -> only declared class of this package will be accessible. generally used when two packages have same class name.
42
Q

Sequence of program

A

package -> import -> class

43
Q

What is an Interface?

A

a reference type in Java. Similar to class. A collection of abstract methods. Interface are implicitly abstract, you do not need the abstract keyword. Cannot instantiate an interface. interface does not contain any constructors. All of the methods in interface are abstract. Class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements.

44
Q

What are exceptions?

A

Events that occurs during the execution of programs that disrupt the normal flow of instructions. Object that wraps an error event that occurred within a method and contains:
- information about the error including its type
- the state of the program when the error occurred
- other custom information
-> three categories of exceptions:
- checked exceptions
- unchecked exceptions
- errors

45
Q

what are checked exceptions?

A
  • compiler enforces that you handle them explicitly.
  • Methods that generate checked exceptions must declare that they throw them.
  • methods that invoke other methods that throw checked exceptions must either handle them or let them propagate by declaring that they throw them.
46
Q

what are unchecked exceptions?

A

Errors and runTimeExceptions, the compiler does not enforce that you handle them explicitly.
- methods do not have to declare that they throw them.

47
Q

catching exceptions:

A

using try and catch keywords.

48
Q

finally block:

A

follows a try block or a catch block. finally block always executes, irrespective of occurrence of an exception.

49
Q

Data Structures:

A

linkedList, ArrayList, Set interface, map interface

50
Q

Set Interface:

A
  • collection that cannot contain duplicates.
  • contains only methods inherited from collection and adds the restriction that duplicate elements are prohibited.
51
Q

Map Interface:

A

given a key and a value, you can stare value in a map object. retrieve value by using its key.

52
Q

What is primary key in SQL?

A

uniquely identifies each row/record in a database table.

53
Q

What is a foreign key?

A

used to link two tables together. a column or a combination of columns whose values match a primary key in a different table.

54
Q

What is “where” clause?

A

used to specify a condition while fetching the data from a single table or by joining with multiple tables.