Wednesday, 6 July 2016

Basics Interview Questions Of Java For Beginners Part-2

Question 26 :- What is the use of hash table?

Answer:-  A hash table stores information using a special calculation on the object to be stored. A hash code is produced as a result of the calculation. The hash code is used to choose the location in which to store the object.

Question 27:- How to define an Interface in Java?

Answer :- In Java Interface defines the methods but does not implement them. Interface can include constants. A class that implements the interfaces is bound to implement all the methods defined in Interface.

Example of Interface:

public interface sampleInterface {

public void functionOne();

public long CONSTANT_ONE = 1000;

}



Question 28:- What is the difference between class and interface in java?

Answer:- The difference between interface and java are-


CLASS INTERFACE
1. The members of a class can be constant or variables 1. The members of an interface are always declared as constant, i.e. their values are final
2.The class definition can contain the code for each of its methods 2. The methods in interface are abstract in nature i.e. there is no code associated with them.
3. It can be instantiated by declaring objects 3. It cannot be used to declare objects
4. It can use various specifiers like public,private,protected

4. It can only use the public access specifier
Question 29:-What are the similarities between interfaces and classes?

Answer:-

 The similarities between interfaces and classes are-

 An interface is basically a kind of class.

 Like classes, an interface contains methods and variables.

 Both can be inherited.


Question 30:- What is a package?

Answer:- Packages are java’s ways of grouping a variety of classes and /or interfaces together. They are containers for classes that are used to compartmentalize the class name space. Packages are stored in a hierarchical manner and are explicitly imported into new class definitions.

Question 31:- What is static import? How is it useful?

Answer:- It is another language feature introduced with the J2SE 5.0 release. This feature eliminates the need of qualifying a static member with the class name. The static import declaration is similar to that of import. Also static import statement used to import static members from classes and use them without qualifying the class name.

Question 32:- What is a thread?

Answer:- A thread is similar to a program that has a single flow of control. It has a beginning a body, and an end, and executes commands sequentially.

Question 33:- Define multithreaded program.

Answer:- A program that contains multiple flows of control is known as multithreaded program.

Question 34 :- Define concurrency.

Answer:- The ability of a language to support multithreads is referred to as concurrency.

Question 35:- Define lightweight threads.

Answer:- Threads in java are subprograms of a main application program and share the same memory space are known as lightweight threads.

Question 36:- What is the difference between multithreading and multitasking?

Answer :- The difference between multithreading and multitasking are-

Multithreading Multitasking
1.It is a programming concept in which a program or a process is divided into two or more subprograms or threads that are executed at the same time in parallel. 1. It is an operating system concept in which multiple tasks are performed simultaneously.
2.It supports execution of multiple parts of a single program simultaneously. 2. It supports execution of multiple programs simultaneously.
3. The processor has to switch between different parts or threads of a program. 3. The processor has to switch between different programs or processes.
4. It is highly efficient. 4. It is less efficient in comparison to multithreading.
5. A thread is smallest unit in multithreading. 5 .A program or process is the smallest unit in a multitasking environment.
6. It helps in developing efficient programs. 6. It helps in developing efficient operating systems.
7. It is cost- effective in case of context switching. 7. It is expensive in case of context switching



Question 37:- What are the ways to create threads?

Answer:- A new thread can be created in two ways:-

1. By creating a thread class: Define a class thread that extends Thread class and override its run () method with the code required by the thread.

2. By converting a class to a thread: Define a class that implements Runnable interface. The Runnable interface has only one method, run (), that is to be defined in the method with the code to be executed by the thread.

Question 38 :-How do we start a thread?

Answer:- To Start a thread we must write the following code:-

ClassnameaThread = new classname ();

aThread.start ( );

Question 39:- What are the methods by which we may block threads?

Answer:-A thread can be temporarily suspended or blocked from entering into the runnable and subsequently running state

by using either of the following thread methods:-

1. sleep () // blocked for a specified

2. suspend () // blocked until further orders

3. wait () // blocked until certain condition occurs

Question 40 :-What is the difference between suspending and stopping a thread?

Answer:-Suspending a thread means that thread can be revived by using the resume () method. This approach is useful when we want to suspend a thread for some time due to certain reason, but do not want to kill it. Stopping a thread causes the thread to move to the dead state. The stop () method may be used when the premature death of a thread is desired.

