Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Chapter 3

1.Define Interface with its syntax.


-​An ​interface​ is an abstract "class" that is used to group related methods with
"empty" bodies.
-We cannot define any methods in interface.
-all the variables must be in public class
-Syntax:-​interface​ interface_name {
​public​ ​void​ method1(); ​// interface method (does not have a body)
​public​ ​void​ method2(); ​// interface method (does not have a body)
}
2.Differance between class and interface.

3. State need of interface with suitable examples.


There are mainly three reasons to use interface. They are given below.

○ It is used to achieve abstraction.

○ By interface, we can support the functionality of multiple inheritance.

○ It can be used to achieve loose coupling.


EXAMPLE:-
// interface
interface​ ​Animal {
​public​ ​void​ animalSound(); ​// interface method (does not have a body)
​public​ ​void​ sleep(); ​// interface method (does not have a body)
}

// Pig "implements" the Animal interface


class​ Pig ​implements​ Animal {
​public​ ​void​ animalSound() {
// The body of animalSound() is provided here
System.​out​.​println​(​"The pig says: wee wee"​);
}
​public​ ​void​ sleep() {
// The body of sleep() is provided here
System.​out​.​println​(​"Zzz"​);
}
}

class​ MyMainClass {
​public​ ​static​ ​void​ main(​String​[] args) {
​Pig myPig = ​new​ Pig(); ​// Create a Pig object
myPig.​animalSound​();
​myPig.​sleep​();
}
}
4.Write a program to implement multiple inheritance
interface A
{
void display1();
}
interface B
{
void display2();
}
class C implements A,B
{
public void display1()
{
System.out.println(“Interface A Method”);
}
public void display2()
{
System.out.println(“Interface B Method”);
}
void display3()
{
System.out.println(“Class C Method”);
}
}
class inh
{
public static void main(String args[])
{
C obj = new c();
obj.display1();
obj.display2();
obj.display3();
}
}
5.Define Package and its type​.
-​A package in Java is used to group related classes. Think of it as ​a folder in a
file directory​. We use packages to avoid name conflicts, and to write a better
maintainable code. Packages are divided into two types:

● Built-in Packages (packages from the Java API)


○ The Java API is a library of prewritten classes, that are free to use,
included in the Java Development Environment.

● User-defined Packages (create your own packages)


○ These packages are defined by the user as per the application
requirement.

6. How to create and access user define package. Explain with example.
-Step 1: First create a folder and give name of package to it.
Eg:- c:\mypack
-step 2:Set classpath on command prompt as:
c:\jdk\bin>set classpath=c:\mypack
-step 3:Create first class to include in package:
firstcl.java
package mypack;
public class firstcl
{
public void add(int a,int b)
{
int r = a+b;
System.out.println(“Summation is:”+r);
}
}

-Compile the program


- -d will copy the class file at c:\mypack
- Remember that the program do not contain main() method and we dot run the
program.It just creates the package.
Step 4: Create second class to include in package:
secondcl.java
Package mypack;
public class secondcl
{
public void sub(int a,int b)
{
int r=a-b;
System.out.println(“Subtraction:”+r);
}
}
ACCESSING THE PACKAGES
Step 5: Write program to import package
Import mypack.*;
class thirdcl
{
public static void main(String args[])
{
firstcl obj1 = new firstcl();
secondcl obj2= new secondcl();
obj1.add(10,5);
obj2.sub(10,5);
}
}
Chapter 4
1 . Describe types of Errors and Exceptions in details.
-error is a situation which prevents the normal execution of our program
-it is obvious responsibility of a programmer to deal with all kinds of errors.
-there are 3 types if errors.
-1.Syntax Error:-
-Syntax Errors refer to the grammatical errors while writing the code of program.
-Examples:-misspelled variables,missing semi colons
-2.Runtime Error:-
-Runtime errors occur when a program with no syntax errors asks the computer
to do something that the computer is unable to reliably do.
-These errors occur in the run mode of a program.
-Examples:-dividing by 0,opening a non-existing file.
-3.Logical Error:-
-Logical errors occur when there is something wrong in the logic of a program.
-Examples:-multiplying where you should divide.Opening wrong File.Display
wrong message.adding where you should subtract.

