0% found this document useful (0 votes)
3 views8 pages

JPR Solution

The document provides definitions and explanations of key programming concepts in Java, including exceptions, type casting, the super keyword, access specifiers, JVM, threads, packages, and the final keyword. It also includes code examples for creating threads, handling exceptions, and implementing user-defined packages. Additionally, it outlines the differences between final and abstract methods, and provides a program to demonstrate thread management and object-oriented principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

JPR Solution

The document provides definitions and explanations of key programming concepts in Java, including exceptions, type casting, the super keyword, access specifiers, JVM, threads, packages, and the final keyword. It also includes code examples for creating threads, handling exceptions, and implementing user-defined packages. Additionally, it outlines the differences between final and abstract methods, and provides a program to demonstrate thread management and object-oriented principles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

2 marks:-

1)Define exceptions. List its type.(2M)

An exception is an abnormal event or error that occurs during the execution of a program and
disrupts the normal flow of instructions.

Types of Exceptions

1. Checked Exceptions
o Checked at compile time
o Must be handled using try-catch or declared with throws
o Example: IOException, SQLException
2. Unchecked Exceptions
o Occur at runtime
o Not checked by the compiler
o Example: ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException
3. Errors
o Serious problems that applications usually cannot handle
o Occur due to system failure
o Example: OutOfMemoryError, StackOverflowError

2) Define type casting. List its types

Type casting is the process of converting a variable from one data type to another.

Types of Type Casting

1. Implicit Type Casting (Automatic Casting)


o Done automatically by the compiler
o Converts a smaller data type into a larger data type
o Example:
int → float → double
2. Explicit Type Casting (Manual Casting)
o Done manually by the programmer
o Converts a larger data type into a smaller data type
o May cause data loss
o Example:
double → float → int

3) Explain the use of super keyword(

super keyword is used in object-oriented programming (especially in Java) to refer to the


immediate parent (superclass) object.

Uses of super keyword


1. To access parent class variables
When a child class and parent class have variables with the same name.
2. [Link];
3. To call parent class methods
Used when a child class overrides a method and wants to use the parent version.

[Link]();

4)Enlist different access specifier in Java

The different access specifiers in Java are:

1. private
o Accessible only within the same class
2. default (no keyword used)
o Accessible within the same package only
3. protected
o Accessible within the same package and in subclasses of other packages
4. public
o Accessible from anywhere

5) Explain JVM.

JVM (Java Virtual Machine) is a virtual machine that enables a computer to run Java
programs.

Explanation of JVM

 JVM acts as an intermediate layer between Java programs and the operating system.
 Java source code is first compiled by the Java Compiler (javac) into bytecode.
 This bytecode is then executed by the JVM, making Java platform independent.

Advantages of JVM

 Platform independent execution


 Automatic memory management
 Security and robustness

6) Define Thread. Mention two ways to create a Thread.

A thread is a lightweight sub-process that represents the smallest unit of execution within a
program. Multiple threads can run concurrently to improve performance and resource utilization.

Two ways to create a Thread in Java

1. By extending the Thread class


o Override the run() method
o Start the thread using the start() method
2. By implementing the Runnable interface
o Implement the run() method
o Pass the Runnable object to a Thread constructor

4 marks:-

1)Write a program to accept age from user and throw an exception if age is less than 18.
import [Link];

public class AgeCheck {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);

[Link]("Enter your age: ");


int age = [Link]();

if (age < 18) {


throw new ArithmeticException("Age must be 18 or above");
} else {
[Link]("You are eligible.");
}

[Link]();
}
}

2)Explain final with respect to inheritance. Describe how final method is different from abstract method

final with respect to Inheritance

In Java, the final keyword restricts inheritance and modification.

In inheritance, final is used to restrict what child classes can do.

1. Final class
o A final class cannot be inherited.
o Purpose: prevent modification of behavior.
o Example:
o final class Vehicle { }
o // class Car extends Vehicle {} ❌ Not allowed

2. Final method
o A final method cannot be overridden in a subclass.
o Inheritance is allowed, but behavior is locked.
o Example:
o class Vehicle {
o final void speed() { }
o }
o
o class Car extends Vehicle {
o // void speed() { } ❌ Not allowed
o }

