0% found this document useful (0 votes)
2 views26 pages

Java Imp Notes

The document outlines important questions and answers for a Java programming course, covering topics such as object-oriented programming features, inheritance, event handling, exception handling, and multithreading. It provides a structured approach to studying Java, highlighting key concepts and examples. Each unit includes essential questions that are crucial for understanding Java and passing assessments.

Uploaded by

harikasuresh175
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)
2 views26 pages

Java Imp Notes

The document outlines important questions and answers for a Java programming course, covering topics such as object-oriented programming features, inheritance, event handling, exception handling, and multithreading. It provides a structured approach to studying Java, highlighting key concepts and examples. Each unit includes essential questions that are crucial for understanding Java and passing assessments.

Uploaded by

harikasuresh175
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

Soundarya Educational Trust (Regd.

)
SOUNDARYA INSTITUTE OF MANAGEMENT AND SCIENCE
Soundarya Nagar Sidedahalli Nagasandra, Post, Bengaluru, Karnataka 560073
Subject Name: OOP’S USING Java
Department of Computer Science (BCA)

Important Questions and Answers of the Java Subject


Most Important Questions for Passing (Prepare These First)
Unit 1
✔ Features of Java
✔ Constructors
✔ Wrapper Classes
✔ Arrays
✔ String vs StringBuffer
Unit 2
✔ Inheritance and its types
✔ Method Overloading vs Method Overriding
✔ Interface
✔ Packages
✔ Types of Streams
Unit 3
✔ Event Handling
✔ Mouse and Keyboard Events
✔ Layout Managers
✔ GUI Components
✔ Applet Life Cycle
Unit 4
✔ Checked vs Unchecked Exceptions
✔ try-catch-finally
✔ Thread Life Cycle
✔ Two Ways of Creating Threads
✔ Collections Framework
✔ Generics

Prof,Swamy M R Dept. Of BCA SIMS


UNIT I
Introduction to Java
1. Compare Procedure-Oriented Programming and Object-Oriented Programming.
Procedure-Oriented Programming Object-Oriented Programming
Program is divided into functions Program is divided into objects
Data is less secure Data is secure through encapsulation
Follows top-down approach Follows bottom-up approach
Example: C Example: Java
No inheritance or polymorphism Supports inheritance and polymorphism
Conclusion: OOP provides better security, reusability and maintainability.

2. Explain Features of Java. (5 Marks)


Important features of Java are:
1. Platform Independent – Write once, run anywhere.
2. Object-Oriented – Uses classes and objects.
3. Simple – Similar to C/C++ and easy to learn.
4. Secure – No pointers and supports bytecode verification.
5. Robust – Automatic garbage collection.
6. Multithreaded – Supports execution of multiple threads.
7. Portable – Can run on different operating systems.

3. Explain Structure of a Java Program. (5 Marks)


import [Link].*;

class Demo
{
public static void main(String args[])
{
[Link]("Hello Java");
}
}
Components:
• Import statement
• Class declaration
• Main method
• Statements inside main method

Prof,Swamy M R Dept. Of BCA SIMS


4. Explain Data Types in Java. (5 Marks)
Primitive Data Types
Type Size
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 2 bytes
boolean true/false
Non-Primitive Data Types
• String
• Arrays
• Classes
• Interfaces

5. Explain Constructors in Java. (5 Marks)


A constructor initializes an object and has the same name as the class.
Types
1. Default constructor
2. Parameterized constructor
class Student
{
Student()
{
[Link]("Constructor called");
}
}

6. Explain Wrapper Classes. (5 Marks)


Wrapper classes convert primitive data into objects.
Primitive Wrapper Class
int Integer
float Float
double Double
char Character

Prof,Swamy M R Dept. Of BCA SIMS


Primitive Wrapper Class
boolean Boolean
Example:
Integer a = [Link](10);

7. Explain String and StringBuffer Classes. (5 Marks)


