You are on page 1of 11

Kuvempu University

Assignments for B.Sc.(IT) & M.Sc.(IT) Courses


Subject: JAVA Programming
Subject Code: BSIT - 42

Assignment: TA (Compulsory)

1) Explain basic features of Java.


2) Explain how Java differs from C and C++.
3) What is the difference between object oriented and procedure oriented programming ?
4) Explain the following OOPs concept.
a) object and classes.
b) Abstraction.
c) Encapsulation.
d) Inheritance.
e) Polymorphism.
f) Message passing.
5) What are the advantages of object oriented programming ?
6) Explain the structure of a Java program.
7) With an example explain Java tokens.
8) With an example explain different data types in Java.
9) With an example explain all the operators in Java.
10) With an example explain the following statements in Java.
a) simple if
b) if..else
c) nested if
d) else..if ladder
11) With an example explain the switch statement in Java.
12) Compare while and do..while statements in Java.
13) With an example explain for statement in Java.
14) With the help of an example program explain break and continue statements in Java.
Assignment: TB (Compulsory)

PART-A
1. How platform independence is achieved in JAVA ?

Java is a platform independent language.After


compiling the ".java" file ,that will be converting into
the ".class" file,which is a byte code having the
capability run on any OS.Basing on the concept byte code
java achieving the platform independent,it leads to
"Write
onece run anyware".

2. List any three features of JAVA.

The Java Virtual Machine


- Garbage Collection
- Code Security

Java Features:
● Java Virtual Machine (JVM)
- an imaginary machine that is implemented by emulating software on a real
machine
- provides the hardware platform specifications to which you compile all Java
technology code

Bytecode
- a special machine language that can be understood by the Java Virtual
Machine (JVM)
- independent of any particular computer hardware, so any computer with a
Java interpreter can execute the compiled Java program, no matter what
type of computer the program was compiled on.

● Garbage collection thread


- responsible for freeing any memory that can be freed. This happens
automatically during the lifetime of the Java program.
- programmer is freed from the burden of having to deallocate that
memory themselves

● Code security is attained in Java through the implementation


of its Java Runtime Environment (JRE).

3. What is Java C, Javadoc and Jdbc?

The javac command is used to invoke Java's compiler and compile a Java source file.
A typical invocation of the javac command would look like below:
javac [options] [source files]

Javadoc is a documentation generator from Sun Microsystems for


generating API documentation in HTML format from Java source code.

(Java DataBase Connectivity) A programming interface that lets Java applications


access a database via the SQL language. Since Java interpreters (Java Virtual
Machines) are available for all major client platforms, this allows a platform-
independent database application to be written

4. List different features of OOP.

1. Encapsulation
2. Polymorphism
3. Inheritance

5. Name different types of Java tokens.

In a Java program, all characters are grouped into symbols called tokens. Larger language features
are built from the first five categories of tokens (the sixth kind of token is recognized, but is then
discarded by the Java compiler from further processing). We must learn how to identify all six kind of
tokens that can appear in Java programs. In EBNF we write one simple rule that captures this
structure:
token <= identifier | keyword | separator | operator | literal | comment
The different types of Tokens are:

1. Identifiers: names the programmer chooses


2. Keywords: names already in the programming language
3. Separators (also known as punctuators): punctuation characters and paired-delimiters
4. Operators: symbols that operate on arguments and produce results
5. Literals (specified by their type)
o Numeric: int and double
o Logical: boolean
o Textual: char and String
o Reference: null
6. Comments
o Line

6. What is the task of the main method in Java program ?

If you run an java file(as an .class or .jar file) there's always 1 method being called: The main(String[]
args) method.
The method is only called once.
Example of an main method:
public static void main(String args[]) throws IOException {
LoggingBootstrap.bootstrap();
gui = new GUI();
}
In this case it only bootstraps the logger and uses the constuctor method of GUI(the graphical user
interface of the program)

7. Why can’t we use a keyword as a variable name ?

It is because, the keywords are defined in the java language for a specific purpose. If we start
declaring variables with the same name, the compiler would not know if you are trying to use a
variable are trying to tell it to perform an action and hence keywords cannot be used as a variable
name.

8. Why main method in Java is declared as static ?

Because, the main method is the starting point of the java program and if we need an object of that
class even before the main can be invoked, it is not possible. Hence it is declared static so that the
JVM Can acess the main method without having to instantiate that particular class

9. Write an equivalence of switch statement and if statement.

Write an equivalent Switch Statement for the if statement


if (ch=='+')
cout <<"arithmetic operator";
else if (ch=='<')
cout<<"relational operator";
else if (ch==':')
cout<< "punctuation";
else
cout <<"identification is not supported".

