Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

1) explain composition in Java ?

ans -> def -> i)it is type of HAS-A Relationship where one object will be depends
on another object
that means if we make any changes in one object it will be affect for
another
object is known as composition.

ii)composition also known as tight coupling .


iii)tight coupling -> if we make any changes in one object it will be
affect for
another object.
iv)here there is always strong bond b/w two objects.
v)composition involves "HAS-A Relationship" between two objects ,while
inheritance
involves "IS-A Relationship" between two classes.
vi)Symbol for composition is -> <> (Open Diamond)
2)sort an Array of Strings in Alphabetic Order?
3)Fibonacci series / Prime number / String Palindrome / Reverse Number / Number
Palindrome ?
4)Inner Interface in Java explain with example ?
ans -> i)inner interface is also called as Nested Interface in Java.
ii)Inner interface is nothing but An interface creating/declaring in another
interface
(one interface inside another interface) or class is known as inner
interface.
iii)with help of inner interfaces we can
a)solving the name spacing issue when the interface has
common
name.
b)inner interface will increase encapsulation.
c)increasing readability by grouping related interfaces in
one
place.
d)data security.
iv)best example -> Entry interface which is declared inside the Map
interface.
v)How to call/use/invoke inner interface ->
syntax : OuterInterfaceName.InnerInterfaceName
ex : Map.Entry
Demo.Sample
vi)inner interface do not have global scope.
vii)in Inner interface all the data member's are by default final and static
and all
methods by default public and abstract in nature.
viii)it is recommended provide implementation in subclass only.
example ->
1) public interface Customer
{
interface List
{
// body of interface
}
// body of outer interface;
}
public Details implements Customer.List
{

}
2) public class Customer
{
public interface List
{
void add();
}
}
public class demo extends Customer.List
{

}
5)Difference Stack and Queue in DSA and what are applications ?
6)What are the Advantages and Disadvantages of an Collection Framework in Java ?
ans -> Advantages of an Collections Framework :
a)Provides Unified Architecture b) Reusable in Nature c) easy to
use/perform
d)provides good performance e)provides Type safety with generics
f)provides extensibility g)provides standardization

Disadvantages of an Collections Framework :


a)Memory Overhead b)Performance Overhead c)complexity d)Limitations of
generics
e)Thread safety f)inefficiency for primitive types

7)List of commonly used classes in Collection ?


8)Explain boxing , auto-boxing and unboxing and write program for boxing,autoboxing
and unboxing?
-> Boxing : i)converting a primitive value to wrapper class Object (non-
primitives) by using
new keyword/operator is known as boxing.
Program for boxing in Java :
---------------------------
public class Program
{
public static void main(String [] args)
{
int x = 10; // 10 is integer primitive format
System.out.println(x); // primitive
Integer i = new Integer(x); // boxing
System.out.println(i); // wrapper class object value
}
}

Auto-Boxing : Converting primitive value to wrapper class Object(non-


primitive) without
new keyword/operator is known as Auto-Boxing.
Program for Auto-Boxing :
-------------------------
public class Program
{
public static void main(String [] args)
{
int x = 10; // 10 is integer primitive format
System.out.println(x); // primitive
Integer i =x; // auto-boxing
System.out.println(i); // wrapper class object value
}

}
Un-Boxing : i)converting wrapper class object back to primitive value by using
value method
is known unboxing .
i)to perform un-boxing we need perform boxing first.
Program to understand Un-Boxing in Java :
----------------------------------------------
public class Program
{
public static void main(String [] args)
{
int x = 10; // 10 is integer primitive format
System.out.println(x); // primitive
Integer i =x; // auto-boxing
System.out.println(i); // wrapper class object value
// un-boxing
int x1 = i.intValue();
}
}
note : value() methods are available for all primitives.

7)role/use of 'transient' keyword in Java serialization ?


ans -
role of an transient keyword -> i) transient keyword mainly used in Java
Serialization.
ii)transient keyword is used to indicate that particular
variable
field of an object should not be serialized.
iii)when object is serialized , all of its fields are
included
in serialization process unless they are marked as
transient.
iv)transient keyword will make fields as non-serializable
in Java
serialization.
v)serialization -> converting stream of bytes into Java
Object.
Drawbacks/Effects of an Object Serialization ->
i) Exclusion from Serialization
ii)Security and Privacy
iii)Performance Optimization will reduce.
iv)control over serialization

8)Explain Method Chaining in Java ?


def -> method chaining is an process/mechanism here multiple/chain of methods being
called one
after another and it will behaves like constructor chaining only but
difference
is there we will call constructor's and here we will call methods.

Syntax to perform Method Chaining -> obj.method1().method2().method3();


Applications of method chaining :
1)it used to implement method cascading.
2)it is used to implement in fluent interfaces.
Program to understand Method Chaining :
---------------------------------------
public class Student
{
public void data(String name)
{
System.out.println("name : "+name);
}
public void data(String name, int age)
{
System.out.println("name : "+name+" age : "+age);
}
public void display()
{
System.out.println("non-static");
}
public void view()
{
System.out.println("non-static");
}
public static void main(String [] args)
{
Student s = new Student();
s.data("v").data("name",16);
s.display().view();
}
}

Q)Component Library based Program

Here is a detailed design:


Key Classes and Interfaces
1)Component: An abstract class or interface representing a UI component.
2)TextBox: A concrete implementation of the Component for a textbox.
3)Validator: An abstract class or interface representing a validator.
4)EmailValidator: A concrete implementation of the Validator for email validation.
5)ComponentFactory: An interface for factories that create components.
6)TextBoxFactory: A concrete implementation of the ComponentFactory that creates
textboxes with specific validators.

Class Design and Relationships


---------------------------------
1)Component Interface
public interface Component {
void render();
// Other common methods for UI components
}
2)TextBox Class
-------------------
public class TextBox implements Component {
private String value;
private Validator validator;

public TextBox(Validator validator) {


this.validator = validator;
}

public void render() {


// Render the textbox
}

public boolean validate() {


return validator == null || validator.validate(value);
}
public void setValue(String value) {
this.value = value;
}
}
3)Validator Interface
------------------------
public interface Validator {
boolean validate(String value);
}
4)EmailValidator Class
------------------------
public class EmailValidator implements Validator {
@Override
public boolean validate(String value) {
// Implement email validation logic
return value != null && value.contains("@");
}
}
5)ComponentFactory Interface
public interface ComponentFactory {
Component createComponent();
}
6)TextBoxFactory Class
------------------------
public class TextBoxFactory implements ComponentFactory {
private Validator validator;

public TextBoxFactory(Validator validator) {


this.validator = validator;
}

@Override
public Component createComponent() {
return new TextBox(validator);
}
}

Using the Library


i)The developers will use the ComponentFactory to create components.
ii)They will specify the type of component and the type of validation they require
without knowing the internals of how the validator is attached.
public class Main {
public static void main(String[] args) {
// Developer wants a textbox with email validator
ComponentFactory emailTextBoxFactory = new TextBoxFactory(new
EmailValidator());
Component emailTextBox = emailTextBoxFactory.createComponent();

emailTextBox.render();

// For demonstration, casting to TextBox to set value and validate


if (emailTextBox instanceof TextBox) {
TextBox tb = (TextBox) emailTextBox;
tb.setValue("example@example.com");
boolean isValid = tb.validate();
System.out.println("Is valid: " + isValid);
}
}
}

You might also like