You are on page 1of 10

JAYAWANT SHIKSHAN PRASARAK MANDAL’s

Bhivrabai Sawant Polytechnic


(Approved by AICTE, New Delhi, Govt. of Maharashtra, Affiliated to MSBTE Mumbai)
Gat No. 720 (1&2), Wagholi, Pune-Nagar Road, Pune-412207)
Phone: 020 – 65335100 Tele fax: - + 91-020-65335100
E-mail: bspoly@rediffmail.com Website: www.jspm.edu.in

Computer Engineering Department


Academic Year 2022-23
PIP Questions on Chapter 2
Course: Java Programming Course Code: 22412 Semester: IV
Que. Bloom’s Marks Assignment Questions Relevance
No. Level to CO
1 1 4 What is the difference between array and vector.

2 1 4 Compare string class and string buffer class with any 4 points

3 2 4 What is constructor? Demonstrate the use of parameterized constructor with an example COI401.2
4 2 4 What is garbage collection and finalize method in java

5 2 4 Describe the following String class methods with examples


a) Length() b) Charato c) Compareto()

6 2 4 Explain following metods of vectors class


a) elementat b) addelement() c) removeelementat()

7 3 4 What are the access control parameters? Explain the concept with suitable example.

8 1,2 4 WAP to create a vector with seven elements as (10,30,50,20,40,10,20) remove element at 3rd
and 4th element position. Insert new element at 3rd position .display then original and
current size of vector
9 1,2 4 Define wrapper class? Give the following wrapper class method with syntax and use
A)to convert integer number into string
B) to convert string to integer number
C)to convert object number into primitive number using type value method
10 3 4 Write a program to accept a number as command line argument and print the number is
even or odd.

11 2 4 Explain the command line arguments with suitable example.


12 2 4 Describe use of this" keyword.

Sign of Course Coordinator Sign of Module Coordinator Sign of H.O.D.

JAYAWANT SHIKSHAN PRASARAK MANDAL’s

Bhivrabai Sawant Polytechnic


(Approved by AICTE, New Delhi, Govt. of Maharashtra, Affiliated to MSBTE Mumbai)
Gat No. 720 (1&2), Wagholi, Pune-Nagar Road, Pune-412207)
Phone: 020 – 65335100 Tele fax: - + 91-020-65335100
E-mail: bspoly@rediffmail.com Website: www.jspm.edu.in

Computer Engineering Department


Academic Year 2022-23
PIP Chapter 2 Solution
Course: Java Programming Course Code: 22412 Semester: IV

Que. Bloom’s Relevance


Marks Assignment Questions
No. Level to CO
1 1 2 What is the difference between array and vector.

Marking Scheme 4m

Answer Array Vector


Array can accommodate fixed number of Vectors can accommodate unknown number of
elements. elements

Arrays can hold primitive data type & Vectors can hold only objects.
objects
All elements of array should be of the The objects in a vector need not have to be
same data type. i.e. it can contain only homogeneous.
homogeneous elements.

Syntax: Datatype[] arrayname= new Syntax: Vector objectname= new Vector():


datatype[size]:

For accessing elements of an array no Vector class provides different methods for
special methods are available as it is not a
class, but derived type. accessing and managing Vector elements.

2 1 2 Compare string class and string buffer class with any 4 points

Marking Scheme 4m
String StringBuffer

String is a major class StringBuffer is a peer claws of String

Length is fixed Length is flexible

Answer Contents of object cannot be modified Contents of object can be modified

Objects can be created by asigning String Objects can be created by calling constructor
constants enclosed in double quotes of StringBuffer class using ‘new’

Ex-String s=”abc”; Ex:- StringBuffer s-new StringBuffer(“ABC”)

3 2 4 What is constructor? Demonstrate the use of parameterized constructor with an example


Marking Scheme 4M

Answer A constructor is a special member which initializes an object immediately upon creation It has the
same name as class name in which it resides and it is syntactically similar to any method • When a
constructor is not defined, java executes a default constructor which initializes all numeric
members to zero and other types to null or spaces. Once defined, constructor is automatically
called immediately after the object is created before new operator completes.

