Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Java OOPS Concepts Summary (1)

Uploaded by

Shashank shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Java OOPS Concepts Summary (1)

Uploaded by

Shashank shekhar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Java OOPS Concepts Summary

OOPS with JAVA


BCS306A
Question Bank
1. Explain different type of inheritance in JAVA along with sample programs.
2. What is inheritance? What is the advantage of inheritance? Develop a JAVA
program to create a class named shape. Create three sub classes namely:
circle, triangle and square, each class has two member functions named
draw () and erase (). Demonstrate polymorphism concepts by developing
suitable methods, defining member data and main program.
3. With suitable code segments illustrate the various uses of ‘final’ keyword?
4. What is abstract class? Explain with example
5. What is method overriding? Explain how method overriding supports
polymorphism with example.
6. Demonstrate dynamic dispatch using abstract class in JAVA.
7. What is an interface? Illustrate with an example how multiple Inheritances is
implemented in Java.
8. Define package. Write a Java program to create and import the package to
make use of class declared in package
9. Write a JAVA program to demonstrate the producer consumer problem using
synchronized threads.
10. Define a thread. Explain the various ways the threads can be created in Java.
11. With a sample program to describe the exception handling mechanism in
java?
12. What is enumeration? Explain values() of valueOf() methods
13. What is autoboxing and autounboxing in arithmetic expression? Explain
with example

Here's a breakdown of your OOPS with JAVA (BCS306A) Question Bank:

1. Explain different types of inheritance in JAVA along with sample programs.

Types of Inheritance:
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance (not directly supported in Java)
Multiple Inheritance (achieved using interfaces in Java)

Example:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/10
java

// Single Inheritance
class Parent {
void display() {
System.out.println("This is the Parent class");
}
}
class Child extends Parent {
void show() {
System.out.println("This is the Child class");
}
}
public class SingleInheritance {
public static void main(String[] args) {
Child c = new Child();
c.display();
c.show();
}
}

2. What is inheritance? What is the advantage of inheritance?

Definition: Inheritance allows one class (child/subclass) to acquire the properties and methods of
another class (parent/superclass).
Advantages:
Reusability
Extensibility
Code maintainability
Method overriding and polymorphism

Polymorphism Example:

java

class Shape {
void draw() {
System.out.println("Drawing Shape");
}
void erase() {
System.out.println("Erasing Shape");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing Circle");
}
void erase() {
System.out.println("Erasing Circle");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Drawing Triangle");
}
void erase() {
System.out.println("Erasing Triangle");
}
}
class Square extends Shape {
void draw() {
System.out.println("Drawing Square");
}

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/10
void erase() {
System.out.println("Erasing Square");
}
}
public class PolymorphismExample {
public static void main(String[] args) {
Shape s;
s = new Circle();
s.draw();
s.erase();

s = new Triangle();
s.draw();
s.erase();

s = new Square();
s.draw();
s.erase();
}
}

3. With suitable code segments illustrate the various uses of the `final` keyword.

Final class: Prevents inheritance.


Final method: Prevents overriding.
Final variable: Makes the variable constant.

Example:

java

final class FinalClass {


void display() {
System.out.println("Final Class cannot be extended");
}
}

class FinalMethodExample {
final void show() {
System.out.println("Final Method cannot be overridden");
}
}

public class FinalDemo {


public static void main(String[] args) {
final int value = 10;
System.out.println("Final variable: " + value);
// value = 20; // Error: Cannot modify final variable
}
}

4. What is abstract class? Explain with example.

Abstract Class: A class that cannot be instantiated and may contain abstract methods (methods
without a body).

Example:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/10
java

abstract class Animal {


abstract void sound(); // Abstract method
}
class Dog extends Animal {
void sound() {
System.out.println("Dog barks");
}
}
public class AbstractExample {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}

5. What is method overriding? How does it support polymorphism?

Definition: When a subclass provides a specific implementation for a method already defined in its
superclass.
Polymorphism: At runtime, the appropriate method is invoked depending on the object.

Example:

java

class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class MethodOverriding {
public static void main(String[] args) {
Animal a = new Dog(); // Runtime Polymorphism
a.sound();
}
}

6. Demonstrate dynamic dispatch using abstract class in JAVA.

Dynamic Dispatch: Method call resolved at runtime.

Example:

java

abstract class Shape {


abstract void draw();
}
class Circle extends Shape {
void draw() {

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/10
System.out.println("Drawing Circle");
}
}
class Triangle extends Shape {
void draw() {
System.out.println("Drawing Triangle");
}
}
public class DynamicDispatch {
public static void main(String[] args) {
Shape s;
s = new Circle();
s.draw();
s = new Triangle();
s.draw();
}
}

7. What is an interface? Illustrate multiple inheritance using interfaces.

Definition: An interface defines a contract with abstract methods.

Example:

java

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 class InterfaceExample {
public static void main(String[] args) {
C obj = new C();
obj.methodA();
obj.methodB();
}
}

8. Define package. Write a Java program to create and import a package.

Package: A namespace to organize classes.

Example:

java

// File: mypack/MyClass.java
package mypack;
public class MyClass {
public void display() {
System.out.println("This is MyClass in package");
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/10
}
}

