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

Intoduction to Java_Important Questions

The document covers various fundamental concepts in Java, including lexical issues, arrays, operators, object-oriented principles, constructors, recursion, access specifiers, call by value/reference, stack operations, and the use of 'this'. It provides code examples for matrix addition, sorting, Fibonacci calculation, and multilevel inheritance. Each section explains the concepts clearly and provides practical Java code implementations.
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)
2 views

Intoduction to Java_Important Questions

The document covers various fundamental concepts in Java, including lexical issues, arrays, operators, object-oriented principles, constructors, recursion, access specifiers, call by value/reference, stack operations, and the use of 'this'. It provides code examples for matrix addition, sorting, Fibonacci calculation, and multilevel inheritance. Each section explains the concepts clearly and provides practical Java code implementations.
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/ 24

Module – 1

1. Explain different lexical issues in JAVA.


Answer:
In Java, the lexical analysis phase is the first step in compiling a program. During this phase, the
source code is broken into basic units called tokens. Key lexical issues include:
• Tokens: The smallest elements such as identifiers, keywords, literals, operators, and
separators. Each token is defined by a specific pattern.
• Identifiers and Keywords: Java reserves certain words (like class, public, etc.) as
keywords. Identifiers must follow naming conventions (e.g., start with a letter, underscore, or
dollar sign, and not conflict with keywords).
• Literals: These include numbers, characters, strings, and boolean values. Their format must
conform to Java’s rules (for example, string literals are enclosed in double quotes).
• Comments and Whitespace: Java supports single-line (//) and multi-line (/* ... */)
comments, which are ignored by the compiler. Whitespace is used to separate tokens but is
otherwise not significant.
• Operators and Separators: Operators (such as +, -, *, /) and separators (such as commas,
semicolons, and braces) are recognized as distinct tokens.
• Error Handling: Lexical errors occur when the source code contains an invalid token (for
instance, an illegal character or malformed literal). The compiler’s lexer must detect and
report such issues before the parsing stage.
2. Define Array. Write a Java program to implement the addition of two matrixes.
Answer:
An array in Java is a container object that holds a fixed number of values of a single type. The
length of an array is established when the array is created. After creation, its length cannot be
changed.
Below is a sample Java program that adds two matrices:
public class MatrixAddition {
public static void main(String[] args) {
// Define two 2D arrays (matrices)
int[][] matrix1 = {
{1, 2, 3},
{4, 5, 6}
};
int[][] matrix2 = {
{7, 8, 9},
{10, 11, 12}
};

// Assume both matrices have the same dimensions


int rows = matrix1.length;
int cols = matrix1[0].length;

// Create a matrix to hold the sum


int[][] sum = new int[rows][cols];

// Add corresponding elements of matrix1 and matrix2


for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}

// Print the resulting sum matrix


System.out.println("Sum of the two matrices:");
for (int i = 0; i < rows; i++){
for (int j = 0; j < cols; j++){
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
}
}
This program initializes two matrices, adds them element by element, and prints the resulting matrix.
3. Explain the following operations with examples. (i) << (ii) >> (iii) >>>
Answer:
• << (Left Shift Operator):
This operator shifts the bits of the number to the left by the specified number of positions.
Zeroes are shifted in from the right.
Example:
Consider the integer 8 (binary: 00001000).
o 8 << 2 shifts the bits two places to the left, resulting in binary 00100000, which is 32
in decimal.
• >> (Signed Right Shift Operator):
This operator shifts the bits of the number to the right by the specified number of positions.
The leftmost bit (sign bit) is replicated (also known as sign extension) to maintain the sign of
the number.
Example:
For the integer 16 (binary: 00010000):
o 16 >> 2 shifts bits two places to the right, resulting in binary 00000100, which is 4 in
decimal.
For a negative number, say -16 (binary representation in two’s complement):
o -16 >> 2 will keep the sign bit as 1 (indicating a negative number) after shifting.
• >>> (Unsigned Right Shift Operator):
This operator shifts the bits to the right by the specified number of positions but always fills
the leftmost bits with zeros regardless of the sign. This is especially useful for unsigned
operations.
Example:
Consider the negative integer -16.
o When using -16 >>> 2, the sign bit is not preserved; zeros are shifted in from the left,
which yields a large positive number due to the binary representation.

4. Explain object-oriented principles.


Answer:
Java is built around object-oriented programming (OOP) principles, which help in designing modular
and reusable software. The core OOP principles include:
• Encapsulation:
Bundling data (attributes) and methods (functions) that operate on the data into a single unit
(class) and restricting access to some of the object’s components. This helps to protect the
internal state of the object.
• Inheritance:
Allows a new class (subclass) to inherit properties and behavior (methods) from an existing
class (superclass), promoting code reuse and a hierarchical classification.
• Polymorphism:
The ability of different classes to be treated as instances of the same class through
inheritance. It allows methods to perform differently based on the object that is calling them
(method overriding and method overloading).
• Abstraction:
Hiding the complex implementation details and showing only the necessary features of an
object. This is achieved through abstract classes and interfaces in Java.
5. Write a Java program to sort the elements using a for loop.
Answer:
Below is an example Java program that uses a simple bubble sort algorithm with nested for loops to
sort an array of integers in ascending order:
public class BubbleSortExample {
public static void main(String[] args) {
int[] numbers = {64, 34, 25, 12, 22, 11, 90};
int n = numbers.length;
int temp;

// Bubble sort using for loops


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (numbers[j] > numbers[j+1]) {
// Swap numbers[j] and numbers[j+1]
temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}

// Print the sorted array


System.out.println("Sorted array:");
for (int number : numbers) {
System.out.print(number + " ");
}
}
}
This program uses two nested for loops to repeatedly swap adjacent elements if they are in the wrong
order.
6. Explain different types of if statements in JAVA.
Answer:
Java supports several forms of conditional statements using if. The main types are:
Simple if Statement:
Executes a block of code if a specified condition is true.
if (condition) {
// code to be executed if condition is true
}
if-else Statement:
Provides an alternative block of code if the condition is false.
if (condition) {
// code if condition is true
} else {
// code if condition is false
}
if-else if-else Ladder:
Tests multiple conditions in sequence. The first true condition’s block is executed.
if (condition1) {
// code if condition1 is true
} else if (condition2) {
// code if condition2 is true
} else {
// code if all above conditions are false
}
Nested if Statements:
One if statement inside another, used for checking multiple levels of conditions.
if (condition1) {
if (condition2) {
// code if both condition1 and condition2 are true
}
}
Module – 2

1. What are constructors? Explain two types of constructors with an example


program.
Answer:
Constructors in Java are special methods used to initialize new objects. They have the same name as
the class and do not have a return type. There are two main types:
• Default Constructor:
A no-argument constructor automatically provided by Java if no other constructor is defined.
It initializes object fields to default values.
• Parameterized Constructor:
A constructor that accepts arguments to allow initializing the object with specific values.
Example Program:
public class Person {
String name;
int age;

// Default constructor
public Person() {
name = "Unknown";
age = 0;
}

// Parameterized constructor
public Person(String name, int age) {
this.name = name; // 'this' differentiates the instance variable from the
parameter
this.age = age;
}

public void display() {


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

public static void main(String[] args) {


// Using default constructor
Person p1 = new Person();
p1.display();

// Using parameterized constructor


Person p2 = new Person("Alice", 30);
p2.display();
}
}
This program shows both constructors: one that assigns default values and another that initializes
with given values.
2. Define recursion. Write a recursive program to find nth Fibonacci number.
Answer:
Recursion is a programming technique where a method calls itself to solve a smaller instance of the
same problem until a base condition is met.
Below is a Java program that calculates the nth Fibonacci number using recursion:
public class FibonacciRecursion {
// Recursive method to find nth Fibonacci number
public static int fibonacci(int n) {
if (n <= 1) {
return n; // Base condition: return n if n is 0 or 1
} else {
return fibonacci(n - 1) + fibonacci(n - 2); // Recursive calls
}
}

public static void main(String[] args) {


int n = 10; // For example, find the 10th Fibonacci number
System.out.println("The " + n + "th Fibonacci number is " +
fibonacci(n));
}
}
In this program, the method fibonacci calls itself with decremented values until reaching the base
cases (n == 0 or n == 1).
3. Explain the various access specifiers in Java.
Answer:
Java provides several access specifiers to set the accessibility (visibility) of classes, methods, and
variables:
• public:
The member is accessible from any other class in any package.
• protected:
The member is accessible within its own package and by subclasses in other packages.
• default (package-private):
If no access specifier is used, the member is accessible only within its own package.
• private:
The member is accessible only within the class in which it is declared.
These specifiers help in encapsulating and protecting data by restricting access to class members as
needed.

4. Explain call by value and call by reference with an example program.


Answer:
In Java, call by value is the method of passing arguments to a function. Java always passes a copy of
the value:
• For primitives: The actual value is copied. Modifications in the method do not affect the
original variable.
• For objects: The reference (address) to the object is copied. This means that while you
cannot change the reference itself, you can modify the object’s state via that reference.
Example Program:
public class CallByValueExample {
// Method to modify a primitive type and an object's field
public static void modifyValues(int num, Person person) {
num = 100; // This change will not affect the original primitive
person.name = "Modified"; // This change affects the object referenced
outside
}

public static void main(String[] args) {


int number = 50;
Person person = new Person("Original", 25);
System.out.println("Before modification: number = " + number + ",
person.name = " + person.name);
modifyValues(number, person);
System.out.println("After modification: number = " + number + ",
person.name = " + person.name);
}
}

class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
}
In this program, changing num inside modifyValues does not affect number in main because
primitives are passed by value. However, the object person is passed as a copy of its reference, so
modifying its field changes the object itself.
5. Write a program to perform Stack operations using proper class and Methods.
Answer:
Below is a simple Java program that implements a stack using an array. The program provides
methods for push, pop, peek, and checking if the stack is empty.
public class StackOperations {
private int maxSize;
private int[] stackArray;
private int top;

// Constructor to initialize the stack


public StackOperations(int size) {
maxSize = size;
stackArray = new int[maxSize];
top = -1;
}

// Push an element onto the stack


public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value;
System.out.println("Pushed: " + value);
} else {
System.out.println("Stack is full!");
}
}

// Pop an element from the stack


public int pop() {
if (top >= 0) {
int value = stackArray[top--];
System.out.println("Popped: " + value);
return value;
} else {
System.out.println("Stack is empty!");
return -1;
}
}
// Peek at the top element without removing it
public int peek() {
if (top >= 0) {
return stackArray[top];
} else {
System.out.println("Stack is empty!");
return -1;
}
}

// Check if the stack is empty


public boolean isEmpty() {
return (top == -1);
}

public static void main(String[] args) {


StackOperations stack = new StackOperations(5);

// Perform stack operations


stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Top element is: " + stack.peek());
stack.pop();
stack.pop();
stack.push(40);
System.out.println("Top element is: " + stack.peek());
}
}
This program creates a StackOperations class that encapsulates stack behavior and demonstrates
common stack operations.
6. Explain the use of this in JAVA with an example.
Answer:
In Java, the keyword this is a reference to the current object. It is used to:
• Distinguish between instance variables and parameters when they have the same name.
• Invoke other constructors in the same class (constructor chaining).
• Pass the current object as an argument to other methods.
Example Program:
public class ThisExample {
private int number;
private String message;

// Parameterized constructor using 'this' to distinguish instance variables


public ThisExample(int number, String message) {
this.number = number; // 'this.number' refers to the instance variable
this.message = message; // 'this.message' refers to the instance
variable
}

public void display() {


System.out.println("Number: " + this.number + ", Message: " +
this.message);
}

public static void main(String[] args) {


ThisExample obj = new ThisExample(42, "Hello, Java!");
obj.display();
}
}
In this example, this.number and this.message clearly refer to the instance variables of the
current object, avoiding confusion with the constructor parameters.
Module – 3
1. Write a Java program to implement multilevel inheritance with 3 levels of
hierarchy.
Answer:
Multilevel inheritance is a process in which a class is derived from a class that is already derived
from another class. In Java, this means you can have a base class (level 1), a derived class (level 2),
and a further derived class (level 3). For example, consider an inheritance hierarchy where an
Animal is the base, Mammal is derived from Animal, and Dog is derived from Mammal.
// Level 1: Base class
class Animal {
void eat() {
System.out.println("Animal eats.");
}
}

// Level 2: Derived class from Animal


class Mammal extends Animal {
void breathe() {
System.out.println("Mammal breathes air.");
}
}

// Level 3: Derived class from Mammal


class Dog extends Mammal {
void bark() {
System.out.println("Dog barks.");
}
}

public class MultiLevelInheritance {


public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Inherited from Animal
dog.breathe(); // Inherited from Mammal
dog.bark(); // Defined in Dog
}
}
In this example, the Dog class inherits methods from both Mammal and Animal, demonstrating
multilevel inheritance.

2. Explain how an interface is used to achieve multiple inheritances in Java.


Answer:
Java does not support multiple inheritance of classes (i.e., a class cannot extend more than one class).
However, multiple inheritance of type is achieved through interfaces. A class can implement more
than one interface, allowing it to inherit the abstract method signatures (and default methods, if
provided) from multiple sources.
For example, suppose we have two interfaces, Printable and Showable. A class can implement
both, thereby inheriting behavior from both interfaces:
interface Printable {
void print();
}
interface Showable {
void show();
}

class Demo implements Printable, Showable {


// Providing implementation for print() from Printable interface
public void print() {
System.out.println("Print method.");
}
// Providing implementation for show() from Showable interface
public void show() {
System.out.println("Show method.");
}

public static void main(String[] args) {


Demo obj = new Demo();
obj.print();
obj.show();
}
}
In this example, the Demo class achieves multiple inheritance by implementing both Printable and
Showable interfaces.

3. Explain the method overriding with a suitable example.


Answer:
Method overriding occurs when a subclass provides its own implementation of a method that is
already defined in its superclass. This is a key feature of runtime polymorphism in Java, allowing a
subclass to tailor or completely replace the behavior of a method inherited from the parent class.
For example, consider a base class Animal with a method sound(), and a subclass Dog that overrides
this method:
class Animal {
void sound() {
System.out.println("Animal makes a sound.");
}
}

class Dog extends Animal {


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

public class MethodOverriding {


public static void main(String[] args) {
// Upcasting: Animal reference to a Dog object
Animal a = new Dog();
a.sound(); // Calls the overridden method in Dog
}
}
Here, although the reference type is Animal, the actual object is of type Dog, so the overridden
sound() method in the Dog class is invoked.
4. What is single-level inheritance? Write a Java program to implement single-
level inheritance.
Answer:
Single-level inheritance involves one class (child or subclass) extending another class (parent or
superclass). It is the simplest form of inheritance.
For example, consider a class Person as the base and a class Student as a derived class:
// Superclass
class Person {
String name = "John Doe";

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

// Subclass extending Person (single-level inheritance)


class Student extends Person {
int studentId = 101;

void displayStudent() {
System.out.println("Student ID: " + studentId);
}
}

public class SingleLevelInheritance {


public static void main(String[] args) {
Student s = new Student();
s.display(); // Method inherited from Person
s.displayStudent(); // Method defined in Student
}
}
In this example, Student inherits from Person, making it an example of single-level inheritance.

5. What is the importance of the super keyword in inheritance? Illustrate with a


suitable example.
Answer:
The super keyword in Java is used to refer to the immediate parent class. It is useful for:
• Accessing the parent class’s methods and variables when they are hidden by the
subclass.
• Invoking the parent class’s constructor from the subclass constructor.
Consider the following example:
class Animal {
String type = "Animal";

Animal() {
System.out.println("Animal Constructor");
}
}

class Dog extends Animal {


String type = "Dog";

Dog() {
// Calls the parent class (Animal) constructor
super();
System.out.println("Dog Constructor");
}

void display() {
// Accessing the subclass's type and the superclass's type using super
System.out.println("Type from Dog: " + type);
System.out.println("Type from Animal: " + super.type);
}
}

public class SuperKeywordExample {


public static void main(String[] args) {
Dog d = new Dog();
d.display();
}
}
In this example, super() is used in the Dog constructor to call the Animal constructor. In the
display() method, super.type accesses the type variable of the parent class, even though the
subclass has its own type variable.

6. What is abstract class and abstract method? Explain with an example.


Answer:
An abstract class in Java is a class that cannot be instantiated directly. It may contain abstract
methods—methods declared without an implementation—as well as concrete methods. An abstract
method is declared using the abstract keyword and does not have a body. Subclasses are
responsible for providing the implementation of abstract methods.
For example, consider an abstract class Shape with an abstract method draw():
// Abstract class
abstract class Shape {
// Abstract method (no implementation)
abstract void draw();

// Concrete method
void display() {
System.out.println("This is a shape.");
}
}

// Subclass providing implementation for the abstract method


class Circle extends Shape {
@Override
void draw() {
System.out.println("Drawing a circle.");
}
}

public class AbstractExample {


public static void main(String[] args) {
// You cannot instantiate Shape directly:
// Shape s = new Shape(); // This would cause a compilation error

// Instead, instantiate a subclass that implements all abstract methods


Shape shape = new Circle();
shape.display();
shape.draw();
}
}
Here, Shape is an abstract class and cannot be instantiated. The subclass Circle implements the
abstract method draw(), providing the specific behavior.
Module – 4
1. Define package. Explain the steps involved in creating a user-defined package
with an example.
Answer:
A package in Java is a namespace that organizes a set of related classes and interfaces. It helps to
avoid name conflicts and to control access. To create a user-defined package, follow these steps:
1. Create a directory structure:
Create a folder structure that corresponds to your package name (for example, mypackage).
2. Declare the package in your Java file:
At the top of your Java file, add a package declaration such as package mypackage;.
3. Compile the Java file:
Compile your Java file using the javac command (e.g., javac mypackage/Hello.java).
4. Use the package in another file:
In another Java file, import the package using the import statement.
Example:
File: mypackage/Hello.java
package mypackage;

public class Hello {


public void sayHello() {
System.out.println("Hello from package mypackage!");
}
}
File: TestPackage.java
import mypackage.Hello;

public class TestPackage {


public static void main(String[] args) {
Hello h = new Hello();
h.sayHello();
}
}
Compile both files (ensuring the directory structure is maintained) and run TestPackage to see the
output.

2. Write a program that contains one method that will throw an


IllegalAccessException and use proper exception handles so that the exception
should be printed.
Answer:
Below is a simple program where a method intentionally throws an IllegalAccessException. The
exception is caught using a try-catch block so that its details are printed.
public class ExceptionHandlingExample {
// Method that throws an IllegalAccessException
public static void riskyMethod() throws IllegalAccessException {
throw new IllegalAccessException("Illegal access detected!");
}

public static void main(String[] args) {


try {
riskyMethod();
} catch (IllegalAccessException e) {
System.out.println("Caught Exception: " + e);
}
}
}
In this program, calling riskyMethod() throws an IllegalAccessException that is then caught
and printed in the catch block.

3. Define an exception. What are the key terms used in exception handling?
Explain.
Answer:
An exception is an event that occurs during the execution of a program that disrupts the normal flow
of instructions. Exceptions are objects that represent errors or unexpected events that may occur
during runtime.
Key terms in exception handling include:
• try:
A block of code where exceptions might occur. Code within the try block is executed until
an exception is thrown.
• catch:
A block that catches and handles exceptions thrown in the try block. Multiple catch blocks
can be used to handle different types of exceptions.
• finally:
A block that always executes after the try and catch blocks, regardless of whether an
exception was thrown. It is typically used for cleanup operations.
• throw:
A keyword used to explicitly throw an exception from a method or block of code.
• throws:
Used in a method signature to declare that the method might throw one or more exceptions,
thereby shifting the responsibility of handling those exceptions to the caller.
These constructs provide a structured way to detect and handle errors, ensuring the program can
either recover or terminate gracefully.

4. Explain the concept of importing packages in Java and provide an example


demonstrating the usage of the import statement.
Answer:
In Java, the import statement allows you to bring classes or entire packages into visibility, so you
can use them without providing their fully qualified names. This is especially useful when using
Java’s standard libraries or user-defined packages.
For example, to use the Scanner class from the java.util package:
import java.util.Scanner; // Import the Scanner class

public class ImportExample {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = scanner.nextLine();
System.out.println("Hello, " + name);
scanner.close();
}
}
Here, the import statement makes the Scanner class available, so you don’t have to refer to it as
java.util.Scanner every time.

5. How do you create your own exception class? Explain with a program.
Answer:
You can create your own exception class by extending either the Exception class (for a checked
exception) or the RuntimeException class (for an unchecked exception). Here’s an example of a
custom exception:
// Custom Exception class extending Exception (checked exception)
class MyCustomException extends Exception {
public MyCustomException(String message) {
super(message);
}
}

public class CustomExceptionExample {


// Method that throws the custom exception
public static void testMethod(int num) throws MyCustomException {
if (num < 0) {
throw new MyCustomException("Number must be non-negative");
} else {
System.out.println("Number is: " + num);
}
}

public static void main(String[] args) {


try {
testMethod(-5);
} catch (MyCustomException e) {
System.out.println("Caught Exception: " + e.getMessage());
}
}
}
In this example, MyCustomException is a user-defined exception. The testMethod method checks a
condition and throws the exception when the condition is met. The exception is then caught in the
main method.

6. Demonstrate the working of a nested try block with an example.


Answer:
Nested try blocks involve having a try-catch block within another try block. This allows you to
handle exceptions at different levels. Below is an example that demonstrates nested try blocks:
public class NestedTryExample {
public static void main(String[] args) {
try {
System.out.println("Outer try block");

// Inner try block


try {
System.out.println("Inner try block");
int result = 10 / 0; // This will throw ArithmeticException
} catch (ArithmeticException e) {
System.out.println("Inner catch: " + e);
}
// Code in outer try block continues after inner try-catch
int[] arr = new int[2];
System.out.println(arr[3]); // This will throw
ArrayIndexOutOfBoundsException
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Outer catch: " + e);
}
}
}
In this example:
• The inner try block catches an ArithmeticException caused by dividing by zero.
• After handling the inner exception, the outer try block continues and then encounters an
ArrayIndexOutOfBoundsException, which is caught by the outer catch block.
Module – 5

1. What do you mean by a thread? Explain the different ways of creating threads.
Answer:
A thread in Java represents a single sequential flow of control within a program. Threads allow
concurrent execution of two or more parts of a program to maximize the use of CPU. In Java, a
thread is an instance of the class Thread (or an instance of a class that implements the Runnable
interface) that can run concurrently with other threads.
There are several ways to create threads in Java:
1. Extending the Thread Class:
You can create a new thread by subclassing the Thread class and overriding its run()
method. When you create an instance of your subclass and call its start() method, a new
thread of execution begins.
class MyThread extends Thread {
public void run() {
System.out.println("Thread running by extending Thread class.");
}
}

public class ThreadDemo1 {


public static void main(String[] args) {
MyThread t = new MyThread();
t.start(); // Starts a new thread
}
}
2. Implementing the Runnable Interface:
Alternatively, you can implement the Runnable interface. This approach is more flexible
because your class can extend another class if needed. You pass an instance of your Runnable
class to a Thread object and then call start().
class MyRunnable implements Runnable {
public void run() {
System.out.println("Thread running by implementing Runnable
interface.");
}
}

public class ThreadDemo2 {


public static void main(String[] args) {
Thread t = new Thread(new MyRunnable());
t.start(); // Starts a new thread
}
}
3. Using Callable and Future (for Return Values):
When you need a thread to return a result, you can implement the Callable interface and
submit the task to an ExecutorService. This method allows you to use the Future object to
retrieve the result.
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
class MyCallable implements Callable<String> {
public String call() throws Exception {
return "Callable thread execution result.";
}
}

public class ThreadDemo3 {


public static void main(String[] args) {
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<String> result = executor.submit(new MyCallable());
try {
System.out.println(result.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
executor.shutdown();
}
}
Each method has its advantages. Extending Thread is straightforward but limits inheritance, while
implementing Runnable allows for greater flexibility.

2. What is the need of synchronization? Explain with an example how


synchronization is implemented in JAVA.
Answer:
Synchronization is needed in multithreaded applications to prevent thread interference and
memory consistency errors when multiple threads access shared resources (such as variables or
objects) concurrently. Without synchronization, concurrent modifications can lead to unpredictable
results or corrupted data.
How synchronization is implemented in Java:
• Synchronized Methods:
By declaring a method with the synchronized keyword, only one thread can execute that
method on a given object at a time.
• Synchronized Blocks:
You can also synchronize a block of code within a method using a designated lock object.
This provides finer control over the scope of synchronization.
Example using a synchronized method (a simple bank account update):
class BankAccount {
private int balance = 0;

// Synchronized method to prevent multiple threads from modifying the balance


concurrently
public synchronized void deposit(int amount) {
balance += amount;
System.out.println("Deposited: " + amount + ", New Balance: " + balance);
}

public int getBalance() {


return balance;
}
}

public class SynchronizationExample {


public static void main(String[] args) {
BankAccount account = new BankAccount();
// Create two threads that attempt to deposit money concurrently
Thread t1 = new Thread(() -> {
for (int i = 0; i < 5; i++) {
account.deposit(100);
}
});

Thread t2 = new Thread(() -> {


for (int i = 0; i < 5; i++) {
account.deposit(200);
}
});

t1.start();
t2.start();
}
}
In this example, the deposit method is synchronized, so even if multiple threads call it
simultaneously, each deposit operation is performed completely before another begins, ensuring the
account balance remains consistent.

3. Discuss values() and valueOf() methods in Enumerations with suitable


examples.
Answer:
In Java enumerations, the two commonly used methods are:
• values():
This method is automatically generated by the compiler for every enum. It returns an array of
all the enum constants in the order they are declared.
• valueOf(String name):
This method returns the enum constant of the specified enum type with the given name. The
string must match exactly an identifier used to declare an enum constant in this type.
Example:
enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY;
}

public class EnumExample {


public static void main(String[] args) {
// Using values() to iterate over enum constants
for (Day d : Day.values()) {
System.out.println(d);
}

// Using valueOf() to convert a string to an enum constant


Day today = Day.valueOf("FRIDAY");
System.out.println("Today is: " + today);
}
}
Here, Day.values() returns an array of all days of the week, and Day.valueOf("FRIDAY") returns
the enum constant Day.FRIDAY. These methods provide a convenient way to work with enum types
dynamically.
4. What is multithreading? Write a program to create multiple threads in JAVA.
Answer:
Multithreading is the capability of a CPU (or a single program) to execute multiple threads
concurrently, thereby improving the performance of applications through parallel execution. In Java,
multithreading is used to perform multiple operations simultaneously by creating several threads.
Example: Creating multiple threads using the Runnable interface:
class Task implements Runnable {
private String threadName;

public Task(String name) {


this.threadName = name;
}

public void run() {


for (int i = 1; i <= 5; i++) {
System.out.println(threadName + " is running, iteration: " + i);
try {
Thread.sleep(500); // Pause for 500ms
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
}
}

public class MultiThreadingExample {


public static void main(String[] args) {
Thread thread1 = new Thread(new Task("Thread-1"));
Thread thread2 = new Thread(new Task("Thread-2"));
Thread thread3 = new Thread(new Task("Thread-3"));

// Start the threads


thread1.start();
thread2.start();
thread3.start();
}
}
In this example, three threads are created using the Runnable interface and started concurrently.
Each thread executes its run() method independently.

5. Explain with an example how inter-thread communication is implemented in


JAVA.
Answer:
Inter-thread communication in Java allows threads to communicate with each other using methods
like wait(), notify(), and notifyAll(). This mechanism is typically used in producer-consumer
scenarios where one thread waits for a condition while another thread notifies it when the condition
is met.
Example: A simple producer-consumer scenario using a shared resource:
class SharedResource {
private int data;
private boolean available = false;

// Producer puts data


public synchronized void put(int value) {
// Wait until the data is consumed
while (available) {
try {
wait(); // current thread releases the lock and waits
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
data = value;
available = true;
System.out.println("Produced: " + data);
notify(); // Notify the waiting consumer
}

// Consumer gets data


public synchronized int get() {
// Wait until data is available
while (!available) {
try {
wait(); // current thread releases the lock and waits
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
available = false;
System.out.println("Consumed: " + data);
notify(); // Notify the waiting producer
return data;
}
}

class Producer implements Runnable {


private SharedResource resource;

Producer(SharedResource resource) {
this.resource = resource;
}

public void run() {


for (int i = 1; i <= 5; i++) {
resource.put(i);
}
}
}

class Consumer implements Runnable {


private SharedResource resource;

Consumer(SharedResource resource) {
this.resource = resource;
}

public void run() {


for (int i = 1; i <= 5; i++) {
resource.get();
}
}
}
public class InterThreadCommunicationExample {
public static void main(String[] args) {
SharedResource resource = new SharedResource();
Thread producerThread = new Thread(new Producer(resource));
Thread consumerThread = new Thread(new Consumer(resource));

producerThread.start();
consumerThread.start();
}
}
In this example, the SharedResource class uses wait() and notify() to coordinate between the
producer (which puts data) and the consumer (which retrieves data). The producer waits if the
resource is full (i.e., available is true), and the consumer waits if there is no data (i.e., available
is false).

6. Explain auto-boxing/unboxing in expressions.


Answer:
Auto-boxing is the automatic conversion that the Java compiler makes between the primitive types
(like int, double, etc.) and their corresponding object wrapper classes (Integer, Double, etc.).
Unboxing is the reverse process—converting an object of a wrapper type to its corresponding
primitive type.
Example:
public class AutoBoxingExample {
public static void main(String[] args) {
// Auto-boxing: primitive int is automatically converted to Integer
object
Integer boxedInt = 10;
System.out.println("Boxed value: " + boxedInt);

// Unboxing: Integer object is automatically converted to primitive int


int unboxedInt = boxedInt;
System.out.println("Unboxed value: " + unboxedInt);

// Auto-boxing in expressions
Integer a = 5;
Integer b = 10;
// The following expression unboxes a and b, performs addition, and then
auto-boxes the result
Integer sum = a + b;
System.out.println("Sum: " + sum);
}
}
In this example, the conversion between int and Integer happens automatically by the compiler.
When the expression a + b is evaluated, the values are unboxed to perform arithmetic and then the
result is boxed back into an Integer object.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy