UNIT
I INTRODUCTION TO OOP AND JAVA FUNDAMENTALS
Object Oriented Programming -
Abstraction – objects and classes - Encapsulation- Inheritance - Polymorphism-
OOP in Java – Characteristics of Java – The Java Environment - Java
Source File -Structure – Compilation. Fundamental Programming Structures in
Java – Defining classes in Java – constructors, methods -access specifiers -
static members -Comments, Data Types, Variables, Operators, Control Flow,
Arrays , Packages - JavaDoc comments.
Important Questions
PART A QUESTIONS
1. Analyse how
Java is platform independant.
When java compiler compiles any code then it generates
the byte code not the machine native code(unlike C compiler). Now this byte
code needs an interpreter to execute on a machine. This interpreter is JVM. So
JVM reads that byte code(that is machine indepedent) amd execute it. Different
JVM is designed for different OS and byte code is able to run on different OS.
In case of C or C++(language that are not platform
indepedent) compiler generate the .exe file that is OS depedent so when we run
this .exe file on another OS it will not run because this file is OS depedent
so is not compatible with the another OS. JVM which executes the compiled code
is not platform independent. You need JVM specific to that platform i.e.
windows JVM, linux JVM, Mac OS JVM etc and there are different implementations
of JVM for the same platform. But Java API, Java Language and Java Compiler are
platform independent.
2. Differentiate
object oriented programming with function oriented progrmming.
FOP - is a programming style where we look at the
problem as a set of sub tasks or functionalities. Functions are written for
each sub task. The data and methods are independant of each other and there is
no certainity about a data at any point in time during the execution of the
program.
Not suitable for complex, large scale software
projects.
Maintanance is very difficult.
OOP - is a programming style where we look at the
problem as a collection of objects and communication among them. Data and
Functionalities are encapsulated into a single unit called object. So the data
is always under the control of pre defined methods.
Suitable for complex and large scale software
projects.
Maintanance is easier.
3. What are
static members in a java class?
Static members are variables and methods in a Java
class which are common for all the objects created for the class. They do not
have different values for each object. Eg. nameoofCollege in the class Student.
They can be used even before instantiating the class
ie., without the object
Eg.Student.nameofCollege
They are called class variables.
They are created with the keyword 'static'
Eg. Class Student{
public static String classroom = “room5”; // static variable – All objects have same
value
public String name; // instance variable – each object
has different values
}
4. Explain the
use of 'this' keyword.
'this' keyword always refers the current object.
this keyword in java can
be used inside the Method or constructor of Class. It(this) works as a
reference to the current Object whose Method or constructor is being invoked.
This keyword can be used to refer to any member of the current
object from within an instance Method or a constructor.
5. How
constructors can be overloaded in Java?
Sometimes there is a need
of initializing an object in different ways. This can be done using constructor
overloading.
// Java program to illustrate
// Constructor Overloading
class Box
{
double width, height, depth;
// constructor used when all
dimensions specified
Box(double w, double h, double
d)
{
width = w;
height = h;
depth = d;
}
// constructor used when no
dimensions specified
Box()
{
width = height = depth =
0;
}
// constructor used when cube
is created
Box(double len)
{
width = height = depth =
len;
}
}
public class Test
{
public static void main(String
args[])
{
// create boxes using the
various
// constructors
Box mybox1 = new Box(10,
20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);
}
}
6. List down
all the data types of Java and their size
7. Write a
simple program to declare, initialize and display the contents of an Array.
class Testarray1{
public
static void main(String args[]){
int a[]={33,3,4,5};//declaration,
instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the
property of array
System.out.println(a[i]);
}
}
8. Give a class
definition for the entity 'Vehicle'
9. Illustrate the significance of the class 'Object'.
Object
class is present
in java.lang package. Every class in Java is
directly or indirectly derived from the Object class. ... Therefore
the Object class methods are available to all Java
classes. Hence Object class acts as a root of inheritance
hierarchy in any Java Program.
10. Why should we use packages in Java?
A package is
a pack(group) of classes, interfaces and other packages. In java we use
packages to organize our classes and interfaces.
To import any required feature in the new programs.
Eg. Java.util
To resolve name conflicts
Advantages of using a package in Java
These are the reasons why you should use packages
in Java:
·
Reusability: While developing a project in java, we often feel that there are few
things that we are writing again and again in our code. Using packages, you can
create such things in form of classes inside a package and whenever you need to
perform that same task, just import that package and use the class.
·
Better Organization: Again, in large java projects where we have several hundreds of classes,
it is always required to group the similar types of classes in a meaningful
package name so that you can organize your project better and when you need
something you can quickly locate it and use it, which improves the efficiency.
·
Name Conflicts: We can define two classes with the same name in different packages so to
avoid name collision, we can use packages
11. Distinguish
Class and Object.
Object: is a bundle of data and its
behaviour(often known as methods).
Examples of states and behaviors
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
Example 1:
Object: House
State: Address, Color, Area
Behavior: Open door, close door
A class can be
considered as a blueprint using which you can create as many objects as you
like.
12 Explain
Encapsulation in object oriented programming.
Encapsulation is one of
the four fundamental OOP concepts. The other three are inheritance,
polymorphism, and abstraction. Encapsulation in Java is a
mechanism of wrapping the data (variables) and code acting on the data
(methods) together as a single unit. In
encapsulation, the variables of a class will be hidden from other classes, and
can be accessed only through the methods of their current class.
Eg.
13. Write a
simple java class to define the real world entity 'Car'
14. How
packages are used in Java?
15. Differentiate instance variables and static
variables.
instance variables - have different values in all instances ie., objects. Eg. variable 'name' in Student class
static variables - same value in all the objects. not specific to object but specific to the entire class.
Eg. college_name in the class Student.
16. Explain method overloading with a simple example.
- same name - different no.of arguments or type of arguments
17. What is abstraction?
- implementation of car is hidden and only the interface such as break pedal and gear lever are available to the external world.
18. Illustrate the use of 'protected' keyword.
19. Define object oriented programming.
20. Illustrate the different data types in Java.
21. Given an example Java code to illustrate the use
of keyword 'super'.
Refer lab programs.
22. What are the steps in executing a Java program?
- write source code
- compile it with javac and get the byte code file
- execute it on the JVM
23. What is Java Virtual Machine?
- it converts from bytecode to machine code
- platform dependant
- converts bytecode generated in any platform to the machine code
24. Explain about JDK
JDK - Java development kit - set of tools used to execute java programs and to perform various operations.
25. Why multiple inheritance is not available in Java?
Two base classes means two different implementations.
Subclass object will have no clarity - which one to invoke.
PART B QUESTIONS
1. Explain the features / special characteristics of
Java in detail
Refer the text book:
Explain all the following points.
• Portable
• Object-oriented
• Robust
• Multithreaded
• Architecture-neutral
• Interpreted
• High performance
• Distributed
2. Examine the use of all access modifiers in java
with proper examples.
Partial answer is given. Explain about all the access modifiers and examples.
There are 4 types of java access modifiers:
private
default
protected
public
Private :
The private access modifier is accessible only within
class.
In this example, we have created two classes A and
Simple. A class contains private data member and private method. We are
accessing these private members from outside the class, so there is compile
time error.
class A{
private int data=40;
private void msg(){System.out.println("Hello
java");}
}
public class Simple{
public static
void main(String args[]){
A obj=new
A();
System.out.println(obj.data);//Compile Time Error
obj.msg();//Compile Time Error
}
}
default access modifier
If you don't use any modifier, it is treated as
default bydefault. The default modifier is accessible only within package.
3. Develop a java program to illustrate the concept of
Inheritance. Create a base class and set of subclasses and explain how the
objects are
created by the concept of 'constructor chaining'.
4. Develop a Java program to illustrate the concept of
Interfaces in Java.
Refer:
interface Drawable
{
int RED = 1;
int GREEN =
2;
int BLUE = 3;
int BLACK =
4;
int WHITE =
5;
void draw(int
color);
}
public class CircleA implements Drawable
{
private
double x, y, radius;
CircleA(double x, double y, double radius)
{
this.x = x;
this.y =
y;
this.radius = radius;
}
@Override
public void
draw(int color)
{
System.out.println("Circle drawn at (" + x + ", " +
y +
"), with radius
" + radius + ", and color " + color);
}
double
getRadius()
{
return
radius;
}
double getX()
{
return x;
}
double getY()
{
return y;
}
}
class CircleAtest
{
public
static void main(String a[]){
CircleA
c = new CircleA(50,50,25);
Drawable
d = new CircleA(56,56,55);
c.draw(Drawable.RED);
}
}
Interface is a group of abstract methods and static
final variables.
Interface in java is used as a contract which
specifies what a class must do but it does not specify how it is to be done.
All the methods in interface are declared with empty
body and are public and all variables are public, static and final by default.
A class that implement interface must implement all the methods declared in the
interface. To implement interface use implements keyword.
If a class implements an interface and does not
provide method bodies for all functions specified in the interface, then class
must be declared abstract
Circle implements the Drawable interface by appending
implements Drawable to the Circle class name, and by overriding Drawable's
draw() method.
5. Develop a java program to calculate salary for
Employees in an organization.
Create a hierarchy of base classes and sub classes.
Apply different payment calculation formula according to the type
of employment and experience.