MCS-024 ASSIGNMENT SOLUTION (2020-21)

If you have any queries please leave a message here
Your Message
×


Q1. a) What is Object Oriented Programming (OOP)? Explain its advantages. Also describe concept of data hiding in OOP.

Answer : - Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts :


Advantage of Object Oriented Programming System (OOPs) -


Data Hiding - Data hiding is a software development technique specifically used in object-oriented programming (OOP) to hide internal object details (data members). Data hiding ensures exclusive data access to class members and protects object integrity by preventing unintended or intended changes.

Data hiding also reduces system complexity for increased robustness by limiting interdependencies between software components.




Q1. b) Explain different data types available in Java.

Answer : - In Java data types are divided into two groups :

Primitive Data Types

Data TypeSizeDescription and Example
byte1 bytesStores whole numbers from -128 to 127
Example : byte a = 10, byte b = -20;
short2 bytesStores whole numbers from -32,768 to 32,767
Example : short s = 10000, short r = -5000;
int4 bytesStores whole numbers from -2,147,483,648 to 2,147,483,647
Example : int a = 100000, int b = -200000;
long8 bytesStores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Note that you should end the value with an "L".
Example : long a = 100000L, long b = -200000L;
float4 bytesStores fractional numbers. Sufficient for storing 6 to 7 decimal digits
Note that you should end the value with an "f":
Example : float x = 234.5f;
double8 bytesStores fractional numbers. Sufficient for storing 15 decimal digits
Example : double y = 12.3;
boolean1 bitStores true or false values
Example : char letter = 'A';
char2 bytesStores a single character/letter or ASCII values
Example : Boolean b = false;


Non-Primitive Data Types

Non-primitive data types are called reference types because they refer to objects.

The main difference between primitive and non-primitive data types are :

String - The String data type is used to store a sequence of characters (text). String values must be surrounded by double quotes :
Example - String message = "Hello World";


Q1. c) Describe features of Java programming language.

Answer : - The primary objective of Java programming language creation was to make it portable, simple and secure programming language. Apart from this, there are also some excellent features which play an important role in the popularity of this language.

A list of most important features of Java language is given below.




Q2. a) What is a class? Define a class in Java and with the help of that class describe meaning of data members and member functions. Also describe use of different types of access spcifiers available in Java.

Answer : -

class Student
{
//Data Members
private int id;
private String name;

//Member Functions
public static void main(String args[])
{
id=10;
name="Debabrata";
System.out.println(id + "\t" + name);
}
}


There are 4 types of access spcifiers in Java :




Q2. b) Describe advantages of abstract method. Write a java program to create a Shape class with an abstract method Find_Area( ). Inherit Circle and Rectangle classes from Shape class. Implement Find_Area( ) method in derived classes. Make necessary assumptions.

Answer : - A method which is declared as abstract and does not have implementation is known as an abstract method.

Example : abstract void printStatus( ); // No Method Body

If a class includes abstract methods, then the class itself must be declared abstract.

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Another way, it shows only essential things to the user and hides the internal details, for example, sending SMS where you type the text and send the message. You don't know the internal processing about the message delivery.

public abstract class Shape
{
// Abstract Method
abstract void Find_Area();
}


import java.io.*;
public class Circle extends Shape
{
private double radius;
// Input the value of Radius from the User
void Input()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of Radius ...");
radius = Double.parseDouble(br.readLine());
}
// Calculate the area of a Circle based on given Input
void Find_Area()
{
double area = 3.14 * radius * radius;
System.out.println("Area of the Circle is = " + area);
}
}


import java.io.*;
public class Rectangle extends Shape
{
private double length;
private double breadth;
// Input the value of Length and Breadth from the User
void Input()throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the value of Length ...");
length = Double.parseDouble(br.readLine());
System.out.println("Enter the value of Breadth ...");
breadth = Double.parseDouble(br.readLine());
}
// Calculate the area of a Rectangle based on given Input
void Find_Area()
{
double area = length * breadth;
System.out.println("Area of the Rectangle is = " + area);
}
}


import java.io.*;
public class CallMethod
{
public static void main(String args[])throws IOException
{
Circle ob1 = new Circle();
Rectangle ob2 = new Rectangle();
// Calculate the Area of a Circle
ob1.Input();
ob1.Find_Area();
// Calculate the Area of a Rectangle
ob2.Input();
ob2.Find_Area();
}
}


Output :
Enter the value of Radius ...
10.5
Area of the Circle is = 346.185
Enter the value of Length ...
20
Enter the value of Breadth ...
10
Area of the Rectangle is = 200.




Q3. a) Explain use(s) of following keywords of Java with the help of program/example.

Answer : -

final Keyword

