Tuesday, June 30, 2020

Features of JAVA 8 Introduction


1. What is an objective of Java 1.8 features?

  • To simplify the programming steps
  • To enable the functional programming benefits in java
  • To enable parallel programming (multi core processing)

2. What are the features available in java 1.8?

  • Lambda Expression
  • Functional Interfaces
  • Default methods
  • Predicates
  • Functions
  • Double colon Operator ::
  • Stream API
  • Date and Time API

Sunday, June 7, 2020

C programming_ Day 1

Importance of Programming Language
  • A programming language is an instruction set written machine understandable language that instructs computer to do the desired task. 
  • A programmer writes source code text in the programming language to create programs. It is used to make all the computer programs and computer software to control the assign task in certain procedure.
  • For example: Binary language, Assembly language, B, PASCAL, GOBAL, C, C++, C#, JAVA, Python, Go and etc.

Introduction to C language:
  • C is a general-purpose high level language that was originally developed by Dennis Ritchie for the Unix operating system.
  • It was first implemented on the Digital Equipment Corporation PDP-11 computer in 1972.
  • It is an upgrade version of languages B and BCPL.
Steps to Learn Programming Language



//80% usually Means "excellent"
#include <stdio.h>
int main()
{   int n=80;
    char ch='a';
    printf("%d%% usually\n Means \?excellent\?\v",n);
    printf("Learn\t with durga\v");//Learn       with durga
    printf("Never stop learning\n");                        //Never stop learning
    printf("Food\rGuard");//\r-> carriage return ==>Good
    return 0;

}

Structure of C programming Language


Declaration and Initialization:

int a,b,c=10;
int a=2,b;
if(pf)
for
//#include <stdio.h> //printf and scanf
#include"stdio.h"//userdefined modification
int main()

  //  int a=7;
   // int a='c';//a=99
    int a=printf("Learn with Durga\n");//Learn with Durga
    printf("%d",a);//a=?
    return 0;
}

2.
#include<stdio.h>//userdefined modification
int main()

   int a=20;
   printf("%d\n",a); //20
   printf("%4d....\n",a); //__20
   printf("%-4d....\n",a);//20__
   printf("%6.5d....\n",a);//_00020
   printf("%-6.5d....\n",a);//00020_
    return 0;
}
Storage classes available in C:
  • auto
  • extern
  • static
  • register
auto:
#include <stdio.h>
int main()
{   auto int i=5;
    {
        auto int i=4;
        printf("%d\t",i);
        {
        auto int i=3;
        printf("%d\t",i);
        }
        printf("%d\t",i);
    }
     printf("%d",i);
    return 0;

}

static:
#include <stdio.h>
int main()
{   static int a=4;
    printf("%d\t",a--);//4 3 2 1
    if(a>0)
    main();
    return 0;

}

extern:
#include <stdio.h>
int main()
{   extern int a;//a=9
    printf("%d\t",a);//9
    a=10;
    printf("%d\t",a--);//10
    printf("%d\t",a);//9
    return 0;
}

a=9;
Classification of Datatype:
  • Primitive datatype - int,float, double and char
  • User defined datatype - typedef and enum
  • Derived datatype: primitive derived - array and user-defined derived - structure and union

step 3: Instruction set

  • Decision making statement - if else and switch
  • Looping statement - while, do while and for


7.1. Multithreading in JAVA

1. What is Multi threading?
The process of executing two or more threads concurrently, where each thread responsible for different task at the same time is called as multi threading.

2. Difference between sleep() and wait()

3. How to call a thread? 
  • Threads may not execute sequentially, meaning thread 1 may not be the first to write its name to System.out. This is because the threads are principle executing in parallel and not sequentially. The JVM and/or operating system determines the order in which the threads are executed. This order does not have to be same order in which they were started.
  • Main threads automatically start by JVM and the other user defined threads calls by using start() method.

4. Can Java thread object invoke start method twice?
No. After starting a thread, it can never be started again. If you do so, an IllegalThreadStateException is thrown. In such a case, thread will run once but for a second time, it will throw an exception.

5. What is Daemon thread?
Daemon thread is a low priority thread,works with JVM when all the user threads finishes their execution, JVM doesn't care whether Daemon thread is running or not because it terminates the thread and after that shutdown itself.
Example:
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().setDaemon(true);
System.out.println(Thread.currentThread().isDaemon());
       }
}
Output:
True


6. Can we change the name of the main thread ?

Yes, we can change the name of the main thread. It can be done by first getting the reference of the main thread by using currentThread() method and then calling setName() method on it.
For example:
public class ThreadDemo {
public static void main(String[] args) throws InterruptedException {
Thread t=Thread.currentThread();
t.setName("Durga");
                System.out.println(t.getName() +"Thread");
        }
}
Output:
Durga Thread.

Wednesday, June 3, 2020

6.1 Exception Handling

1.What are the differences between an error and an exception?


2. Draw the hierarchy of exception.

3. Difference between Checked and Unchecked Exception.

Checked Exception
Unchecked Exception
Exception which is checked by the compiler whether programmer handling or not, for normal execution of the program at runtime, is called checked exception. 
Exception which is not checked by the compiler whether programmer handling or not, is called unchecked exception.
Eg: 
InvalidAgeException
HallTicketMissingException
FileNotFoundException
Eg:
ArithmeticException
BombBlastException
NullPointerException