-An exception is an event, which occurs during the execution of a program that
interrupts the normal flow of the program.
-All the exceptions are usually handled by the java compiler.
-There are 2 types of exceptions:
1.Checked Exceptions:
-A checked exception occurs at the time of program compilation.
-Checked exception is also known as compile time exception.
2.Unchecked Exceptions:
-Unchecked exception are the exceptions that are not checked at compiled
time.that means it is not compulsory for the method to handle these exceptions.

2. Explain try, catch, throw ,throws and finally keyword with syntax.
--1.Try Keyword-
-Try is a block which we have to write the code in which the exceptions may
occur.
-A try block must be followed by a catch or finally block or both
Syntax:-
Try
{
Statements;
}
--2.Catch Keyword-
-This block is also known as exception handler.when as exception is thrown by
try block ,then the suitable matching catch block catches the exception and handles it.
-Syntax:-
Catch(Exception e)
{
Block of code to handle the error
}
--3.Throw Keyword-
-​The ​throw​ statement allows you to create a custom error
-The ​throw​ statement is used together with an ​exception type​. There
are many exception types available in Java: ​ArithmeticException​,
ClassNotFoundException​, ​ArrayIndexOutOfBoundsException​,
SecurityException​, etc.
-Syntax:-
throw​ ​new​ ArithmeticException(​"Error to print "​);
--4.Throws Keyword-
-​the ​throws​ keyword is used to declare that a method may throw one or some
exceptions. The caller has to catch the exceptions (catching is optional if the exceptions
are of type unchecked exceptions)​.
-​Syntax:-
void​ ​aMethod() ​throws​ ​Exception1, Exception2 {

​// statements...
​if​ ​(an exception occurs) {
​throw​ ​new​ ​Exception1();
}​

​// statements...
​if​ ​(another exception occurs) {
​throw​ ​new​ ​Exception2();
}​
}
--5.Final Keyword-
-​The ​finally​ statement lets you execute code, after ​try...catch​, regardless of
the result.
-Syntax:-
finally​ { //Statement:
}
3. Explain nested try and multiple catch with example.
--Nested Try:--
--​The try block within a try block is known as nested try block in java.
--Syntax:-
try

statement ​1​;

statement ​2​;

​try

statement ​1​;

statement ​2​;

​catch​(Exception e)

catch​(Exception e)

--multiple catch

-A try block can be followed by one or more catch blocks. Each catch block must contain a
different exception handler. So, if you have to perform different tasks at the occurrence of
different exceptions, use java multi-catch block.

--syntax

try​{

Statement:

​catch​(ArithmeticException e)

System.out.println(​"Arithmetic Exception occurs"​);


}

​catch​(ArrayIndexOutOfBoundsException e)

System.out.println(​"ArrayIndexOutOfBounds Exception occurs"​);

​catch​(Exception e)

System.out.println(​"Parent Exception occurs"​);

4.Explain life cycle of thread in detail.


-​A thread can be in one of the five states. According to sun, there is only 4 states in ​thread
life cycle in java​ new, runnable, non-runnable and terminated. There is no running state.
The life cycle of the thread in java is controlled by JVM. The java thread states are as
follows:

1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
1) New
The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.

2) Runnable
The thread is in runnable state after invocation of start() method, but the thread scheduler
has not selected it to be the running thread.

3) Running
The thread is in running state if the thread scheduler has selected it.

4) Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not eligible to run.

5) Terminated
A thread is in terminated or dead state when its run() method exits.

5. Explain thread priority in java with example.