Example :
class Speed
{
//final variable
final int speedlimit=90;
void run()
{
speedlimit=400;
}
public static void main(String args[])
{
Speed obj=new Speed();
obj.run();
}
}

Output : Compile Time Error


finally Keyword -

Example :

class TestFinallyBlock
{
public static void main(String args[])
{
try
{
int n=5/0;
System.out.println("Answer = " + n);
}
catch(Exception e) { System.out.println(e); }
finally
{
System.out.println("finally block is always executed");
}
System.out.println("rest of the code...");
}
}

Output :
java.lang.ArithmeticException: / by zero
finally block is always executed
rest of the code...


super Keyword - We can use super keyword to access the data member and methods of parent class. It is used if parent class and child class have same methods or variables.

Example :
//Parent Class
class Base
{
int fact;
void Factorial(int n)
{
fact=1;
for(int i=1;i<=n;i++)
fact=fact*i;
}
}

//Child Class
class Derived extends Base
{
void Factorial(int n)
{
//Call the Overriding Method of Parent Class
super.Factorial(n);
System.out.println("Factorial = "+super.fact);
}
}




Q3. b) What is interface? How interface is different from abstract class? Write a program in Java to explain how interfaces are implemented.

Answer : - Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body). Interfaces specify what a class must do and not how.

A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body). Method that are declared without any body within an abstract class are called abstract method. The method body will be defined by its subclass. Abstract method can never be final and static. Any class that extends an abstract class must implement all the abstract methods declared by the super class.

Abstract ClassInterface
Abstract class can have abstract and non-abstract methods.Interface can have only abstract methods.
Abstract class doesn't support multiple inheritance.Interface supports multiple inheritance.
Abstract class can have final, non-final, static and non-static variables.Interface has only static and final variables.
Abstract class can provide the implementation of interface.Interface can't provide the implementation of abstract class.
An abstract class can extend another Java class and implement multiple Java interfaces.An interface can extend another Java interface only.
A Java abstract class can have class members like private, protected, etc.Members of a Java interface are public by default.


Java Interface Example

interface Bank
{
double RateOfInterest();
double ATMcharges();
}

class SBI implements Bank
{
public double RateOfInterest() { return 6.2; }
public double ATMcharges() { return 100.0; }
}

class RBL implements Bank
{
public double RateOfInterest() { return 6.8; }
}

class InterestCheck
{
public static void main(String args[])
{
Bank ob1=new SBI();
System.out.println("SBI Interest Rate = " + ob1.RateOfInterest());
System.out.println("SBI ATM Charges = " + ob1.ATMcharges());
Bank ob2=new RBL();
System.out.println("RBL Interest Rate = " + ob2.RateOfInterest());
}
}




Q4. a) What is polymorphism? Explain use of polymorphism in java programming.

Answer : - Polymorphism in Java is a concept by which we can perform a single action in different ways. There are two types of polymorphism in Java : compile-time polymorphism and runtime polymorphism.

Run time Polymorphism also known as method overriding. Method overriding means having two or more methods with the same name, same signature but with different implementation.

Compile time Polymorphism also known as method overloading. Method overloading means having two or more methods with the same name but with different signatures.

Example of Polymorphism

class Bank
{
double getRateOfInterest() { return 0.0; }
}

class SBI extends Bank
{
double getRateOfInterest() { return 6.2; }
}

class ICICI extends Bank
{
double getRateOfInterest() { return 6.8; }
}

class AXIS extends Bank
{
double getRateOfInterest() { return 6.5; }
}

class TestPolymorphism
{
public static void main(String args[])
{
Bank b;
b=new SBI();
System.out.println("SBI Interest Rate = " + b.getRateOfInterest());
b=new ICICI();
System.out.println("ICICI Interest Rate = " + b.getRateOfInterest());
b=new AXIS();
System.out.println("AXIS Interest Rate = " + b.getRateOfInterest());
}
}




Q4. b) What is exception? What are different types of exceptions? Explain need of exceptions handling with the help of a program.

Answer : - Exceptions are events that occur during the execution of programs that distrup the normal flow of instructions (example - divide by zero, array access out of bound, etc.).

In Java, an exception is an object that wraps an error event that occurred within a method and contains :


Different Types of Java Exceptions


There are 5 keywords which are used in handling exceptions in Java.

KeywordDescription
tryThe "try" keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can't use try block alone.
catchThe "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.
finallyThe "finally" block is used to execute the important code of the program. It is executed whether an exception is handled or not.
throwThe "throw" keyword is used to throw an exception.
throwsThe "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.


Java Exception Handling Example

public class JavaExceptionExample
{
public static void main(String args[])
{
try
{
// Code that may raise exception
int data=100/0;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("Rest of the code...");
}
}

Output :
java.lang.ArithmeticException:/ by zero
Rest of the code...




Q5. a) What is multithreading? Explain advantages of multithreading. Describe use of setPriority and getPriority methods for Java multithreading. Also describe how threads are synchronized in Java with the help of a program.