4.What is the order of catch blocks when catching more than one exception?
case 1: Exception catch block defined before NumberFormatException catch block will throw an unreachable catch block error. Because it is already handled by Exception catch block.
public class MultipleCatchBlock {
public static void main(String[] args) {
try {
int a=Integer.parseInt("One");
}
catch(Exception e) {
System.out.println(e.getMessage());
}
catch(NumberFormatException e) {
System.out.println(e.getMessage());
}
}
}
Output:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unreachable catch block for NumberFormatException. It is already handled by the catch block for Exception at Batch4.MultipleCatchBlock.main(MultipleCatchBlock.java:11)

case 2: NumberFormatException catch block defined before Exception catch block handles the specified exception and terminate the program normally without showing any error.
public class MultipleCatchBlock {
public static void main(String[] args) {
try {
int a=Integer.parseInt("One");
}
catch(NumberFormatException e) {
System.out.println(e.getMessage());
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
Output:
java.lang.NumberFormatException: For input string: "One"

5. How many different ways to print the Exception message?
There are three different way to print exception information in different format as follow:
  • printStackTrace() → prints Exception package and class: Name of the exception and its path.
  • toString() → prints Exception package class: Name of the exception
  • getmessage() → prints only Exception name
For Example:
public class ExceptionDemo {
public static void main(String[] args) {
try {
System.out.println(25/0);
}
catch(Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println(e.toString());
}
System.out.println("Learn with Durga");
}
}
Output:
java.lang.ArithmeticException: / by zero
at Batch4.ExceptionDemo.main(ExceptionDemo.java:6)
/ by zero
java.lang.ArithmeticException: / by zero
Learn with Durga

Note: default exception handler internally uses printStackTrace() method to print exception information to the console.

6. Describe behavior of following exception?
RuntimeException - unchecked
Error -unchecked
IOException - fully checked
Exception - partially checked
InterruptedException - fully checked
CloneNotSupportedException - checked
Throwable - partially checked
ArithmeticException - unchecked
ConcurrentModificationException - unchecked
NullPointerException - unchecked
FileNotFoundException  - fully checked

7. Difference between throw and throws.



8. Which are the valid declaration of try catch and finally block as in the following options?




9. What is finally block?
finally block is used at the end of exception handling try-catch block and it terminated the exception thrown by the try block even it is handled/not handled or checked/unchecked. finally block will not executed if there is no try block mentioned in the program. 

5.1. Java Collection

1. Difference between Collection and Collections
Key
Collection
Collections
Basic 
interface in Java collection framework 
utility class in Collection framework 
Static Methods 
doesn't has all static methods 
has all static method 
Operation 
used to store list of object in a single object  
used to operate on collection.

Difference between ArrayList, LinkedList and Vector.

Difference between HashSet,LinkedHashSet and Treeset.

What is legacy class?

What is concurrentModificationException?

What is the difference between Hashtable and HashMap?

What is concurrentHashMap?


Is Map comes under collection framework?
No. Map interface has key and value pair stored under the common name. Cursor available under collection hierarchy such as Iterator, ListIterator, Enumeration and foreach doesn't iterates the map interface variable.

4.3 OOPS basics - Inheritance and Polymorphism

1. What is Polymorphism?
Polymorphism is the ability of an object to take on many forms. In a same class or classes inheriting other class having same method name with different behavior (Overriding) and same method with different in number,type and order arguments(Over loading) is know as Polymorphism.

2. What is runtime polymorphism or dynamic method dispatch?


3. Difference between overloading and method overriding?


4. How to prevent override method in Java?
There are three different ways to prevent overriding of method in Java. The following three approach does that
  • Private method cannot be overridden, because it is not visible to the other class.
  • Static method can also not be overridden, because it relates with class rather than objects. So it hides the method to the child class.
  • Final method cannot be overridden, because it doesn't allows redefinition of the method.

5. What is Inheritance and its types?
Inheritance is the process of using extends keyword,  the attributes and behaviors of one class used in  the other class.

6. What is multiple inheritance and does java support?
In multiple inheritance, child class inherits the properties of two parents classes. Multiple parent classes may contains the same method signature causes the confusion in the child class which method to inherit during runtime, called as diamond problem. There is no virtual keyword in java to avoid this problem. Due to this problem, java doesn't allow inheritance with classes but it allows using interface.

7. What is the difference between super() and this()?
super() is constructor of an immediate parent class. this() is a current class constructor.
Example:
class A{
A(){
System.out.println("Class A no argument constructor called");
}
}
class B extends A{
B(){
System.out.println("Class B no argument constructor called");
}
B(String s){
this();
System.out.println("Class B argument constructor called");
}
}
public class Constructor{
public static void main(String[] args) {
new B("Learn");
}

}

Output & Explanation:
Class A no argument constructor called
Class B no argument constructor called

Class B argument constructor called



8. What is the super class of all java classes?

Object class is a super class of all java classes which is available in java.lang.object. All user defined class extends from object class and it follows simple inheritance, because it collects the behaviors of objects to the implemented class.

9. How can you create an immutable class in java?
  • Define class as final public (extends are not allowed)
  • Define the properties inside class also private and final
  • Assign the values with the help of constructor
  • Declare only getter method() (setter method not allowed)
Example:
final public class Immutable{
private final int a;
public Immutable(int a){
this.a=a;
}
public int getA(){
return this.a;
}
}
10.