Question 41:- How do we set priorities for thread?

Answer:- Java permits us to set the priority of a thread using the setPriority () method as follows:

Thread Name.SetPriority (int Number);

The intNumber is an integer value to which the thread’s priority is set. The Thread class defines several priority constants:

MIN_PRIORITY =1

NORM_PRIORITY =5

MAX_PRIORITY =10

The int number may assume one of these constants or any value between 1 and 10. The default setting is

NORM_PRIORITY.

Question 42:-Describe the complete life cycle of a thread.

Answer:- During the life time of a thread, there are many states it can enter. They include:

1. Newborn state: When we create a thread object, the thread is born and is said to be in newborn state.

2. Runnable state: It means that the thread is ready for execution and is waiting for the availability of the processor.

3. Running state: It means that the processor has given its time to the thread for its execution.

4. Blocked state: It is when thread is prevented from entering into the runnable state and subsequently the running state.

5. Dead state: It is the last stage of the thread. In this stage the thread is killed or it has completed executing its run () method.

Question 43:-Define time-slicing.

Answer :- The process of assigning time to threads is known as time-slicing.

Question 44 :- What is synchronization? When do we use it?

Answer :-Synchronization is a process of controlling the access of shared resources by the multiple threads such a manner that only one thread can access one resource at a time. In non-synchronized multi threaded application, it is possible for one thread to modify a shared object while another thread is in the process of using or updating the object's value. Synchronization prevents such type of data corruption. We use this when a situation such as one thread may try to read a record from a file while another is still writing to the same file arises.

= Question 45:- What is an applet?

Answer :- Applets are small java programs that are primarily used in internet computing. They can be transported over the internet from one computer to another and run using the Applet viewer or any web browser that supports java. It can perform arithmetic operations, display graphics, play sounds, accept user input, create animation, and play interactive games.

Question 46:- What is a local applet?

Answer:- An applet developed locally and stored in a local system is known as alocal applet. When a web page is trying to find as a local applet, it does not need to use internet connection and therefore the local system does not use the internet connection. It simply searches the directories in the local system and locates and loads the specified applet.

Question 47 :- What is a remote applet?

Answer:- A remote applet is that which is developed by someone else and stored on a remote computer connected to the internet. If our system is connected to the internet, we can download the remote applet onto our system via at the internet and run it.

Question 48 :- How do applets differ from application programs?

Answer:- Applets differ from application programs in the following ways-

 Applets do not use the main () method for initiating the execution of the code. Applets, when loaded, automatically call certain methods of Applet class to start and execute the applet code

 Unlike stand-alone applications, applets cannot be run independently. They are run from inside a web page using a special feature known as HTML tag

 Applets cannot read from or write to the files in the local computer

 Applets cannot communicate with other servers on the network

 Applets cannot run any program from the local computer

 Applets are restricted from using libraries from other languages such as C or C++

Question 49:- What are the various sections of Web Page?

Answer:- A web page is basically made up of text and HTML tags that can be interpreted by a web browser or an applet viewer. A web page is marked by an opening HTML tag <HTML> and a closing HTML tag<\HTML> and is divided into the following three major sections-

1. Comment section ( optional )

2. Head section ( optional )

3. Body section


Header section: - The head section is defined with a starting <HEAD> tag and a closing <\HEAD> tag. This section usually contains a title for the web page.

Body section:-This section contains the entire information about the web page and its behaviour. We can set u many options to indicate how our page must appear on the screen (like color, location, sound etc.) It contains applet tag also.

Question 50:- How many arguments can be passed to an applet using <PARAM> tags?

Answer:- We can supply user-defined parameters to an applet using <PARAM> tags. Each <PARAM> tag has a name attribute such as color, and a value attribute such as red. Inside the applet code, the applet can refer to that parameter by name to find its value.

Tuesday, 5 July 2016

Basics Interview Questions Of Java For Beginners

Question 1 :- When and who invented java?

Answer:- Java is developed by Sun Microsystems of USA in 1991.

Question 2:- Why is java known as platform-neutral language?

