FifthSemester_JAVA_Quesiton Answer
1. Major Features of Java
- Simple – Easy syntax,
no pointers, automatic memory management.
- Object Oriented – Uses class
and object, supports encapsulation, inheritance, polymorphism,
abstraction.
- Platform
Independent – “Write Once, Run Anywhere” (Bytecode runs on JVM).
- Secure – No pointer
manipulation, bytecode verification, security manager.
- Robust – Strong
memory management, exception handling.
- Multithreaded – Supports
multiple threads execution.
- Distributed – Supports
RMI, networking API.
- Portable – Same size of
primitive types on all systems.
- High
Performance – Uses JIT compiler.
2. Multithreading
Definition:
Multithreading is the process of executing multiple threads simultaneously
within a single program.
Example:
class A extends Thread{
public void run(){System.out.println("Thread
running");}
public static void main(String[] args){
A t=new A();
t.start();
}
}
3. Difference Between while and
do-while
|
while |
do-while |
|
Entry controlled
loop |
Exit controlled
loop |
|
Condition checked
first |
Condition checked
after execution |
|
May execute 0
times |
Executes at least
once |
Example:
int i=1;
while(i<=3){System.out.println(i);i++;}
int j=1;
do{System.out.println(j);j++;}while(j<=3);
4. Sum of Two Numbers (Using Console)
import java.util.*;
class Sum{
public static void main(String[] a){
Scanner s=new Scanner(System.in);
int x=s.nextInt(),y=s.nextInt();
System.out.println(x+y);
}
}
5. Why Use Interface in Java?
Interface is used to achieve:
- 100%
abstraction
- Multiple
inheritance
- Loose coupling
- Standardization
It contains abstract methods (by default public & abstract).
Example:
interface A{void show();}
class B implements A{
public void show(){System.out.println("Hello");}
public static void main(String[] a){new
B().show();}
}
6. UI Program (Factorial)
import java.awt.*;
import java.awt.event.*;
class F extends Frame implements ActionListener{
TextField t1=new TextField(10),t2=new TextField(10);
Button b=new Button("OK");
F(){
setLayout(new FlowLayout());
add(new Label("Enter Number"));add(t1);
add(new Label("Factorial"));add(t2);
add(b);
b.addActionListener(this);
setSize(300,200);setVisible(true);
}
public void actionPerformed(ActionEvent
e){
int
n=Integer.parseInt(t1.getText()),f=1;
for(int i=1;i<=n;i++)f*=i;
t2.setText(String.valueOf(f));
}
public static void main(String[] a){new
F();}
}
1. What is JVM? Explain features of
Java.
JVM (Java Virtual Machine)
JVM is a virtual machine that executes Java bytecode.
It converts bytecode into machine code and makes Java platform independent.
Main Functions:
- Loads class
files
- Verifies
bytecode
- Executes
program
- Manages memory
(Garbage Collection)
Features of Java:
- Simple
- Object Oriented
- Platform
Independent
- Secure
- Robust
- Multithreaded
- Portable
- Distributed
- High
Performance (JIT)
2. Constructor & Types
Constructor
A constructor is a special method having same name as class, used to
initialize objects.
It has no return type and is called automatically when object is created.
Types:
- Default
Constructor
- Parameterized
Constructor
Example:
class A{
A(){System.out.println("Default");}
A(int x){System.out.println("Parameterized
"+x);}
public static void main(String[] a){
new A();
new A(10);
}
}
OR
Method Overloading
Method overloading means same method name with different parameters in
same class.
Condition:
- Parameters must
differ (number/type)
Example:
class A{
int add(int a,int b){return a+b;}
int add(int a,int b,int c){return
a+b+c;}
public static void main(String[] x){
A o=new A();
System.out.println(o.add(2,3));
System.out.println(o.add(2,3,4));
}
}
3. Types of Inheritance
- Single
- Multilevel
- Hierarchical
- Multiple
(through interface)
- Hybrid (using
interface)
Multilevel Inheritance Example:
class A{void show(){System.out.println("A");}}
class B extends A{}
class C extends B{
public static void main(String[] a){
new C().show();
}
}
Interface vs Class
|
Class |
Interface |
|
Can have method
body |
Methods are
abstract (default) |
|
Supports single
inheritance |
Supports multiple
inheritance |
|
Can have
constructors |
No constructor |
|
Can have instance
variables |
Only constants |
Method Overriding
When subclass provides specific implementation of parent method.
Condition:
- Same method
name
- Same parameters
Example:
class A{void show(){System.out.println("Parent");}}
class B extends A{
void show(){System.out.println("Child");}
public static void main(String[] a){
A o=new B();
o.show();
}
}
4. Categories of Exceptions
- Checked
Exception (Compile-time)
Example: IOException - Unchecked
Exception (Runtime)
Example: ArithmeticException - Error (Serious
problem)
Example: OutOfMemoryError
throws Keyword
Declares exception.
throw Keyword
Explicitly throws exception.
Example:
class A{
static void m() throws Exception{throw
new Exception();}
public static void main(String[] a) throws
Exception{
m();
}
}
5. What is Stream?
Stream is a sequence of data used for input and output.
Types:
- Byte Stream
Classes: InputStream, OutputStream - Character
Stream
Classes: Reader, Writer
Character Stream Classes:
- FileReader
- FileWriter
- BufferedReader
- BufferedWriter
Example:
import java.io.*;
class A{
public static void main(String[] a) throws
Exception{
FileWriter f=new FileWriter("a.txt");
f.write("Hello");
f.close();
}
}
6. Component vs Container
Component
GUI element like Button, Label, TextField.
Container
Holds components (Frame, Panel).
Container can add components using add() method.
Event Handling (Action Event)
When user clicks button, event is generated.
Steps:
- Implement
ActionListener
- Register
listener
- Override
actionPerformed()
Example:
import java.awt.*;
import java.awt.event.*;
class A extends Frame implements ActionListener{
Button b=new Button("OK");
A(){
add(b);
b.addActionListener(this);
setSize(200,200);
setVisible(true);
}
public void actionPerformed(ActionEvent
e){
System.out.println("Clicked");
}
public static void main(String[] a){new
A();}
}
✅ 1. Byte Code & Java Architecture
Byte Code
Byte code is the intermediate code generated by Java compiler (.class
file).
It is platform independent and executed by JVM.
Java Source (.java) → Compiler (javac) → Bytecode (.class) → JVM →
Machine Code
Java Architecture
Components:
- JDK – Development
tools (javac, debugger).
- JRE – Runtime
environment.
- JVM – Executes
bytecode.
JVM Internal Parts:
- Class Loader
- Method Area
- Heap
- Stack
- Program Counter
- Execution
Engine
- Garbage
Collector
Diagram (Exam Draw Format)
.java → javac → .class → JVM → OS →
Hardware
✅ 2. Types of Loops in Java
- for loop
- while loop
- do-while loop
Factorial Program
import java.util.*;
class A{
public static void main(String[] a){
Scanner s=new Scanner(System.in);
int n=s.nextInt(),f=1;
for(int i=1;i<=n;i++)f*=i;
System.out.println(f);
}
}
OR
Static Data Members & Static
Methods
Static Data Member
- Belongs to
class, not object
- Shared by all
objects
- Accessed using
class name
Static Method
- Belongs to
class
- Can access only
static members
- Called without
object
Example:
class A{
static int x=10;
static void show(){System.out.println(x);}
public static void main(String[] a){
A.show();
}
}
✅ 3. Interface & Multiple Inheritance
Interface
Interface is a collection of abstract methods.
Used to achieve 100% abstraction and multiple inheritance.
Multiple Inheritance Using Interface
interface A{void show();}
interface B{void display();}
class C implements A,B{
public void show(){System.out.println("A");}
public void display(){System.out.println("B");}
public static void main(String[] a){
C o=new C();
o.show();o.display();
}
}
OR
Importance of Inheritance
- Code
reusability
- Method
overriding
- Extensibility
- Reduces
duplication
Single Inheritance Example
class A{void show(){System.out.println("Parent");}}
class B extends A{
public static void main(String[] a){
new B().show();
}
}
✅ 4. Exception & try-catch-finally
Exception
An exception is an abnormal condition that disrupts program flow.
Keywords:
- try → risky
code
- catch → handle
exception
- finally →
always executes
Example:
class A{
public static void main(String[] a){
try{
int x=10/0;
}
catch(Exception e){
System.out.println("Error");
}
finally{
System.out.println("Done");
}
}
}
✅ 5. Byte Stream Classes
Byte stream is used for binary data (image, audio).
Superclasses:
- InputStream
- OutputStream
Important Classes:
- FileInputStream
- FileOutputStream
- BufferedInputStream
- BufferedOutputStream
Example:
import java.io.*;
class A{
public static void main(String[] a) throws
Exception{
FileOutputStream f=new FileOutputStream("a.txt");
f.write(65);
f.close();
}
}
✅ 6. Event Handling
Event handling is mechanism to handle user actions (click, key press).
Uses:
- Event Source
- Event Listener
- Event Object
Handling Action Event
Steps:
- Implement
ActionListener
- Register
listener
- Override
actionPerformed()
Example:
import java.awt.*;
import java.awt.event.*;
class A extends Frame implements ActionListener{
Button b=new Button("OK");
A(){
add(b);
b.addActionListener(this);
setSize(200,200);
setVisible(true);
}
public void actionPerformed(ActionEvent
e){
System.out.println("Clicked");
}
public static void main(String[] a){new
A();}
}
1. What is polymorphism? Write example
program to illustrate method overriding.
Polymorphism
Polymorphism means one name, many forms.
In Java, polymorphism allows a method to behave differently in different
classes.
Types:
- Compile-time
(Method Overloading)
- Run-time
(Method Overriding)
Method Overriding
When a child class provides its own implementation of a method
already defined in parent class.
Example Program
class A {
void show() {
System.out.println("Parent class");
}
}
class B extends A {
void show() {
System.out.println("Child class");
}
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
2. Differentiate between switch and if
statement with example program.
Difference between if and switch
|
if statement |
switch statement |
|
Used for
conditions |
Used for fixed
values |
|
Supports logical
operators |
Does not support
logical operators |
|
Slower for many
conditions |
Faster for
multiple choices |
|
Can test ranges |
Cannot test ranges |
Example (if)
int a = 5;
if(a == 5)
System.out.println("Five");
Example (switch)
int a = 5;
switch(a) {
case 5: System.out.println("Five");
}
3. Why throws keyword is used in
exception handling? Write program to handle array index out of bound error.
throws Keyword
The throws keyword is used to declare exceptions that a method may pass to
the calling method.
Purpose:
- Delegates
exception handling
- Improves
program readability
Example Program
class Test {
static void show() throws ArrayIndexOutOfBoundsException {
int a[] = {1, 2};
System.out.println(a[5]);
}
public static void main(String[] args) {
try {
show();
} catch(Exception e) {
System.out.println("Array
index error");
}
}
}
4. How abstract class is created in
Java? Explain why abstract class is used with example.
Abstract Class
An abstract class is a class that cannot be instantiated and may
contain abstract methods.
Creation
- Use abstract keyword
- Can have
abstract and non-abstract methods
Why Abstract Class is Used
- To achieve
abstraction
- To define
common behavior
- To force child
classes to implement methods
Example
abstract class A {
abstract void show();
}
class B extends A {
void show() {
System.out.println("Abstract method implemented");
}
public static void main(String[] args) {
B obj = new B();
obj.show();
}
}
OR
Java program to create database to
store 2 book records
import java.sql.*;
class BookDB {
public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test",
"root", "");
Statement st = con.createStatement();
st.execute("create table book(isbn int, title varchar(20), author
varchar(20), price int, page int)");
st.execute("insert into book values(1,'Java','James',500,300)");
st.execute("insert into book values(2,'C','Dennis',400,250)");
System.out.println("Records inserted");
}
}
(Only write logic part if short on time)
5. Java program to create GUI window
with two text fields and two buttons using GridLayout.
import java.awt.*;
class GridDemo {
GridDemo() {
Frame f = new Frame();
f.setLayout(new GridLayout(2,2));
f.add(new TextField());
f.add(new TextField());
f.add(new Button("OK"));
f.add(new Button("Cancel"));
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String[] args) {
new GridDemo();
}
}
OR
What is adapter class? Why adapter
class is used? Explain with example.
Adapter Class
Adapter class is a predefined class that provides empty
implementation of listener interfaces.
Why Adapter Class is Used
- Avoid
implementing all methods
- Improves code
readability
- Reduces coding
effort
Example
import java.awt.event.*;
class MyAdapter extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked");
}
}
6. Java program to take 2 double data
from user and store them in tendouble.txt file.
import java.io.*;
import java.util.*;
class FileDemo {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble();
double b = sc.nextDouble();
FileWriter fw = new FileWriter("tendouble.txt");
fw.write(a + "\n" + b);
fw.close();
System.out.println("Data stored");
}
}
1. Define inheritance. Identify
different types of inheritance with example.
Inheritance
Inheritance is the process by which one class acquires properties and
methods of another class using the extends keyword.
Advantages:
Code reusability, easy maintenance, less redundancy.
Types of Inheritance in Java
- Single
- Multilevel
- Hierarchical
- Multiple (using
interface)
Example (Single Inheritance)
class A{ void show(){
System.out.println("A"); } }
class B extends A{
public static void main(String[] a){
new B().show();
}
}
2. Create a GUI window to collect
name, address, phone, gender, hobbies and submit button.
import java.awt.*;
class Form{
Form(){
Frame f=new Frame();
f.setLayout(new GridLayout(6,2));
f.add(new Label("Name")); f.add(new TextField());
f.add(new Label("Address")); f.add(new TextField());
f.add(new Label("Phone")); f.add(new TextField());
f.add(new Label("Gender")); f.add(new Checkbox("Male"));
f.add(new Label("Hobby")); f.add(new Checkbox("Reading"));
f.add(new Button("Submit"));
f.setSize(300,300); f.setVisible(true);
}
public static void main(String[] a){ new Form();
}
}
OR
Java program using GridLayout and
FlowLayout
import java.awt.*;
class Layout{
Layout(){
Frame f1=new Frame();
f1.setLayout(new FlowLayout());
f1.add(new Button("A"));
Frame f2=new Frame();
f2.setLayout(new GridLayout(2,2));
f2.add(new Button("1")); f2.add(new Button("2"));
f2.add(new Button("3")); f2.add(new Button("4"));
f1.setSize(200,100); f2.setSize(200,200);
f1.setVisible(true); f2.setVisible(true);
}
public static void main(String[] a){ new Layout();
}
}
3. Differentiate between method
overloading and method overriding. Write program to show overriding.
Difference
|
Overloading |
Overriding |
|
Same class |
Parent–child class |
|
Different
parameters |
Same parameters |
|
Compile-time |
Run-time |
Overriding Program
class A{ void show(){
System.out.println("A"); } }
class B extends A{
void show(){ System.out.println("B");
}
public static void main(String[] a){
A o=new B(); o.show();
}
}
4. What is exception handling? Write
program to show array index out of bound exception.
Exception Handling
Exception handling is a mechanism to handle runtime errors and
prevent program termination.
Program
class Test{
public static void main(String[] a){
try{
int x[]={1};
System.out.println(x[5]);
}
catch(Exception e){
System.out.println("Error");
}
}
}
5. Define interface. Why interface is
used? Write example program.
Interface
An interface is a collection of abstract methods used to achieve abstraction
and multiple inheritance.
Uses:
Multiple inheritance, loose coupling, full abstraction.
Example
interface A{ void show(); }
class B implements A{
public void show(){ System.out.println("Done");
}
public static void main(String[] a){
new B().show();
}
}
6. Difference between listener and
adapter classes. Write program to handle any one event.
Difference
|
Listener |
Adapter |
|
Interface |
Class |
|
All methods needed |
Optional methods |
|
More code |
Less code |
Action Event Program
import java.awt.*;
import java.awt.event.*;
class Event extends Frame implements ActionListener{
Event(){
Button b=new Button("Click");
b.addActionListener(this);
add(b); setSize(200,200); setVisible(true);
}
public void actionPerformed(ActionEvent e){
System.out.println("Clicked");
}
public static void main(String[] a){ new Event();
}
}
OR
JDBC program to create MySQL database mydb_2025
import java.sql.*;
class JDBC{
public static void main(String[] a)throws
Exception{
Connection c=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/","root","");
c.createStatement().execute(
"create database mydb_2025");
}
}
Comments
Post a Comment