java model paper 1 BCA
java model paper 1 BCA
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.
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.
So, Java is both compiled (to bytecode) and interpreted (by JVM).
Variable: A variable is a name used to store data (like numbers or text) in a program.
It can change while the program runs.
Syntax:
Example:
Here:
Method overriding means writing a new version of a method in the child class that
already exists in the parent class.
Example:
Class Animal {
Void sound() {
Void sound() {
System.out.println(“Dog barks”);
Here, the Dog class overrides the sound() method of the Animal class.
Abstract Method:
An abstract method is a method without a body. It only has a name and no code inside.
Example:
Abstract Class:
Other classes must extend it and give the body for the abstract methods.
Example:
Example:
// class code
Stream: A stream in Java is used to read or write data (like from files, keyboard, or
network).
2. Character Streams
AWT is a set of predefined classes in Java used to create Graphical User Interface (GUI)
programs.
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.*;
f.setSize(300, 200);
f.setVisible(true);
Applets are part of the java.applet package and use AWT for GUI.
Key points:
They use special methods like init(), start(), stop(), and destroy().
Example:
Import java.applet.Applet;
Import java.awt.Graphics;
6 marks
Example-Languages C, Pascal
Package mypackage;
2. Import statement – imports other classes.
Import java.util.Scanner;
Example:
Class Animal {
3. Multilevel Inheritance – A class inherits from a class, which itself inherits from
another.
5. Hybrid and Multiple Inheritance – Java does not support multiple inheritance
with classes, but it can be 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 {
Explanation:
Layout managers are used to arrange components (like buttons) in a GUI window.
Types:
1. FlowLayout:
setLayout(new FlowLayout());
2. BorderLayout:
Divides the window into 5 regions: North, South, East, West, Center.
setLayout(new BorderLayout());
3. GridLayout:
1. New:
2. Runnable:
3. Running:
4. Blocked/Waiting:
5. Terminated (Dead):
Example:
System.out.println(“Thread is running...”);
}
8 marks
JVM makes Java programs platform-independent, meaning the same code works on
Windows, Mac, or Linux.
Components of JVM:
1. Class Loader:
2. Bytecode Verifier:
3. Interpreter:
Program Counter (PC) Register: Keeps track of which line of code is running.
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.
Examples:
Byte a = 10;
Byte b = 20;
Int x = 5;
Float y = 6.5f;
Double d = 10.5;
Int i = 3;
This automatic conversion makes sure the result is correct and no important data is
lost.
1. Arithmetic Operators
These operators are used for basic math operations like addition, subtraction, etc.
+ Addition 10 + 5 15
- Subtraction. 10 – 5 5
* Multiplication 10 * 2 20
/ Division 10 / 2 5
% Modulus (remainder) 10 % 3 1
These operators compare two values and give a true or false result.
== Equal to 5 == 5 true
3. Logical Operators
` ` OR
4. Assignment Operators
5. Unary Operators
6. Bitwise Operators
` ` OR
^ XOR 5^3
~ NOT ~5
Int a = 5, b = 10;
7. Instanceof Operator
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
Takes no arguments.
2. Parameterized Constructor
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);
S1.display();
S2.display();
S3.display();
(b) What is Scanner class? Explain any five methods of 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.
Method Description
Example:
Import java.util.Scanner;
20. (a) What is the significance of the final keyword in Java? Explain its different uses.
What is final?
Uses of final:
1. Final Variable
Example:
2. Final Method
Example:
3. Final Class
Cannot be inherited.
Example:
Example:
System.out.println(“Speed: “ + speed);
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.
Javac -d . ClassName.java
Import packagename.ClassName;
Example:
1. Create folder mypack
// File: MyClass.java
Package mypack;
4. Compile it:
Javac -d . MyClass.java
Import mypack.MyClass;
Obj.show();
InputStream
Method Description
OutputStream
Method Description
Write(int b) Writes one byte
Example:
Import java.io.*;
Out.close();
In.close();
Example Program:
Import java.awt.*;
AWTExample() {
f.add(l);
f.add(tf);
f.add(b);
f.setSize(300, 200);
f.setLayout(null);
f.setVisible(true);
New AWTExample();
22. (a) Explain how Java handles exception handling using try-catch-finally blocks.
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
3. finally – This block always runs (used to close files, release resources).
Syntax:
Try {
} catch (ExceptionType e) {
// code to handle the exception
} finally {
Example:
Int a = 10, b = 0;
Try {
Output:
Summary:
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
Summary:
23. (a) What is Java Collection Framework? Explain the Features of 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:
Import java.util.*;
List.add(“Apple”);
List.add(“Banana”);
List.add(“Mango”);
Output:
Apple
Banana
Mango
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.
Example:
This.name = name;
Return name;
This.age = age;
Return age;
Features of JavaBeans:
Feature Description