JAVA / OOP Flashcards

1
Q

What are the 4 pillars of Object Oriented Programming?

A
  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a class?

A

A class acts as a blueprint in which objects can be created. A simple example is a pattern to create a dress. The pattern is the class and the dress that is made from it is the object. Classes contain the relevant data and behavior(s) an object has.

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

What is an object?

A

An instance of a class - contains the basic properties / states and behavior. Objects are instantiated using the ‘new’ keyword

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

Discuss some of Java’s naming conventions:

A
  • Class names are always the same as the file name, with each class being its own unique & separate file.
  • Class names are capitalized.
  • Variables should be logical & concise.
  • CamelCasing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Java Naming Conventions

A
  • Class names are always the same as the file name, with each class being its own unique & separate file.
  • Class names are capitalized and should be a noun
  • Methods should be a verb and start with a lowercase letter
  • Variables start with a lowercase letter, should be logical & concise, avoiding single character names.
  • Packages should be all lowercase. Separate multiple words with dots:
    java.lang
  • Constants should be all uppercase and multiple words separated by underscores:
    MAX_VALUE
  • CamelCasing
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Difference between a Class & an Object?

A

Class: A class is the building block that leads to Object-Oriented Programming. It is a user-defined data type, that holds its own data members and member functions, which can be accessed and used by creating an instance of that class. It is the blueprint of any object.

Object: An object is an instance of a class. All data members and member functions of the class can be accessed with the help of objects. When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created), memory is allocated. For example, considering the objects for the Account class is Saving account, Current account, etc.

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

Method Overloading

A

Method Overloading is a Compile time polymorphism. It occurs between methods in the same class. In method overloading, more than one method shares the same method name with a different signature in the class. In method overloading, the return type can or can not be the same, but we have to change the parameter because, in java, we can not achieve the method overloading by changing only the return type of the method.

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

Method Overriding

A

Overriding occurs between superclass and subclass. Method Overriding is a Run time polymorphism. In method overriding, the derived class provides the specific implementation of the method that is already provided by the base class or parent class. In method overriding, the return type must be the same or co-variant (return type may vary in the same direction as the derived class).

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

What is a constructor?

A

A constructor is a special method that is used to create an object out of a class definition. In Java they have the same name as the class Constructors are called first when an object of a class is instantiated. It is essentially responsible for setting up that object including assigning specific data members with values passed to it. If a class lacks a constructor, Java assigns one at run time. This empty constructor which maintains no parameters is known as the default constructor. Constructors cannot be private, they are always public. You can have more than 1 constructor which accepts different parameters (overloading).

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

Where are objects / variables stored?

A
  • Normal variables are stored in the stack
  • Objects are stored in the heap. The “variables” that hold references to them can be on the stack or they can be contained in other objects (then they are not really variables, but fields), which puts them on the heap also.
    • The JVM allocates the heap, inside which resides the string pool
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are some of the benefits / features of Java?

A
  • Object-oriented - every is an object / easily extended.
  • Platform independent - Compiled and run on the JVM (“Write once, run anywhere” / Bytecode)
  • Simple - relatively simple to learn if understand OOP
  • Secure - enables virus / tamper-free systems.
  • Architecture-Neutral - file format which compiled code can run on many processors.
  • Portable - due to its arch. neutrality / no implementation independent aspects.
  • Supports Multi-threading
  • Robust - emphasizes compile time & runtime error checks
  • Interpreted - byte code translated on the fly to native machine languages.
  • Memory Management (JVM handles garbage collection)
  • Backward Compatible (Older versions supported despite new changes)
  • Maturity & Support (Libraries & extensive documentation)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Discuss Exception Handling (Errors vs. Exceptions)

A
  • Errors refers to an illegal operation performed by the user which results in the abnormal working of the program. Programming errors often remain undetected until the program is compiled or executed. Some of the errors inhibit the program from getting compiled or executed. Thus errors should be removed before compiling and executing. It is of three types: Compile-time, Run-time, Logical.
  • Exceptions refer to an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions. They indicates conditions that a reasonable application might want to catch. Exceptions are the conditions that occur at runtime and may cause the termination of the program. But they are recoverable using try, catch and throw keywords. (Checked & Unchecked Exceptions)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Discuss Abstract Classes:

A

Abstract classes aim to implement the same or different behavior among multiple related objects. They provide just a method signature which allows you to implement a framework / template method design pattern. They can contain both abstract and concrete methods but the abstract methods must be implemented elsewhere. Concrete methods are distinguished by having a full name & method body. They cannot be instantiated. Can have final methods which will force the subclass to not change the method body.

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

Discuss Interfaces:

A

Interfaces are totally incomplete. They do not have any properties. They must show that the inheriting children are capable of doing something. You cannot instantiate abstract classes or interfaces. By default, all methods are abstract. Methods themselves are the only things composing an interface, it merely acts as a contract with the compiler which states that these specific methods must be implemented by whatever class implements the interface. They cannot be static or involve any implementation.

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

Can you achieve multiple inheritance in Java?

