Sie sind auf Seite 1von 12

S.E.

CMPN (Object Oriented Programming Methodology)


Internal Assessment Test-I Solution
Date: 26/08/2015

Time: 10.30 am-11.30am

Max Marks: 20

1. Attempt any five

10

A. Write a program to demonstrate System.arraycopy().


1. The java.lang.System.arraycopy() method copies an array from the specified
source array, beginning at the specified position, to the specified position of the
destination array. A subsequence of array components is copied from the
source array referenced by src to the destination array referenced by
destination.
2. The number of components copied is equal to the length argument.
3. The components at positions srcPos through srcPos + length - 1 in the source
array are copied into positions destPos through destPos + length - 1,
respectively, of the destination array.

4. Syntax:
public static void arraycopy(Object s, int spos, Object d, int dpos, int length)
s : Source Array
d : Destination Array
This method returns no values.
5. Example Program:
public class SystemDemo {
1
public static void main(String[] args) {
int arr1[] = { 0, 1, 2, 3, 4, 5 };
int arr2[] = { 5, 10, 20, 30, 40, 50 };
System.arraycopy(arr1, 0, arr2, 0, 1); // copies an array from the
System.out.print("array2 = ");
// specified source array

System.out.print(arr2[0] + " ");


System.out.print(arr2[1] + " ");
System.out.print(arr2[2] + " ");
System.out.print(arr2[3] + " ");
System.out.print(arr2[4] + " ");
}
}
Output: array2 = 0 10 20 30 40
B. Explain how java is platform independent and high performance.
1. Unlike other programming languages such as C, C++ etc which are compiled
into platform specific machines. Java is guaranteed to be write-once, runanywhere language.
2. Java program is compiled into bytecode (Intermediate Code). This bytecode is
platform independent and can be run on any machine, this bytecode format also
provide security.

Figure 1: Java Code Execution

3. Any machine with Virtual Machine installed on it, can run Java Programs.
4. Java Runtime Environment has Just in Time (JIT) Compiler for translation of
bytecode to native machine code. This makes execution of java code with high

performance.

Write syntax for creating array of objects in Java with an example.


Array is collection of homogeneous data element, stored in a sequential order. An
array storing objects of a class is referred as array of object.
Class Student containing integer field variable marks
class Student {
int marks;
}

An array of objects is created just like an array of primitive type data items in the
following way.
Student[] studentArray = new Student[7];

The above statement creates the array which can hold references to seven Student
objects. It doesn't create the Student objects themselves. They have to be created
separately using the constructor of the Student class.
The studentArray contains seven memory spaces in which the address of seven
Student objects may be stored. If we try to access the Student objects even before
creating them, run time errors would occur. For instance, the following statement
throws a NullPointerException during runtime which indicates that studentArray[0]
isn't yet pointing to a Student object.
studentArray[0].marks = 100;

The Student objects have to be instantiated using the constructor of the Student
class and their references should be assigned to the array elements in the following
way.
studentArray[0] = new Student();

To create all objects for loop can be used


for ( int i=0; i<studentArray.length; i++) {
studentArray[i]=new Student();
}

The above for loop creates seven Student objects and assigns their reference to the
array elements. Now, a statement like the following would be valid.
studentArray[0].marks=100;

Following illustrates this concept.


public static void main(String[] args) {
Student[] studentArray = new Student[7];
studentArray[0] = new Student();
studentArray[0].marks = 99;
System.out.println(studentArray[0].marks); // prints 99
modify(studentArray[0]);
System.out.println(studentArray[0].marks); // prints 100 and not 99
// code
}
public static void modify(Student s) {
s.marks = 100;
}

What is meant by command line arguments explain with example


1. Main method in java has one array of string with the name args to accept input

from the command line.


2. Input arguments are given from the console while executing the code.

3. To pass three argument from command line to main function of Add class the
command is given as java Add 1 2 3
4. After executing the above command an string array of size 3 is created and
given arguments 1,2,3 are stored in args[0],args[1],args[2] respectively.
5.

Example:
public class Add {
public static void main(String[] args) {
int sum = 0;

for (int i = 0; i < args.length; i++) {


sum = sum + Integer.parseInt(args[i]);
}
System.out.println("The sum of the arguments passed is " + sum);
}
}
Output:
java Add 1 2 3
The sum of the arguments passed is 6
E

Explain method overloading and overriding