Answer : - Multithreading in java is a process of executing multiple threads simultaneously. Thread is basically a lightweight sub-process, a smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

We use multithreading than multiprocessing because threads share a common memory area. They do not allocate separate memory area so saves memory, and context-switching between the threads takes less time than process. Java Multithreading is mostly used in games, animation etc.

Advantages of Java Multithreading


setPriority( ) - The setPriority( ) method of thread class is used to change the thread's priority. Every thread has a priority which is represented by the integer number between 1 to 10.

Syntax : public final void setPriority(int a)

getPriority( ) - The getPriority( ) method of thread class is used to check the priority of the thread. When we create a thread, it has some priority assigned to it. Priority of thread can either be assigned by the JVM or by the programmer explicitly while creating the thread.

Syntax : public final int getPriority( )

Thread class provides 3 constant properties :

  1. public static int MIN_PRIORITY
  2. public static int NORM_PRIORITY
  3. public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.


Example of priority of a Thread

class PriorityExample extends Thread
{
public void run()
{
System.out.println("running thread priority : "+Thread.currentThread().getPriority());
}
public static void main(String args[])
{
PriorityExample p1=new PriorityExample();
p1.setPriority(Thread.MAX_PRIORITY);
p1.start();
}
}

Output :

running thread name : Thread-0
running thread priority : 10


Java - Thread Synchronization

When we start two or more threads within a program, there may be a situation when multiple threads try to access the same resource and finally they can produce unforeseen result due to concurrency issues. For example, if multiple threads try to write within a same file then they may corrupt the data because one of the threads can override data or while one thread is opening the same file at the same time another thread might be closing the same file.

So there is a need to synchronize the action of multiple threads and make sure that only one thread can access the resource at a given point in time. This is implemented using a concept called monitors. Each object in Java is associated with a monitor, which a thread can lock or unlock. Only one thread at a time may hold a lock on a monitor.

Java programming language provides a very handy way of creating threads and synchronizing their task by using synchronized blocks. You keep shared resources within this block.


Without Synchronization

public class Table
{
//Method not Synchronized
void printTable(int n)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e) { System.out.println(e); }
}
}
}


class Thread1 extends Thread
{
Table ob1;
Thread1(Table ob1)
{
this.ob1 = ob1;
}
public void run()
{
ob1.printTable(5);
}
}


class Thread2 extends Thread
{
Table ob1;
Thread2(Table ob1)
{
this.ob1 = ob1;
}
public void run()
{
ob1.printTable(100);
}
}


class TestSynchronization
{
public static void main(String args[])
{
// Only one Object
Table obj = new Table();
Thread1 t1=new Thread1(obj);
Thread2 t2=new Thread2(obj);
t1.start();
t2.start();
}
}

Output :
5
100
200
10
300
15
20
400
500
25


With Synchronization

public class Table
{
//Synchronized Method
synchronized void printTable(int n)
{
for(int i=1;i<=5;i++)
{
System.out.println(n*i);
try
{
Thread.sleep(500);
}
catch(Exception e) { System.out.println(e); }
}
}
}


class Thread1 extends Thread
{
Table ob1;
Thread1(Table ob1)
{
this.ob1 = ob1;
}
public void run()
{
ob1.printTable(5);
}
}


class Thread2 extends Thread
{
Table ob1;
Thread2(Table ob1)
{
this.ob1 = ob1;
}
public void run()
{
ob1.printTable(100);
}
}


class TestSynchronization
{
public static void main(String args[])
{
// Only one Object
Table obj = new Table();
Thread1 t1=new Thread1(obj);
Thread2 t2=new Thread2(obj);
t1.start();
t2.start();
}
}

Output :
100
200
300
400
500
5
10
15
20
25




Q5. b) Write program to create an Applet which draw a circle inside a triangle. Keep color of circle blue and triangle yellow.

Answer : -

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class DrawingExample extends Applet
{
public void paint(Graphics g)
{
// x coordinates of vertices
int x[] = { 200, 50, 350 };
// y coordinates of vertices
int y[] = { 50, 300, 300 };
// Number of vertices
int vertices = 3;
// Create a Polygon with given x, y coordinates
Polygon p = new Polygon(x, y, vertices);
// Draw a Triangle
g.setColor(Color.yellow);
g.fillPolygon(p);
// Draw a Circle
g.setColor(Color.blue);
g.fillOval(120, 135, 160, 160);
}
}




Q6. a) What is layout manager? Describe use of flow layout and grid layout with the help of program code.

Answer : - Coming Soon




Q6. b) What is event driven program? Describe different components of event in Java