String
• Immutable.
• Cannot be modified.
String s="Java";
StringBuffer
• Mutable.
• Can be modified.
StringBuffer sb=new StringBuffer("Java");
[Link](" Programming");

8. Explain Arrays in Java. (5 Marks)


An array stores multiple values of the same type.
int a[]={10,20,30};

for(int i=0;i<3;i++)
{
[Link](a[i]);
}
Advantages:
• Stores large data.
• Easy access using index.

UNIT 2
Inheritance and Polymorphism
9. Explain Inheritance and its Types. (5 Marks)
Inheritance allows one class to acquire properties of another.
Types
1. Single inheritance
2. Multilevel inheritance
3. Hierarchical inheritance
4. Hybrid inheritance
Example:

Prof,Swamy M R Dept. Of BCA SIMS


class A
{
}

class B extends A
{
}
Advantages:
• Code reusability.
• Reduces redundancy.

10. Explain Method Overloading and Method Overriding. (5 Marks)


Method Overloading
Same method name with different parameters.
void add(int a,int b)
void add(int a,int b,int c)
Method Overriding
Subclass redefines parent method.
class A
{
void display()
{
}
}

class B extends A
{
void display()
{
}
}

11. Explain Interface in Java. (5 Marks)


An interface contains abstract methods.
interface Shape
{
void area();
}

class Circle implements Shape

Prof,Swamy M R Dept. Of BCA SIMS


{
public void area()
{
[Link]("Area");
}
}
Advantages:
• Supports multiple inheritance.
• Provides abstraction.

12. Explain Packages in Java. (5 Marks)


A package is a collection of related classes.
Types
1. Built-in package
2. User-defined package
Examples:
• [Link]
• [Link]
• [Link]
Import statement:
import [Link].*;
Benefits:
• Avoids naming conflicts.
• Improves code organization.

13. Explain Standard I/O Streams in Java. (5 Marks)


Standard streams:
1. [Link] – Input stream.
2. [Link] – Output stream.
3. [Link] – Error stream.
Example:
[Link]("Hello");

14. Explain Types of Streams in Java. (5 Marks)


Based on Data Type
1. Byte Stream
o InputStream
o OutputStream
2. Character Stream
o Reader

Prof,Swamy M R Dept. Of BCA SIMS


o Writer
Based on Operation
• Input streams
• Output streams
Used for reading and writing files.

UNIT 3
Event Handling, GUI and Applets
15. Explain Event Handling in Java. (5 Marks)
An event is an action generated by the user.
Examples:
• Mouse click
• Key press
Components:
1. Event Source
2. Event Object
3. Event Listener
Listener interfaces:
• ActionListener
• MouseListener
• KeyListener

16. Explain Mouse and Keyboard Events. (5 Marks)


Mouse Events
Methods:
• mouseClicked()
• mousePressed()
• mouseReleased()
Keyboard Events
Methods:
• keyPressed()
• keyReleased()
• keyTyped()
These are handled using MouseListener and KeyListener interfaces.

Prof,Swamy M R Dept. Of BCA SIMS


17. Explain Layout Managers in Java. (5 Marks)
Flow Layout
Components arranged left to right.
Border Layout
Five regions:
• North
• South
• East
• West
• Center
Grid Layout
Components arranged in rows and columns.

18. Explain GUI Components in Java. (5 Marks)


Common GUI controls:
• Button
• Label
• TextField
• TextArea
• Checkbox
• Radio Button
• Combo Box
• Scroll Bar
• Slider
• Menu
• Dialog Box
Used for developing graphical applications.

19. Explain Applet Life Cycle. (5 Marks)


Methods:
1. init()
2. start()
3. paint()
4. stop()
5. destroy()
Flow:
init()

start()

Prof,Swamy M R Dept. Of BCA SIMS


paint()

stop()

destroy()

20. Explain String Operations in Java. (5 Marks)


Important methods:
length()
charAt()
equals()
compareTo()
substring()
concat()
toUpperCase()
toLowerCase()
replace()
Example:
String s="Java";

[Link]([Link]());

