Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
91 views

Java Programming Q and With Answers

The document discusses several questions and answers related to Java programming concepts. It defines the differences between object-oriented programming and procedural programming. It also explains Java program structure, access modifiers, the Java Virtual Machine, classes, objects, methods, Vectors, and the differences between local and remote applets.

Uploaded by

Hitesh Warke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views

Java Programming Q and With Answers

The document discusses several questions and answers related to Java programming concepts. It defines the differences between object-oriented programming and procedural programming. It also explains Java program structure, access modifiers, the Java Virtual Machine, classes, objects, methods, Vectors, and the differences between local and remote applets.

Uploaded by

Hitesh Warke
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

A.P.

J ABDUL KALAM UNIVERSITY , INDORE


JAVA PROGRAMMING

QUESTIONS AND ANSWERS :

Q.1.Write different between OOP’s and POP’s programming language ?


Ans :
PROCEDURAL ORIENTED PROGRAMMING OBJECT ORIENTED PROGRAMMING

In procedural programming, program is divided In object oriented programming, program is

into small parts called functions. divided into small parts called objects.

Procedural programming follows top down Object oriented programming follows bottom up

approach. approach.

There is no access specifier in procedural Object oriented programming have access

programming. specifiers like private, public, protected etc.

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

way for hiding data so it is less secure. hiding so it is more secure.

In procedural programming, overloading is not Overloading is possible in object oriented

possible. programming.

In procedural programming, function is more In object oriented programming, data is more

important than data. important than function.

Object oriented programming is based on real


Procedural programming is based on unreal world.
world.

Q.2.Explain Java program structure ? explain Access modifier used in java ?


Ans : Java Program structure :
A Java program involves the following sections:

 Documentation Section
 Package Statement
 Import Statements
 Interface Statement
 Class Definition
 Main Method Class
o Main Method Definition
Section Description

Documentation You can write a comment in this section. Comments are


Section beneficial for the programmer because they help them
understand the code. These are optional, but we suggest
you use them because they are useful to understand the
operation of the program, so you must write comments
within the program.

Package You can create a package with any name. A package is a


statement group of classes that are defined by a name. That is, if
you want to declare many classes within one element,
then you can declare it within a package. It is an
optional part of the program, i.e., if you do not want to
declare any package, then there will be no problem with
it, and you will not get any errors. Here, the package is a
keyword that tells the compiler that package has been
created.

It is declared as:

package package_name;

Import This line indicates that if you want to use a class of


statements another package, then you can do this by importing it
directly into your program.
Example:
import calc.add;

Interface Interfaces are like a class that includes a group of


statement method declarations. It's an optional section and can be
used when programmers want to implement multiple
inheritances within a program.

Class Definition A Java program may contain several class definitions.


Classes are the main and essential elements of any Java
program.

Main Method Every Java stand-alone program requires the main


Class method as the starting point of the program. This is an
essential part of a Java program. There may be many
classes in a Java program, and only one class defines the
main method. Methods contain data type declaration
and executable statements.

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.

There are four types of Java access modifiers:

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.

Q.3.What is Java Virtual Machine ? Explain.


Ans :

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

The JVM performs following operation:

o Loads code
o Verifies code
o Executes code
o Provides runtime environment

JVM provides definitions for the:

o Memory area
o Class file format
o Register set
o Garbage-collected heap
o Fatal error reporting etc.

Q.4.Explain the concept of class , object and methods ?


Ans : https://www.geeksforgeeks.org/classes-objects-java/
Class : A class is a user defined blueprint or prototype from which objects are created.  It represents the set of properties or methods
that are common to all objects of one type. In general, class declarations can include these components, in order:
1. Modifiers : A class can be public or has default access (Refer this for details).
2. Class name: The name should begin with a initial letter (capitalized by convention).
3. Superclass(if any): The name of the class’s parent (superclass), if any, preceded by the keyword extends. A class can only extend
(subclass) one parent.
4. Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A
class can implement more than one interface.
5. Body: The class body surrounded by braces, { }.

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.

Q.5.What is Vector ? How vectors are better than array ?


Ans : The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible
with collections.
 Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can
be accessed using an integer index
 They are very similar to ArrayList but Vector is synchronised and have some legacy method which collection framework does not
contain.
 It extends AbstractList and implements List interfaces.
Constructor:
 Vector(): Creates a default vector of initial capacity is 10.
 Vector(int size): Creates a vector whose initial capacity is specified by size.
 Vector(int size, int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr. It specifies
the number of elements to allocate each time that a vector is resized upward.
 Vector(Collection c): Creates a vector that contains the elements of collection c.

Q.6.What is an Applet ? Difference between local and remote applet ?


Ans : An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the
entire Java API at its disposal.
There are some important differences between an applet and a standalone Java application, including the following −
 An applet is a Java class that extends the java.applet.Applet class.
 A main() method is not invoked on an applet, and an applet class will not define main().
 Applets are designed to be embedded within an HTML page.
 When a user views an HTML page that contains an applet, the code for the applet is downloaded to the user's machine.
 A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.
 The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet's lifetime.
Local Applet :
We can create our own applet by own design and embed them into web pages. Local applets are developed in a single system and it is
stored in a local system. The web page will search the local system directories, find the local applet and execute it. Execution of local
applets doesn't required any internet connection.
1.1.1.1 Specifying a Local Applet
<applet codebase="path" code="NewApplet.class" width=120 height=120 >
</apple>
In the above listing , the codebase attribute specifies a path name on your system for the local applet, whereas the code attribute
specifies the name of the byte-code file that contains the applet's code. The path specified in the codebase attribute is relative to the
folder containing the HTML document that references the applet.

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 :

LIFE CYCLE OF A THREAD (THREAD STATES)


1. Life cycle of a thread
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated

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

A thread is in terminated or dead state when its run() method exits.

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.

2. Overriding vs. Overloading


Here are some important facts about Overriding and Overloading:

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 −
.

Q.11.What is type casting ? Different between java and c++ ?


Ans : https://javarevisited.blogspot.com/2012/12/what-is-type-casting-in-java-class-interface-example.html
Difference :

Comparison C++ Java


Index

Platform- C++ is platform-dependent. Java is platform-independent.


independent

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.

Goto C++ supports Java doesn't support the goto


the goto statement. statement.

Multiple C++ supports multiple Java doesn't support multiple


inheritance inheritance. inheritance through class. It can be
achieved by interfaces in java.

Operator C++ supports operator Java doesn't support operator


Overloading overloading. overloading.

Pointers C++ supports pointers. You can Java supports pointer internally.


write pointer program in C++. However, you can't write the pointer
program in java. It means java has
restricted pointer support in java.

Q.12.What is thread ? How do you create thread in a java with example ?


Ans : A thread, in the context of Java, is the path followed when executing a program. All Java programs have at least one thread,
known as the main thread, which is created by the Java Virtual Machine (JVM) at the program’s start, when the main() method is
invoked with the main thread.
In Java, creating a thread is accomplished by implementing an interface and extending a class. Every Java thread is created and
controlled by the java.lang.Thread class.

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/

Q.14.Write a java program to calculate the factorial of a given number ?


Ans : https://www.javatpoint.com/factorial-program-in-java

Q.15.What is Interface ? how can we use multiple interface in a class ? Explain ?


Ans : https://www.javatpoint.com/interface-in-java

https://www.guru99.com/java-interface.html

Q.16.Describe the various section of a web page ? What is runnable interface ?


Ans :

Q.17.Explain API java package ? Explain packing naming conventions ?


Ans : https://hajsoftutorial.com/java-api-packages/
https://www.w3schools.com/java/java_packages.asp

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

You might also like