Blog Archives

01: 19 Java 8 Functional Programming (FP) Interview Q&As

Java 8 Functional Programming Interview questions focusing on FP. Getting a good handle on Java OOP concepts and FP concepts are very important NOT only from writing quality code perspective, but also from job interviews perspective to be able to do well in written tests & technical interviews.

Q1. Can you explain your understanding of Functional Programming (FP)?
A1. In a very simplistic approach FP means:

#1 Programming without assignments: For example, in imperative style programming like OOP, you can say “x = x + 5”, which is an assignment, but in mathematical or functional programming you need to say f(x) -> x + 5. If, x were to be 2, it is mathematically incorrect to say “2 = 2 + 5”. In imperative style you are assigning a new value of 7 to x by saying “x = x + 5”. So, imperative programming has state and you can assign a new state. FP treats computation as the evaluation of mathematical functions and avoids storing state and mutating data.

#2 FP focuses on WHAT to be done, and NOT on HOW to be done: For example, in the ‘functional style loop’ you’ll focus only on the action of what to do on each element and you don’t have to concentrate on how to go through each element. The “forEach” function will take care of it.

Here is a simple example that shows imperative (e.g.… Read more ...

Tags:

03: Q16 – Q17 Java Polymorphism vs Overriding vs Overloading

Polymorphism is the ability of a class instance to behave as if it were an instance of another class in its inheritance tree, most often one of its ancestor classes. Overriding is the means by which you achieve polymorphism. Java Polymorphism vs Overriding vs Overloading explained in detail to handle drill down Java OOP interview questions with ease when quizzed further.

Q16. Can you describe “method overloading” versus “method overriding”? Does it happen at compile time or runtime?
A16. Method overloading: Overloading deals with multiple methods in the same class with the same name but different method signatures. Both the below methods have the same method names but different method signatures, which mean the methods are overloaded.

This happens at compile-time. This is also called compile-time polymorphism because the compiler must decide how to select which method to run based on the data types of the arguments. If the compiler were to compile the statement:

it could see that the argument was a string literal, and generate byte code that called method #1.… Read more ...



04: Q18 Explain abstraction, encapsulation, Inheritance, and polymorphism with the given Java code?

Q18. Given code:

A18. Firstly, let’s take abstraction and encapsulation as the difference is subtle. Abstraction is often not possible without encapsulation because if a class exposes its internal state, it can’t change its inner workings. Encapsulation hides the underlying state. One of the ways to achieve abstraction is by sub classing. The interface “List” is an abstraction for a sequence of items indexed by their position. The concrete examples of a list are “ArrayList<E>”, “LinkedList<E>”, CopyOnWriteArrayList<E>, etc.

Screen shot 2015-03-07 at 9.25.16 PM

Both abstraction and encapsulation solve same problem of complexity in different dimensions. Encapsulation exposes only the required details of an object to the caller by forbidding access to certain members,

Q. Why is it good to access state via public methods like getAge(), setAge(), etc as opposed to directly accessing the state “age”?
A.

1) The methods like setAge(int age) can perform precondition checks when setting the age by validating and throwing exception when age is -ve or very high positive like 120+.
2) Encapsulation promotes abstraction by only exposing public methods and the private methods are for internal implementation use. If a class exposes its internal state, it can’t change its inner workings in different implementations.… Read more ...



05: Q19-Q24 How to create a well designed Java application?

A software application is built by coupling various classes, modules, and components. Without coupling, you can’t build a software system. But, the software applications are always subject to changes and enhancements. So, you need to build your applications such a way that they can not only adapt to growing requirements,…

Read more ...


06: Q25 – Q32 SOLID design principles interview questions & answers for Java developers

Design principles interview questions & answers for Java developers so that you can expand your OOP skills to design robust Java apps Q25. What are the SOLID design principles? A25. SOLID is an abbreviation for 5 design principles. SRP (Single Responsibility Principle) If you have a class with calculation logic,…

Read more ...
Tags:

07: Q33 Top 5 OOP tips for Java developers

Tip #1: Tightly encapsulate your classes. A class generally contains data as well as methods, and is responsible for the integrity of its own data. The standard way to protect the data is to make it private, so that no other class can get direct access to it, and then write…

Read more ...
Tags:

08: Write code to add, subtract, multiply, and divide given numbers?

A trivial coding example (i.e. a Calculator) tackled using the following programming paradigms in Java not only to perform well in coding interviews, but also to learn these programming paradigms.

Approach 1: Procedural Programming
Approaches 2 – 4: Object Oriented Programming
Approach 5: Functional Programming (Java 8)

Approach 1: Procedural

Read more ...


5 FAQs on transforming your thinking from OOP to FP

Functional Programming (i.e. FP) is prevalent in Big Data (i.e. Spark programming, etc) roles. A must know for Big Data Engineers and Analysts.

One needs to get used to the transformation from imperative programming to functional programming. You like it or not, you will be using functional programming in Java, and interviewers are going to quiz you on functional programming. Fortunately, Java is not a fully functional programming language, and hence one does not require the full leap to functional programming. Java 8 supports both imperative and functional programming approaches.

Q1. What is the difference between imperative and declarative programming paradigms?
A1. Imperative (or procedural) programming: is about defining the computation how to do something in terms of statements and state changes, and as a result what you want to happen will happen.

Declarative programming: is about declaratively telling what you would like to happen, and let the library or functions figure out how to do it. SQL, XSLT and regular expressions are declarative languages.

Q2. Does functional programming use imperative or declarative approach?
A2. Functional programming is a form of declarative programming, where functions are composed of other functions — g(f(x)) where g and f are functions. An output of one function becomes the input for the composing function.

A typical example of functional programming is Unix pipe lines where output of one function becomes input of next.… Read more ...



Q1 – Q10 Java OOPs interview Q&As

If you don’t get Java OOPs interview questions & answers right in the job interviews you can say OOPS !!!! to your Java interview success. Q1. Is Java a 100% Object Oriented (OO) language? if yes why? and if no, why not? A1. I would say Java is not 100%…

Read more ...
Tags:

Why favour composition over inheritance?

This is not only one of the most popular Java OOP Interview Questions & Answers asked 90% of the time in job interviews, but also a key OOP concept you must know well. The correct answer depends on the problem you are trying to solve, and the answer you give…

Read more ...


500+ Enterprise & Core Java programmer & architect Q&As

Java & Big Data Tutorials

Top