10. Constructor is_____________.

A constructor creates an Object of the class that it is in by initializing all the instance variables and
creating a place in memory to hold the Object. It is always used with the keyword new and then the
Class name. For instance, new String(); constructs a new String object.

PART - B
1. a) What is inheritance and how one can achieve code reusability? Explain
with an example.

Inheritance in Java refers to the feature wherein the code/functionality of one class can be used in
another class that extends this class. Example:

public class Parent {


...
..
}
public class Child extends Parent {
...
..
.
}

Here the class Child extends the class Parent and hence the methods of Parent are available for Child
to use. This way we are re-using the code in the parent class in the child class instead of re-writing
the whole thing again.

b) What is a class? How does it accomplish data hiding? Explain with example.

A class is a collection of data and methods to work with that, and other, data. An object is a specific
instance of a class. For example, "ArrayList" is a class that contains methods such as "add" and
"remove" for changing the items in the list. The instances (objects) of ArrayList can use these
methods to perform operations. It's easier to show the structure of things with a custom class.

public class MyClass


{
//Data / "Fields":
private int num;

//Constructors:
public MyClass()
{
}

public MyClass(int initialNumValue)


{
num = initialNumValue;
}

//"Getters" and "Setters":


public int getNum()
{
return num;
}

public void setNum(int newNumValue)


{
num = newNumValue;
}
}

2. a) Compare in terms of their functions and semantics the following pairs of


statements:
i) do while and while
While : In While loop the condition is tested first and then the statements are
executed if the condition turns out to be true.

Do-While : In do while the statements are executed for the first time and then the
conditions are tested, if the condition turns out to be true then the
statements are executed again.

ii) while and for


iii) nested if and switch
iv) break and continue.
b) Write a program to find sum of the following harmonic series for a given value
of n 1+ 1/2 + 1/3 + . . . .+ 1/n.

There is no correct simple general formula for sum to n terms of the series
1+1/2+1/3+1/4+ ............. + 1/n

The following expression is relatively a very good approximation.

S = ln(n + 0.5) + 0.5772 + 0.03759/(n*n + 1.171)

Deviation from the actual value fluctuates but remains relatively low.

3. a) Explain different data types in Java with example.

 byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of
-128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in
large arrays, where the memory savings actually matters. They can also be used in place
of int where their limits help to clarify your code; the fact that a variable's range is limited can serve
as a form of documentation.
 short: The short data type is a 16-bit signed two's complement integer. It has a minimum value
of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you
can use a short to save memory in large arrays, in situations where the memory savings actually
matters.
 int: The int data type is a 32-bit signed two's complement integer. It has a minimum value of
-2,147,483,648 and a maximum value of 2,147,483,647 (inclusive). For integral values, this data type
is generally the default choice unless there is a reason (like the above) to choose something else. This
data type will most likely be large enough for the numbers your program will use, but if you need a
wider range of values, use long instead.
 long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of
-9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use
this data type when you need a range of values wider than those provided by int.
 float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values
is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language
Specification. As with the recommendations for byte and short, use a float (instead of double) if
you need to save memory in large arrays of floating point numbers. This data type should never be
used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal
class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java
platform.
 double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of
values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language
Specification. For decimal values, this data type is generally the default choice. As mentioned above,
this data type should never be used for precise values, such as currency.
 boolean: The boolean data type has only two possible values: true and false. Use this data
type for simple flags that track true/false conditions. This data type represents one bit of information,
but its "size" isn't something that's precisely defined.
 char: The char data type is a single 16-bit Unicode character. It has a minimum value
of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).

b) Describe the structure of a typical Java program.

A Typical Java class would have the following.

Package statement

import statements

Class comments (Optional)

Class Declaration {

Instance variable declarations

Constructor declaration

Method declarations

4. a) What is JVM ? How does it help to achieve platform independence? If JVM is


available for windows then can we run program written on Unix platform ?
Comment.

A Java Virtual Machine (JVM) is a set of computer software programs that use a virtual machine model
for the execution of Java computer programs and scripts.

The JVM accepts data in a form commonly referred to as Java bytecode. This language conceptually
represents the instruction set of a stack-oriented, capability architecture.

javac - it is compiler which converst java source code to byte code ie.class file. This byte
code is standard for all platforms, machines or operating systems.

java - this is interpreter. this interprets the .class file based on a particular platform and
excutes them.e jvm -> java virtual machine comes into play. jvm for windows will be
different from jvm for solarais or linux . but all the jvm take the same byte code and
executes them in that platform.

Source code -> javac ->Universal byte code

Universal byte ->jvm/java -> execute them on a particular machine.


