Java Programming
Java Programming
SECTION A SECTION B
UNIT CHAPTER
2 MARKS 10 MARKS
Introduction to JAVA 1 -
Inheritance - 1
II
Arrays, Strings and Vectors 1 1
Wrapper Class - -
Interfaces 1 1
III Packages 1 1
Multithreaded Programming: 1 2
Managing Exceptions 1 1
IV
Applet Programming 1 2
TOTAL 12 16
TOTAL MARKS 20 50
SECTION – A ( 2 Marks)
UNIT-I
[Nov / Dec 2015]
1. What do you mean by command line argument?
2. What are the two ways of giving values to the variable?
3. Write down the default values of byte and char data types?
SECTION – A ( 2 Marks)
UNIT-II
[Nov / Dec 2015]
SECTION – A ( 2 Marks)
UNIT-IV
[Nov / Dec 2015]
1. What is exception?
SECTION – A ( 2 Marks)
UNIT-V
[ Nov / Dec 2015 ]
1. What is the difference between character oriented and byte oriented streams?
2. What is stream? How stream are classified?
3. What is the use of Graphic class?
SECTION – B( 5Marks)
UNIT-II
[Nov / Dec 2015]
1. Differentiate between string and string buffer.
2. What is vector? Mention its advantages over an array.
3. How string class different from string buffer class? Give two methods of string class.
4. What is method overriding? Write a program to demonstrate method overriding.
5. Explain any seven string methods with example.
6. Write a note on inheritance.
SECTION – B( 5 Marks)
UNIT-III
[Nov / Dec 2015]
1. What is package? Write down the steps for creating user defined package.
2. What is thread ?explain thread cycle with neat diagram.
3. What is interface? Write a program to demonstrate interface.
SECTION – B( 5 Marks)
UNIT-IV
SECTION – B( 5 Marks)
UNIT-V
2. Write down the default values of byte and char data types?
The default value of byte=0
The default value of char=NULL CHARACTER
8. What is exception?
Ans. An exception is an error that occurs at run time.
Ans.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloCLASSApplet extends Applet
{
public void paint(Graphics g){
g.drawString("Hello CLASS", 50, 50);
}
}
Ans. Java is one of the simple programming language because java removes some
complex concept like pointer and execution time will be less.
Reasons:-
Much easier to write bug free code.
Java has considerably more functionality than c.
statement. Ans.
switch(expression)
{
Case value1:
………. break;
default:
}
Ans. Errors are related to errors that occur in the java virtual machine itself, and not in
a program. These types of exceptions are beyond our control, and a program will not
handle them.An exception is an error which can be handled .An error is an error which
cannot be handled.
30. What are the default values of float and char primitive data types in java?
Ans.
Default value of float =0.0f
Default value of char = NULL CHARACTER
Ans. Labeled break: the break statement breaks out the closest loop or switch
statement.
Ex: for(int i=0;i<10;i++){
while(true){
break;
}
}
A constructor name should be always The method and class name can be same
same as class name or different
Ans.
CurrentThread()
getName()
run()
sleep()
java? Ans.
Private
Default
Protected
Public
42. Define a stream in java. Briefly mention the broad classification of java stream classes?
Ans. A stream can be defined as a sequence of data. The input stream is used to read data
from a source and the Output Stream is used for writing data to a destination.
It is slow and consumes more memory It is fast and consumes less memory
Declaration:
class staticdemo{
int x,y;
static int z;
}
3. Explain any three string methods with example?
concat(): This method creates a new string by appending the contents of string
object passed as arguments to the contents of string on which the method is
invoked.
Example:
publicString concat(String str)
String str=”Skyward”;
System.out.println(str.concat(“Publishers”)); //”Skyward Publishers” is printed.
replace(): This method creates a new string using the same contents as that of the
string object on which the method is invoked.
Example:
public String replace(char old,char new)
String original=”Java ProgrAmming”;
Public: The public modifiers is the least restrictive of all access modifiers. It can apply it
to a class, its methods and its member variables. A public class can be instantiated
without any restrictions.
EXAMPLE:
package pack;
public class A{
public void msg(){System.out.println(“Hello”);}
}
//save by B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
Obj.msg();
}
}
6. What is Interface? Explain with an example how a class implements an interface.
Ans: An interface is a description of a set of abstract methods that is supported to be
implemented by the classes. In an interface no method can include a body. It specifies
what can be done, but no implementation. Once an interface is defined any number of
classes can be implemented. The interface can be defined using the interface keyword.
Syntax:
interface interface_name {
public static varibles
public abstract methods
}
Ans: Java provides us facility to create our own exceptions which are basically derived
classes of exception. Things to remember before writing an exception
All exceptions must be a child of Throwable
To write a runtime exception, extend the RuntimeException class.
Ans: Start(): When the start() method is called, the thread enters in a ready- to- run
state. This thread is now in the pool of threads ready for the execution. Sometimes
Running: When the thread enters in this state, the JVM starts to execute the thread
run() method. The thread remains in this state and keep running until it is either
swapped out by thread scheduler or it voluntarily give up its turn for some reasons.
Not-ready-to-run (Blocked state): A thread moves out of the running state when it is
waiting for something to happen.
Sleeping: We may want a thread to do-nothing for some time. We can call
Thread.sleep() method in the thread run() method. This method tells the currently
running thread to sleep for some period of time.
Waiting: Sometimes a thread might wait(), just because we have asked it to wait in its
run method. In that case,the thread changes its state from running to waiting .
Blocked: Sometimes a thread needs to wait for a resource while running. For
instance ,if it is reading from a network resources in its run method, it has to wait until
that resource becomes available.
Dead state: A java thread enters this state when it has finished the execution of its run
method. We cannot start the thread once it is dead. The thread can be started only once
in its life time. If we re-invoke start() on a thread which is dead,it does not start again.
Using isAlive() method we can test whether the thread is alive or dead.
not-ready-to-run
[waiting/blocked/sleep
Ans: Applet can get different input from the HTML file that contains the
<APPLET>tag through the use of applet parameter. To set up and handle parameters
in the applet , we need two things:
1. A special parameter tag in the HTML file.
2. Code in our applet to read those
parameters. Example:
import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
public class MyFontApplet extends Applet {
String fontName;
int fontSize;
public void init() {
fontName = getParameter(“font”);
fontSize = Integer.parseInt(getParameter(“size”));
}
Public void paint (Graphics g) {
Font f = new Font(fontName and fontSize);
g.setFont(f);
g.drawString(“Skyward Publishers”,50,50);
}
}
11. Explain any seven methods of graphics class with an example for
each? Ans:
METHODS DESCRIPTION
draw3Drect() Draws a 3-D rectangle
drawArc() Draws an arc
drawLine() Draws a line
drawOval() Draws an oval
fillArc() Draws a filled arc
fillOval() Draws a filled oval
fillRect Draws a filled rectangle
Example:
File f = new File(“myFile.dat”);
FileOutputStream fos;
try {
fos = new FileOutputStream(f);
}catch(IOException) {
}
13. Explain with example:
Method overloading: Method Overloading means to have two or more methods with
same name in the same class with different arguments.
Example:
class Myclass {
public void getAmount (int rate) {….}
public void getAmount (int rate, long principal) {….}
}
Method overriding: Method overriding occurs when sub class declares a method that
has the same type arguments as a method declared by one of its super class.
Example:
class Baseclass {
public void getAmount (int rate) {….}
}
class Myclass extends Baseclass{
public void getAmount (int rate) {….}
}
Abstract method: A method without body is known as abstract method. A method must
always be declared in an abstract class.
Example:
abstract void method1(); // Abstract method
Input stream
Input stream is an abstract class that provides the framework from which all
the other input streams are derived
We cannot create an instance of inputstream class as it is abstract class .
Whenever we want to read the data in bytes format, then we use the input
stream classes
The inputsream class contains lot of methods for reading bytes , closing stream
,skipping part of data in the streams. finding the number of bytes present in the
input data ,etc.
Output stream
Output stream is an abstract class that provides the framework from which all
other output streams are derived
We cannot create an instance of outputstream class as it is abstract class
Whenever we want to write data in byte format, then we use the output stream
classes.
The outputstream class contains lot of methods for writing bytes, closing streams
,etc.
}
start( ): start( ) is automatically called to begin the execution of the applet. This
method is called each time the applet is revisited by the user.
Syntax:
public void start( )
{
}
stop( ): This method is called by the browser when an applet is to stopped.
Syntax:
public void stop( )
{
}
destroy( ): this method is called when an applet is being terminated from the
memory. The stop( ) method will always be called before destroy( ). The
code related to releasing memory allocated to the applet should be done in
this method.
Syntax:
public void destroy( )
{
}
17. Difference between JDK and JRE?
JDK JRE
JDK needs more disk space as it JRE is smaller than JDK so it needs less disk
contains JRE along with various
Operands
Operand
Right Operand .
Overloading
1. Signature has to be different just a difference in return type is not enough.
2. Any access modifier can be used.
3. The methods exception list may vary freely
4. The method to be called will be decided at the time of compilation.
5. Methods can be static or non-static.
23) How to create objects? What happens when you create objects?
An object is created by instantiating a class. The process of creating an object of a class
is called as instantiation and created object is called as an instance.
To create a new object, java uses the new keyword
The object are created using the new operator with the name of the class we
want to create an instance of, then parentheses after that. The general form of
creating an object is
<classname><reference-
variable>=new<classname>([arguments])
Where Account is a class name, acc is reference variable and new is the
operator to create an object. The Account object is created in the heap
memory. The address of that object is assigned to the reference variable acc.
The reference variable is declared in the stack.
class Test
{
int a;
int b;
Test(int a, int b)
{
this.a = a;
this.b = b;
}
void display()
{
System.out.println("a = " + a + "b = " + b +);
}
Logical operators return a true or false value based on the state of the variables.
&& (Short circuit If both the operands are non zero then ( A&&B ) is false
AND operator) the condition becomes true.
| (OR operator) If any of the two operands are non zero ( A|B ) is true
then the condition becomes true
|| (Short circuit OR If any of the two operands are non zero ( A||B ) is true
operator) then the condition becomes true.
^ (XOR operator) This return true only if its operand. If ( A^B ) is true
its operand are different otherwise
false.
! (NOT operator) Use to reverses the logical state of its !( A&&B ) is true
operand. If a condition is true then
logical NOT operator make false.
27. Illustrate array declaration and accessing data elements using an example.
Array is collection of elements of same type. The array stores a fixed-size sequential
collection of elements of the same type.
Declaring an array
The array declaration is usually the data type followed by a pair of square brackets
followed by the name of the array
Datatype[ ] arrayName;
Method
A method is used for any general purpose tasks like calculations.
The method name and class name can be same or different.
A method can be called after creating the object.
A method can be called any number of times on the object
A method is executed only when we want it.
29) Explain try and catch with an example.
Ans. The core of exception handling is try and catch . these keywords works together.
we cannot have a try without a try.
try
{
K=i/j;
}
catch (ArithmeticException e)
{
System.out.println(“exception occurred: divition by zero”);
K=i/(j+2);
}
System.out.println(“the value of k is :” + k);
}
}
Component.getGraphics();
c. This method is defined in component and returns a graphics object. Therefore,
any class that is a subclass of component can be drawn upon. The functionality
of the graphics class can be accessed even though it is abstract class.
31) Write program to sort a list of elements in ascending order.
class sorting
{
public static void main(String args[])
{
int a[ ] = new int[5];
System.out.println(“ enter five elements \n”);
for( int i=0;i<5;i++)
a[i]=Integer.parseInt(args[i]);
System.out.println(“\n before sorting \n”);
for( int i=0;i<5;i++)
System.out.print(“ “ +a[i]);
bubbleSort(a,5);
System.out.println(“\n\n after sorting \n”);
System.out.println(“ Ascending order \n”);
for( int i=0;i<5;i++)
System.out.print(“ “ +a[i]);
}
}
Here, pkg is the name of the package. for example, the following statement
creates a package called project1.
package pack1;
Java uses the file system to manage packages, with each package stored in its
own directory. for example, the class files for any classes we declare to be part of
pack1 must be stored in directory called pack1.
Step1: create a program which is part of package called pack1
pack2; Import
33) Explain steps of executing an applet using simple code.
pack1.*;;
Ans.
PublicWriting
Step1: class packageDemo
applet code
{
Create
Publicthe applet
static voidprogram called
main(String MyFirstApplet
args[ ]) as shown
{
packageExample obj=new
packageExample(); obj.packageMethod();
}
}
<HTML>
<HEAD>
Step4: Executing an applet using appletviewer
<TITLE> this is my first applet </TITLE>
There are two ways in which we can run an applet: inside a browser or with a
<BODY>
<appletspecial development tool that
code=MyFirstApplet.class displays
width= applets. The tool provided with the
400 height=500>
standard java JDK is called appletviewer.
</Applet>
The appletviewer is much easier to use during development.
</BODY>
Executing an applet using appletviewer
<HEAD>
</HTML>
Appletviewer appletExample.html