Saturday, January 30, 2016

JAVA Programs in Interview

In This Post we will discuss basic java programs .Interviewer want to test your programming skills and way of writing your stranded code in logical way.I had already post frequently asked java program is to find prime numbers between 1 to 100 .Some of the java programs we will discuss in this post.These Programs are fundamental  java programs,therefore before face java technical Round brush up these programs specially for beginners. These will get nervous feeling while facing interviews.lack of confidence in programming.So, I am writing frequently asked java programs in interview chamber. Let's start friends!!





Program 1: Java program to find whether the given number is palindrome or not


palindrome: A number is palindrome if and only if  the sequence of characters which reads the same forward and reverse directions is called as palindrome number.

Ex:  


Source Code:

import java.util.*;
class PalindromeDemo{
public static void main (String args[])
{
int reverse=0,rem;
int number=Integer.parseInt(args[0]);
int n=number;//here we are using n variable to check  last time to check
while(number>0)
{
rem=number%10;
reverse=reverse*10+rem;
number=number/10;
}
if(reverse==n)
{
System.out.println("The given number is palindrome");
}
else
{
System.out.println("The given number is not palindrome");
}
}

output:






Program 2: Java Program To Print Fibonacci Series of given number

Definition:

The First two numbers in Fibonacci series are 0 and 1  and each subsequent number is sum of the previous two numbers.






Source Code:

import java.util.Scanner;
class FibDemo
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a=1,b=1,c=0,n;
System.out.println("Enter n value");
n=sc.nextInt();
System.out.print(a);
System.out.print(" "+b);
for(i=0;i<n-2;i++)
{
c=a+b;
a=b;
b=c;
System.out.print(" "+c);
}
System.out.println();
System.out.println(" the series is"+c);
}
}

Output:



Also check This Post: Java Program to check whether the String is Palindrome or not





Linked List in java

In this article we will discuss about linked list 
in data structure using java programming 
language.I had already posted Introduction
to Data Structure . while you develop computer
programming software projects you should 
include linked list data structure to organize 
your data in the computer memory.







Linked list is a common and simple data structure.This data structure comes under linear data structure which consists of group of nodes and  the elements are stored in the sequence order in the computer memory. A node contains two fields,they are Data item & Link address.Data Item contains actual value and link contains next node address. In this we use a special pointer called HEAD, contains first node address.



Difference between Array and Linked List:

Array:


  • Array is a collection of Homogeneous elements
  • we can easily identify a particular element in the list
  • It is a static Data structure
  • Here is size is fixed
Linked List:

  • Linked list is a linear data structure
  • It is not easy to identify a particular element in the list
  • It is a dynamic Data Structure
  • Here size is not fixed

Advantages of Linked List:

  1. we can easily implement stack,queue using linked list
  2. we can easily insert and delete elements in the list
  3. linked list has dynamic nature so memory size increase automatically. So we need to know the size in advance

Types of Linked Lists:

Lined Lists are three types. They are

1. Singly Linked List
2.Doubly Linked List
3.Circular Linked List

Singly Linked List:

It is a collections of linear data elements.In this an element is referred as a node. A node contains two fields,they are data item & link address. Data item contains actual value and link contains next node address.Int this we use a special pointer called HEAD,contains first node address.It means singly linked list first node address will be store in the Head pointer.Last node link contains the NULL pointer.

The representation of single Linked List is as follows:


we can develop the single linked list by using the following operations:

1.Creation
2.Traversing
3.Insertion
4.Deletion

1.Creation:

To create a linked list first we check the condition whether the head is null or not. If the head is null then create a new node and assign that address to the head. Otherwise create a new node and assign that address to the previous node link.

Algorithm:

Step 1: Start
Step 2: if(Head==NULL)
                p=new node
                p---->data=num
                p---->link=NULL
                head=p

Step 3: else
                t=new node
                t----->data=num
                t----->link=NULL
                p---->link=t
                p=t

Step 4: stop

2.Traversing:

In this we display each and every element in the list.In single linked list traversing can possible only one direction that is from head to last element.


Algorithm:

Step 1: Start
Step 2: Set a=head
Step 3: Repeat the steps 4,5 while(a!=NULL)
Step 4: print a----->data
Step 5: set a=a----->link
Step 6: stop


3. Insertion:

In single linked list we can insert the element in three ways: they are

1. Insert the element at the beginning of the list
2.Insert the element at the end of the list
3.Insert the element at after specified node


Insert the element at the beginning of the list:

If we want insert the element at the beginning of the list then we create new node and assign that new node address to the head and also establish to the next node.


