0% found this document useful (0 votes)
7 views12 pages

Java Assessment

The document provides a comprehensive overview of various Java concepts, including assertions, garbage collection, functional interfaces, exceptions, inheritance, collections, and more. It includes code examples for each concept, demonstrating their implementation and usage in Java. Additionally, it covers differences between related concepts, such as compile-time and runtime exceptions, abstract classes and interfaces, and upcasting and downcasting.
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)
7 views12 pages

Java Assessment

The document provides a comprehensive overview of various Java concepts, including assertions, garbage collection, functional interfaces, exceptions, inheritance, collections, and more. It includes code examples for each concept, demonstrating their implementation and usage in Java. Additionally, it covers differences between related concepts, such as compile-time and runtime exceptions, abstract classes and interfaces, and upcasting and downcasting.
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

JAVA ASSESSMENT

1. What is assertion? Can u write a program for it?

an assertion is a statement used in program to test assumptions made by the programmer it checks weather a
condition is true during execution .If it’s not true ,the program throws an assertionerror .

Program :

Public class assertionexample

Public static double getsquareroot(double number){

Assert number >=0 :”number must be non-negative ;

Retrun [Link](number);

Public static void main (string[]args )

[Link] (getsquareroot(25));

[Link](getsquareroot(0));

[Link](getsquareroot(-4));

2. How is java compiler works? Details?


Java Compiler is used to convert java source code into byte code where it is platform independent and executed by
java virtual machine.

Steps:

 Write code
 compile code
 Byte code Generation
 Execute via JVM

3. What is garbage collector in java?code for it?

Java’s garbage collector is used to free and remove unused variables and objects to free up memory for better
performance.

public class Main {

public void finalize() {

[Link]("Object garbage collected");

}
public static void main(String[] args) {

GarbageDemo g = new GarbageDemo();

g = null;

[Link]();

4. Implement functional interface with code?


@FunctionalInterface

interface Greet {

void sayHello();

public class Main {

public static void main(String[] args) {

Greet greet = () -> [Link]("Hello Functional Interface!");

[Link]();

5. Implement runtime exception? with code.


In Java, Runtime Exceptions are unchecked exceptions, which occur during program execution
ArithmeticException – Division by zero

public class Main {

public static void main(String[] args) {

int a = 10, b = 0;

int result = a / b;

[Link]("Result: " + result);

}
}

NullPointerException

public class Main {

public static void main(String[] args) {

String str = null;

[Link]([Link]());

6. Difference between compile and runtime exception

Compile - time Run-time

Checked exceptions Unchecked exceptions

Handled at compile time Occurs during exceptions

IO Exception,SQL Exception NullPointer


Exception ,ArithmeticException

[Link] multilevel inheritance with Example

class Animal {

void eat() { [Link]("Eating..."); }

class Dog extends Animal {

void bark() { [Link]("Barking..."); }

class Puppy extends Dog {

void weep() { [Link]("Weeping..."); }

public class Test {

public static void main(String[] args) {

Puppy p = new Puppy();

[Link](); [Link](); [Link]();


}

9. Implement interface with example ?

interface Vehicle {

void start();

class Bike implements Vehicle {

public void start() {

[Link]("Bike started");

[Link] abstract class with code ?Aabstract class Shape {

abstract void draw();

class Circle extends Shape {

void draw() {

[Link]("Drawing Circle");

[Link] between abstract class and interface?

Feature Abstract class Interface


Methods Can be both implemented & abstract Only abstract (java 8+)
Constructors Yes No
Multiple Inheritance No Yes
Fields Variables + constants Only Constants(final)

13. Wrapper class and its importance?


A wrapper class in Java is used to wrap primitive data types (int, char, boolean, etc.) into [Link] provides
wrapper classes for each primitive type

1. Used with Collections


Collections like ArrayList can only store objects, not primitives.

[Link] and Unboxing

 Autoboxing: primitive → object automatically


 Unboxing: object → primitive automatically

14. Collection diagram? Define set ,Arraylist, map ?

 Set: A collection of unique elements, no duplicates allowed.


 ArrayList: A resizable array that allows duplicates and maintains insertion order.
 Map: Stores key-value pairs, keys must be unique, values can be duplicate.

Diagram :
Collection ← List, Set
Map is separate

15. swap two numbers without using third variable?

public class Main {

public static void main(String[] args) {

int a = 10, b = 20;


[Link]("Before Swap: a = " + a + ", b = " + b);

a=a+b

b = a - b;

a = a - b;

[Link]("After Swap: a = " + a + ", b = " + b);

16. Implement hash map and TreeMap .


HashMap:

import [Link];

import [Link];

public class HashMapExample {

public static void main(String[] args) {

HashMap<String, Integer> map = new HashMap<>();

[Link]("Apple", 50);

[Link]("HashMap:");

for ([Link]<String, Integer> entry : [Link]()) {


[Link]([Link]() + " -> " + [Link]());

TreeMap:

import [Link];

import [Link];

public class TreeMapExample {

public static void main(String[] args) {

TreeMap<String, Integer> map = new TreeMap<>();

[Link]("Apple", 50);

[Link]("TreeMap:");

for ([Link]<String, Integer> entry : [Link]()) {

[Link]([Link]() + " -> " + [Link]());

[Link] bank ATM concept.

public class ATM {

double balance = 1000

void withdraw(double amount) {

if(amount <= balance) {

balance -= amount;

[Link]("Withdrawn: " + amount);

} else [Link]("Insufficient balance");


}

18. Implement string buffer and string builder.


StringBuffer:

public class Main{

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("Hello");

[Link](" World");

[Link](5, " Java");

[Link](6, 10, "Awesome");

[Link]();

[Link]("StringBuffer result: " + sb);

String Builder:

public class Main{

public static void main(String[] args) {

StringBuilder sb = new StringBuilder("Hello");

[Link](" World");

[Link](5, " Java");

[Link](6, 10, "Awesome");

[Link]();

[Link]("StringBuilder result: " + sb);

}
20. Define Enums why it is needed?

Enum is a special class to define fixed set of constants.

To improve code clarity and safety .

Example: Days, Months, Directions.

enum Day {

MONDAY, TUESDAY, WEDNESDAY

public class Test {

public static void main(String[] args) {

Day today = [Link];

[Link](today);

[Link] Bubble Sort.

public class BubbleSortExample {

public static void main(String[] args) {

int[] arr = {5, 2, 9, 1, 3};

for (int i = 0; i < [Link] - 1; i++) {

for (int j = 0; j < [Link] - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
}

for (int num : arr) {

[Link](num + " ");

24. Upcasting and Downcasting in detail?


Upcasting in Java

Converting a subclass (child) type to a superclass (parent) type


class Animal {

void sound() {

[Link]("Animal sound");

class Dog extends Animal {

void bark() {

[Link]("Dog barks");

public class Main {

public static void main(String[] args) {

Animal a = new Dog();

[Link]();

}
}

Downcasting:

Converting a superclass (parent) type to a subclass (child) type


class Animal {

void sound() {

[Link]("Animal makes a sound");

class Dog extends Animal {

void bark() {

[Link]("Dog barks");

public class DowncastingExample {

public static void main(String[] args) {

Animal a = new Dog();

[Link]();

Dog d = (Dog) a;

[Link]();

25. implement real time example for exception handling

import [Link];
public class Main{

public static void main(String[] args) {

int balance = 1000;

Scanner sc = new Scanner([Link]);

[Link]("Enter withdrawal amount: ");

int amount = [Link]();

try {

if (amount > balance) {

throw new ArithmeticException("Insufficient Balance");

} else if (amount <= 0) {

throw new IllegalArgumentException("Invalid amount");

} else {

balance -= amount;

[Link]("Transaction Successful! Remaining Balance: " + balance);

} catch (ArithmeticException | IllegalArgumentException e) {

[Link]("Error: " + [Link]());

22. implement Regex function code?

Regrex function is for pattern Matching .


import [Link].*;
public class Main{

public static void main(String[] args) {

String email = "test123@[Link]";

String regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-z]{2,6}$";

Pattern pattern = [Link](regex);

Matcher matcher = [Link](email);

if ([Link]()) {

[Link]("Valid Email");

} else {

[Link]("Invalid Email");

You might also like