Sie sind auf Seite 1von 5

BC0047- Java Programming

Question 1- Describe about the primitives and derived data types in


java?
Answer 1 - Primitives data types are the data types that are built into
the language. The java compiler holds details instructions on each
operation the data types that are built into the java language. The
compiler holds detailed instruction on each legal instruction the data
types supports. There are eight primitivesdata types in java. The data
types byte, short, int, long, float, and double are numeric data types.
The first four of these can hold only whole numbers whereas at last two
(float and double) can hold decimal values like 5.05. All these data types
can hold negative values. The keyword unsigned can be restricting the
ranges of values to positive numbers. Boolean can hold only the true or
false and char can hold only a single character.
Derived data types are based on primitives data types and have more
functionally that the primitives data types. For example, string is an
abstract data types that can store alphabets, digits and other character
like /, (). You cannot perform any calculations on a variable of the string
data type even if the data stored in it has digits.
Question 2- Explain the types of the relationships in java?
Answer 2- Relationships are classified as follows:
A kind-of relationship
A is-A relationship
A part-Of-relationship
A Has-A relationship
A kind-of relationship
Taking the example of a human being and an elephant both are kind-of
mammals. As human beings and elephants are kind-of mammals, they
share the attributes and behaviours of mammals. Human being and
elephants are subset of the mammals class. The following figure depicts
the relationship between the mammals and human being classes:



A is-A relationship
Lets take an instance of the human being class peter , who is-a
human being and, therefore a mammal. The following figure depicts the
is-a relationship.







A Has-A relationship/ Part-of Relationship
A human being has a heart. This represents a has-a relationship. The
following figure depicts the relationship between a human being and a
heart.





Question 3- Explain the methods of input stream class and output
stream class?
Answer 3- Methods of the input stream class
Methods Explanation
Int read () -Reads a byte of data from the input stream
Human Being Mammals
Peter
Human
Being

Mammal
A kind-of
Is-A

A-kind-of
Is-A
Human Being
Heart
Has-A
Part-of
Int read (byte [] b) - Reads a byte of data from the input stream and
stores them in the array
Int read (byte [] b, int len) - Reads the number of bytes specified by the
third argument from the input stream and stores it in the array. The
second argument specifies the position from where the bytes have to be
read.
Int available () - returns the number of bytes available for reading from
the input stream
Void close () - close an input stream and releases all the resources
associated with it.
Method of the output stream class
Method explanation
Voids write (int n) - writes the specified byte of data to the output stream.
Void writes (byte [] b) - writes an array of bytes to the output stream.
Void write (byte [] b, int off, int len) - writes a segment of an array to the
output stream.
Void flush () - force writes whenever the data accumulates in the output
stream.
Void close () - causes the output stream to close. Any data present on it
is stored before the stream is deallocated from the memory.
Question 4- What are exceptions classes? Explain the common
exceptions in java.
Answer 4- The class at the top of the exception classes hierarchy is
called Throwable. Two classes are derived from the Throwable class
error and exception. The exception class is used for the exceptional
conditions that have to be trapped in a program. The error class defines
a condition that does not occur under normal circumstances. In other
words, the error class is used forcatastrophic failures such as virtual
machine error. These classes are available in the java.lang package.

Common exceptions
Java has several predefined exceptions. The most common exceptions
that you may counter are described below.
Arithmetic Exceptions
This exception is thrown when an exceptional arithmetic condition has
occurred. For example, a division zero generates such an exception.
NullPointer Exception
This exception is thrown when an applications attempts to use null
where an object is required. An object that has not been allocated
memory holds a null value. The situations in which an exceptions is
thrown include:
Using an object without allocating memory for it.
Calling the methods of a null object.
Accessing or modifying the attributes of a null object .
Array IndexOutOfBounds Exceptions
The exceptions ArrayIndexOutOfBounds Exceptions is thrown when an
attempt is made to access an array element beyond the index of the
array. For example, if you try to access the eleventh element of an array
that has only ten elements, the exceptions will be thrown.
Question 5- Write a program to find the sum of 1+3+5+., for 10
terms in a series.
Answer 5- Class Example
{
public static void main(String args[])

{

for (int I=1;I=<10;I++)

{int sum=0;

sum=sum+I;
system.out.println ( sum of 1 to 10 is="+sum)

}

}
Question 6- Write a program in java to check whether a given year
is leap year or not?
Answer 6- public class Leap Year {
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
Boolean isLeapYear;

// divisible by 4
isLeapYear = (year % 4 == 0);

// divisible by 4 and not 100
isLeapYear = isLeapYear && (year % 100 != 0);

// divisible by 4 and not 100 unless divisible by 400
isLeapYear = isLeapYear || (year % 400 == 0);

system.out.println(isLeapYear);
}
}

Das könnte Ihnen auch gefallen