0% found this document useful (0 votes)
3 views

javaans(2)

The document outlines various features of Java, including data abstraction, encapsulation, inheritance, and polymorphism. It also discusses Java's exception handling, access specifiers, and provides examples of thread creation and vector manipulation. Additionally, it defines exceptions, lists built-in exceptions, and includes code snippets for checking prime numbers and handling custom exceptions.
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)
3 views

javaans(2)

The document outlines various features of Java, including data abstraction, encapsulation, inheritance, and polymorphism. It also discusses Java's exception handling, access specifiers, and provides examples of thread creation and vector manipulation. Additionally, it defines exceptions, lists built-in exceptions, and includes code snippets for checking prime numbers and handling custom exceptions.
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
You are on page 1/ 11

2 Marks

a] List any eight features of Java.


Features of Java:
1. Data Abstraction and Encapsulation
2. Inheritance
3. Polymorphism
4. Platform independence
5. Portability
6. Robust
7. Supports multithreading
8. Supports distributed applications
9. Secure
10. Architectural neutral
11. Dynamic

B] Name the wrapper class methods for the following:


(i) To convert string objects to primitive int.
(ii) To convert primitive int to string objects.
(i) To convert string objects to primitive int:
String str=”5”;
int value = Integer.parseInt(str);
(ii) To convert primitive int to string objects:
int value=5;
String str=Integer.toString(value);
c] Write the syntax of try-catch-finally blocks.
try{
//Statements to be monitored for any exception
} catch(ThrowableInstance1 obj) {
//Statements to execute if this type of exception occurs
} catch(ThrowableInstance2 obj2) {
//Statements
}finally{
//Statements which should be executed even if any exception happens
}

d] Differentiate between method overloading and method overriding.

e]List any 4 java API packages(any 4)


1.java.lang
2.java.util
3.java.io
4.java.awt
5.java.net
6.ava.applet

f]Differentiate between class and interfaces(any 4)

g]Give the syntax and example for the following functions


i]min() ii] sqrt()

h]List the types of inheritance which is supported by java


i]Explain elementAt() and addElement() methods
elementAT( ):
The elementAt() method of Java Vector class is used to get the element at the
specified index in the vector. Or The elementAt() method returns an element
at the specified index.
addElement( ):
The addElement() method of Java Vector class is used to add the specified
element to the
end of this vector. Adding an element increases the vector size by one

4 marks
a] Explain any 4 access specifiers in java
There are 4 types of java access modifiers:
1. private 2. default 3. Protected 4. public
1) private access modifier: The private access modifier is accessible
only within class.
2) default access specifier: If you don’t specify any access control
specifier, it is default, i.e. it becomes implicit public and it is
accessible within the program.
3) protected access specifier: The protected access specifier is
accessible within package and outside the package but through
inheritance only.
4) public access specifier: The public access specifier is accessible
everywhere. It has the widest scope among all other modifiers

b] Write a program to create two threads. One thread will display the numbers from 1 to 50
(ascending order) and other thread will display numbers from 50 to 1 (descending order).

class Ascending extends Thread


{
public void run()
{
for(int i=1; i<=15;i++)
{
System.out.println("Ascending Thread : " + i);
}
}
}
class Descending extends Thread
{
public void run()
{
for(int i=15; i>0;i--) {
System.out.println("Descending Thread : " + i);
}
}
}
public class AscendingDescending Thread
{
public static void main(String[] args)
{
Ascending a=new Ascending();
a.start();
Descending d=new Descending();
d.start();
}
}
C] Differentiate between string and string buffer
D] Define Exception. State built-in exceptions
An exception is a problem that arises during the execution of a
program.
Java exception handling is used to handle error conditions in a
program systematically by taking the necessary action
Built-in exceptions:
• Arithmetic exception: Arithmetic error such as division by
zero.
• ArrayIndexOutOfBounds Exception: Array index is out
of bound
• ClassNotFoundException
• FileNotFoundException: Caused by an attempt to access
a nonexistent file.
• IO Exception: Caused by general I/O failures, such as
inability to read from a file.
• NullPointerException: Caused by referencing a null object.
• NumberFormatException: Caused when a conversion
between strings and number fails.
• StringIndexOutOfBoundsException: Caused when a
program attempts to access a nonexistent character position
in a string.
• OutOfMemoryException: Caused when there’s not
enough memory to allocate a new object.
• SecurityException: Caused when an applet tries to perform
an action not allowed by the browser’s security setting.
• StackOverflowException: Caused when the system runs out
of stack space.

e) Describe Life cycle of thread with suitable diagram


f] Write a program to create a vector with five elements as (5, 15, 25, 35, 45). Insert new element at
2nd position. Remove 1st and 4th element from vector.
import java.util.*;
class VectorDemo
{
public static void main(String[] args)
{
Vector v = new Vector();
v.addElement(new Integer(5));
v.addElement(new Integer(15));
v.addElement(new Integer(25));
v.addElement(new Integer(35));
v.addElement(new Integer(45));
System.out.println("Original array elements are
");
for(int i=0;i<v.size();i++)
{
System.out.println(v.elementAt(i));
}
v.insertElementAt(new Integer(20),1); // insert
new element at 2nd position
v.removeElementAt(0);
//remove first element
v.removeElementAt(3);
//remove fourth element
System.out.println("Array elements after insert
and remove operation ");
for(int i=0;i<v.size();i++)
{
System.out.println(v.elementAt(i));
}}}

G] Write a program to check whether the given number is prime or not


8)Define exception called ‘No Match Exception’ that is thrown when the password
accepted is not equal to ‘MSBTE’. Write the program

class PrimeExample
{
public static void main(String args[]){
int i,m=0,flag=0;
int n=7;//it is the number to be checked
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
}
}
if(flag==0) { System.out.println(n+" is prime number"); }
}//end of else
}
}
Output:
7 is prime number

You might also like