// File: Main.java
import mypack.MyClass;
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.display();
}
}

9. Producer-Consumer Problem using synchronized threads.

Example:

java

class Q {
int n;
boolean valueSet = false;
synchronized void put(int n) {
while (valueSet) try { wait(); } catch (Exception e) {}
this.n = n;
valueSet = true;
System.out.println("Put: " + n);
notify();
}
synchronized int get() {
while (!valueSet) try { wait(); } catch (Exception e) {}
System.out.println("Got: " + n);
valueSet = false;
notify();
return n;
}
}
class Producer implements Runnable {
Q q;
Producer(Q q) { this.q = q; new Thread(this).start(); }
public void run() {
int i = 0;
while (true) q.put(i++);
}
}
class Consumer implements Runnable {
Q q;
Consumer(Q q) { this.q = q; new Thread(this).start(); }
public void run() {
while (true) q.get();
}
}
public class PCExample {
public static void main(String[] args) {
Q q = new Q();
new Producer(q);
new Consumer(q);
}
}

Let me know if you need explanations or solutions for other questions! 😊

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/10
10. Define a thread. Explain the various ways the threads can be created in Java.
11. With a sample program to describe the exception handling mechanism in
java?
12. What is enumeration? Explain values() of valueOf() methods
13. What is autoboxing and autounboxing in arithmetic expression? Explain
with example

Here are detailed explanations and sample programs for questions 10 to 13:

10. Define a thread. Explain the various ways threads can be created in Java.

Definition of Thread:

A thread is the smallest unit of a process that runs concurrently within a program. Java provides
multithreading support to improve performance and efficiency.

Ways to Create Threads in Java:

1. Extending the `Thread` class:


Override the `run()` method in a class that extends `Thread`.

Example:

java

class MyThread extends Thread {


public void run() {
System.out.println("Thread running using Thread class");
}
}
public class ThreadExample1 {
public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // Starts the thread and calls run()
}
}

2. Implementing the `Runnable` interface:


Implement the `Runnable` interface and define the `run()` method.

Example:

java

class MyRunnable implements Runnable {


public void run() {
System.out.println("Thread running using Runnable interface");
}
}
public class ThreadExample2 {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r); // Pass the runnable object to Thread

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/10
t.start();
}
}

3. Using Lambda Expression (Java 8 and above):


Threads can also be created with a lambda for shorter code.

Example:

java

public class LambdaThreadExample {


public static void main(String[] args) {
Thread t = new Thread(() -> {
System.out.println("Thread running using Lambda expression");
});
t.start();
}
}

11. Describe the exception handling mechanism in Java with a sample program.

Exception Handling in Java:

Exceptions: Errors during the execution of a program.


Java provides `try`, `catch`, `finally`, `throw`, and `throws` to handle exceptions.

Key Concepts:

1. `try` block: Contains code that might throw an exception.


2. `catch` block: Handles the exception.
3. `finally` block: Executes regardless of whether an exception is thrown or not.
4. `throw`: Explicitly throws an exception.
5. `throws`: Declares exceptions that a method might throw.

Example:

java

public class ExceptionHandlingExample {


public static void main(String[] args) {
try {
int a = 10, b = 0;
int result = a / b; // This will throw ArithmeticException
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero is not allowed.");
} finally {
System.out.println("Finally block always executes.");
}
System.out.println("Program continues...");
}
}

Output:

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/10
vbnet

Exception caught: Division by zero is not allowed.


Finally block always executes.
Program continues...

12. What is enumeration? Explain `values()` and `valueOf()` methods.

Enumeration (enum):

An `enum` is a special class in Java that represents a fixed set of constants.


It is defined using the `enum` keyword.

Methods:

1. `values()`: Returns an array of all the enum constants.


2. `valueOf()`: Returns the enum constant for a given string.

Example:

java

enum Days {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
}
public class EnumExample {
public static void main(String[] args) {
// Using values() method
for (Days day : Days.values()) {
System.out.println(day);
}

// Using valueOf() method


Days d = Days.valueOf("MONDAY");
System.out.println("Selected day: " + d);
}
}

Output:

sql

MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
Selected day: MONDAY

13. What is autoboxing and autounboxing in arithmetic expressions? Explain with


an example.

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 9/10
Definition:

Autoboxing: Automatic conversion of a primitive type to its corresponding wrapper class.


Autounboxing: Automatic conversion of a wrapper class to its corresponding primitive type.

Example:

java

public class AutoBoxingExample {


public static void main(String[] args) {
// Autoboxing: int to Integer
int primitiveValue = 10;
Integer wrapperValue = primitiveValue;
System.out.println("Autoboxed value: " + wrapperValue);

// Autounboxing: Integer to int


Integer boxedValue = 20;
int unboxedValue = boxedValue;
System.out.println("Autounboxed value: " + unboxedValue);

// Autoboxing in arithmetic expression


Integer a = 10, b = 20;
Integer sum = a + b; // Autounboxing happens, arithmetic is performed, then autoboxi
ng
System.out.println("Sum: " + sum);
}
}

Output:

yaml

Autoboxed value: 10
Autounboxed value: 20
Sum: 30

Let me know if you need further clarifications or additional examples! 😊

Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 10/10

You might also like