Before Insertion:






After Insertion:



Algorithm: insert-beginning(num)

Step 1:  Start
Step 2: t=new node
step 3: t----->data=num
Step 4: t------>link=head
step 5: head=t
step 6: Stop


Insert the element at the end of the list:

To insert the element at the end of the list we can create a new node and assign that node address to previous node link and assign NULL Pointer.

Before Insertion:



After Insertion:




Algorithm:  Insert-ending(num)

Step 1: Start
Step 2: t=new node
step 3: t----->data=num
step 4: t----->link=NULL
step 5: p----->link=t
step 6: p=t
step 7: Stop


Insert the element at the after specified node:

To insert an element at the specified node first we check the whether the node is in the list or not. If the specified node is found then that node points to the new node element and the node element points to the next node.Otherwise display element is not found.


Before Insertion:



After Insertion:




Algorithm: Insert-after(ele,num)

Step 1: Start
Step 2: set a=head
Step 3: repeat the steps 4&5 while(a!=NULL)
Step 4: if(ele==a--->data)then
               t=new node
               t----->data=num
               t----->link=a----->link
               a----->link=t
Step 5: else
               a=a----->=t

Step 6: Stop


Deletion:

If we want to delete an element from the list we can follow two methods:

a. if the deleted element is first element,then assign second node address to the head.
b. if the deleted element is middle element,then assign next node address to the previous node.


Before Deletion:



After Deletion:



Algorithm: deletion(num)

Step 1: Start
Step 2: set a=head
Step 3: if(a---->data==num)then
              head=a----->link,delete a;
step 4: else
               set b=a,
               set a=a----->link

step 5: repeat the steps 6&7 while(a!=NULL)
Step 6: if(a---->data==num)then
                b---->link=a---->link
              delete a;
step 7:  else
                 set b=a
                 a=a----->link

Step 8: Stop





















Monday, January 25, 2016

TOP HR Interview Questions for Freshers

I am back with frequently asked HR Interview Questions especially for freshers and campus Interviews.Now a days many Engineering Students  do not have idea the way interview process will be conduct in MNC company and in campus interviews. For that purpose i had already discussed visit this link Interview procedure in MNC Company. So Here we go some of the most frequently asked HR Interview Questions for freshers.This post only for freshers not for experienced if you want experienced HR interview Questions for Experienced.


1. Why do you want to work at our company?

Ans: Interviewer want to know what he knows about this company and what services are providing , So you can tell like this,before applying to this company, i went to your website and was very happy to see that your company deals in X,Y,Z . The products developed by your company are very good and highly recognized in the industry.My friends  who work in your company are very with their job and work they perform.Though i am a fresher,I am confident that given a chance i will perform to the best of my abilities and will prove to be a strong performer of the company.




2. Can you work under pressure?

Ans: Any Software company will look for the people who can work under pressure. So you should say "YES"with a tremendous example which should convince the HR that you are damn fit for the company. You can tell answer like this,pressure comes when we really bored of our job or forcing taking the job on but i take my job as my passion and interest.I believe that if we love what we do,we never feel pressure and we must always be ready to face the upcoming challenges.so i think i can do well.

3. What are your career options right now?

Ans:  simply you can tell the answer to this question, my career option is to get placed in a reputed company like yours.if you give opportunity for me to work in your company,i would like to enrich my professional skills and will contribute myself for the growth of the company.

4. Can you tell how would be an asset to this Organization?

Ans:  The best answer for this question is, i will be an assert to our company by having the qualities as of determination,dedication and punctuality of an employee makes him an asset for the company.And also should have hardworking nature and having attitude of moving spirit makes me unique from others will makes me an asset to our company.

5. Who has inspired you in your life and why?

Ans:  I am giving my best answer for this questions is, you can tell like this my inspiration is my mother because she may be the home maker but she control's and manages everything in the home. Even though she has more pressure to do work at home but she never share sadness with everyone but she always seems happiness on her face. I have learnt lot from her like hardworking,greeting the work done,discipline. 



6. Where do you see yourself five years from now?

Ans: This question is frequently ask for the fresher,you can tell like this,after 5 years i see myself as a efficient,skillful and holding a responsible position in your organization. where i utilize all my skills and dedication for the development of the organization.

7. How much salary do you expect?

Ans: The best answer to this question is , i hope i will get salary according to the company standards and designation to which i am posting and it must be equal to my knowledge. Being a fresher,i need a platform to increase my skills and knowledge and salary does not matter for fresher.I hope you will give salary according to company standards and designation.

