You are on page 1of 9

1. State and explain any four features of java?

➢ Java is an object-oriented language: - It follows all the principles of object-oriented programming namely
inheritance, polymorphism and abstraction. Multiple inheritance is possible with the concept of interface
Java is both compiled and interpreted: - Most of the programming languages either uses a compiler or an
interpreter. Java programs are to be compiled to get an intermediate byte code (a .class file) and then
interpreted making it more secure and platform independent
Architecture-neutral: There is no implementation dependent features e.g., size of primitive types is fixed
Platform independent and Portable: java byte code can be carried to any platform
2. Define a class item having data member code and price. Accept data for one object and display it.
➢ import java.io.*;
class Item_details
{
int code;
float price;
Item_details()
{
code=0;
price=0;
}
Item_details(int e,float p)
{
code=e;
price=p;
}
void putdata()
{
System.out.println("Code of item :"+code);
System.out.println("Price of item:"+price);
}
public static void main(String args[]) throws IOException
{
intco;floatpr;
BufferedReaderbr=new BufferedReader (new
InputStreamReader(System.in));
System.out.println("enter code and price of item");
co=Integer.parseInt(br.readLine());
pr=Float.parseFloat(br.readLine());
Item_detailsobj=new Item_details(co,pr);
obj.putdata();
}
}
3. What is type casting? Explain and its types with proper syntax and example.
➢ Assigning a value of one type to a variable of another type is known as Type Casting. There are 2 types of
type casting a) Widening or Implicit type casting b) Narrowing or Explicit type casting

Implicitly Type casting take place when


a) The two types are compatible
b) The target type is larger than the source type
Program:
4. In what ways does a switch statement differ from a if statement.

5. What is the use of new operator? Is it necessary to be used whenever object of the class is created? Why?
➢ Use:
a) new operator is used to dynamically allocate memory to the object of the class. It is the operator which
is used to create usable instance of the class. It is generally used along with the constructor of the class
so as to get memory allocated to the object.
b) It is necessary to use new operator whenever an object requires memory allocation after creation.
Otherwise object in the form of reference is created which will point to Null, i.e. with no allocated space
in memory.
6. Write a program to generate Fibonacci series 1 1 2 3 5 8 13 21 34 55 89.
➢ import java.io.*;
class fibonacci {
static int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
public static void main(String args[])
{
int n = 9;
System.out.println(fib(n));
}
)
7. Write a java program to display all the odd numbers between 1 to 30 using for loop.
➢ class OddNum
{
public static void main(String args[])
{
for(int i=1;i<=30;i++)
{
if(i%2 ==1)
{
System.out.print("Odd number :"+i +"\n");
}
}
}
}
8. Write a java program to display
1
12
123
1234
12345
public class Series_12
{
public static void main(String[] args)
{
int size=5;
for(int i=1;i<=size;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(j);
}
System.out.println("");
}
}
}
9. Write a program to check whether an entered number is prime or not?
➢ import java.io.*;
class PrimeNo
{
public static void main(String args[]) throws IOException
{
BufferedReader bin=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter number: ");
int num=Integer.parseInt(bin.readLine());
int flag=0;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
System.out.println(num + " is not a prime number");
flag=1;
break;
}
}
if(flag==0)
System.out.println(num + " is a prime number");
}
}
10. Write all primitive data types available in java with their storage sizes in bytes.

11. What are the types of constructors?


➢ Types of constructors in java:
a) Default constructor (no-arg constructor)
b) Parameterized constructor
c) Copy constructor
1. Default constructor: A constructor is called "Default Constructor" when it doesn't have any
parameter.

Syntax of default constructor:


<class_name>()
{}

Example:
class Bike1{
Bike1()
{
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike1 b=new Bike1();
}
}
2. Parameterized constructor: A constructor which has aspecific number of parameters is called
parameterized constructor.
Parameterized constructor is used to provide different values to the distinct objects

Example:
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display()
{ System.out.println(id+" "+name);}
public static void main(String args[])
{
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
s1.display();
s2.display();
}
}
3. Copy constructor: A copy constructor is used for copying the values of one object to another
object.

Example:
class Student6{
int id;
String name;
Student6(int i,String n)
{
id = i;
name = n;
}
Student6(Student6 s){
id = s.id;
name =s.name;
}
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
Student6 s1 = new Student6(111,"Karan");
Student6 s2 = new Student6(s1); //copy constructor called
s1.display();
s2.display();
}
}
12. How garbage collection is done in Java? Which methods are used for it?.
➢ a) Garbage collection is a process in which the memory allocated to objects, which are no longer in use can
be freed for further use.
b) Garbage collector runs either synchronously when system is out of memory or asynchronously when
system is idle.
c) In Java it is performed automatically. So it provides better memory management.
Method used for Garbage Collection:
The java.lang.Object.finalize() is called by garbage collector on an object when garbage collection
determines that there are no more reference to the object.
A subclass override the finalize method to dispose of system resources or to perform other cleanup.
General Form :
protected void finalize()
{ // finalization code here
}
13. Describe access control specifiers with example.
➢ There are 4 types of java access modifiers: 1. private 2. default 3. protected 4. Public
a) private access modifier:
The private access modifier is accessible only within class.
Example:
class test
{
private int data=40;
private void show()
{
System.out.println("Hello java");
}
}
public class test1{
public static void main(String args[]){
testobj=new test();
System.out.println(obj.data);//Compile Time Error
obj.show();//Compile Time Error
}
}
In this example, we have created two classes test and test1. test class contains private data member
and private method. We are accessing these private members from outside the class, so there is
compile time error
b) 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 anywhere.
Example:
class test
{
int data=40; //default access
void show() // default access
{
System.out.println("Hello java");
}
}
public class test1{
public static void main(String args[]){
testobj=new test();
System.out.println(obj.data);
obj.show();
}
}
c) protected access specifier:
The protected access specifier is accessible within package and outside the package but through
inheritance only.
Example:
test.java
packagemypack;
public class test
{
protected void show()
{
System.out.println("Hello java");
}
}
test1.java
importmypack.test;
class test1 extends test
{
public static void main(String args[])
{
test1obj=new test1();
obj.show();
}
}
d) public access specifier:
The public access specifier is accessible everywhere. It has the widest scope among all other
modifiers.
Example:
test.java
packagemypack;
public class test
{
public void show()
{
System.out.println("Hello java");
}
}
test1.java
importmypack.test;
class test1 ///inheritance not required
{
public static void main(String args[])
{
test1obj=new test1();
obj.show();
)
)
14. Differentiate between array and vector with any 4 points.

15. What is addElement( ) and elementAt( ) command in vectors.


➢ elementAt( ): Returns the element at the location specified by index.
Syntax: Object elementAt(int index)
Example:
Vector v = new Vector();
v.elementAt(2); //return 2nd element from vector
b) addElement ( ): Adds the specified component to the end of
this vector, increasing its size by one.
Syntax: void addElement(Object element)
Example:
Vector v = new Vector();
v.addElement(new Integer(1)); //add integer object 1 to
vector

You might also like