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

Java Imp Questions

The document explains various concepts in Java programming, including packages, interfaces, threads, synchronization, inheritance, and exception handling. It provides code examples for each concept, illustrating how to implement them in Java. Key topics include creating packages, achieving multiple inheritance through interfaces, managing threads, and handling exceptions with try-catch blocks.

Uploaded by

sr9231305
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)
12 views12 pages

Java Imp Questions

The document explains various concepts in Java programming, including packages, interfaces, threads, synchronization, inheritance, and exception handling. It provides code examples for each concept, illustrating how to implement them in Java. Key topics include creating packages, achieving multiple inheritance through interfaces, managing threads, and handling exceptions with try-catch blocks.

Uploaded by

sr9231305
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

1. What is Package?

Write steps to create package named


mypack and importing the package.
Explanation: A package is a way to group related classes and
interfaces together. It provides modularity, avoids name
conflicts, and makes code easier to maintain.
Steps:
1. Write package mypack; at the top of the class file.
2. Compile with javac -d . [Link].
3. Import with import [Link]; in another program.
Program:
java
// [Link]
package mypack;
public class Demo {
public void show() { [Link]("Hello from
mypack"); }
}

// [Link]
import [Link];
class Test {
public static void main(String[] args) {
Demo d = new Demo();
[Link]();
}
}
Output:
Code
Hello from mypack
2. What is Interface? How to achieve multiple inheritance
using interface.
Explanation: An interface is a collection of abstract methods.
A class can implement multiple interfaces, which allows Java
to achieve multiple inheritance.
Program:
java
interface A { void show(); }
interface B { void display(); }

class C implements A, B {
public void show(){ [Link]("From A"); }
public void display(){ [Link]("From B"); }
}

class Test {
public static void main(String[] args){
C obj = new C();
[Link](); [Link]();
}
}
Output:
Code
From A
From B
3. What is Thread? Creating a thread in two ways.
Explanation: Threads allow concurrent execution. Two ways:
1. Extending Thread class.
2. Implementing Runnable interface.
Way 1: Extending Thread
java
class MyThread extends Thread {
public void run(){ [Link]("Thread running"); }
}
class Test {
public static void main(String[] args){ new
MyThread().start(); }
}
Output:
Code
Thread running
Way 2: Implementing Runnable
java
class MyRunnable implements Runnable {
public void run(){ [Link]("Runnable running"); }
}
class Test {
public static void main(String[] args){
Thread t = new Thread(new MyRunnable());
[Link]();
}
}
Output:
Code
Runnable running
4. Lifecycle of Thread. Obtaining a thread stack.
Explanation: A thread goes through states: New → Runnable
→ Running → Waiting/Blocked → Terminated. The stack is
created when start() is called.
5. Explain join() with program.
Explanation: join() makes one thread wait until another
finishes.
Program:
java
class MyThread extends Thread {
public void run(){ for(int i=1;i<=3;i++)
[Link]("Child "+i); }
}
class Test {
public static void main(String[] args) throws
InterruptedException {
MyThread t = new MyThread();
[Link]();
[Link](); // main waits
[Link]("Main ends after child");
}
}
Output:
Code
Child 1
Child 2
Child 3
Main ends after child
6. What is Synchronization? With program.
Explanation: Synchronization ensures only one thread
accesses a critical section at a time.
Program:
java
class Table {
synchronized void printTable(int n){
for(int i=1;i<=3;i++) [Link](n*i);
}
}
class MyThread extends Thread {
Table t; MyThread(Table t){this.t=t;}
public void run(){ [Link](2); }
}
class Test {
public static void main(String[] args){
Table obj=new Table();
new MyThread(obj).start();
new MyThread(obj).start();
}
}
Output:
Code
2
4
6
2
4
6
7. What is the use of Interface?
Explanation: Interfaces provide abstraction, allow multiple
inheritance, and define contracts. They enforce structure
across classes.
8. Nested Interface. Program showing use of interface.
Explanation: An interface declared inside another interface is
called a nested interface.
Program:
java
interface Outer {
interface Inner { void msg(); }
}
class Demo implements [Link] {
public void msg(){ [Link]("Hello Nested
Interface"); }
}
class Test {
public static void main(String[] args){
[Link] obj = new Demo();
[Link]();
}
}
Output:
Code
Hello Nested Interface
9. What is Inheritance? Program to explain how to achieve
multiple inheritance.
Explanation: Inheritance allows one class to acquire
properties of another. Multiple inheritance is achieved using
interfaces.
Program:
java
class A { void show(){[Link]("A");} }
class B extends A { void display(){[Link]("B");} }
class Test { public static void main(String[] args){
B obj=new B(); [Link](); [Link]();
}}
Output:
Code
A
B
10. What is the use of super keyword with member variable
inheritance.
Explanation: super is used to access parent class members
when they are hidden by child class members.
Program:
java
class Parent { int x=10; }
class Child extends Parent {
int x=20;
void show(){ [Link](super.x); }
}
class Test { public static void main(String[] args){ new
Child().show(); } }
Output:
Code
10
11. Types of Nested Class (static & non-static).
Explanation: Static nested class behaves like a static member.
Non-static inner class is tied to an object.
Program:
java
class Outer {
static class StaticInner { void
msg(){[Link]("Static");} }
class Inner { void msg(){[Link]("Non-static");} }
}
class Test {
public static void main(String[] args){
new [Link]().msg();
new Outer().new Inner().msg();
}
}
Output:
Code
Static
Non-static
12. What is Exception? Program with try, catch, throw to
explain multiple catch.
Explanation: Exceptions handle runtime errors. Multiple
catch blocks allow handling different exceptions separately.
Program:
java
class Test {
public static void main(String[] args){
try {
int a=10/0;
} catch(ArithmeticException e){ [Link]("Divide
by zero"); }
catch(Exception e){ [Link]("Other Exception");
}
}
}
Output:
Code
Divide by zero
13. Program to divide by zero error using try, catch, throw
nested try.
Explanation: Nested try blocks allow handling exceptions at
different levels.
Program:
java
class Test {
public static void main(String[] args){
try {
try {
int a=10/0;
} catch(ArithmeticException e){ [Link]("Inner
divide by zero"); }
} catch(Exception e){ [Link]("Outer
Exception"); }
}
}
Output:
Code
Inner divide by zero

You might also like