UNIT 4
Exception Handling and Multithreading
21. Explain Checked and Unchecked Exceptions. (5 Marks)
Checked Exceptions
Checked at compile time.
Examples:
• IOException
• SQLException
Unchecked Exceptions
Checked at runtime.
Examples:
• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException

22. Explain try-catch-finally Block. (5 Marks)


try
{

Prof,Swamy M R Dept. Of BCA SIMS


int a=10/0;
}
catch(Exception e)
{
[Link]("Exception handled");
}
finally
{
[Link]("Executed always");
}
Purpose
• try → risky code
• catch → handles exception
• finally → executed always

23. Explain Thread Life Cycle. (5 Marks)


States of a thread:
1. New
2. Runnable
3. Running
4. Waiting/Blocked
5. Terminated
Diagram:
New

Runnable

Running

Waiting

Terminated

24. Explain Two Ways of Creating Threads. (5 Marks)


By Extending Thread Class
class Demo extends Thread
{
public void run()
{
[Link]("Thread");

Prof,Swamy M R Dept. Of BCA SIMS


}
}
By Implementing Runnable Interface
class Demo implements Runnable
{
public void run()
{
[Link]("Thread");
}
}
Thread t=new Thread(new Demo());
[Link]();

25. Explain Thread Synchronization. (5 Marks)


Synchronization prevents multiple threads from accessing shared resources simultaneously.
synchronized void display()
{
}
Advantages:
• Prevents data inconsistency.
• Avoids race conditions.

26. Explain Collections Framework. (5 Marks)


Collection framework stores and manipulates groups of objects.
Important interfaces:
• List
• Set
• Queue
Classes:
• ArrayList
• LinkedList
• HashSet
• HashMap
Advantages:
• Dynamic size.
• Easy insertion and deletion.

Prof,Swamy M R Dept. Of BCA SIMS


27. Explain Generic Programming in Java. (5 Marks)
Generics provide type safety.
Example:
ArrayList<String> list=new ArrayList<String>();

[Link](“Java”);
Advantages:
• Avoids type casting.
• Improves code reusability.
• Reduces runtime errors.

Prof,Swamy M R Dept. Of BCA SIMS


10 marks Important Questions and Answers
UNIT 1
1. Explain the Features of Java. (10 Marks)
Java is a high-level, object-oriented programming language developed by Sun Microsystems. It
provides several features that make it powerful and widely used.
Features of Java
1. Platform Independence
Java programs are compiled into bytecode, which can run on any operating system using JVM
(Java Virtual Machine).
Write Once, Run Anywhere (WORA).

2. Object-Oriented
Java is based on classes and objects and supports:
• Encapsulation
• Inheritance
• Polymorphism
• Abstraction

3. Simple
• Syntax is similar to C and C++.
• Eliminates complex features like pointers and operator overloading.

4. Secure
• No direct pointer access.
• Bytecode verification provides security.
• Supports Security Manager.

5. Robust
• Strong memory management.
• Automatic garbage collection.
• Exception handling mechanism.

6. Multithreaded
Java allows execution of multiple tasks simultaneously using threads.

7. Portable
Programs can run on different platforms without modification.

8. Distributed
Supports network programming and Internet applications.

Prof,Swamy M R Dept. Of BCA SIMS


9. Dynamic
Classes can be loaded dynamically during runtime.

10. High Performance


Uses Just-In-Time (JIT) compiler for faster execution.

Conclusion
Java is popular because it is secure, portable, object-oriented and platform independent.

2. Explain Structure of Java Program, Data Types and Operators. (10 Marks)
Structure of Java Program
import [Link].*;

class Demo
{
public static void main(String args[])
{
int a=10;
int b=20;

[Link](a+b);
}
}
Components
Import Statement
Used to import predefined packages.
import [Link].*;

Class Declaration
class Demo
Defines the class.

Main Method
public static void main(String args[])
Execution begins from main().

Data Types in Java


Primitive Data Types