Answer:- It is so because java is not tied to any particular hardware or operating system. Programs developed in java can be executed anywhere on any system.

Question 3:- How is java more secured than other languages?

Answer:- It is so because java systems not only verify all memory access but also ensure that no viruses are communicated with an applet. The absence of pointers in java ensures that programs cannot gain access to memory locations without proper authorization.

Question 4:- What is the difference between java and C?

Answer:-

                               Java                                                                     C

1.It doesn’t have C unique statement keywords       1.It contains the unique keywords sizeof, and
sizeof,andtypedef.                                                        tyedef.

2.It doesn’t have data types struct and union.           2.It contains data types struct and union.

3.It doesn’t support an explicit pointer type.            3.It support an explicit pointer type.

4.It adds labelled break and continue statements    .4.It doesn’t have break and continuestatements.

5.It doesn’t have a preprocessor.                              5.It have preprocessor.


Question 5:- What is the difference between Java and C++?

Answer:-

                      Java                                                                              C++

1. It doesn’t support operator overloading.                1. It supports operator overloading.

2. It doesn’t have template classes.                            2. It has template classes.

3. It doesn’t have multiple inheritance.                      3. It has multiple inheritance.

4. It doesn’t support global variables.                        4. It supports global variables.

5. It doesn’t use pointers.                                           5. It uses pointers.

6. It uses finalize () function.                                     6. It uses destructor () function.

7. There are no header files in java.                           7. It has header files.

Question 6:- Why do we need import statement in java?

Answer:- It is because using import statements we can have access to classes that are part of other named package.

Question 7:- What is the task of main () method in a java program?

Answer:- The main method creates objects of various classes and establishes communication between them. On reaching the end of main, the program terminates and the control passes back to the operating system.

Question 8:- What are separators? What are the various separators used in java?

Answer:- Separators are symbols used to indicate where groups of code are divided and arranged. They basically define the shape and function of our code.

Java includes six types of separators:-

1. Parentheses ()

2, Braces {}

3. Brackets []

4. Semicolon ;

5. Comma ,

6. Period

Question 9:- What are command line arguments? How are they useful?

Answer:- They are the parameters that are supplied to the application program at the time of invoking it for execution. They are useful as we make like our program to act in a particular way depending on the input provided at the time of execution which is achieved by command line arguments.

Question 10 :- Java is a freeform language. Comment.

Answer:- It is a freeform language as we need not have to indent any lines to make the program work properly. Also java system does not care where on the line we begin typing.

Question 11:- List the features of java?

Answer:-Java has the following features:-

 Simple and powerful

 Secure

 Portable

 Object-oriented

 Robust

 Multithreaded

 Architecture-neutral

 Interpreted and High performance

 Distributed

 Dynamic

Question 12:- What are the newly added features in java 2?

Answer:- The following are the newly added features in java 2:-

 Swing

 Collections framework

 Various tools such as javac, java and javadoc have been enhanced.

 Just-In- Time (JIT) compiler

 Policy files

 Digital certificates

 Advanced features for working with shapes, images and text.

 Various security tools

 The user can now play audio files such as MIDI, AU, WAV and RMF files using java programs.

Sub domain:-Fundamentals of java

Question 13 :- What is type casting? Why is it required in programming?

Answer :-The process of converting one data type to another is called casting. It is required because we often encounter situations where there is a need to store a value of one type into a variable of other type. Casting is often necessary when a method returns a type different than the one we require.

Syntax:-

Type variable1 = (type) variable2;

Question 14:- What are wrapper classes? What is their use?

Answer:- Wrapper classes are provided for the primitive data types in order to use these types as objects. The wrapper classes for the primitive data types have the same name as the primitive type, but with the first letter capitalized. The advantage of using wrappers is that when an object is passed to a method, the method is able to work on the object itself. Also the user can derive his/her own classes from java’s built-in wrapper classes.

Question 15:- What is a literal? What are the difference types of literals?

Answer:- A literal represents a value of a certain type where the type describes the behavior of the value. The different types of literals are:-

 Number literals

 Character literals

 Boolean literals

 String literals

Question 16 :- What is a variable? What are the different types of variables?

Answer :- Variables are locations in the memory that can hold values. Java has three kinds of variables namely,

 Instance variable

 Local variable

 Class Variable

Local variables are used inside blocks as counters or in methods as temporary variables. Once the block or the method is executed, the variable ceases to exist. Instance variables are used to define attributes or the state of a particular object. These are used to store information needed by multiple methods in the objects.

Domain: -Classes,Objects, Methods

Question 17:- What is Collection API?

Answer:- The Collection API is a set of classes and interfaces that support operation on collections of objects. These classes and interfaces are more flexible, more powerful, and more regular than the vectors, arrays, and hash tables if effectively replaces.

Example of classes: Hash Set, Hash Map, Array List, Linked List, Tree Set and Tree Map.

Example of interfaces: Collection, Set, List and Map.

Question 18:- Is Iterator a Class or Interface? What is its use?

Answer:- Iterator is an interface which is used to step through the elements of a Collection.

Question 19 :- What is similarities/difference between an Abstract class and Interface?

Answer:- Differences are as follows:

Interfaces provide a form of multiple inheritances. A class can extend only one other class. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.

Interfaces are slow as it requires extra indirection to find corresponding method in in the actual class. Abstract classes are fast.

Similarities:

Neither Abstract classes or Interface can be instantiated.

Question 20:- How to define an Abstract class?

Answer:- A class containing abstract method is called Abstract class. An Abstract class can't be instantiated.

Example of Abstract class:

abstract class testAbstractClass {

protected String myString;

public String getMyString() {

return myString;

}

public abstract string anyAbstractFunction();

}

Question 21 :-When do we create member of a class static?

Answer:- Static variables are used when we want to have a variable common to all instances of a class. Java creates only one copy for static variable which can be used even if the class is never actually instantiated.

Question 22:- Describe the syntax of single inheritance in java?

Answer:-

Syntax:-

Class subclassname extends superclassname

{

Variables declaration;

Methods declaration;

}

Question 23 :- Describe method overloading and method overriding?

Answer :- The process in which methods have same name, but different parameter lists and different definitions is called method overloading. It is used when objects are required to perform similar tasks but using different input parameters. These are the forms of Static polymorphism. Domain:-Arrays and Strings

Question 24 :-What is string Buffer?

Answer :-It is a peer class of string that provides much of the common functionality of strings. Strings represent fixed-length character sequences. String Buffer represents varied length character sequences. String buffer may have characters and substrings inserted in the middle, or appended at the end. The complier automatically creates a String Buffer to evaluate certain expressions, in particular when the overloaded operators + and += are used with string objects.

Question 25:- What are arrays?

Answer:- An array is an object that stores a list of items. Each slot in an array holds individual elements. An array should be of a single type, comprising of integers, strings and so on.

Sunday, 3 July 2016

Variable in Java

Variable
Variable is name of reserved area allocated in memory.

Syntax of Declaration of Variable:-

Data_type var_name1,var_name2,…. ;

The Data_type is one of the java’s data type and var_name is the name of the variable. To declare more than one variable of the specified types, used the comma separated list.


For Example:-
int v=10;                      //Here v is variable  
int a,b,c;                      //  declare three variable a,b, c and its data types is integer

Types of Variable
There are three types of variables in java
      1.   local variable
      2.  instance variable
      3.   static variable

1.Local Variable
A variable that is declared inside the method, constructor or block is called local variable.
A local variable are created when the method, constructor or block is entered and the variable will be destroyed once it exists the methods, constructor or blocks. And Access modifier cannot be used for local variables. A local variable are implemented at stack level internally.

For Example:- Here age is the local variable and this is defined in showAge() method. Its scopes is limited in the method only.

Public class Test{
Public void showAge()
    {
             int age=0;
         age=age+10;
         System.out.println(“Age is:-” +age);
             }
public static void main(String []args)
{
Test t=new Test();
t.showAge();
} }

Output:- Age is:- 10


2.Instance Variable
A variable that is declared inside the class but outside the method, constructor or block is called instance variable . It is not declared as static. An instance variable are also created when an object is created with the use of the key word  “ new” and it is destroyed when object is destroyed. And an access modifier can be used for instance variable. An instance variable have default value. For number the default values is 0, for Booleans it is false and for object references it is null. Values can be assigned during the declaration or with in the constructor.

  3.Static variable

