0% found this document useful (0 votes)
0 views

ket

The document explains various concepts in Java including copy constructors, design patterns, composition, delegation, thread life cycle, generics, and OOP principles. It provides examples for each concept, such as the Factory Pattern and method overloading/overriding. Additionally, it discusses advanced collection types and generic interfaces.

Uploaded by

mr.bhayani009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

ket

The document explains various concepts in Java including copy constructors, design patterns, composition, delegation, thread life cycle, generics, and OOP principles. It provides examples for each concept, such as the Factory Pattern and method overloading/overriding. Additionally, it discusses advanced collection types and generic interfaces.

Uploaded by

mr.bhayani009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1

1. Explain Copy Constructor with Example. (4 Marks)

A copy constructor is a special constructor in Java that is used to create a new object by
copying the values of an existing object of the same class.

Example:

class Person {
String name;
int age;

// Parameterized Constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}

// Copy Constructor
Person(Person other) {
this.name = other.name;
this.age = other.age;
}

void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

public class Main {


public static void main(String[] args) {
Person p1 = new Person("John", 25);
Person p2 = new Person(p1); // Copying p1 to p2
p2.display();
}
}

Output:

Name: John, Age: 25

2. List Down Design Patterns in Java. Explain Any One with Example. (6 Marks)

Design Patterns in Java:

1. Singleton Pattern
2. Factory Pattern
3. Observer Pattern
4. Strategy Pattern
5. Command Pattern
2

6. Adapter Pattern
7. Decorator Pattern
8. Builder Pattern
9. Prototype Pattern
10. Composite Pattern

Example: Factory Design Pattern

The Factory Pattern provides a way to create objects without specifying the exact class of
object that will be created.

Example:

// Product interface
interface Product {
void create();
}

// ConcreteProduct1
class ConcreteProduct1 implements Product {
public void create() {
System.out.println("Product 1 created");
}
}

// ConcreteProduct2
class ConcreteProduct2 implements Product {
public void create() {
System.out.println("Product 2 created");
}
}

// Factory
class ProductFactory {
public Product createProduct(String productType) {
if (productType.equals("Product1")) {
return new ConcreteProduct1();
} else if (productType.equals("Product2")) {
return new ConcreteProduct2();
}
return null;
}
}

public class Main {


public static void main(String[] args) {
ProductFactory factory = new ProductFactory();

Product product1 = factory.createProduct("Product1");


product1.create();
3

Product product2 = factory.createProduct("Product2");


product2.create();
}
}

Output:

Product 1 created
Product 2 created

3. What is Composition and Delegation in Java? (6 Marks)

Composition:

• Composition is a design principle where one class contains an object of another class as
a member. It represents a "has-a" relationship between classes. If the container class is
destroyed, the contained objects are destroyed as well.

Example:

class Engine {
void start() {
System.out.println("Engine started");
}
}

class Car {
private Engine engine;

Car() {
engine = new Engine();
}

void drive() {
engine.start();
System.out.println("Car is driving");
}
}

public class Main {


public static void main(String[] args) {
Car car = new Car();
car.drive();
}
}

Delegation:
4

• Delegation is a design pattern where one object handles a request by delegating the task
to another object. It helps in separating concerns and enhancing reusability.

Example:

interface Printer {
void print();
}

class PrinterDelegate implements Printer {


public void print() {
System.out.println("Printing from delegated printer");
}
}

class PrintService {
private Printer printer;

PrintService(Printer printer) {
this.printer = printer;
}

void delegatePrint() {
printer.print();
}
}

public class Main {


public static void main(String[] args) {
Printer printer = new PrinterDelegate();
PrintService service = new PrintService(printer);
service.delegatePrint();
}
}

4. Explain Life Cycle of Thread with Diagram. (6 Marks)

The life cycle of a thread includes the following states:

1. New: A thread is created but not yet started.


2. Runnable: A thread is ready for execution and waiting for CPU time.
3. Blocked: A thread is blocked waiting for resources (e.g., IO operations).
4. Waiting: A thread is waiting indefinitely for another thread to perform a particular
action.
5. Timed Waiting: A thread is waiting for a specific time period.
6. Terminated: A thread has finished its execution.

Diagram:
5

New --> Runnable --> Running --> Terminated


| ^
| |
Blocked Waiting/Timed Waiting

5. Explain Generic Class with Example. (4 Marks)

A generic class is a class that can work with any data type. Java provides generics to ensure type
safety and to eliminate the need for casting.

Example:

class Box<T> {
private T value;

public void setValue(T value) {


this.value = value;
}

public T getValue() {
return value;
}
}

public class Main {


public static void main(String[] args) {
Box<Integer> intBox = new Box<>();
intBox.setValue(10);
System.out.println(intBox.getValue());

Box<String> strBox = new Box<>();


strBox.setValue("Hello");
System.out.println(strBox.getValue());
}
}

Output:

10
Hello

6. Explain Bounded Wild Card with Example. (4 Marks)

A bounded wildcard allows you to specify a range of types that can be passed to a method. You
can specify an upper bound or lower bound for the type parameter.

• Upper Bounded Wildcard (? extends T): Restricts the type parameter to be of type T
or a subclass of T.
6

• Lower Bounded Wildcard (? super T): Restricts the type parameter to be of type T or
a superclass of T.

Example:

class Fruit {}
class Apple extends Fruit {}
class Banana extends Fruit {}

public class Main {


public static void printFruits(List<? extends Fruit> fruits) {
for (Fruit fruit : fruits) {
System.out.println(fruit);
}
}

public static void main(String[] args) {


List<Apple> apples = new ArrayList<>();
apples.add(new Apple());

List<Banana> bananas = new ArrayList<>();


bananas.add(new Banana());

printFruits(apples);
printFruits(bananas);
}
}

7. List Out the Basic Concept of OOP. Explain Any Two in Brief. (5 Marks)

Basic Concepts of OOP:

1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism

Explanation of Two Concepts:

• Encapsulation: It is the mechanism of hiding the internal details of an object and only
exposing the necessary functionalities. This is typically achieved using private variables
and public methods.
• Polymorphism: It allows one interface to be used for different data types. It can be
achieved through method overloading (compile-time polymorphism) and method
overriding (runtime polymorphism).

8. Explain Use of this Keyword. (3 Marks)


7

The this keyword is used to refer to the current instance of the class. It is used:

• To differentiate between instance variables and local variables when they have the same
name.
• To invoke the current class method or constructor.

Example:

class Car {
String model;

Car(String model) {
this.model = model; // "this" refers to the current object
}

void display() {
System.out.println("Model: " + this.model);
}
}

9. What is Inheritance in Java? List Down the Types of Inheritance Supported


by Java. Explain Any One with Example. (5 Marks)

Inheritance is a mechanism where one class acquires the properties and behaviors of another
class. It supports the "is-a" relationship.

Types of Inheritance in Java:

1. Single Inheritance
2. Multilevel Inheritance
3. Hierarchical Inheritance

Example: Single Inheritance:

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void bark() {
System.out.println("Dog is barking");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();
8

dog.eat();
dog.bark();
}
}