8. How long would you expect to work for us if hired?

Ans: The best answer to this question is, as long as i feel that i am contributing and that my contribution is recognized. I am looking a long term commitment. I will do my best for the growth of your company for a long period of time .

9. What is difference between hard work and smart work?

Ans:  simply say answer to this question is, Hard work gives good results but smart work gives the best results a very short span of time.

10. Do you have any questions for me?

Ans: Finally interviewer asks you this question,do not hesitate  to ask question, ask like this thank you for giving me this opportunity,after my overall performance till now if i would get selected what i need to improve myself  and if i would  not selected how can i succeed further can you give any advice sir.  

Top 10 Java MultiThreading Interview Questions and Answers

MultiThreading topic is very important feature of Java Programming language. This post is dedicated to all Java Developers. On this topic many questions will ask in all the interviews where the companies working on banking solutions,Big data processing and async functionality pretty heavily often use multithreding . I had already discussed Multithreading in java with example. You should have awareness of multithreading in java and also life cycle of thread in java.

1. What is Thread in java?

Ans: Thread is an Independent sequential path of execution within a process. Threads are light weight process. Threads are exists in common memory space and can share resources data and code.Threads can run parallel. In java, Thread  is represented and controlled by an object of class Java.lang.Thread.


2. What is difference between Thread and Process?

Ans:  A Thread is lightweight compared to process.Threads are subdivision of process.Threads exists within a process and shares the process resources like memory whereas Process is self contained execution environment. Each process has its own memory space.
Threads have direct access  to the data segment of its process whereas process  has own copy of the data segment.
Context-switching between threads in the same process is very fast whereas context-switching between process is slower.
Threads can easily communicate within the process whereas  process communicate only system provided inter process communication mechanism.

3. How many ways you can create Thread in java?

Ans:  You can create a Thread in two ways in java. They are

    a) Extending a Thread class
    b)  Implementing a Runnable interface

For more details visit: multithreading in java

4. What is Difference between Wait() and sleep() in java?

Ans:  Firstly you must aware of where these methods are exists. wait() method exist in Object class whereas sleep() method available in the Thread class. sleep() method is used the currently executing thread to sleep for specific period of time(in milliseconds)  whereas wait() method causes the current thread to wait until another thread invokes notify() or notifyall() for this object. Here thread releases the lock as soon as wait is called,but in case of sleep() method the Thread does not release the lock.

5. What is demon Thread in java?

Ans:  demon threads are  runs in the background. These threads are service provider threads. Garbage collector is the best example for demon threads,because it is used to reclaim the unused memory.

We can make a thread demon by using setDaemon(boolean status) which is available in Thread class.

Ex: public void setDaemon(boolean status)

If you want check a thread is daemon or not:

public boolean isDaemon()


6) What deadlock in java? how to prevent it?

Ans:  This is very important interview question.Interviewer want to test  your capability how to create and detecting deadlock . Here is the link deadlock in java with example .

7) What is the use of Synchronized in java?

Ans: Synchronized keyword is used  in java to control the access of multiple threads on shared resources. Synchronized keyword can be applied to static and instance methods and synchronized block in java of  the code. If you apply synchronized keyword for method only one thread can access the same thread then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object. 

TO KNOW MORE DETAILS : Synchronized block in java

8)  What is the internal process when start() method is called?

Ans:  A new Thread of execution with a new call stack starts. And then the state of thread change from new to Runnable. When  thread gets chance to execute its target run() method start to run.

9) What will happen if you do not override run()method?

Ans:  This is basic question on thread , interviewer want to know your knowledge on thread api and you really have knowledge how to create and run a thread.

When we call start() method it internally calls run() method  with newly created thread.So if we do not override run() method a newly created thread will not called and nothing happen. that run() method executes just like normal method.

10) What is difference between notify() and notifyall() method in java?

Ans: Their might be several threads waiting for this object and only one of them is chosen.notify() method  is main work is wake up a single thread  that is waiting on this object whereas notifyall() methods wakes up all the threads that are waiting on this object's monitor.


Sunday, January 24, 2016

How to Create User defined Exception In Java

In This Post We Will discuss custom exceptions or  User defined Exception in java, In the earlier post we had already discussed Exception Handling in java. You just go through that link to aware of exception in java.While developing  in your programming  projects you will be required to define Custom Exception or User defined Exception.

User defined Exception means Exception class is created by user.Some of the Built in Exception classes are provided by the Exception and error classes may not be enough to trap errors occurring in the program.You can also see this link top exceptions in java frequently raising while developing programming project.So in some cases you need to create your own exceptions. User defined class should be subclass of Exception class.