A

No, BUT - you can achieve multiple inheritance through the use of interfaces. (I.e. extending an abstract class, and implementing multiple interfaces).

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

Using an abstract class vs. an interface:

A

Use abstract classes when:

- You need both static / non-static methods  
- You need both abstract & non-abstract methods  
- You don't want all of your fields to be final

Use interfaces when:

- All methods are abstract  
- You need future devs to follow a pattern in development  
- You need to use more than one interface (A class can implement multiple interfaces
17
Q

Prove: Everything in Java is an object.

A

With the exception of primatives, references, variables - everything in java inherits from the object class. java.lang.Object

18
Q

Difference between constructor vs. method?

A

A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method. Methods are used to perform certain actions and are an ordinary member of a class used to describe a behavior of some object / class.

A constructor is a special member of the class with the same name as the class with no return type. It is called in order to create / instantiate an object from that class.

19
Q

Give an example of checked vs. unchecked exceptions

A
  • Checked Exceptions - fall under Exceptions; must be thrown or surrounded in a try/catch block otherwise the JVM won’t compile the code. Outside resource failing.
    • Exception, IOException, FileNotFoundException, SQLException
  • Unchecked Exceptions - fall under RuntimeExceptions, which is a subclass of Exception. Unchecked Exceptions can be compiled and will throw an exception at runtime. Unchecked exceptions can be caught or handled with logic.
    • RuntimeException, ArrayIndexOutOfBoundsException, ArithmeticException
20
Q

Final vs. Finally vs. Finalize

A
  • FINAL is used to denote a variable whose value is constant, meaning that it cannot change in the future. Can also be used in method declaration to assert that the method cannot be overridden by sub-classes. Final classes cannot be inherited, variables value cannot be changed, and methods cannot be overridden.
  • The keyword FINALLY used in the exception handler to clear some post-task after the execution of try or catch block.
  • The FINALIZE keyword used to clean up some tasks before the object is removed from memory. The finalize method is mainly used in garbage collection.
21
Q

Public Access Modifier

A

Can be accessed anywhere, within the package, outside it, in or outside the class.

22
Q

Private Access Modifier

A

Access is restricted to only within the class. (i.e. need methods to interact with a private field). Cannot be overridden

23
Q

Protected Access Modifier

A

Access level is protected within the package and outside it through a child class. If you don’t create a child class, you cannot access it from outside the package.

24
Q

Default Access Modifier

A

Set by default, access level only within the package

25
Q

How are Java & JavaScript different?

A
  • JAVA:
    • OOP – Programming Language
    • Compiled & runs on JVM
    • Creates full scale applications
    • Supports multi-threading (thread-based concurrency)
  • JAVASCRIPT:
    • OOP – Scripting Language
    • Runs on the browser / client side
    • Provides a level of interactivity unable to be produced through normal web development (HTML)
    • No multi-threading (event-based concurrency)
26
Q

Discuss Multithreading

A

Multithreading is a technique in which a process is divided into threads that can run concurrently. There are two ways to create threads in java: Implement Runnable interface or By extending Thread class

27
Q

What is Synchronization?

A

Acts as a safety for a thread. A technique to control access of a method with multiple threads at the same time. If we declare a method synchronized, then only one thread can use this method at a time.

28
Q

Discuss Mutable Objects

A

Mutable objects support both setter & getter methods. They allow for the states and fields of an object to be changed after the object has been created. Conversely, immutable objects cannot be changed after creation.

29
Q

StringBuffer vs. StringBuilder

A
  • StringBuffer is mutable and the object created through it is stored in the heap. It is also thread-safe. It is synchronized and does not let two threads to simultaneously access the same method.
  • StringBuilder allows for dynamic modification of a string. It has its own methods. The difference between StringBuilder and StringBuffer is that StringBuilder is non-thread safe (not synchronized) where StringBuffer is.

Concatenating a string involves creating a new string with each character that is copied over. Thus string builder is a using in such cases where concatenation is nessecary.

30
Q

Explain Overriding

A
  • Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
    • It replaces the superclass method with a different version.
  • In case of method overriding, parameter must be same.
  • Method overriding is the example of run time polymorphism.
  • Return type must be same or covariant in method overriding.
  • Cannot override private, static, or final methods. (Only non-static)… (Hiding for static methods)
31
Q

What is a Stream? What qualifies as one?

A

A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The keyword collection is a prerequisite to be considered a stream.

  • A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
  • Streams don’t change the original data structure, they only provide the result as per the pipelined methods.
  • Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.
32
Q

How would you order the filtering & sorting of a stream?

A

The most appropriate approach is to filter the collection first, then sort it. Filter will reduce the total number of elements to sort and thus result in fewer calls when sorting.

33
Q

What is the Optional Class?

A

Optional is a special class used to deal with NullPointerExceptions. It can be used if you suspect a value to be null rather than having to handle numerous null checks. Optional is a container object which may or may not contain a non-null value. You can specify alternate values to return or alternate code to run. This makes the code more readable because the facts which were hidden are now visible.