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

java model paper 1 BCA

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including definitions of classes and objects, the compilation and interpretation process, variable declaration, method overriding, abstract classes, and packages. It also covers Java's stream classification, AWT for GUI, applets, inheritance types, and the structure of a Java program. Additionally, it discusses operators, constructors, the Scanner class, and the significance of the final keyword.

Uploaded by

usharushar238
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 views24 pages

java model paper 1 BCA

The document provides an overview of Object-Oriented Programming (OOP) concepts in Java, including definitions of classes and objects, the compilation and interpretation process, variable declaration, method overriding, abstract classes, and packages. It also covers Java's stream classification, AWT for GUI, applets, inheritance types, and the structure of a Java program. Additionally, it discusses operators, constructors, the Scanner class, and the significance of the final keyword.

Uploaded by

usharushar238
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/ 24

Oop’s using java

2marks

1. Define class and object. Class: A class is like a blueprint. It tells what an object will
have and what it can do.

Example: A class called Car can have details like color, speed, and brand.

Object: An object is a real thing made from a class. It uses the class’s blueprint.

Example: A red Car with 100 km/h speed is an object of the Car class.

2. Why java is called as complied and interpreted language?

Java is called compiled and interpreted because it works in two steps:

1. Compiled:

First, Java code is converted into bytecode by the Java compiler (javac).

This bytecode is not machine code, but a special code for the Java Virtual Machine
(JVM).

2. Interpreted:

Then, the JVM reads and runs the bytecode using an interpreter or JIT compiler.

This makes Java programs run on any device with a JVM.

So, Java is both compiled (to bytecode) and interpreted (by JVM).

3. What is a variable? How to declare and initialize a variable ?

Variable: A variable is a name used to store data (like numbers or text) in a program.
It can change while the program runs.

Example: int age = 20; — here, age is a variable.

How to declare and initialize a variable:

Declaration: Tell the type and name of the variable.

Example: int age;

Initialization: Give a value to the variable.

Example: age = 20;

Together: You can declare and initialize in one line:

Example: int age = 20;

4. How is an object created in java? Give an example.


In Java, an object is created using the new keyword.

Syntax:

ClassName objectName = new ClassName();

Example:

Car myCar = new Car();

Here:

Car is the class.

myCar is the object.

New Car() creates the object.

4. What is method overriding?

Method overriding means writing a new version of a method in the child class that
already exists in the parent class.

The method name, return type, and parameters must be same.

It is used in inheritance to change or improve the behavior of a parent class method.

Example:

Class Animal {

Void sound() {

System.out.println(“Animal makes sound”);

Class Dog extends Animal {

Void sound() {

System.out.println(“Dog barks”);

Here, the Dog class overrides the sound() method of the Animal class.

6. What is an abstract method abstract class?

Abstract Method:
An abstract method is a method without a body. It only has a name and no code inside.

It is written using the abstract keyword.

Example:

Abstract void draw();

Abstract Class:

An abstract class is a class that has one or more abstract methods.

It cannot be used to create objects directly.

Other classes must extend it and give the body for the abstract methods.

Example:

Abstract class Shape {

Abstract void draw();

7. What is package in Java?

A package in Java is a group of related classes and interfaces.

It helps to organize code and avoid name conflicts.

Java has two types of packages:

1. Built-in packages – like java.util, java.io

2. User-defined packages – created by programmers.

Example:

Package mypackage; // user-defined package

Public class MyClass {

// class code

8. What is a stream? How streams are classified in java?

Stream: A stream in Java is used to read or write data (like from files, keyboard, or
network).

It is a flow of data — either input (reading) or output (writing).

Streams are classified into two types:


1. Byte Streams

Used for handling binary data (images, videos).

Classes: InputStream, OutputStream

2. Character Streams

Used for handling text data (characters).

Classes: Reader, Writer

9. What is abstract window toolkit(AWT) in java?

AWT is a set of predefined classes in Java used to create Graphical User Interface (GUI)
programs.

It provides components like:

Buttons

Text fields

Labels

Windows

AWT is part of the java.awt package and works with the system’s native window system.

Example:

Import java.awt.*;

Public class MyWindow {

Public static void main(String[] args) {

Frame f = new Frame(“My AWT Window”);

f.setSize(300, 200);

f.setVisible(true);

10. What is an applet?

An applet is a small Java program that runs inside a web browser.

It is used to make interactive web pages.

Applets are part of the java.applet package and use AWT for GUI.
Key points:

Applets do not have a main() method.

They use special methods like init(), start(), stop(), and destroy().

Example:

Import java.applet.Applet;

Import java.awt.Graphics;

Public class MyApplet extends Applet {

Public void paint(Graphics g) {

g.drawString(“Hello Applet”, 50, 50);

6 marks

11. Compare Procedure Oriented Programming (POP) and Object Oriented


Programming (OOP)

Focus- Focuses on functions (procedures)

Focuses on objects and classes

Data-Data is not secure Data is secure using encapsulation

Reusability -Code is not reusable

Code can be reused using inheritance

Example-Languages C, Pascal

Java, C++, Python

Program Structure- Program is divided into functions Program is divided into


classes/objects

Data Handling -Data is global and shared

Data is private and used through methods

12. Explain the Structure of a Java Program

A basic Java program has the following parts:

1. Package statement (optional) – declares the package.

Package mypackage;
2. Import statement – imports other classes.

Import java.util.Scanner;

3. Class definition – main class where code is written.

Public class MyProgram {

4. Main method – starting point of the program.

Public static void main(String[] args) {

// code goes here

5. Statements – logic of the program.

Example:

Public class Hello {

Public static void main(String[] args) { System.out.println(“Hello World”);

13. Explain different types of inheritance in Java with examples

1. Single Inheritance – One class inherits from another.

Class Animal {

Void sound() { System.out.println(“Animal sound”); }

Class Dog extends Animal {

Void bark() { System.out.println(“Dog barks”); }

3. Multilevel Inheritance – A class inherits from a class, which itself inherits from
another.

Class Animal { ... }

Class Dog extends Animal { ... }

Class Puppy extends Dog { ... }

4. Hierarchical Inheritance – One class is a parent of multiple classes.


Class Animal { ... }

Class Dog extends Animal { ... }

Class Cat extends Animal { ... }

5. Hybrid and Multiple Inheritance – Java does not support multiple inheritance
with classes, but it can be achieved using interfaces.

14. Explain how multiple inheritance is achieved using Interfaces

Java does not support multiple inheritance through classes but allows it using
interfaces.

Example:

Interface A {

Void show();

Interface B {

Void display();

Class C implements A, B {

Public void show() {

System.out.println(“Show from A”);

Public void display() {

System.out.println(“Display from B”);

Explanation:

Interface A and B are like blueprints.

Class C implements both interfaces.

This is how Java supports multiple inheritance.

15. Explain the Types of Layout Managers in Java AWT

Layout managers are used to arrange components (like buttons) in a GUI window.
Types:

1. FlowLayout:

Arranges components in a line (left to right).

Default for Applet.

setLayout(new FlowLayout());

2. BorderLayout:

Divides the window into 5 regions: North, South, East, West, Center.

setLayout(new BorderLayout());

3. GridLayout:

Arranges components in rows and columns.

setLayout(new GridLayout(2, 3)); // 2 rows, 3 columns

4. CardLayout and GridBagLayout are advanced layouts for complex designs.

16. Explain the Life Cycle of Thread

Java thread has the following states:

1. New:

Thread is created but not started.

2. Runnable:

Start() is called, and thread is ready to run.

3. Running:

Thread is actively executing.

4. Blocked/Waiting:

Thread is waiting for a resource or time.

5. Terminated (Dead):

Thread has finished execution.

Example:

Class MyThread extends Thread {

Public void run() {

System.out.println(“Thread is running...”);
}

Public class Test {

Public static void main(String[] args) {

MyThread t = new MyThread(); // New state

t.start(); // Runnable -> Running

8 marks

17. (a) What is JVM? Explain the components of JVM.

(b) Explain type promotion in java expressions with examples.

(a) What is JVM? Explain the components of JVM.

JVM (Java Virtual Machine):

JVM is a part of Java that runs Java programs.

It takes the bytecode (compiled Java code) and executes it.

JVM makes Java programs platform-independent, meaning the same code works on
Windows, Mac, or Linux.

Components of JVM:

1. Class Loader:

Loads .class files (bytecode) into memory.

It checks and organizes the classes needed to run the program.

2. Bytecode Verifier:

Checks if the bytecode is safe to run.

Makes sure there are no errors or harmful code.

3. Interpreter:

Reads bytecode line by line and runs it.

4. JIT Compiler (Just-In-Time):

Converts bytecode to machine code at runtime.


Makes the program faster by reducing interpretation time.

5. Runtime Data Areas (Memory Areas):

Method Area: Stores class data like methods and variables.

Heap: Stores objects created in the program.

Stack: Stores method calls and local variables.

Program Counter (PC) Register: Keeps track of which line of code is running.

Native Method Stack: Used to run native (non-Java) methods.

(b) Explain Type Promotion in Java Expressions with Examples.

Type Promotion:

In Java, when different data types are used in an expression, Java automatically
converts smaller types to larger types to avoid data loss. This is called type promotion.

Rules of Type Promotion:

1. Byte, short, and char are promoted to int.

2. If any operand is long, the result is long.

3. If any operand is float, the result is float.

4. If any operand is double, the result is double.

Examples:

Byte a = 10;

Byte b = 20;

Int result = a + b; // a and b are promoted to int

Int x = 5;

Float y = 6.5f;

Float result = x + y; // x is promoted to float

Double d = 10.5;

Int i = 3;

Double res = d * i; // i is promoted to double

This automatic conversion makes sure the result is correct and no important data is
lost.

18. Explain the different types of operators in Java with examples.


In Java, operators are special symbols that perform operations on variables and values.
Java has different types of operators, and each one has a special use. Below are the
main types:

1. Arithmetic Operators

These operators are used for basic math operations like addition, subtraction, etc.

Operator Meaning Example Result

+ Addition 10 + 5 15

- Subtraction. 10 – 5 5

* Multiplication 10 * 2 20

/ Division 10 / 2 5

% Modulus (remainder) 10 % 3 1

2. Relational (Comparison) Operators

These operators compare two values and give a true or false result.

Operator Meaning Example Result

== Equal to 5 == 5 true

!= Not equal to 5 != 4 true

➢ Greater than 6 > 3 true

< Less than 4<2 false

>= Greater or equal 5 >= 5 true

<= Less or equal 3 <= 2 false

3. Logical Operators

These are used to combine multiple conditions.

Operator Meaning Example Result

&& AND (5 > 2 && 4 > 3) true

` ` OR

! NOT !(5 > 2) false

4. Assignment Operators

These are used to assign values to variables.

Operator Meaning Example Result


= Assign value a = 10 10

+= Add and assign a += 5 a = a + 5

-= Subtract and assign a -= 3 a = a – 3

*= Multiply and assign a *= 2 a = a * 2

5. Unary Operators

These work with only one operand.

Operator Meaning Example Result

+ Positive sign +a Same value

- Negative sign -a Negative value

++ Increment a++ or ++a Increase by 1

-- Decrement a—or –a Decrease by 1

6. Bitwise Operators

Used for operations on bits (0s and 1s).

Operator Name Example

& AND 5&3

` ` OR

^ XOR 5^3

~ NOT ~5

6. Conditional (Ternary) Operator

Used as a shortcut for if-else.

Int a = 5, b = 10;

Int max = (a > b) ? a : b; // returns 10

7. Instanceof Operator

Checks if an object belongs to a class.

String name = “John”;

System.out.println(name instanceof String); // true

Conclusion:
Java provides many operators for different types of operations like math, comparison,
logic, and assignment. These help make programs easier to write and understand.

19. (a) What are the different types of constructors? Explain with an example.

What is a Constructor?

A constructor is a special method in Java used to create objects. It has the same name
as the class and does not have any return type.

Types of Constructors:

1. Default Constructor

Java provides it automatically if no constructor is written.

Takes no arguments.

2. Parameterized Constructor

Takes parameters (arguments) to set values when creating an object.

3. Copy Constructor (User-defined)

Copies values from one object to another.

Not built-in in Java like in C++, but can be created manually.

Example:

Class Student {

String name;

Int age;

// Default Constructor

Student() {

Name = “Unknown”;

Age = 0;

// Parameterized Constructor

Student(String n, int a) {

Name = n;

Age = a;
}

// Copy Constructor

Student(Student s) {

Name = s.name;

Age = s.age;

Void display() {

System.out.println(name + “ “ + age);

Public class Main {

Public static void main(String[] args) {

Student s1 = new Student(); // Default

Student s2 = new Student(“Rahul”, 20); // Parameterized

Student s3 = new Student(s2); // Copy

S1.display();

S2.display();

S3.display();

(b) What is Scanner class? Explain any five methods of Scanner class.

What is Scanner Class?

The Scanner class in Java is used to read input from the user (like keyboard input). It is
present in the java.util package.

Common Methods of Scanner:

Method Description

Next() Reads one word (no spaces).

nextLine() Reads a full line (includes spaces).


nextInt() Reads an integer value.

nextFloat() Reads a float (decimal number).

nextDouble() Reads a double value.

Example:

Import java.util.Scanner;

Public class InputExample {

Public static void main(String[] args) {

Scanner sc = new Scanner(System.in); System.out.println(“Enter name:”);

String name = sc.nextLine(); System.out.println(“Enter age:”);

Int age = sc.nextInt(); System.out.println(“Enter salary:”);

Float salary = sc.nextFloat(); System.out.println(“Name: “ + name);


System.out.println(“Age: “ + age); System.out.println(“Salary: “ + salary);

20. (a) What is the significance of the final keyword in Java? Explain its different uses.

What is final?

The final keyword in Java is used to make something constant or unchangeable.

Uses of final:

1. Final Variable

Value cannot be changed once assigned.

Example:

Final int speed = 60;

2. Final Method

Cannot be overridden in a subclass.

Example:

Final void show() { System.out.println(“Hello”);

3. Final Class
Cannot be inherited.

Example:

Final class Animal { }

Example:

Final class Bike {

Final int speed = 90;

Final void showSpeed() {

System.out.println(“Speed: “ + speed);

Public class Main {

Public static void main(String[] args) {

Bike b = new Bike();

b.showSpeed();

(b) Explain the steps to create and compile a user-defined package in Java.

What is a Package?

A package is a group of similar types of classes. It helps in organizing code and avoiding
name conflicts.

Steps to Create a Package:

1. Create a Folder with a package name.

2. Write Java file using the package keyword.

3. Compile the file using:

Javac -d . ClassName.java

4. Use it in another file using:

Import packagename.ClassName;

Example:
1. Create folder mypack

2. Inside mypack, create this file:

// File: MyClass.java

Package mypack;

Public class MyClass {

Public void show() { System.out.println(“Inside my package”);

4. Compile it:

Javac -d . MyClass.java

5. Use it in another file:

Import mypack.MyClass;

Public class Test {

Public static void main(String[] args) {

MyClass obj = new MyClass();

Obj.show();

21. (a) Write the important methods of InputStream and OutputStream.

InputStream

Used to read data (like from keyboard, file).

Method Description

Read() Reads one byte

Available() Number of bytes available

Close() Closes the stream

OutputStream

Used to write data (to screen, file).

Method Description
Write(int b) Writes one byte

Flush() Forces data to be written

Close() Closes the stream

Example:

Import java.io.*;

Public class StreamExample {

Public static void main(String[] args) throws IOException {

FileOutputStream out = new FileOutputStream(“test.txt”);

Out.write(65); // Writes ‘A’

Out.close();

FileInputStream in = new FileInputStream(“test.txt”);

Int i = in.read(); System.out.println((char)i); // Prints ‘A’

In.close();

(b) Explain any five AWT controls in Java with examples.

AWT Controls (GUI elements)

1. Label – Shows text.

2. TextField – Accepts user input.

3. Button – Clickable button.

4. Checkbox – On/Off choice.

5. Choice – Drop-down menu.

Example Program:

Import java.awt.*;

Public class AWTExample {

AWTExample() {

Frame f = new Frame(“AWT Demo”);

Label l = new Label(“Name:”);


l.setBounds(20, 50, 50, 20);

TextField tf = new TextField();

Tf.setBounds(80, 50, 100, 20);

Button b = new Button(“Submit”);

b.setBounds(50, 100, 60, 30);

f.add(l);

f.add(tf);

f.add(b);

f.setSize(300, 200);

f.setLayout(null);

f.setVisible(true);

Public static void main(String[] args) {

New AWTExample();

22. (a) Explain how Java handles exception handling using try-catch-finally blocks.

What is exception Handling?

An exception is an error that occurs during program execution. Java provides a way to
catch and handle these errors to avoid crashing the program.

Keywords Used

1. try – Block where error might happen.

2. catch – Block that handles the error.

3. finally – This block always runs (used to close files, release resources).

Syntax:

Try {

// code that might throw an exception

} catch (ExceptionType e) {
// code to handle the exception

} finally {

// cleanup code that always runs

Example:

Public class Example {

Public static void main(String[] args) {

Int a = 10, b = 0;

Try {

Int result = a / b; // This will cause ArithmeticException


System.out.println(“Result: “ + result);

} catch (ArithmeticException e) { System.out.println(“Cannot divide by zero!”);

} finally { System.out.println(“This block always runs.”);

Output:

Cannot divide by zero!

This block always runs.

Summary:

Try: Checks for error.

Catch: Handles the error.

Finally: Runs always, error or no error.

(b) Compare Multitasking and Multithreading.

Feature Multitasking Multithreading

Definition Running many programs at once. Running many threads in one program.

Example Running Word and Chrome together. Printing and saving at same
time in app.
Type Process-based Thread-based

Memory usage Uses more memory Uses less memory

Performance Slower compared to multithreading Faster and efficient

Context switch More overhead Less overhead

Example of Multithreading in Java:

Class MyThread extends Thread {

Public void run() { System.out.println(“Thread is running...”);

Public static void main(String[] args) {

MyThread t1 = new MyThread();

T1.start(); // Start a new thread

Summary:

Multitasking: Running many programs.

Multithreading: Running many threads inside one program.

Java supports multithreading using the Thread class or Runnable interface.

23. (a) What is Java Collection Framework? Explain the Features of Collection
Framework.

What is Java Collection Framework?

The Java Collection Framework is a set of classes and interfaces that help to store and
manipulate groups of objects (like arrays, lists, sets, etc.).

Important Interfaces:

List – Ordered collection (e.g., ArrayList, LinkedList)

Set – No duplicate elements (e.g., HashSet, TreeSet)

Map – Key-value pairs (e.g., HashMap, TreeMap)

Features of Collection Framework:

1. Consistent API – All collections have similar method names.


2. Growable – Collections can increase in size automatically.

3. Multiple Data Types – Supports different object types.

4. Sorting & Searching – Built-in methods for sort and search.

5. Thread-Safe Versions – Can be made safe for multi-threading.

6. Easy Traversal – Using loops or iterators.

Example using ArrayList:

Import java.util.*;

Public class CollectionExample {

Public static void main(String[] args) {

ArrayList<String> list = new ArrayList<>();

List.add(“Apple”);

List.add(“Banana”);

List.add(“Mango”);

For (String fruit : list) { System.out.println(fruit);

Output:

Apple

Banana

Mango

(b) What is JavaBean? Explain the Features of JavaBeans.

What is a JavaBean?

A JavaBean is a simple reusable Java class that follows certain rules. It is mainly used in
Java GUI, Enterprise Apps, and frameworks like Spring.

Rules of JavaBeans:

1. Must be public.

2. Must have a no-argument constructor.


3. Properties should be private.

4. Provide getter and setter methods for each property.

5. Should be serializable (optional).

Example:

Public class StudentBean implements java.io.Serializable {

Private String name;

Private int age;

Public StudentBean() {} // No-arg constructor

Public void setName(String name) {

This.name = name;

Public String getName() {

Return name;

Public void setAge(int age) {

This.age = age;

Public int getAge() {

Return age;

Features of JavaBeans:

Feature Description

Reusable Can be reused in many programs.

Encapsulated Uses private data with public methods.

Portable Works on any Java platform.

Serializable Can be saved and loaded (persisted).

Easy integration Can be used with GUI tools or frameworks.

You might also like