Interview Questions

Java

Recursive Templates in Underscore Js

Use the following code to call a template recursively in Underscore JS.
This example use Backbone JS, JSTree(Jquery) to display tree nodes

<!DOCTYPE html>
<html lang="en">
        <head>
                <meta charset="utf-8">
                <title>Tree Structure</title>
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <meta name="description" content="">
                <meta name="author" content="">
                <!-- Le styles -->
                <link href="css/bootstrap.css" rel="stylesheet">
                <link href="css/bootstrap-responsive.css" rel="stylesheet">

Java Interview Questions & Answers

Java Interview Questions & Answers

How to avoid cyclic redundancy using Jackson library for Many-to-Many entities?

Use the following code, to avoid cyclic redundancy using jackson library for converting Many-to-Many java entities to JSON.

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class,
                  property = "@id")
public class Student{}

To avoid creating '@id',for every object in JSON, ObjectIdGenerators can be set to None as follows.

@JsonIdentityInfo(generator = ObjectIdGenerators.None,
                  property = "@id")
public class Student{}

Basic Example Using JPA

The following code example illustrates creating/upating database tables using JPA.
The two tables used are Employee and Department. When the main class EmpDAO is run the tables get created in the database and the table records can be queried using JPA API's. Hibernate has been used as implementation for JPA. The dependency jars are referred in the pom.xml.

Employee.java

import java.util.Date;
import java.util.UUID;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;

Basic CSS3 Selectors Usage in HTML

The following code sample illustrates the CSS3 selectors/combinators usage in html documents.

<!DOCTYPE html>
<html>
<style>

section > hgroup{color:#0FFFF}

#form1{margin:20px;font-size:30px;width:300px;border:5px solid green;padding-left:20px}


label:hover {color:red}


p{border:10px solid black}
form:hover {color:blue;font-weight:bold}

b{text-decoration:underline;text-shadow:2px 2px 2px gray,0 0 10px red;
font-weight:bold}

label label{font-size:20cm}
</style>
<body>
<section>
<hgroup>
<h1>Demo</h1>
<p><b>Registration</b></br></br>

Java Program - ThreadSafe Circular Queue

The following is a basic thread safe implementation of a circular queue of user-specified size using a simple array. It contains routines to initialize(), enqueue() and dequeue() the queue.

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR

Java Program to Reverse a Linked List

Use the following code to reverse a Linked list using Java.

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF

How to Auto refresh DIV content in jsp?

Use the following code to auto refresh DIV content in jsp page.

<%@ taglib prefix="c"           uri="http://java.sun.com/jsp/jstl/core"  %>

<html>
<script language="javascript">
var delay = 10000;
var autoRefreshContent = setInterval(
                function ()
                {
                       
                                var url = "<c:url value='/tdd/content.htm'> </c:url>";
                                url = url +"?sId="+Math.random();
                         $('#contentDiv').load(url).fadeIn("slow");
                }, delay);
</script>
<body>
<div id="contentDiv" >
<%@ include file="content.jsp"%>
        </div>
</body>

</html>

Java - Encryption Decryption Example

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS

Java IP Address Generator

/**
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS