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.

No comments:

Post a Comment