Parameterized constructor: When constructor method is defined with parameters inside it, different
value sets can be provided to different constructor with the same name.
Example:
ClassRect
{
int length, breadth;
Rect(int 1, int b) // parameterized constructor
{
length=1; breadth=b;
}
public static void main(String args[])
{
Rect r- new Rect(4.5); // constructor with parameters
Rect r1new Rect(6,7);
System.out.println("Area: "+r length*rbreadth)); // o/p Area: 20
System.out.println("Area:"+rl length r1.breadth)); //o/p Area: 42
}
}

4 2 4 What is garbage collection and finalize method in java

Marking Scheme 4M
Answer Garbage collection-

The objects are dynamically allocated in java by the use of the new operator. The objects which
are no longer in use are to be destroyed and the memory should be released for later reallocation.
Java, unlike C++, handles deallocation of memory automatically. This technique is called garbage
collection. When no references to an object exist, that object is assumed to be no longer needed,
and the memory occupied by the object can be reclaimed. Garbage collection occurs sporadically
during the execution of programs. It will not occur just because one or more objects exist that are
no longer used. Different run-time implementations will take varying approaches to garbage
collection.

finalize() method:-
Sometimes an object will need to perform some action when it is destroyed. Eg. If an object
holding some non java resources such as file handle or window character font, then before the
object is garbage collected these resources should be freed. To handle such situations java
provide a mechanism called finalization. In finalization, specific actions that are to be done when
an object is garbage collected can be defined. To add finalizer to a class define the finalize()
method. The java run-time calls this method whenever it is about to recycle an object. Inside the
finalize() method, the actions that are to be performed before an object is to be destroyed, can be
defined. Before an object is freed, the java run-time calls the finalize() method on the object.
The general form of the finalize() method is:
protected void finalize()
{
//finalization code
}
Describe the following String class methods with examples
a) Length() b) Charato c) Compareto()
5 2 4

Marking Scheme 4m
a) Length():
Syntax: int length()
It is used to return length of given string in integer
Eg. String str "INDIA";
System.out.println(str); System.out.println(str.length());

ii) charAt():
Syntax: char charAt(int position)

The charAt() will obtain a character from specified position

Eg String s INDIA"; System.out.println(s.charAt(2) ). // returns D

Answer iii) compareTo();

Syntax: int compareTo(Object o) or int compareTo(String anotherString)


There are two variants of this method. First method compares this String to another Object and
second method
compares two strings lexicographically,
Eg. String strl-"Strings are immutable":
String str2-"Strings are immutable";
String str3-Integers are not immutable";
int result-stri.compareTo(str2); System.out.println(result);
result-str2.compareTo(str3); System.out.println(result);

Explain following metods of vectors class


a) elementat b) addelement() c) removeelementat()
6 2 4

4m
Marking Scheme
Answer 1) Object elementAt(int index)
Returns the component at the specified index.

ii) void addElement(Object obj)


Adds the specified component to the end of this vector, increasing its size by one.

iii) boolean removeElement(Object obj)

Removes the first (lowest-indexed) occurrence of the argument from this vector.

What are the access control parameters? Explain the concept with suitable example.
7 2 4

Marking Scheme 4m
Answer Java provides a number of access modifiers to set access levels for classes, variables, methods
and constructors.
1) Default Access Modifier- No keyword:
A variable or method declared without any access control modifier is available to any other
class in the same package.
visible to all class in its package
e.g variables and methods can be declared without any modifiers,
String version=”1.5.1”;
Boolean processOrder()
{
return true;
}

2) Private Access Modifier - private:


Methods, Variables and Constructors that are declared private can only be accessed within the
declared class itself Private access modifier is the most restrictive access level. Class and
interfaces cannot be private. Using the private modifier is the main way that an object
encapsulates itself and hide data from the outside world.
Examples: private String format;
private void get()
{
}

3) Public Access Modifier - public:


A class, method, constructor, interface etc declared public can be accessed from any other class.
Therefore fields, methods. blocks declared inside a public class can be accessed from any class
belonging to the Java Universe. However if the public class we are trying to access is in a different
package, then the public class still need to be imported. Because of class inheritance, all public
methods and variables of a class are inherited by its subclasses. Examples: public double pi =
3.14;
public static void main(String[] arguments)
{
//-
}

4) Protected Access Modifier - protected:

Variables, methods and constructors which are declared protected in a super class can be
accessed only by the subclasses in other package or any class within the package of the
protected members' class. The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected, however methods and fields in a interface
cannot be declared protected. Protected access gives the subclass a chance to use the helper
method or variable, while preventing a nonrelated class from trying to use it.

E.g The following parent class uses protected access control, to allow its child class override
protected void show() { }

5) private protected: Variables, methods which are declared protected in a super class can be
accessed only by the subclasses in same package. It means visible to class and its subclasses.

Example: private protected void show() { }

WAP to create a vector with seven elements as (10,30,50,20,40,10,20) remove element at 3rd
and 4th element position. Insert new element at 3rd position .display then original and
8 1,2 4 current size of vector

Marking Scheme 4m
Answer
import java.util.";
public class Vector Demo
{
public static void main(String args[])
{
Vector v = new Vector();

v.addElement(new Integer(10));
v.addElement(newInteger(20));

v.addElement(newInteger(30)).

v.addElement(newInteger(10));
v.addElement(new Integer(20));
System.out println(v.size());// display original size

v.removeElementAt(2); // remove 3rd element


v.removeElementAt(3); // remove 4th element

v.insertElementAt(11,2) // new element inserted at 3rd position


System.out.println("Size of vector after insert delete operations: "+v.size());
}
}
Define wrapper class? Give the following wrapper class method with syntax and use
A)to convert integer number into string
9 1,2 4 B) to convert string to integer number
C)to convert object number into primitive number using type value method

Marking Scheme 4m
Objects like vector cannot handle primitive data types like int, float, long char and double. Wrapper
classes are used to convert primitive data types into object types. Wrapper classes are contained
in the java.lang package. The some of the Wrapper class are:
Simple type Wrapper Class

boolean Boolean

Int Integer

Char Character

float Float

i)To convert integer number into string: Method is toString()


String str= Integer.toString(i)-converts the integer i to string value.

ii) To convert string to integer number: Method is parseInt()


int i= Integer.ParseInt(str)-convert the string value of numeric string str to int i.

C)To convert object number into primitive number using type value method:
The method here is typeValue(), meaning that type can be the data type of number.

Write a program to accept a number as command line argument and print the number is
10 3 4 even or odd.

Marking Scheme 4m
public class oe
{
public static void main(String key[]) {
{
int x=Integer.parseInt(key[0]);
if (x%2==0)

{
System.out.println("Even Number");
}
else
{
System.out.println("Odd Number"); }
}
}
}

11 2 4 Explain the command line arguments with suitable example.


Marking Scheme
he java command-line argument is an argument Le passed at the time of running the
java program.
The arguments passed from the console can be received in the java program and it can
be used as an input So, it provides a convenient way to check the behaviour of the
program for the different values. You can passN(1.2.3 and so on) numbers of arguments
from the command prompt.Command Line Arguments can be used to specify
configuration information while launching your application. There is no restriction on the
number of java command line arguments. You can specify any number of arguments
Information is passed as Strings. They are captured into the String args of your main
methodSim example of command-line argument in java this example, we are receiving only one
argument and printing it. 1o run this java program, you must pass at least one argument from the
command prompt.

class CommandLineExample
{

public static void main(String args[]) {

System.out.println("Your first argument is: "+args[0]);

compile by > javac CommandLine


Example.java run by > java CommandLineExample sonoo

Describe use of this" keyword.


12 2 4

Marking Scheme
The this Keyword:
Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the
this keyword. this can be used inside any method to refer to the current object. That is, this is
always a reference to the object on which the method was invoked. You can use this anywhere a
reference to an object of the current class" type is permitted. To better understand what this refers
to, consider the following version of Box(): // A redundant use of this. Box(double w, double h,
double d) { this.width = w; this height = h; this.depth = d; }

Instance Variable Hiding:


when a local variable has the same name as an instance variable, the local variable hides the
Answer instance variable. This is why width, height, and depth were not used as the names of the
parameters to the Box() constructor inside the Box class. If they had been, then width would have
referred to the formal parameter, hiding the instance variable
width.//Use this to resolve name-space collisions.
Box(double width, double height, double depth)
{
this.width-width;
height=height;
this.depth = depth;
}

You might also like