10. Explain Method Overloading and Method Overriding with Example. (5


Marks)

Method Overloading is the ability to define multiple methods with the same name but with
different parameters.

Example:

class Calculator {
int add(int a, int b) {
return a + b;
}

double add(double a, double b) {


return a + b;
}
}

Method Overriding occurs when a subclass provides a specific implementation of a method that
is already defined in its superclass.

Example:

class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Animal animal = new Dog();
animal.sound();
}
}

11. Explain Abstract Class with Example. (4 Marks)


9

An abstract class is a class that cannot be instantiated directly. It can have abstract methods
(without implementation) and concrete methods (with implementation).

Example:

abstract class Animal {


abstract void sound();

void eat() {
System.out.println("Animal is eating");
}
}

class Dog extends Animal {


void sound() {
System.out.println("Dog barks");
}
}

public class Main {


public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
dog.eat();
}
}

12. Difference Between Class and Interface. (4 Marks)

Aspect Class Interface


A class is a blueprint for creating An interface is a contract that classes must
Definition
objects. implement.
An interface cannot have method
A class can have method
Implementation implementations (except default/static
implementations.
methods).
A class can inherit from only one
Inheritance A class can implement multiple interfaces.
class (single inheritance).
Constructor A class has constructors. An interface does not have constructors.

13. How Multiple Inheritance is Implemented in Java? Explain with Example. (6


Marks)

In Java, multiple inheritance is not allowed directly with classes (i.e., a class cannot inherit
from more than one class). However, it can be achieved using interfaces.

Example:
10

interface Animal {
void eat();
}

interface Bird {
void fly();
}

class Penguin implements Animal, Bird {


public void eat() {
System.out.println("Penguin is eating");
}

public void fly() {


System.out.println("Penguin cannot fly");
}
}

public class Main {


public static void main(String[] args) {
Penguin penguin = new Penguin();
penguin.eat();
penguin.fly();
}
}

14. Explain Parameterized Collection and Explain Any One with Example. (4
Marks)

A parameterized collection allows collections to hold data of a specific type, ensuring type
safety. It is typically used with generics.

Example (ArrayList):

import java.util.ArrayList;

public class Main {


public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);

for (Integer num : list) {


System.out.println(num);
}
}
}
11

15. Enlist Advanced Collection Types and Explain Any One with Example. (4
Marks)

Advanced Collection Types:

1. ArrayList
2. LinkedList
3. HashMap
4. HashSet
5. TreeMap
6. TreeSet
7. PriorityQueue

Example: HashMap

import java.util.HashMap;

public class Main {


public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("Apple", 10);
map.put("Banana", 20);

System.out.println(map.get("Apple"));
}
}

16. What is Generic Type Interface? (4 Marks)

A generic type interface defines a contract that can be implemented by any class using a
specific data type.

Example:

interface Box<T> {
void setValue(T value);
T getValue();
}

class IntegerBox implements Box<Integer> {


private Integer value;

public void setValue(Integer value) {


this.value = value;
}

public Integer getValue() {


return value; } }

You might also like