Unit 2 Java Programmingg Question Answer
Unit 2 Java Programmingg Question Answer
UNIT2
SubjectName:Java Programming ModelAnswer SubjectCode:22412
3 Define a class student with int id and string name as data members and a 4m
method void SetData ( ). Accept and display the data for five students.
(summer 2019)
Ans: import java.io.*;
class student
{
int id;
String name;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
void SetData()
{
try
{
System.out.println("enter id and name for student");
id=Integer.parseInt(br.readLine());
name=br.readLine();
}
catch(Exception ex)
{}
}
void display()
{
System.out.println("The id is " + id + " and the name is "+ name);
}
public static void main(String are[])
{
student[] arr;
arr = new student[5];
int i;
for(i=0;i<5;i++)
{
arr[i] = new student();
}
for(i=0;i<5;i++)
{
arr[i].SetData();
}
for(i=0;i<5;i++)
{
Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page3
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
arr[i].display();
}
}
}
4 Describe the use of any methods of vector class with their syntax. 6m
(Note: Any method other than this but in vector class shall be
considered for answer). (summer 19)
Ans: boolean add(Object obj)-Appends the specified element to the
end of this Vector.
Boolean add(int index,Object obj)-Inserts the specified element at
the specified position in this Vector.
void addElement(Object obj)-Adds the specified component to
the end of this vector, increasing its size by one.
int capacity()-Returns the current capacity of this vector.
void clear()-Removes all of the elements from this vector.
Object clone()-Returns a clone of this vector.
boolean contains(Object elem)-Tests if the specified object is a
component in this vector.
void copyInto(Object[] anArray)-Copies the components of this
vector into the specified array.
Object firstElement()-Returns the first component (the item at
index 0) of this vector.
Object elementAt(int index)-Returns the component at the
specified index.
int indexOf(Object elem)-Searches for the first occurence of the
given argument, testing for equality using the equals method.
Object lastElement()-Returns the last component of the vector.
Object insertElementAt(Object obj,int index)-Inserts the specified
object as a component in this vector at the specified index.
Object remove(int index)-Removes the element at the specified
position in this vector.
void removeAllElements()-Removes all components from this
svector and sets its size to zero
8 ) List any four methods of string class and state the use of each. (Winter 4M(
19,Summer 22)
Ans: The java.lang.String class provides a lot of methods to work on
string. By the help of these methods,
We can perform operations on string such as trimming,
concatenating, converting, comparing, replacing strings etc.
1) to Lowercase (): Converts all of the characters in this String
to lower case.
Syntax: s1.toLowerCase()
Example: String s="Sachin";
System.out.println(s.toLowerCase());
Output: sachin
2)to Uppercase():Converts all of the characters in this String to
upper case
Syntax: s1.toUpperCase()
Example:
String s="Sachin";
System.out.println(s.toUpperCase());
Output: SACHIN
3) trim (): Returns a copy of the string, with leading and trailing
whitespace omitted.
Prof.Neha Pavitrakar ShreeRamchandraCollegeofEngineering Page6
SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
Syntax: s1.trim()
Example:
String s=" Sachin ";
System.out.println(s.trim());
Output:Sachin
4) replace ():Returns a new string resulting from replacing all
occurrences of old Char in this string with new Char.
Syntax: s1.replace(‘x’,’y’)
Example:
String s1="Java is a programming language. Java is a platform.";
String s2=s1.replace("Java","Kava"); //replaces all occurrences
of "Java" to "Kava"
System.out.println(s2);
Output: Kava is a programming language. Kava is a platform
9 Write a program to create a vector with five elements as (5,15, 25, 35, 45). 6m
st
Insert new element at 2nd position. Remove 1 and 4th element from
vector. (winter 19)
Ans: import java.util.*;
class VectorDemo
{
public static void main(String[] args)
{
Vector v = new Vector();
v.addElement(new Integer(5));
v.addElement(new Integer(15));
v.addElement(new Integer(25));
v.addElement(new Integer(35));
v.addElement(new Integer(45));
System.out.println("Original array elements are
");
for(int i=0;i<v.size();i++)
{
System.out.println(v.elementAt(i));
}
v.insertElementAt(new Integer(20),1); // insert
new element at 2nd position
v.removeElementAt(0);
//remove first element
v.removeElementAt(3);
//remove fourth element
System.out.println("Array elements after insert
and remove operation ");
for(int i=0;i<v.size();i++)
{
System.out.println(v.elementAt(i));
}}}
package p1;
package p1;
class A
{
private void display()
{
System.out.println("GeeksforGeeks");
}
class B
{
public static void main(String args[])
{
A obj = new A();
// Trying to access private method
// of another class
obj.display();
}
}
3. Protected Access Modifier
The protected access modifier is specified using the keyword protected.
The methods or data members declared as protected are accessible within the
same package or subclasses in different packages.
package p1;
// Class A
public class A
{
protected void display()
{
System.out.println("GeeksforGeeks");
}
}
package p2;
import p1.*; // importing all classes in package p1
// Class B is subclass of A
class B extends A
{
public static void main(String args[])
{
B obj = new B();
obj.display();
}
The add() is a Java Vector class method which is used to insert the specified
element in the given Vector. There are two different types of Java add() method
which can be differentiated depending on its parameter. These are:
Example:
import java.util.Vector;
public class VectorAddExample1 {
public static void main(String args[]) {
//Create an empty Vector with an initial capacity of 5
Vector<String> vc = new Vector<>(4);
//Add elements in the vector by using add() method
vc.add("A");
vc.add("B");
vc.add("C");
vc.add("D");
vc.add("E");
//Print all the elements of a Vector
System.out.println("--Elements of Vector are--");
for (String str : vc) {
System.out.println("Alphabet= " +str);
} } }
The clear() method of Java Vector class is used to remove all of the elements
from the vector which is in use.
Syntax:
Example:
import java.util.Vector;
public class VectorClearExample1 {
public static void main(String arg[]) {
//Create an empty Vector
Vector<String> vc = new Vector<>();
//Add elements in the vector by using add() method
vc.add("A");
vc.add("B");
vc.add("C");
//Print the size of vector
System.out.println("Size of Vector before clear() method: "+vc.size()
);
//Clear the vector
vc.clear();
System.out.println("Size of Vector after clear() method: "+vc.size());
}
}
Output:
Syntax
Output:
Element at index 1 is = 2
Element at index 3 is = 4
4 Java Vector capacity() Method
The capacity() method of Java Vector class is used to get the current capacity
of the vector which is in use.
Syntax:
Output:
e Draw structural diagram of fiber optic cable and write its functions 2
M
2 M for
diagram
and 2 M
f
2 M for
diagram
and 2 M
for
functions
f What advantages does TDM have over FDM in a circuit switched network? 4M
In TDM, each signal uses all of the bandwidth some of the time, while for
FDM, each signal uses a small portion of the bandwidth all of the time. consider
TDM uses the entire frequency range but dynamically allocates time, certain 4 points
jobs might require less or more time, which TDM can offer but FDM is for 4 M
unable to as it cannot change the width of the allocated frequency.
TDM provides much better flexibility compared to FDM.
TDM offers efficient utilization of bandwidth
Low interference of signal and minimizes cross talk
Circuit switching
Packet switching
Message switching
because:
Kevlar strands are placed inside the jacket to strengthen the cable.
Below the Kevlar strands, there is another plastic coating which acts as
acushion.
The fiber is at the center of the cable, and it consists of cladding and
glass core.
Disadvantages :
1..Radio waves travel through Lowest portion of atmosphere which can have lot of
noise and interfering signals
Time Division Multiplexing: TDM is applied primarily on digital signals but can be
applied on analog signals as well. In TDM the shared channel is divided among its
user by means of time slot. Each user can transmit data within the provided time slot
only. Digital signals are divided in frames, equivalent to time slot i.e. frame of an
optimal size which can be transmitted in given time slot. TDM works in
synchronized mode. Both ends, i.e. Multiplexer and Demultiplexer are timely
synchronized and both switch to next channel simultaneously
When channel A transmits its frame at one end, the De-multiplexer provides media
to channel A on the other end. As soon as the channel A’s time slot expires, this side
switches to channel B. On the other end, the De-multiplexer
Data Transfer: Once the circuit has been established, data and voice are
transferred from the source to the destination. The dedicated connection remains as
long as the end parties communicate.
Circuit switching was designed for voice applications. Telephone is the best suitable
example of circuit switching. Before a user can make a call, a virtual path between
callers and called is established over the network.
Packet Switching: The entire message is broken down into smaller chunks called
packets. The switching information is added in the header of each packet and
transmitted independently.
It is easier for intermediate networking devices to store small size packets and they
do not take much resource either on carrier path or in the internal memory of
switches
Packet switching enhances line efficiency as packets from multiple applications can
be multiplexed over the carrier. The internet uses packet switching technique.
Packet switching enables the user to differentiate data streams based on priorities.
Packets are stored and forwarded according to their priority to provide quality of
service.
n Draw a neat diagram of twisted pair cable and state its types. 4M
4 The delay between data units The delay between data units
in packet switching is not in circuit switching is
uniform. uniform.