Java Programming Q and With Answers
Java Programming Q and With Answers
into small parts called functions. divided into small parts called objects.
Procedural programming follows top down Object oriented programming follows bottom up
approach. approach.
Adding new data and function is not easy. Adding new data and function is easy.
Procedural programming does not have any proper Object oriented programming provides data
possible. programming.
Documentation Section
Package Statement
Import Statements
Interface Statement
Class Definition
Main Method Class
o Main Method Definition
Section Description
It is declared as:
package package_name;
Access Modifier : The access modifiers in Java specifies the accessibility or scope of a field, method, constructor, or class. We can
change the access level of fields, constructors, methods, and class by applying the access modifier on it.
1. Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you
do not specify any access level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do
not make the child class, it cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the
package and outside the package.
JVM (Java Virtual Machine) is an abstract machine. It is a specification that provides runtime environment in which java bytecode
can be executed.
JVMs are available for many hardware and software platforms (i.e. JVM is platform dependent).
It is:
1. A specification where working of Java Virtual Machine is specified. But implementation provider is independent to choose the
algorithm. Its implementation has been provided by Oracle and other companies.
2. An implementation Its implementation is known as JRE (Java Runtime Environment).
3. Runtime Instance Whenever you write java command on the command prompt to run the java class, an instance of JVM is
created.
What it does
o Loads code
o Verifies code
o Executes code
o Provides runtime environment
o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.
Objects : It is a basic unit of Object Oriented Programming and represents the real life entities. A typical Java program creates many
objects, which as you know, interact by invoking methods. An object consists of :
1. State : It is represented by attributes of an object. It also reflects the properties of an object.
2. Behavior : It is represented by methods of an object. It also reflects the response of an object with other objects.
3. Identity : It gives a unique name to an object and enables one object to interact with other objects.
Methods : A method is a collection of statements that perform some specific task and return the result to the caller. A method can
perform some specific task without returning anything. Methods allow us to reuse the code without retyping the code. In Java, every
method must be part of some class which is different from languages like C, C++, and Python.
Methods are time savers and help us to reuse the code without retyping the code.
Remote Applet :
An remote applet is that which is developed by someone else and stored on a remote computer connected to the internet.
1.1.1.2 Specifying a Remote Applet
<applet
codebase="http://www.myconnect.com/applets/"
code="NewApplet.class"
width=120
height=120 >
</applet>
The only difference between Local Applet and Remote Applet is the value of the codebase attribute. In the first case, codebase specifies a
local folder, and in the second case, it specifies the URL at which the applet is located.
Q.7.What is multithreadings ? Explain various states in life cycle of a thread ?
Ans Multithreading : Multithreading in java is a process of executing multiple threads simultaneously.
A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve
multitasking.
However, we use multithreading than multiprocessing because threads use a shared memory area. They don't allocate separate
memory area so saves memory, and context-switching between the threads takes less time than process.
Life Cycle :
A thread can be in one of the five states. According to sun, there is only 4 states
in thread life cycle in java new, runnable, non-runnable and terminated. There
is no running state.
But for better understanding the threads, we are explaining it in the 5 states.
The life cycle of the thread in java is controlled by JVM. The java thread states
are as follows:
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
1) New
The thread is in new state if you create an instance of Thread class but before
the invocation of start() method.
2) Runnable
The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.
3) Running
The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.
5) Terminated
Q.8.Explain the concept of method overloading and method overriding with example ?
Ans :
Overloading occurs when two or more methods in one class have the same method name but different parameters
.
Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in
the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that
is already provided its parent class.
1). The real object type in the run-time, not the reference variable's type, determines which overridden method is used at runtime. In
contrast, reference type determines which overloaded method will be used at compile time.
2). Polymorphism applies to overriding, not to overloading.
3). Overriding is a run-time concept while overloading is a compile-time concept.
3. An Example of Overriding
Here is an example of overriding. After reading the code, guess the output.
class Dog{
public void bark(){
System.out.println("woof ");
}
}
class Hound extends Dog{
public void sniff(){
System.out.println("sniff ");
}
public void bark(){
System.out.println("bowl");
}
}
public class OverridingTest{
public static void main(String [] args){
Dog dog = new Hound();
dog.bark();
}
}
Output:
bowl
In the example above, the dog variable is declared to be a Dog. During compile time, the compiler checks if the Dog class has
the bark() method. As long as the Dog class has the bark() method, the code compilers. At run-time, a Hound is created and assigned
to dog. The JVM knows that dog is referring to the object of Hound, so it calls the bark() method of Hound. This is called Dynamic
Polymorphism.
4. An Example of Overloading
class Dog{
public void bark(){
System.out.println("woof ");
}
//overloading method
public void bark(int num){
for(int i=0; i<num; i++)
System.out.println("woof ");
}
}
In this overloading example, the two bark method can be invoked by using different parameters. Compiler know they are different
because they have different method signature (method name and method parameter list).
References:
1) Defining Method. This tutorial is from Oracle, it explains the components of a method and which of them are used by compiler to
differentiate methods.
Q.10.Write and draw JDBC architecture ? Describe stram classes and JDBC driver ?
Ans : JDBC : JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity
between the Java programming language and a wide range of databases.
The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage.
Making a connection to a database.
Creating SQL or MySQL statements.
JDBC Architecture :
The JDBC API supports both two-tier and three-tier processing models for database access but in general, JDBC Architecture
consists of two layers −
JDBC API: This provides the application-to-JDBC Manager connection.
JDBC Driver API: This supports the JDBC Manager-to-Driver Connection.
The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases.
The JDBC driver manager ensures that the correct driver is used to access each data source. The driver manager is capable of
supporting multiple concurrent drivers connected to multiple heterogeneous databases.
Following is the architectural diagram, which shows the location of the driver manager with respect to the JDBC drivers and the Java
application −
.
Mainly used C++ is mainly used for system Java is mainly used for application
for programming. programming. It is widely used in
window, web-based, enterprise and
mobile applications.
Design Goal C++ was designed for systems Java was designed and created as an
and applications programming. interpreter for printing systems but
It was an extension of C later extended as a support network
programming language. computing. It was designed with a goal
of being easy to use and accessible to a
broader audience.
Java is a multi-threaded application that allows multiple thread execution at any particular time. In a single-threaded application, only
one thread is executed at a time because the application or program can handle only one task at a time.
For example, a single-threaded application may allow for the typing of words. However, this single thread requires an additional single
thread allowing for the recording of keystrokes in order to type the words. Thus, a single-threaded application records the keystrokes,
allowing the next single-threaded application (the typing of words) to follow.
However, a multi-threaded application allows for the handling of both tasks (recording and typing the keystrokes) within one
application.
When a thread is created, it is assigned a priority. The thread with higher priority is executed first, followed by lower-priority threads.
The JVM stops executing threads under either of the following conditions:
If the exit method has been invoked and authorized by the security manager
All the daemon threads of the program have died
Q.13.What is File handling ? How do you read and write to a text file in java ? Explain ?
Ans : https://www.edureka.co/blog/file-handling-in-java/
https://www.geeksforgeeks.org/different-ways-reading-text-file-java/
https://www.guru99.com/java-interface.html
https://howtodoinjava.com/java/basics/java-naming-conventions/
Q.18.Define :
1.Constructor with types : https://beginnersbook.com/2013/03/constructors-in-java/
2.Inheritance with types : https://beginnersbook.com/2013/03/inheritance-in-java/ and https://beginnersbook.com/2013/05/java-
inheritance-types/
3.java program structure : https://www.w3schools.in/java-tutorial/program-structure/
Q.19.Define :
1.Break and continue : https://www.w3schools.com/java/java_break.asp
2.Data types used in java : https://www.w3schools.com/java/java_data_types.asp
3.Symbolic constant : Symbolic constants in Java are named constants. We use the final keyword so they cannot be reassigned a new
value after being declared as final. They are symbolic because they are named. Here are a couple examples of symbolic
constant variables.
Q.20.Explain Applet life cycle with proper diagram ? Different between applet and application ?
Ans : https://www.startertutorials.com/corejava/applet-life-cycle.html
And
https://techdifferences.com/difference-between-applet-and-application.html