<
A variable that is declared  with static keywords  is called static variable. It cannot be local.
There would only one copy of each class variable per class, regardless of how many objects  are created from it. Static variable are rarely used other than being declare as constant. Constant are variable that’s are declare as public/ private, final and static. A conatant variable  never change from initial values.

Example to understand the types of variables

class A
{  
int v=20;                                    //instance variable  

static int s=10;                        //static variable  

void method()
{  
int l=30;                                  //local variable  
}  
}


Wednesday, 29 June 2016

Java Basics Syntax with an Example




Java Basics Syntax:-A java program is a collection of objects that communicate via invoking each other methods.


Java Program:-
public class First
{
public static void main(String []args )
{
System.out.println(“Welcome  You”);    //print Welcome You
}
}


Lets looks how to save the file, compile and run the program. Please follow some step which define below:- 
1.       Firstly, open notepad in your computer and add the code in notepad. 
2.       In second step to save the file as First.java  ( i.e First is file name which same as your class name define in program and save with .java extension) 
3.       Open your command prompt window and go to the directory where you save the file. 
4.       Type:- c:\> javac First.java and press enter. If it is not show any error in window, it compile successfully. 
5.       Type :- c:\>java First   and press enter to run your program. 
6.       It show an output, Welcome You in command prompt window.


In above Java program, Keep some following important points in your mind:- 
1.       Case Sensitivity:- Java is a case sensitive which means identifier First and first  would have different meaning in java. 
2.       Class Name:- According to conventional rule, first letter of class name should be in upper letter.
For  Example:-  class First 
3.       Method Names :- All method first rule should be in lower case and if you use several words in the name of method, then inner word first  letter should be in upper case. 
For Example:-  public void showPicture( ) 
4.       Program File Name:- Program file name should exactly the class name when you saving file with .java extension in your computer. 
For Example:- Assume that your class name is First, then your program file name should exactly First.java 
5.       Public static void main(String []args)  :- java program processing starts from main() method  which is mandatory  in your  java program.

Sunday, 26 June 2016

Top 9 Basic Questions of Java


Questions 1:- Why java program is platform independent ? Why not C and C++  program ?
Answer:- When we compile c source data, it generate the “native code” which can be understood  by current operating System, if Move to this to other operating System. It cannot be understood because ” Native code representation is change from operating system to operating system”. So, C and C++ Program is not platform independent.
In case of java source code, compilation you will get “ byte code firstly instead of native code”. When you run the byte code, it will be converted to native code and then it will be executed. But native code generated from the byte code is temporary and will not be stored in any file. If you want to run the java program in other operating system, just move the byte code and run again. When you run your byte code will be converted to native code again and will be executed. So, Java Program  is platform independent.

Question 2:- What is JDK ?
Answer:-  JDK stands for Java Development Kit which contains tools to compile and run a java program.

Question 3:- What  is JVM ?
Answer:-JVM is java run time environment which provides the necessary supports to run over java program.



Question 4:- What is Java Compile ?
Answer:- Java Compiler is single non java program which is used to verify the programmatically mistake in the source code and also convert the source code to bytecode.

Question 5:- What is Interpreter ?
Answer:- Java interpreter is single non java program which is used to convert byte code to native code and also to execute native code. 

Question 6:- What is JIT compiler ?
Answer:- JIT is the part of the Java Virtual Machine (JVM) that is used to speed up the execution time. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

Question 7:- Which of the following statements are true ?
1.     Java compiler is platform independent .                  (False)
2.     Java interpreter is platform dependent.                    (True)
3.     Java program is platform independent.                    (True)
4.     JVM is platform dependent.                                     (True)
5.     C program is platform independent.                         (False)
6.     JDK platform dependent.                                         (True)
7.     JIT compiler is platform dependent.                        (True)

Question 8:-  Why we have to set the path ?
Answer:-  We need to set the path for executable file to access anywhere with in the operating system drives and folder. Similarly with class path same.

Question 9:- Why we need to use % path %  in the path setting.
Answer:- This is to append the current path to the existing path. If we are not using %path % then existing path will be replaced with new path. Similarly class path same.

Path sets for bin folder where .exe files available.
Class path sets for lib folder where jar file available.