Professional Documents
Culture Documents
QB Java
QB Java
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:
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);
}
}
-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)
catch(ArrayIndexOutOfBoundsException e)
catch(Exception e)
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.
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.
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
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.