Answer : - Coming Soon




Q7. a) Explain use of stream classes in Java. Write a java program to read the contents of a given file and display it.

Answer : -

Java Stream Class


Read the Contents of a given File

import java.io.*;
class CopyFile
{
public static void main(String args[])throws IOException
{
FileInputStream Fread =new FileInputStream("File1.txt");
int c;
while((c=Fread.read()) != -1)
System.out.print((char)c);
Fread.close();
}
}




Q7. b) Explain use of Socket and DatagramPacket classes.

Answer : - Coming Soon




Q8. a) What is Servlet ? Explain Servlet life cycle. Also explain use of GET and POST methods of Servlet.

Answer : - Servlet can be described in many ways, depending on the context.


Servlet Life Cycle

  1. Loading Servlet Class - When the Web Server (e.g. Apache Tomcat) starts up, the Web Container deploy and loads all the servlet classes.

  2. Creating instance of Servlet - Once all the Servlet classes loaded, the Web Container creates instances of each servlet class. The servlet instance is created only once in the Servlet Life Cycle.

  3. Invoke init( ) method - Once all the servlet classes are instantiated, the init() method is invoked for each instantiated servlet. This method initializes the servlet. There are certain init parameters that you can specify in the deployment descriptor (web.xml) file. For example, if a servlet has value >=0 then its init() method is immediately invoked during web container startup.

    Syntax of the init( ) method is given below :
    public void init(ServletConfig config) throws ServletException

    The init( ) method is called only once during the life cycle of servlet.

  4. Invoke service( ) method - Each time the web server receives a request for servlet, it spawns a new thread that calls service() method. If the servlet is GenericServlet then the request is served by the service() method itself, if the servlet is HttpServlet then service() method receives the request and dispatches it to the correct handler method based on the type of request.

    The service() method is called by the container and service method invokes doGet, doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.

    Syntax of the service( ) method is given below :
    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException

  5. Invoke destroy( ) method - When Web Container shuts down(this usually happens when we stop the web server), it unloads all the Servlets and calls destroy() method for each initialized Servlets.

    Syntax of the destroy( ) method is given below :
    public void destroy( )


GET Method

The GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? (question mark) symbol as follows −

http://www.questionsolves.com/Test?firstname=AMIT&lastname=DAS

The GET method is the default method to pass information from browser to web server and it produces a long string that appears in your browser's Location box. Never use the GET method if you have password or other sensitive information to pass to the server. The GET method has size limitation : only 1024 characters can be used in a request string.

This information is passed using QUERY_STRING header and will be accessible through QUERY_STRING environment variable and Servlet handles this type of requests using doGet( ) method.


POST Method

A generally more reliable method of passing information to a backend program is the POST method. This packages the information in exactly the same way as GET method, but instead of sending it as a text string after a ? (question mark) in the URL it sends it as a separate message. This message comes to the backend program in the form of the standard input which you can parse and use for your processing. Servlet handles this type of requests using doPost( ) method.

Servlets handles form data parsing automatically using the following methods depending on the situation −




Q8. b) What is JDBC? Explain how connection is established in JDBC.

Answer : - JDBC stands for Java Database Connectivity. JDBC is a Java API to connect and execute the query with the database. It is a part of JavaSE (Java Standard Edition). JDBC API uses JDBC drivers to connect with the database. There are four types of JDBC drivers :

JDBC-ODBC Bridge Driver

The JDBC Type 1 driver converts JDBC method calls into ODBC function calls. The bridge is usually used when there is no pure-Java driver available for a particular database. The driver is implemented in the sun.jdbc.odbc.JdbcOdbcDriver class and comes with the Java 2 SDK, Standard Edition.


Advantages

Disadvantages


Native-API Driver

The Native API driver uses the client-side libraries of the database. The driver converts JDBC method calls into native calls of the database API. It is not written entirely in java.


Advantages

Disadvantages


Net-Protocol Driver

The Net-Protocol driver uses middleware (application server) that converts JDBC calls directly or indirectly into the vendor-specific database protocol. It is fully written in java.


Advantages

Disadvantages


Native-Protocol Driver

The Native-protocol driver converts JDBC calls directly into the vendor-specific database protocol. It is fully written in Java language and installed inside the Java Virtual Machine of the client.


Advantages

Disadvantages




Q8. c) What is RMI? Explain RMI architecture.

Answer : - The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in java. The RMI allows an object to invoke methods on an object running in another JVM.

The RMI provides remote communication between the applications using two objects stub and skeleton. RMI uses stub and skeleton object for communication with the remote object. A remote object is an object whose method can be invoked from another JVM.


Understanding requirements for the Distributed Applications

If any application performs these tasks, it can be distributed application.

The RMI application have all these features, so it is called the distributed application.



ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us