Showing posts with label Interview Question. Show all posts
Showing posts with label Interview Question. Show all posts

Introduction to Multithreading /Chapter 1 / Multi Threading

Multi Threading

java multithreading


We can define java multithreading in the following two ways:-



  • By extending Thread class

  • By implementing Runable interface


1. By extending Thread class:-


example:-
class MyThread extends Thread
{
public void run ()//This for run method will be executed by child thread
{
for(int i=0;i<10;i++)
{
System.out.println("Child Thread");
}
}
}
class ThreadDemo
{
public static void main(String [] args)
{
MyThread t =new MyThread();

t.start();// starting of thread

for(int i=0;i<10;i++)
{
System.out.println("Main Thread");//This for Loop will be executed by main thread
}
}
}

run method:-


run method present is thread class and it has an empty implementation. child thread is responsible for executing run method. So we have to override the thread class run method in our java program. If we don't override run method then it's super class means thread class run method will be called and it has empty implementation so no output will be produced.

Java Customized Exceptions / Chapter 11 / Exception Handling

Java Customized Exceptions


Sometimes to fulfill the programming requirement we can define our own exceptions such type of exceptions are called customized exceptions.

Let's take an example of customized exceptions.
import java.util.*;
class TooYoungException extends RuntimeException
{
TooYoungException (String s)
{
super(s);
}
}
class TooOldException extends RuntimeException
{
TooOldException (String s)
{
super(s);
}
}
class CustomizeException
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the age ");
int age=sc.nextInt();
if(age>60)
{
throw new TooOldException("Your age is already crossed the marriage age...
try In next birth... ");
}
else if(age<18)
{
throw new TooYoungException("Please wait some time...
You will get your best match soon... ");
}
else
{
System.out.println("Congrats!!!You are eligible for marriage..
you will get details of your match by email!!");
}
}
}

Output is:-

java throw keyword /Chapter 9 /Exception Handling

java throw

java throw keyword:-


throw keyword used to create our own exception object explicitly and we can handover this (our own created) exception object to JVM manually.


example:-java throw

The main objective of throw keyword is to hand over our created exception object to JVM manually.


Let's take two example where the output will be same.

Some Possible Combinations of try catch finally /Chapter 8 / Exception Handling

try catch finally


Some possible combinations of try catch finally


try 
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(Exception e)
{

}

The above combination is valid.

—————————————————————————————————————————————
try
{

}
catch(ArithmeticException e)
{

}
catch(ArithmeticException e)
{

}

The above combination is invalid.And we will get Compile Time Error.

Difference Between final,finally,finalize /Chapter 7 / Exception Handling

Difference Between final, finally, finalize


final is a modifier applicable for method, classes & variables.


1.final method:-


Whatever method parent class has by default available to the child class through inheritance.

If the child class is not satisfied with the parent method implementation than the child is allowed to redefine that method based on its requirement, this process is called "Overriding".


But,

If the parent class method declared as final then we can't override that method in child class. Because it's implementation is final.


2. final class:-

If a class declared as final we can't extend the functionality of that class so that we can not create the child class for that class. That's why inheritance is impossible for final classes.


Introduction of Exception Handling /Chapter -1 / Exception Handling

java exception

Java Exception:-


An unexpected unwanted event that disturbs normal flow of the program, is called an exception.

The purpose of exception handling:-


The main objective of exception handling is "graceful termination " of the program. It is highly recommended to handle the exception in java program.

Explanation:-


Exception Handling doesn't mean repairing an exception. We have to provide an alternative way so that the rest of the program will execute normally, is the concept of exception handling.


for example: -

Java Interface /Chapter -13 /OOPs Concept


Java Interface



  • Any service requirement specification (SRS) is considered as an "interface".

  • Inside "interface", every method is always abstract whether we are declaring or not hence, the interface is considered as 100% pure abstract class.

  • Any contract between client and service provider is considered as an "interface".


Interface declaration and implementation



  1. For each and every method present in the interface we have to provide implementation otherwise, we have to declare the class as abstract then next level child class is responsible for providing the implementation.

  2. Every method present in an interface is always public and abstract we are declaring or not hence, whenever we are implementing an interface's for every method compulsory we should declare as public otherwise we will get Compile-Time Error.


Constructor in Java /Chapter -12 /OOPs Concept

java constructorjava constructor


Whenever we are creating an object some part of the java program(code) will be executed automatically to perform initialization of the object, this part of the program is called "Constructor".
Hence the main objective of the constructor is to perform initialization of an object. 

example 1.

class Employee

{

String name;

int eid;

Employee (String name, int eid)

{

this.name = name;

this.eid = eid;

System.out.println(name+"-----"+eid);

}

public static void main(String args[ ])

{

Employee e1 = new Employee("Dhoni",101);

Employee e2 = new Employee("Virat",102);

}

}

Output of the above program is:-

In how many ways we can create Object in JAVA?/Chapter-11/OOP's Concept

In how many ways we can create Object in JAVA?In five ways we can create Object in java.




  1. By using the new operator:-In how many ways we can create Object in JAVA?




  2. By using newInstance( ) method:- In how many ways we can create Object in JAVA?




  3. By using the clone( ) method:- 

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com