TYBSc Computer Science - OOP Using Java-I (2019
Pattern)
Q1 (a)
Features of Java:
- Simple
- Object-Oriented
- Platform Independent
- Secure
- Robust
- Multithreaded
- Portable
- High Performance
Q1 (b)
JDK: Java Development Kit (compiler, JRE, tools)
JRE: Java Runtime Environment (JVM + libraries)
JVM: Executes bytecode, ensures platform independence
Q1 (c)
Java Program - Prime Check:
class PrimeCheck {
public static void main(String[] args) {
int num = 29;
boolean isPrime = true;
if (num <= 1) isPrime = false;
else {
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
}
if (isPrime) System.out.println(num + " is Prime");
else System.out.println(num + " is Not Prime");
}
}
Q2 (b)
Method Overloading Example:
class OverloadExample {
void show(int a) { System.out.println("Integer: " + a); }
void show(double b) { System.out.println("Double: " + b); }
void show(String s) { System.out.println("String: " + s); }
public static void main(String[] args) {
OverloadExample obj = new OverloadExample();
obj.show(10);
obj.show(10.5);
obj.show("Hello");
}
}
Q3 (b)
Exception Handling Example:
class ExceptionDemo {
public static void main(String[] args) {
int arr[] = {1, 2, 3};
try {
System.out.println(arr[5]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Exception caught: " + e);
}
}
}
Q4 (c)
Multiple Inheritance using Interfaces:
interface A { void methodA(); }
interface B { void methodB(); }
class C implements A, B {
public void methodA() { System.out.println("Method A"); }
public void methodB() { System.out.println("Method B"); }
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}
Q5 (b)
Thread Example:
class MyThread implements Runnable {
public void run() {
for(int i=1; i<=5; i++) {
System.out.println(Thread.currentThread().getName() + " : " + i);
}
}
}
class ThreadDemo {
public static void main(String[] args) {
Thread t1 = new Thread(new MyThread(), "Thread-1");
Thread t2 = new Thread(new MyThread(), "Thread-2");
t1.start();
t2.start();
}
}