Why we Use Custom Exception in Java: 

User defined Exceptions is use to show custom messages to the end users. And also used to hide null pointer exception,arithmetic Exception. So custom exceptions used in our programming project very commonly.


Procedure to create User defined Exception in java:

To create user defined exception we need to write subclass simply extending the java Exception class and you can override toString()function to display your customize message on catch.
  
In the Exception class provides methods to show error message to the end users.They are




















While you want to develop user defined exception in your programming project no need to override any of the above methods which are available in the Exception class in the sub class. Based on your project requirement you have to customize your message to show end users.

Example: Create Custom defined Exception

class TooYoungException extends RuntimeException
{
TooYoungException(String s)
{
super(s);
}
}
class TooOldException extends RuntimeException
{
TooOldException(String s)
{
super(s);
}
}
class CustomExceptionDemo
{
public static void main(String arg[])
{
int age = Integer.parseInt(arg[0]);
if(age > 60)
{
throw new TooOldException("Younger age is already over");
}
else if(age <18)
{
throw new TooYoungException("Please wait same more time");
}
System.out.println("Thanks for register");
}
}

Output:




Note: You can Override toString() method to display custom message.
           You must extend the Exception class to create custom exceptions 







Friday, January 22, 2016

Top 10 OOPS Concepts Interview Questions and answers

In this post we will discuss different types of java interview questions that can be very useful when attending java interview.Basically, interviewer starts technical interview from basic questions such as Object Oriented programming questions to test your skills in java.

1.What is OOP? Use of OOP in java?
Ans: OOP stand for Object Oriented Programming,it is a computer programming language that is completely depends on class-based and Object oriented. If you develop any software by using Object Oriented based Programming language you will have many advantages. They are
  • Re-usability of the code
  • Easy maintenance and modifications
  • for programmers  very easy to understanding the code
  • it provides Security
  • Reliability and flexibility of the code

    2.   What are the Principles concepts in java?
   Ans: Object Oriented Programming contains significant features  they are
  •    encapsulation
  •    polymorphism
  •    inheritance
  •    abstraction       





3.  What is class in java?
Ans: A class is a blueprint of an object. That means we can create class from which objects of same type can be created. For Example take a car. There are thousands of cars with having same characteristics that is having same design and model. so all the same types are created from blueprint called  class.  class combination of variables and methods.

Ex:   
class Test{
int a,b;
void show();
{
}
}
4. what is object in java?
Ans: In Object Oriented Programming this is most basic concept in java.Object is instance of class. Object is an entity that has three characteristics they are:
  • State
  • behavior
  • identity
These are the example of objects, car,bike,dog,pen,table etc...Lets take an example of a car. car has some state like (gear,speed) and behavior (change gear,apply break). It identify the state and behavior of  real world object. This is very suitable program for solving real time problems. software objects are similar to the real world objects. An object stores its state in fields and expose through methods.

5. What is encapsulation?
Ans: Encapsulation can be achieved by combining variables and methods in the class.So class is the base of the encapsulation. This provides protective barrier that prevents the access of the code and data from outside of the class.The main advantages of the encapsulation are given below:
  • The internal state of every object is protected by its attributes
  • it improves the maintenance of the code and usability 

6. What is Inheritance?
Ans:  It is one of the most important feature in Object Oriented Programming. It has the capability of the one class to acquire common state and behavior of another class while adding its own functionality. That means getting properties from one class object to another class object.so re-usability happens here without rewriting same code.that means we can add additional features to a new class without modifying it.

More details on Inheritance: Inheritance in java

7. What is Polymorphism?
Ans: 
polymorphism means many forms. It has ability of one type to take more than one form.So we define multiple methods with same name.we can achieve polymorphism  in java in two ways they are
method overloading
method overriding

More details on Polymorphism: Polymorphism in java

8. What is abstraction?
Ans:It is a process of separating the ides from specific instances. It refers to the act of presenting the essential features without including background details or explanations. That means it hides the some of the details in order to bring out more clearly other aspects. Abstraction can be achieved in two ways

More details on Abstraction:  Abstraction in java

Abstract classes and interfaces

9. What is difference between abstraction and encapsulation?
Ans: Abstract focus on the outside view of an object and hides the unnecessary details whereas encapsulation provides a protective barrier to prevent access of the code and data from outside of the class
    Mainly we use abstraction in design side whereas  encapsulation in implementation side.

10. Is it possible to override main method in java?
Ans: No. Static methods can not be overridden in java 

    

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...