3. Final variables (quick note)


o A final variable’s value cannot be changed.
o With inheritance, subclasses inherit the value but cannot modify it.
4. Constructors
o Constructors cannot be final (because they are never inherited anyway).

Difference between Final Method and Abstract Method

Final Method Abstract Method

Has a method body Does not have a method body

Cannot be overridden Must be overridden in subclass

Used to prevent modification Used to enforce implementation

Can be inherited Cannot be used directly

Cannot be abstract Cannot be final

3) Define package. Explain how to create and access user defined package in java.

A package in Java is a namespace that groups related classes, interfaces, and sub-packages
together.
It helps in organizing code, avoiding name conflicts, improving reusability, and providing
access protection.

Creating a user-defined package

Step 1: Create the package using package keyword


Write this as the first statement in your source file.

package mypack;

public class Demo {


public void show() {
[Link]("User defined package");
}
}

Step 2: Compile the program


javac -d . [Link]

This creates a folder named mypack containing [Link].

Accessing a user-defined package

Method 1: Using import keyword (recommended)


import [Link];

class Test {
public static void main(String[] args) {
Demo d = new Demo();
[Link]();
}
}

Advantages of Packages

 Better code organization


 Reusability
 Security through access control
 Easy maintenance

4)Create vector and add 2 Integer, 2 Double and 1 Float objects.


ii. Add string object to 3rd position.
iii. Remove element specified by user.
iv. Display all elements in vector.
v. Remove all elements from vector.
vi. Display capacity of vector.

import [Link];
import [Link];

class VectorDemo {
public static void main(String[] args) {

// i. Create vector and add 2 Integer, 2 Double and 1 Float objects


Vector<Object> v = new Vector<>();
[Link](10); // Integer
[Link](20); // Integer
[Link](10.5); // Double
[Link](20.5); // Double
[Link](5.5f); // Float

// ii. Add string object to 3rd position


[Link](2, "Java");

// iii. Remove element specified by user


Scanner sc = new Scanner([Link]);
[Link]("Enter element to remove: ");
Object element = [Link]();

[Link](element);

// iv. Display all elements in vector


[Link]("Vector elements:");
for (Object obj : v) {
[Link](obj);
}

// vi. Display capacity of vector


[Link]("Capacity of vector: " + [Link]());

// v. Remove all elements from vector


[Link]();

// Display vector after removing all elements


[Link]("Vector after removing all elements: " + v);
}
}

5)Write a program to create two threads. Thread A should print even numbers
from 1 to 50 and thread B should print odd numbers from 1 to 50. Each thread
should relinquish control to the other frequently.
class EvenThread extends Thread {
public void run() {
for (int i = 2; i <= 50; i += 2) {
[Link]("Even Thread: " + i);
[Link](); // relinquish control
}
}
}

class OddThread extends Thread {


public void run() {
for (int i = 1; i <= 50; i += 2) {
[Link]("Odd Thread: " + i);
[Link](); // relinquish control
}
}
}

class ThreadDemo {
public static void main(String[] args) {

EvenThread t1 = new EvenThread(); // Thread A


OddThread t2 = new OddThread(); // Thread B
[Link]();
[Link]();
}
}
6) Write a program to implement the following Fig. No.

interface exam {
void per_cal();
}

// Student class
class Student {
String name;
int roll_no;
int m1, m2;

Student(String name, int roll_no, int m1, int m2) {


[Link] = name;
this.roll_no = roll_no;
this.m1 = m1;
this.m2 = m2;
}
}

// Result class
class Result extends Student implements exam {

float percentage;

Result(String name, int roll_no, int m1, int m2) {


super(name, roll_no, m1, m2);
}

// Implement interface method


public void per_cal() {
percentage = (m1 + m2) / 2.0f;
}
// Display result
void display() {
[Link]("Name : " + name);
[Link]("Roll No : " + roll_no);
[Link]("Marks 1 : " + m1);
[Link]("Marks 2 : " + m2);
[Link]("Percentage : " + percentage);
}

public static void main(String[] args) {


Result r = new Result("Rahul", 101, 78, 82);
r.per_cal();
[Link]();
}
}

You might also like