There can be any number of jvm's but they all understand the one common language called
byte code and translates them into the binary executable of a particular platform for which
they are developed. There is separate development section in sun microsystems is to
develop the jvm f or a particular platform.
for each upgrade of the jdk , the jvm for each platform is updated.

b) How are data and method organized in an object oriented program ? Illustrate
the same for car object.

There are 2 ways: "private or protected" and "public". A programmer has the choice of making the
object's data either protected or public. Protected means the data associated with that object can be
only accessed by the object's own methods. It can't be globally accessed by simply calling the
variable. Public means it can be accessed from any function.
E.g:
Class Person
{
Protected:
Char name;
Char hair-colour;
change haircolour
change name
The above class can have an object of person with his name and hair-colour. As you can see it is
protected and no one else can change them except your own functions of change hair-colour and
change name.
That's how data and methods are organized. It is a security feature, called data encapsulation.

5. a) Distinguish between the following terms:


i) Dynamic binding and message passing.
ii) Inheritance and polymorphism.
b) What are the difference between C and Java ?How Java and C++ are similar?

Java is an pure object oriented programming language, it uses the concepts of Classes, Objects,
Inheritance, Polymorphism. And the execution of a program is non-linear.
It is so called because you can't write a program with out using classes & objects.

C uses concept of structures (not object oriented).


In C we use the concept of pointers whereas there are no pointers used in JAVA
In C the programmer needs to manage memory manually. "malloc()" and "free()" are the fundamental
memory allocation library calls.

Java was created for the purpose of making a language that could be implemented on many different
types of computers (cell phone, mac, PC, linux, etc.) C on the other hand can only be run on a
computer of the same type as the one that compiled the program.

Java is also object-oriented, whereas C is not. Java allows a user to create classes that contain data
and methods. C is not capable of doing that.

7. a) Define programming. What are the types of programming? Explain.

A programming language is an artificial language designed to express computations that can


be performed by a machine, particularly a computer. Programming languages can be used to
create programs that control the behavior of a machine, to express algorithms precisely, or as a
mode of human communication.

structured programming
procedure oriented programming

object oriented programming

b) Explain bitwise operators in Java with example

Java's bitwise operators operate on individual bits of integer (int and long) values. If an operand is
shorter than an int, it is promoted to int before doing the operations.
It helps to know how integers are represented in binary. For example the decimal number 3 is
represented as 11 in binary and the decimal number 5 is represented as 101 in binary. Negative
integers are store in two's complement form. For example, -4 is 1111 1111 1111 1111 1111 1111
1111 1100.
The bitwise operators
& - and
| - or
^ - Xor
~ - not
<< - left shift
>> - right shift
>>> - right shift

Examples:

3 & 5 = 1 (1 if both bits are 1.)


3 | 5 = 7 (1 if either bits are 1)
3^5 = 6 (1 if both bits are different)
~3 = -4 (Inverts the bits)
3 << 2 = 12 (Shifts the bits of n left p positions. Zero bits are shifted into the low-order positions.)
5 >> 2 = 1 (Shifts the bits of n right p positions. If n is a 2's complement signed number, the sign bit
is shifted into the high-order positions.)
-4 >>> 28 = 15 (Shifts the bits of n right p positions. Zeros are shifted into the high-order positions.)
.
7. a) List and explain different types of loops in Java.

Java has three kinds of loops

1. For Loop
2. While Loop
3. Do - While Loop

Both For loop and While loop would iterate through a certain lines of code within the loop's limit as
long as the loop condition is satisfied.

A do while loop would execute the loop once even before checking the condition. So in a do while loop,
even if the loop condition is not satisfied the loop would execute once.

Example Declarations:

for(int i = 0; i < n; i++) {


.....
}

while (i < n) {
...
i++;
}

do {
...
i++;
} while (i < n) ;

b) What is polymorphism? Explain method overriding with example and prove


that it is a polymorphism.

polymorphism means the ability to take more than one form. This is something similar to a word
having several different meanings depending on the context.

Polymorphism in Java is implemented using Method Overloading and Method Overriding.

Overloading - More than one method in the same class with the same name but different signature

Overriding - Method with the same name in child class masking/hiding the method in the parent class

8. a) What is a parameterised constructor? Explain its usage with an example.

A parameterized constructor in java is just a constructor which take some kind of parameter (variable)
when is invoked. For example.

class MyClass {

//this is a normal constructor


public MyClass(){
//do something
}

//this is a parameterized constructor


public MyClass(int var){
//do something
}

//this is another parameterized constructor


public MyClass(String var, Integer var2){
//do something
}

}
b) Explain the methods
(i) Trim
(ii) Substring
(iii) Length.

You might also like