Each thread have a priority. Priorities are represented by a number between 1 and 10. In
most cases, thread schedular schedules the threads according to their priority (known as
preemptive scheduling). But it is not guaranteed because it depends on JVM specification
that which scheduling it chooses.
3 constants defined in Thread class:

1. public static int MIN_PRIORITY


2. public static int NORM_PRIORITY
3. public static int MAX_PRIORITY

Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the


value of MAX_PRIORITY is 10.
##PROGRAM##
class A extends Thread
{
public void run()
{
System.out.println("Begining of Thread A");
int i;
for(i=1;i<=10;i++)
{
System.out.println("thread A:"+i);
}
System.out.println("End the Thread A");
}
}
class B extends Thread
{
public void run()
{
System.out.println("Begining of Thread B");
int i;
for(i=1;i<=10;i++)
{
System.out.println("Thread B:"+i);
}
System.out.println("End of Thread B");
}
}
class C extends Thread
{
public void run()
{
System.out.println("Beginning of Thread C");
int i;
for(i=1;i<=10;i++)
{
System.out.println("Thread c:"+i);
}
System.out.println("End of Thread C");
}
}
class Main
{
public static void main(String args[])
{
A a=new A();
B b=new B();
C c=new C();
c.setPriority(Thread.MAX_PRIORITY);
c.setPriority(Thread.MIN_PRIORITY);
c.setPriority(Thread.NORM_PRIORITY);
a.start();
b.start();
c.start();
}
}
6.How to create a thread with Runnable interface .Explain with example.

--java.lang.Runnable​ is an interface that is to be implemented by a class whose instances 


