April 26, 2016

Part 3: Java Design Pattern Interview Questions & Answers (Factory and Abstract Factory)

In Part 1 of Java Design Pattern Interview Questions & Answers, we discussed the basics. In Part 2, we implemented the Singleton design pattern. In Part 3, we will understand Factory and Abstract Factory design patterns. 

What is a static factory or factory method Design Pattern?

It is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In the Factory pattern, we create an object without exposing the creation logic to the client and refer to a newly created object using a common interface.

Part 2: Java Design Pattern Interview Questions & Answers (All About Singleton Design Pattern)

In Part 1 of Java Design Pattern Interview Questions & Answers, we discussed the basics. Now in Part 2, we will discuss the Singleton design pattern in detail. 

What is the Singleton pattern?

Singleton pattern is one of the simplest design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

This pattern involves a single class that is responsible to create an object while making sure that only a single object gets created. This class provides a way to access its only object which can be accessed directly without the need to instantiate the object of the class.

There are many classes in JDK which is implemented using Singleton pattern like : Java.lang.Runtime with getRuntime() method, Java.awt.Toolkit with getDefaultToolkit() and Java.awt.Desktop with getDesktop() etc.

Part 2: Scenario Based Java Design Pattern Interview Questions & Answers

Scenario 1

  • A company named XYZ Retail is in the business of selling Books, CD's and Cosmetics. 
  • Books are sales tax exempt and CDs and Cosmetics have a sales tax of 10%. 
  • CD's can be imported and attracts an import tax of 5%. 
  • Write a simple shopping basket program, which will calculate extended price (qty * (unitprice + tax)) inclusive of tax for each item in the basket?

Java Solution
We will first create the TaxCalculator interface and its implementation TaxCalculatorImpl.



Goods class is abstract as it implements the common behavior of Book, CDs, and Cosmetics. Also it composes (i.e composition) 'TaxCalculator' to perform the tax calculation based on if the good is imported and if sales tax is applicable.

Now, we will create Book, CDs, and Cosmetics classes that extends the shared behavior (inherits the shared behavior of Goods). The isTaxable & isImported are specific to the product. We can add new products in the future with specific taxable & importable behaviors.



-K Himaanshu Shuklaa

Part 1: Java Design Pattern Interview Questions & Answers

What are Design Patterns?
Design patterns are tried and tested ways to solve particular design issues by various programmers in the world. We can say design patterns are an extension of code reuse.

Design patterns versus frameworks
Are design patterns the same thing as frameworks? The answer to that is NO. Design patterns are more like general guidelines on how to solve specific programming problems, but they do not specify the detailed code that’s necessary to solve those problems.

What are the benefits of using design patterns?
  • Reusable in multiple projects.
  • Design patterns make our code easy to understand and debug.
  • They provide an industry standard approach to solve a recurring problem.
  • Design patterns also provide transparency to the design of an application.

April 24, 2016

#Java: Part 14-How does substring() inside String works?

Substring creates a new object out of source string by taking a portion of the original string. It is defined in defined in java.lang.String class.

substring() has two variants:-
  • substring(int beginIndex): returns the substring starting from the specified index. The beginIndex is inclusive. This method throws IndexOutOfBoundsException if the beginIndex is less than zero or greater than the length of String.
  • substring(int beginIndex, int endIndex): returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex (beginIndex is inclusive and endIndex is exclusive while getting the substring).
If beginIndex is equal to length in substring(int beginIndex) it won't throw IndexOutOfBoundException instead it will return empty String. Same is the case when beginIndex and endIndex is equal.

#Java: Part 13 JDBC Interview Questions And Answers

> > Part 12 Serialization Interview Questions and Answers for Experienced Developers

What is JDBC?
JDBC (Java Database Connectivity) is a Java API that is used to connect and execute query to the database. JDBC API uses jdbc drivers to connects to the database.

April 22, 2016

Madhuri shoots barefoot for the opening act of So You Think You Can Dance..

