0% found this document useful (0 votes)
12 views4 pages

Java Lab Programs Internal Preparation

The document provides a series of Java lab programs for internal exam preparation, covering key concepts such as classes, constructors, inheritance, polymorphism, string handling, and exception handling. Each section includes example programs, expected outputs, and important viva questions. Final tips for exam success emphasize coding practices and confidence during viva sessions.
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)
12 views4 pages

Java Lab Programs Internal Preparation

The document provides a series of Java lab programs for internal exam preparation, covering key concepts such as classes, constructors, inheritance, polymorphism, string handling, and exception handling. Each section includes example programs, expected outputs, and important viva questions. Final tips for exam success emphasize coding practices and confidence during viva sessions.
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

JAVA LAB PROGRAMS - INTERNAL EXAM PREPARATION

Question 1: Class and Object Mechanism


Concept:
A class is a blueprint; an object is its instance.

Program:
class Laptop {
String brand; int ram;
void set(String b, int r) { brand=b; ram=r; }
void show() { [Link]('Brand:'+brand+' RAM:'+ram+'GB'); }
}
public class Main {
public static void main(String[] args) {
Laptop l = new Laptop();
[Link]('HP', 16);
[Link]();
}
}

Output:
Brand:HP RAM:16GB

Viva:
- Class blueprint, Object instance
- new keyword creates object
- Dot operator accesses members

Question 2: Constructors and Method Overloading


Concept:
Constructors initialize objects; Overloading = same name, different parameters.

Program:
class Student {
String name; int age;
Student(){ name='Unknown'; age=0; }
Student(String n, int a){ name=n; age=a; }
void display(){ [Link](name+' '+age); }
int sum(int a,int b){ return a+b; }
double sum(double a,double b){ return a+b; }
public static void main(String[] args){
Student s1=new Student(); Student s2=new Student('Nikitha',19);
[Link](); [Link]();
[Link]('Sum int:'+[Link](2,3));
[Link]('Sum double:'+[Link](2.5,3.5));
}
}

Viva:
- Constructors have no return type
- Overloading resolved at compile time

Question 3: Inheritance
Concept:
Child inherits parent with 'extends'.

Program:
class Person {
String name; int age;
void set(String n,int a){ name=n; age=a; }
void show(){ [Link](name+' '+age); }
}
class Teacher extends Person {
String subject;
void setSub(String s){ subject=s; }
void display(){ show(); [Link]('Subject:'+subject); }
}
public class Main {
public static void main(String[] args){
Teacher t=new Teacher(); [Link]('Ananya',29); [Link]('Maths'); [Link]();
}
}

Viva:
- extends keyword used
- Parent constructor executes first
Question 4: Polymorphism
Concept:
Runtime polymorphism through overriding and parent reference.

Program:
class Appliance{ void work(){ [Link]('Appliance running'); } }
class Fan extends Appliance{ void work(){ [Link]('Fan rotating'); } }
class Light extends Appliance{ void work(){ [Link]('Light glowing'); } }
public class Main{
public static void main(String[] args){
Appliance a; a=new Fan(); [Link](); a=new Light(); [Link]();
}
}

Viva:
- Overriding -> runtime polymorphism
- Overloading -> compile time

Question 5: String Handling


Concept:
String immutable; StringBuffer & StringBuilder mutable.

Program 1: String Methods


String s='Nikitha';
[Link]([Link]());
[Link]([Link](0,4));
[Link]([Link]('tha','ta'));

Program 2: StringBuffer
StringBuffer sb=new StringBuffer('Java'); [Link](' Lab'); [Link](); [Link](sb);

Program 3: StringBuilder
StringBuilder sb2=new StringBuilder('Welcome'); [Link](' All'); [Link](sb2);

Viva:
StringBuffer=Thread-safe; StringBuilder=Fastest; String=Immutable

Question 6: Exception Handling


Concept:
Handle runtime errors using try, catch, finally.

Built-in Exception:
try{int a=10/0;}catch(ArithmeticException e){[Link]('Divide by
zero');}finally{[Link]('Done');}

Multiple Catch:
try{int[] a={1,2};[Link](a[3]);}catch(ArrayIndexOutOfBoundsException
e){[Link]('Invalid index');}
catch(Exception e){[Link]('General error');}

User-defined Exception:
class AgeInvalidException extends Exception{ AgeInvalidException(String msg){super(msg);} }
public class Test{
static void checkAge(int age)throws AgeInvalidException{
if(age<18)throw new AgeInvalidException('Underage'); else [Link]('Valid age'); }
public static void main(String[] args){ try{checkAge(15);}catch(AgeInvalidException
e){[Link]([Link]());} }
}

Viva:
try, catch, finally, throw, throws
User-defined extends Exception

Final Quick Tips for Exam:


1. Use indentation and braces correctly.
2. Avoid missing semicolons.
3. Output must be meaningful.
4. Dry-run mentally before typing.
5. Answer viva confidently.

All the best for your Java Internal Lab, Nikitha!

You might also like