are intended to be executed by a thread. There are two ways to start a new Thread – 
Subclass ​Thread​ and implement R​ unnable​. There is no need of subclassing ​Thread​ when a 
task can be done by overriding only ​run()​ method of ​Runnable​. 
Steps to create a new T​ hread​ using ​Runnable​ : 
1.​ Create a R​ unnable​ implementer and implement r​ un()​ method. 
2.​ Instantiate ​Thread​ class and pass the implementer to the T​ hread​, ​Thread​ has a 
constructor which accepts R​ unnable​ instance. 
3.​ Invoke s​ tart()​ of T​ hread​ instance, start internally calls ​run()​ of the implementer. 
Invoking s​ tart()​, creates a new ​Thread​ which executes the code written in ​run()​. 
Calling ​run()​ directly doesn’t create and start a new ​Thread​, it will run in the same thread. 
To start a new line of execution, call ​start()​ on the thread. 
Example:- 
public​ ​class​ RunnableDemo { 
  
​public​ s
​ tatic​ v
​ oid​ main(String[] args) 

System.out.println(​"Main thread is- " 
+ Thread.currentThread().getName()); 
Thread t1 = ​new​ Thread(​new​ RunnableDemo().​new​ RunnableImpl()); 
t1.start(); 

  
​private​ c​ lass​ RunnableImpl ​implements​ Runnable { 
  
​public​ ​void​ run() 

System.out.println(Thread.currentThread().getName() 
+ ​", executing run() method!"​); 



 

7 . Explain Synchronization.
--​Synchronization in java is the capability ​to control the access of multiple threads to any
shared resource.​
Java Synchronization is better option where we want to allow only one thread to access the

shared resource.

Why use Synchronization


The synchronization is mainly used to

1. To prevent thread interference.


2. To prevent consistency problem.

###Program Without Synchronization###

1. class​ Table{
2. void​ printTable(​int​ n){​//method not synchronized
3. ​for​(​int​ i=​1​;i<=​5;​ i++){
4. System.out.println(n*i);
5. ​try​{
6. Thread.sleep(​400​);
7. }​catch​(Exception e){System.out.println(e);}
8. }
9.
10. }
11. }
12.
13. class​ MyThread1 ​extends​ Thread{
14. Table t;
15. MyThread1(Table t){
16. this​.t=t;
17. }
18. public​ ​void​ run(){
19. t.printTable(​5​);
20. }
21.
22. }
23. class​ MyThread2 ​extends​ Thread{
24. Table t;
25. MyThread2(Table t){
26. this​.t=t;
27. }
28. public​ ​void​ run(){
29. t.printTable(​100​);
30. }
31. }
32.
33. class​ TestSynchronization1{
34. public​ ​static​ ​void​ main(String args[]){
35. Table obj = ​new​ Table();​//only one object
36. MyThread1 t1=​new​ MyThread1(obj);
37. MyThread2 t2=​new​ MyThread2(obj);
38. t1.start();
39. t2.start();
40. }
41. }
42. class​ Table{
43. void​ printTable(​int​ n){​//method not synchronized
44. ​for​(​int​ i=​1​;i<=​5;​ i++){
45. System.out.println(n*i);
46. ​try​{
47. Thread.sleep(​400​);
48. }​catch​(Exception e){System.out.println(e);}
49. }
50.
51. }
52. }
53.
54. class​ MyThread1 ​extends​ Thread{
55. Table t;
56. MyThread1(Table t){
57. this​.t=t;
58. }
59. public​ ​void​ run(){
60. t.printTable(​5​);
61. }
62.
63. }
64. class​ MyThread2 ​extends​ Thread{
65. Table t;
66. MyThread2(Table t){
67. this​.t=t;
68. }
69. public​ ​void​ run(){
70. t.printTable(​100​);
71. }
72. }
73.
74. class​ TestSynchronization1{
75. public​ ​static​ ​void​ main(String args[]){
76. Table obj = ​new​ Table();​//only one object
77. MyThread1 t1=​new​ MyThread1(obj);
78. MyThread2 t2=​new​ MyThread2(obj);
79. t1.start();
80. t2.start();
81. }
82. }

Output: 5

100

10

200

15

300

20

400

25
500

###Program synchronized ###

1. //example of java synchronized method


2. class​ Table{
3. ​synchronized​ ​void​ printTable(​int​ n){​//synchronized method
4. ​for​(​int​ i=​1​;i<=​5;​ i++){
5. System.out.println(n*i);
6. ​try​{
7. Thread.sleep(​400​);
8. }​catch​(Exception e){System.out.println(e);}
9. }
10.
11. }
12. }
13.
14. class​ MyThread1 ​extends​ Thread{
15. Table t;
16. MyThread1(Table t){
17. this​.t=t;
18. }
19. public​ ​void​ run(){
20. t.printTable(​5​);
21. }
22.
23. }
24. class​ MyThread2 ​extends​ Thread{
25. Table t;
26. MyThread2(Table t){
27. this​.t=t;
28. }
29. public​ ​void​ run(){
30. t.printTable(​100​);
31. }
32. }
33.
34. public​ ​class​ TestSynchronization2{
35. public​ ​static​ ​void​ main(String args[]){
36. Table obj = ​new​ Table();​//only one object
37. MyThread1 t1=​new​ MyThread1(obj);
38. MyThread2 t2=​new​ MyThread2(obj);
39. t1.start();
40. t2.start();
41. }
42. }

Output: 5

10

15

20

25

100

200

300

400

500
Chapter 5
1.​ ​Differentiate between Java Application and Java Applet (any 4 points) .
2.​ ​Describe all attributes of <applet> tag.
3.​ ​Explain life cycle of APPLET
4.​ ​How to draw lines,rectangle,circle arc and polygon in java with syntax(Use specific
method for this from Graphics class).
5.​ ​Explain setColor(),getColor(),setForeground(),setBackground() methods of Color
class.
6.​ ​Explain Font class with its methods.
Chapter 6

1. Give any two methods from File class with their usage.
2. Write a program to copy content of one file into another file
3. Enlist types of stream classes and describe methods for reading and writing data for
each type
4. Write a program to count number of words from a text file using stream classes.
5. Write any two methods from Character Stream classes.

NOTE: Prepare all program based on Exception handling, multithreading ,Applet


,Interface and File Handling

You might also like