IP IMPORTANT QUESTIONS
IP IMPORTANT QUESTIONS
1.Explain java thread models with eg? Or what are stages of thread and
types? * 13M
New (Ready to run) A thread is in New when it gets CPU time.
Running. A thread is in a Running state when it is under execution.
Suspended. A thread is in the Suspended state when it is temporarily inactive or
under execution.
Blocked.
Terminated.
Thread Class
A Thread class has several methods and constructors which allow us to
perform various operations on a thread. The Thread class extends
the Object class. The Object class
There are two ways to create a thread:
implements the Runnable interface. The thread class has the following
constructors that are used to perform various operations.
o Thread()
o Thread(Runnable, String name)
o Thread(Runnable target)
o Thread(ThreadGroup group, Runnable target, String name)
o Thread(ThreadGroup group, Runnable target)
o Thread(ThreadGroup group, String name)
o Thread(ThreadGroup group, Runnable target, String name,
long stackSize)
start() method
The method is used for starting a thread that we have newly created. It
starts a new thread with a new callstack. After executing the start() method,
the thread changes the state from New to Runnable. It executes the run()
method when the thread gets the correct time to execute it.
ThreadExample.java
Output:
- Java applets are used to provide interactive features to web applications and can be
executed by browsers for many platforms. They are small, portable Java programs
embedded in HTML pages and can run automatically when the pages are viewed.
Malware authors have used Java applets as a vehicle for attack.
- A Flash applet is a small application that runs inside the Flash web-browser plugin.
- Applet is mostly used in games and animation. For this purpose image is
required to be displayed. The java.awt.Graphics class provide a method
drawImage() to display the image.
local applet
remote applets
Example
import java.awt.*;
import java.applet.*;
myapplet.html :
<html>
<body>
<applet code="DisplayImage.class" width="300" height="300">
</applet>
</body>
</html>
3.Explain the concepts of inner class and its types? Or nested class 13M
Java inner class or nested class is a class that is declared inside the class
or interface.
We use inner classes to logically group classes and interfaces in one place to
be more readable and maintainable.
Additionally, it can access all the members of the outer class, including
private data members and methods.
Syntax of Inner class
1. class Java_Outer_class{
2. //code
3. class Java_Inner_class{
4. //code
5. }
6. }
If all the class objects are a part of the outer object then it is easier to nest
that class inside the outer class. That way all the outer class can access all
the objects of the inner class.
Type Description
EXAMPLE:
class CPU {
double price;
// nested class
class Processor{
double getCache(){
return 4.3;
}
}
double getClockSpeed(){
return 5.5;
}
}
}
Output:
The applet life cycle can be defined as the process of how the object is
created, started, stopped, and destroyed during the entire execution of its
application. It basically has five core methods namely init(), start(), stop(),
paint() and destroy().These methods are invoked by the browser to execute.
Along with the browser, the applet also works on the client side, thus having
less processing time.
o init(): The init() method is the first method to run that initializes the applet. It
can be invoked only once at the time of initialization. The web browser
creates the initialized objects, i.e., the web browser (after checking the
security settings) runs the init() method within the applet.
o start(): The start() method contains the actual code of the applet and starts
the applet. It is invoked immediately after the init() method is invoked. Every
time the browser is loaded or refreshed, the start() method is invoked. It is
also invoked whenever the applet is maximized, restored, or moving from one
tab to another in the browser. It is in an inactive state until the init() method
is invoked.
o stop(): The stop() method stops the execution of the applet. The stop ()
method is invoked whenever the applet is stopped, minimized, or moving
from one tab to another in the browser, the stop() method is invoked. When
we go back to that page, the start() method is invoked again.
o destroy(): The destroy() method destroys the applet after its work is done. It
is invoked when the applet window is closed or when the tab containing the
webpage is closed. It removes the applet object from memory and is
executed only once. We cannot start the applet once it is destroyed.
o paint(): The paint() method belongs to the Graphics class in Java. It is used
to draw shapes like circle, square, trapezium, etc., in the applet. It is
executed after the start() method and when the browser or applet windows
are resized.
1. init()
2. start()
3. paint()
1. stop()
2. destroy()
Applet Life Cycle Working
o The Java plug-in software is responsible for managing the life cycle of an
applet.
o An applet is a Java application executed in any web browser and works on the
client-side. It doesn't have the main() method because it runs in the browser.
It is thus created to be placed on an HTML page.
o The init(), start(), stop() and destroy() methods belongs to
the applet.Applet class.
o The paint() method belongs to the awt.Component class.
o In Java, if we want to make a class an Applet class, we need to extend
the Applet
o Whenever we create an applet, we are creating the instance of the existing
Applet class. And thus, we can use all the methods of that class.
import java.applet.*;
import java.awt.*;
/*<applet code ="Smiley" width=600 height=600>
</applet>*/
Output:
7. Explain types of constructor with eg? 8M
Constructors in Java
In Java, a constructor is a block of codes similar to the method. It is called
when an instance of the class is created. At the time of calling constructor,
memory for the object is allocated in the memory.
Every time an object is created using the new() keyword, at least one
constructor is called.
Default Constructor
A constructor is called "Default Constructor" when it doesn't have any
parameter.
<class_name>(){}
Output:
Bike is created
Parameterized Constructor
A constructor which has a specific number of parameters is called a
parameterized constructor.
INHERITANCE:
class A extends B
{
}
The concept of parent and child class in Inheritance:
Child Class: The class that extends the features of another class is
known as child class, sub class or derived class. In the above code,
class A is the child class.
Parent Class: The class that shares the fields and methods with the
child class is known as parent class, super class or Base class. In the
above code, Class B is the parent class.
Advantages of Inheritance
Inheritance removes redundancy from the code.
Inheritance allows us to reuse of code, it improves
reusability in your java application.
Reduces code size: By removing redundant code, it
reduces the number of lines of the code.
Improves logical structure: It allows programmer to
visualize the relationship between different classes.
Syntax:
To inherit a class we use extends keyword. Here, class XYZ is a child
class and class ABC is a parent class. The class XYZ is inheriting the
properties and methods of ABC class.
Inheritance Example
In this example, we have a base class Teacher and a sub
class PhysicsTeacher. Child class inherits the following fields and
methods from parent class:
It’s pretty clear with the diagram that in Multilevel inheritance there is
a concept of grand parent class. If we take the example of this
diagram, then class C inherits class B and class B inherits class A which
means B is a parent class of C and A is a parent class of B. So in this
case class C is implicitly inheriting the properties and methods of class
A along with class B that’s what is called multilevel inheritance.
class Car{
public Car()
{
System.out.println("Class Car");
}
public void vehicleType()
{
System.out.println("Vehicle Type: Car");
}
}
class Maruti extends Car{
public Maruti()
{
System.out.println("Class Maruti");
}
public void brand()
{
System.out.println("Brand: Maruti");
}
public void speed()
{
System.out.println("Max: 90Kmph");
}
}
public class Maruti800 extends Maruti{
public Maruti800()
{
System.out.println("Maruti Model: 800");
}
public void speed()
{
System.out.println("Max: 80Kmph");
}
public static void main(String args[])
{
Maruti800 obj=new Maruti800();
obj.vehicleType();
obj.brand();
obj.speed();
}
}
Output:
Class Car
Class Maruti
Maruti Model: 800
Vehicle Type: Car
Brand: Maruti
Max: 80Kmph
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
8. statement 8;
9. statement 9;
10.statement 10;
1. Checked Exception
2. Unchecked Exception
3. Error
1) Checked Exception
2) Unchecked Exception
3) Error
JavaExceptionExample.java
Output:
10.Explain the use of buffered reader object to read characters from the
console using a pgm? 8M
The BufferedReader class of Java is used to read the stream of characters
from the specified source (character-input stream). The constructor of this
class accepts an InputStream object as a parameter.
This class provides a method named read() and readLine() which reads
and returns the character and next line from the source (respectively) and
returns them.
Instantiate an InputStreamReader class bypassing your
InputStream object as a parameter.
Then, create a BufferedReader, bypassing the above obtained
InputStreamReader object as a parameter.
Now, read data from the current reader as String using the
readLine() or read() method.
Example
The following Java program demonstrates how to read integer data from the
user using the BufferedReader class.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Employee{
String name;
int id;
int age;
Employee(String name, int age, int id){
this.name = name;
this.age = age;
this.id = id;
}
public void displayDetails(){
System.out.println("Name: "+this.name);
System.out.println("Age: "+this.age);
System.out.println("Id: "+this.id);
}
}
public class ReadData {
public static void main(String args[]) throws IOException {
BufferedReader reader =new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter your name: ");
String name = reader.readLine();
System.out.println("Enter your age: ");
int age = Integer.parseInt(reader.readLine());
System.out.println("Enter your Id: ");
int id = Integer.parseInt(reader.readLine());
Employee std = new Employee(name, age, id);
std.displayDetails();
}
}
Output
Enter your name:
Krishna
Enter your age:
25
Enter your Id:
1233
Name: Krishna
Age: 25
Id: 1233
Interface in Java
An interface in Java is a blueprint of a class. It has static constants
and abstract methods
In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body.
To declare an interface
An interface is declared by using the interface keyword. It provides total
abstraction; means all the methods in an interface are declared with the
empty body, and all the fields are public, static and final by default. A class
that implements an interface must implement all the methods declared in
the interface.
Syntax:
interface <interface_name>{
TestInterfaceDefault.java
1. interface Drawable{
2. void draw();
3. default void msg(){System.out.println("default method");}
4. }
5. class Rectangle implements Drawable{
6. public void draw(){System.out.println("drawing rectangle");}
7. }
8. class TestInterfaceDefault{
9. public static void main(String args[]){
10.Drawable d=new Rectangle();
11. d.draw();
12.d.msg();
13. }}
Output:
drawing rectangle
default method
interface Drawable{
1. void draw();
2. static int cube(int x){return x*x*x;}
3. }
4. class Rectangle implements Drawable{
5. public void draw(){System.out.println("drawing rectangle");}
6. }
7.
8. class TestInterfaceStatic{
9. public static void main(String args[]){
10. Drawable d=new Rectangle();
11.d.draw();
12. System.out.println(Drawable.cube(3));
13.}}
Output:
drawing rectangle
27
Implements interface:
To declare a class that implements an interface, you include an implements clause in the class
declaration. Your class can implement more than one interface, so the implements keyword is
followed by a comma-separated list of the interfaces implemented by the class.
Example:
import java.io.*;
// A simple interface
interface In1 {
// Driver Code
public static void main(String[] args)
{
TestClass t = new TestClass();
t.display();
System.out.println(a);
}
}
Output
Geek
10
UNIT – 2