Prof,Swamy M R Dept. Of BCA SIMS


Type Size
byte 1 byte
short 2 bytes
int 4 bytes
long 8 bytes
float 4 bytes
double 8 bytes
char 2 bytes
boolean true/false

Non-Primitive Types
• String
• Arrays
• Classes
• Interfaces

Operators in Java
Arithmetic Operators
+, -, *, /, %
Relational Operators
, <, >=, <=, ==
Logical Operators
&&, ||, !
Assignment Operators
=, +=, -=
Increment and Decrement
++, --

Conclusion
Java programs consist of classes and methods, and various data types and operators are used to
perform operations.

3. Explain Methods and Constructors in Java. (10 Marks)


Method
A method is a block of code used to perform a specific task.
Syntax

Prof,Swamy M R Dept. Of BCA SIMS


return_type methodName()
{
}
Example
class Demo
{
void display()
{
[Link]("Hello");
}
}

Advantages of Methods
• Code reuse
• Reduces program size
• Improves readability

Constructor
A constructor initializes objects.
Characteristics
• Same name as class.
• No return type.
• Automatically invoked.

Types of Constructors
Default Constructor
class Student
{
Student()
{
[Link]("Default Constructor");
}
}

Parameterized Constructor
class Student
{
Student(int x)
{
[Link](x);

Prof,Swamy M R Dept. Of BCA SIMS


}
}

Difference Between Method and Constructor


Method Constructor
Performs operations Initializes objects
Has return type No return type
Called explicitly Called automatically
Can have any name Same as class name

Conclusion
Methods improve code reuse, whereas constructors initialize objects.

UNIT 2
4. Explain Inheritance and Types of Inheritance in Java. (10 Marks)
Inheritance allows one class to acquire properties and methods of another class.
Syntax
class B extends A
{
}

Types of Inheritance
1. Single Inheritance
One child class inherits one parent class.
A
|
B

2. Multilevel Inheritance
A
|
B
|
C
Properties pass through multiple levels.

3. Hierarchical Inheritance

Prof,Swamy M R Dept. Of BCA SIMS


A
/ \
B C
Multiple child classes inherit one parent.

4. Hybrid Inheritance
Combination of two or more inheritance types.
Java supports hybrid inheritance through interfaces.

Advantages
• Code reusability.
• Reduces redundancy.
• Easy maintenance.

Example
class Animal
{
void eat()
{
[Link]("Eating");
}
}

class Dog extends Animal


{
void bark()
{
[Link]("Barking");
}
}

Conclusion
Inheritance improves reusability and supports hierarchical relationships among classes.

5. Explain Compile Time and Run Time Polymorphism. (10 Marks)


Polymorphism means one interface with many forms.

1. Compile Time Polymorphism


Achieved by Method Overloading.
Example

Prof,Swamy M R Dept. Of BCA SIMS


class Add
{
int sum(int a,int b)
{
return a+b;
}

int sum(int a,int b,int c)


{
return a+b+c;
}
}
Binding occurs during compilation.

2. Run Time Polymorphism


Achieved by Method Overriding.
Example
class Animal
{
void sound()
{
[Link]("Animal Sound");
}
}

class Dog extends Animal


{
void sound()
{
[Link]("Bark");
}
}
Binding occurs during execution.

Differences
Compile Time Run Time
Method Overloading Method Overriding
Early Binding Late Binding
Faster Comparatively slower

Prof,Swamy M R Dept. Of BCA SIMS


Compile Time Run Time
Same class Parent and child classes

Conclusion
Polymorphism increases flexibility and improves code reusability.

6. Explain Interface and Packages in Java. (10 Marks)


Interface
An interface contains abstract methods.
Syntax
interface Shape
{
void area();
}
Implementation
class Circle implements Shape
{
public void area()
{
[Link]("Area");
}
}

Advantages
• Supports abstraction.
• Supports multiple inheritance.
• Promotes loose coupling.

Packages
A package is a collection of related classes and interfaces.