Everyone is aware of Madhuri’s love for dance. She believes that dance is an art and she has carved her niche in it. Madhuri Dixit who will be soon seen as the judge on &TV’s latest offering ‘So You Think You Can Dance - Ab India Ki Bari’ has done something phenomenal for the show. Read on to know more!

Tiger Shroff joins &TV’s 'So You Think You Can Dance'..


The new Baaghi in town, Tiger Shroff dropped by on the sets of &TV’s upcoming dance reality show SO YOU THINK YOU CAN DANCE. The actor showed off his cool dance moves and impressed everyone with his ‘Street’ style of dancing.

April 21, 2016

BARC Ratings (Impressions)- Week 15, 2016

  
In week 15 (9-15 April, 2016) of the BARC Ratings, Star Plus continued to be the most watched Hindi GEC. Colors remained at No.2 followed by Zee TV at No.3. Life OK took a leap to the fourth spot from seventh in week 14. Sony Pal remained at No.5.

April 15, 2016

A Fairy Tale Visit To Neuschwanstein Castle!!

Neuschwanstein Castle has been on my bucket list for ages because of its mesmerizing aura, breathtaking views and incredible artworks. That's why for me this visit was very, very thrilling. The kind of excitement where I let out little screams in complete anticipation.😻

April 13, 2016

All About jQuery..

What is jQuery?
Its a'lightweight' javascript library. With jQuery we, 'write less, and do more'.  jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation. jQuery run exactly the same in all major browsers, including Internet Explorer 6.

April 07, 2016

Java Reflection API Interview Questions and Answers..

What is Reflection?
Java Reflection makes it possible to inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time. It is also possible to instantiate new objects, invoke methods and get/set field values using reflection. That means with reflection, we can examine or modify the runtime behavior of applications running in the Java virtual machine.

Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.

April 06, 2016

#Java: Part 12 Java Serialization Interview Questions and Answers for Experienced Developers(Serializable and Externalizable)

> > Part 11-Core Java Interview Questions and Answers: Comparator vs Comparable


What are Serialization and De-serialization in Java?

Serialization is a process by which you can save or transfer the state of an object by converting it to a byte stream.

During Serialization, the Object is converted into a binary format, which can be persisted into a disk or sent over the network to any other running Java virtual machine. During deserialization, the reverse process is performed by creating an object from a binary stream.

FYI, the serialization process is platform-independent, which means an object serialized on one platform can be deserialized on a different platform.

#Java: Part 11-Core Java Interview Questions and Answers: Comparator vs Comparable

> > #Java: Part 10-Core Java Interview Questions and Answers

Difference between Comparator and Comparable in Java

Comparable and Comparator both are interfaces and can be used to sort collection elements.

#Java: Part 10-Core Java Interview Questions and Answers

> > Part 9-Core Java Interview Questions and Answers(Java Memory Model, Garbage Collectors in Java)

How  do you prevent SQL Injection in Java Code?
In SQL Injection attack, malicious user pass SQL meta-data combined with input which allowed them to execute sql query of there choice.

ALSO READ: JDBC Interview Questions And Answers

PreparedStatement can be used to avoid SQL injection in Java code. Use of the PreparedStatement for executing SQL queries not only provides better performance but also shield your Java and J2EE application from SQL Injection attack. All the parameters passed as part of place-holder will be escaped automatically by JDBC Driver.

#Java: Part 9-Core Java Interview Questions and Answers(Java Memory Model, Garbage Collectors in Java)

> > Part 7-Core Java Interview Questions and Answers

What is JIT compiler in Java?
  • JIT stands for Just-In-Time compiler. 
  • It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. 
  • The JIT compiler is by default enabled and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it 'just in time' to execute. 
  • Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.
Which are the different segments of memory ?
  • Stack Segment : contains local variables and Reference variables(variables that hold the address of an object in the heap)
  • Heap Segment : contains all created objects in runtime, objects only plus their object attributes (instance variables)
  • Code Segment :  The segment where the actual compiled Java bytecodes resides when loaded.