1. If classes have multiple methods by same name but different parameters, it is
known as Method Overloading.
2. If we have to perform only one operation, having same name of the methods
increases the readability of the program.
3. There are two ways to overload the method in java
a. By changing number of arguments
b. By changing the data type

4. class Calculation{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(int a,int b,int c){
System.out.println(a+b+c);
}
}
5. If subclass (child class) has the same method as declared in the parent class, it
is known as method overriding in java.
6. Method overriding is used to provide specific implementation of a method that
is already provided by its super class.
7. Method overriding is used for runtime polymorphism
8. Rules for method overloading
a. method must have same name as in the parent class
b. Method must have same parameter as in the parent class.

c. Must be IS-A relationship (inheritance).


class Vehicle{
void run(){
System.out.println("Vehicle is running");
}
}
class Bike extends Vehicle{
public static void main(String args[]){
Bike obj = new Bike();
obj.run();
}
}
F

Explain static data members and methods.


The static keyword in java is used for memory management mainly. We can apply
java static keyword with variables, methods, blocks and nested class. The static
keyword belongs to the class than instance of the class.
The static can be:
1. variable (also known as class variable)
2. method (also known as class method)
3. block
4. nested class

Java Static Variable


1. The static variable can be used to refer the common property of all objects
(that is not unique for each object) e.g. company name of employees,
college name of students etc.
2. The static variable gets memory only once in class area at the time of class
loading.
3. static String college ="ITS";
Java Static Method
1. A static method belongs to the class rather than object of a class.
2. A static method can be invoked without the need for creating an instance of
a class.
3. Static method can access static data member and can change the value of it.

1.

class Student{
int rollno;
String name;
static String college = "ITS";

2.
static void change(){
college = "BBDIT";
}
}

Any One
A

Write a program to demonstrate parameterized constructor and super keyword.


class A
//Superclass
{
protected int a;
private int c=10;
A(int x)
{

//Parameterized Constructor

a=x;
}
}
class B extends A
{
private int b,m;
B(int x, int y)
{
super(x);
b=y;
m=super.c;
}
void display()

//Subclass

//Parameterized Constructor
3

{
System.out.println("a="+a+" b="+b+"c="+m);
}
}
class Main
{
public static void main(String args[])
{
B objb = new B(4,5);
objb.display();

}
}

OR
B

Write a program to display area of square, rectangle and circle using abstract class.
Import java.util.Scanner;
abstract class Shape
{
public static final float PI = 3.14f;

abstract void calculateArea();


abstract void display();
}

class Rectangle extends Shape


{
float l,b,a;
Rectangle(float l, float b)
{
this.l=l;
this.b=b;
}
void calculateArea()
{

area=l*b;
}
void display()
{
System.out.println("Area of Rectangle ="+area));
}
}
class Circle extends Shape
{
float r;
Circle(float r)
{
this.r=r;

}
void calculateArea()
{
area=PI*(float)Math.pow(r,2);
}
void display()
{
System.out.println("Area of Circle ="+area);
}
}
class Square extends Shape
{
float s;
Square(float s)
{
this.s=s;
}
void calculateArea()
{
area=(float)Math.pow(s,2);
}
void display()

{
System.out.println("Area of Square ="+area));
} }
class CalculateArea
{
public static void main(String args[])
{
System.out.println(Select any one option);
System.out.println( 1. Circle \n 2. Square \n 3. Rectangle);
Scanner sc = new Scanner(System.in);
int ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.println(Enter Radius of circle);
float r=sc.nextFloat();
Circle c=newCircle(r);
c.claculateArea();
c.display();
break;
case 2:
System.out.println(Enter side of Square);
float s=sc.nextFloat();
Square sq=new Square(s);
sq.claculateArea();
sq.display();
break;
case 3:
System.out.println(Enter two sides of Rectangle);
float l=sc.nextFloat();
float b=sc.nextFloat();
Rectangle r=new Rectangle(l,b);
r.claculateArea();
r.display();
break;
default:
Syastem.out.println(Invalid Choice);

}
}
}
3

Any one
A

Draw class diagram for following scenario, a bank maintains two kinds of accounts
for its customers, saving and current account. Saving account provides withdrawal
and compound interest on deposit facility. Current account provides facility to
issue cheque book but no interest on deposit.

OR
B

Draw class diagram for following scenario, Library maintains books and
magazines. Student can issue a book or return a book. Fine is charged if a book is
returned after 8 days. The magazines are not issued, but student can read it in
library.

Das könnte Ihnen auch gefallen