Types of Packages
1. Built-in Packages
Examples:
• [Link]
• [Link]
• [Link]
• [Link]

2. User-defined Packages

Prof,Swamy M R Dept. Of BCA SIMS


Created by programmer.
package mypack;
Import package:
import mypack.*;

Advantages of Packages
• Avoid naming conflicts.
• Provides security.
• Improves code organization.

Conclusion
Interfaces provide abstraction while packages organize classes efficiently.

UNIT 3
7. Explain Event Handling Mechanism in Java. (10 Marks)
An event is an action generated by the user.
Examples:
• Mouse click
• Keyboard press
• Button click

Components of Event Handling


Event Source
Generates events.
Example:
Button

Event Object
Contains information about event.

Event Listener
Receives and handles events.

Event Listener Interfaces


ActionListener
Handles button events.
MouseListener
Handles mouse events.
KeyListener
Handles keyboard events.

Prof,Swamy M R Dept. Of BCA SIMS


Event Handling Process
Event Source

Event Object

Event Listener

Event Processing

Advantages
• Interactive applications.
• User-friendly GUI.
• Efficient event management.

Conclusion
Event handling enables communication between users and GUI components.

8. Explain GUI Components and Layout Managers in Java. (10 Marks)


GUI Components
Label
Displays text.
Label l=new Label("Name");

Button
Generates events.
Button b=new Button("Submit");

TextField
Accepts single-line input.

TextArea
Accepts multiple lines.

Checkbox
Allows multiple selections.

Radio Button
Allows single selection.

Prof,Swamy M R Dept. Of BCA SIMS


ComboBox
Provides drop-down list.

Layout Managers
Flow Layout
Arranges components from left to right.

Border Layout
Five regions:
• North
• South
• East
• West
• Center

Grid Layout
Arranges components in rows and columns.

Advantages
• Easy component arrangement.
• Improves appearance.
• Makes GUI responsive.

Conclusion
GUI components and layout managers help in developing attractive applications.

UNIT 4
9. Explain Exception Handling in Java. (10 Marks)
Exception is an abnormal condition that interrupts program execution.

Types of Exceptions
Checked Exceptions
Handled at compile time.
Examples:
• IOException
• SQLException

Unchecked Exceptions
Handled during runtime.
Examples:

Prof,Swamy M R Dept. Of BCA SIMS


• ArithmeticException
• NullPointerException
• ArrayIndexOutOfBoundsException

try-catch-finally
try Block
Contains risky code.
catch Block
Handles exceptions.
finally Block
Always executes.

Example
try
{
int a=10/0;
}
catch(Exception e)
{
[Link]("Exception handled");
}
finally
{
[Link]("Always executed");
}

Advantages
• Prevents abnormal termination.
• Maintains normal program flow.
• Improves reliability.

Conclusion
Exception handling makes programs robust and fault tolerant.

Prof,Swamy M R Dept. Of BCA SIMS


10. Explain Multithreading and Thread Life Cycle. (10 Marks)
Multithreading is the execution of multiple threads simultaneously.

Benefits
• Better CPU utilization.
• Faster execution.
• Concurrent processing.

Thread Life Cycle


1. New State
Thread object is created.

2. Runnable State
Thread is ready to execute.

3. Running State
CPU executes thread.

4. Waiting or Blocked State


Thread waits for resources.

5. Terminated State
Execution ends.

Creating Thread
By Extending Thread Class
class Demo extends Thread
{
public void run()
{
[Link]("Thread");
}
}

By Implementing Runnable Interface


class Demo implements Runnable
{
public void run()
{
[Link]("Thread");

Prof,Swamy M R Dept. Of BCA SIMS


}
}

Thread t=new Thread(new Demo());


[Link]();

Thread Synchronization
Prevents multiple threads from accessing shared resources simultaneously.
synchronized void display()
{
}

Conclusion
Multithreading improves efficiency and synchronization prevents data inconsistency.

Prof,Swamy M R Dept. Of BCA SIMS

You might also like