JAVA Model Question Paper XkVmao
JAVA Model Question Paper XkVmao
1. What is a constructor?
Answer:
In Java, a constructor is a special type of method that is automatically called when
an object of a class is created. It is used to initialize the object's state or perform any
necessary setup. Constructors have the same name as the class and do not have a
return type
Example: A a=new A()
Here A() is the Constructor.
3. What is an applet?
Answer:
An applet is a Java program that can be embedded into a web page. It runs inside the
web browser and works at client
side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and
hosted on a web server
• New: The thread is created but not yet started. At this stage, the thread is in
the NEW state.
• Runnable: The thread is ready to run and can be scheduled by the thread
scheduler. It moves to the RUNNABLE state.
• Running: The thread is currently executing its task. It moves to the RUNNING
state.
• Blocked/Waiting: The thread is waiting for a particular condition or resource
to be satisfied. It can be in a blocked state (waiting for I/O, synchronization,
etc.) or a waiting state (waiting for another thread to complete). The thread
moves to the BLOCKED or WAITING state.
• Timed Waiting: The thread is waiting for a specific amount of time before
resuming its execution. It moves to the TIMED_WAITING state.
• Terminated/Dead: The thread has completed its task or has been terminated.
Once terminated, it cannot be restarted. The thread moves to the
TERMINATED state.
• Single Inheritance:
Java supports single inheritance, where a class can inherit from only one superclass.
This means that a class can have only one immediate parent.
class Animal {
// Members and methods related to animals
}
• Multilevel Inheritance:
In multilevel inheritance, a class is derived from another class, which is itself
derived from yet another class.
class Vehicle {
// Members and methods related to vehicles
}
• Multiple Inheritance
Single child class will have two or more parent class or base class.
• Hybrid Inheritance
It is the combination of multiple and Hierarchical inheritance.
10. What is the use of layout manager in Java? Explain all the layouts briefly.
Answer:
Java Layout Manager
The Layout manager is used to layout (or arrange) the GUI Java components inside a
container. There are many layout managers, but the most frequently used are-
• Java BorderLayout
A BorderLayout places components in up to five areas: top, bottom, left, right, and
center. It is the default layout manager for every java JFrame
Example:
import java.awt.*;
class BorderLayoutExample extends Frame
{
BorderLayoutExample()
{
setLayout(new BorderLayout());
add(new Button("NORTH"),BorderLayout.NORTH);
add(new Button("SOUTH"),BorderLayout.SOUTH);
add(new Button("EAST"),BorderLayout.EAST);
add(new Button("WEST"),BorderLayout.WEST);
add(new Button("CENTER"),BorderLayout.CENTER);
}
}
class BorderLayoutJavaExample
{
public static void main(String args[])
{
BorderLayoutExample frame = new BorderLayoutExample();
frame.setTitle("BorderLayout in Java Example");
frame.setSize(400,150);
frame.setVisible(true);
}
}
• Java FlowLayout
FlowLayout is the default layout manager for every JPanel. It simply lays out
components in a single row one after the other.
Constructors
FlowLayout() : Creates a default FlowLayout manager: centered alignment,
horizontal spacing and 5 vertical units.
FlowLayout(int) : Creates a FlowLayout manager with alignment and given horizontal
and vertical spacing of 5 units.
FlowLayout(int, int, int) : Creates a FlowLayout manager with alignment, horizontal
and vertical spacing data.
Example:
import java.awt.*;
class FlowLayoutExample extends Frame
{
FlowLayoutExample()
{
Button[] button =new Button[10];
setLayout(new FlowLayout());
for(int i=0;i<button.length;i++)
{
button[i]=new Button("Button "+(i+1));
add(button[i]);
}
}
}
class FlowLayoutJavaExample
{
public static void main(String args[])
{
FlowLayoutExample frame = new FlowLayoutExample();
frame.setTitle("FlowLayout in Java Example");
frame.setSize(400,150);
frame.setVisible(true);
}}
• Java GridBagLayout
It is the more sophisticated of all layouts. It aligns components by placing them
within a grid of cells, allowing components to span more than one cell.
GridLayout ()
Creates a GridLayout manager with a default row and a column.
GridLayout (int, int)
Creates a GridLayout manager with the number of rows and columns specified.
GridLayout (int, int, int, int)
Creates a GridLayout manager with the number of specified rows and columns and
alignment, horizontal and vertical spacing data.
Example:
import java.awt.*;
class GridLayoutExample extends Frame
{
GridLayoutExample()
{
Button[] button =new Button[12];
setLayout(new GridLayout(4,3));
for(int i=0; i<button.length;i++)
{
button[i]=new Button("Button "+(i+i));
add(button[i]);
}
}
}
class GridLayoutJavaExample
{
public static void main(String args[])
{
GridLayoutExample frame = new GridLayoutExample();
frame.setTitle("GridLayout in Java Example");
frame.setSize(400,150);
frame.setVisible(true);
}
}
• Exceptions
When executing Java code, different errors can occur: coding errors made by the
programmer, errors due to wrong input, or other unforeseeable things.
When an error occurs, Java will normally stop and generate an error message. The
technical term for this is: Java will throw an exception (throw an error).
public class Main {
public static void main(String[ ] args) {
int[] myNumbers = {1, 2, 3};
System.out.println(myNumbers[10]); // error!
}
}
Output :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Main.main(Main.java:4)
The try statement allows you to define a block of code to be tested for errors while it
is being executed.
The catch statement allows you to define a block of code to be executed, if an error
occurs in the try block.
The try and catch keywords come in pairs:
• Syntax
try {
// Block of code to try
}
catch(Exception e) {
// Block of code to handle errors
}
• Public:
Members declared as public are accessible from any class and package.
There are no access restrictions.
• Protected:
Members declared as protected are accessible within the same class, subclasses
(even if they are in different packages), and classes within the same package.
Outside the package, protected members are not accessible.
• Default (package-private):
Members with no explicit access specifier (default) are accessible only within the
same package.
They are not accessible outside the package, even if the classes are subclasses.
• Private:
Members declared as private are only accessible within the same class.
They are not accessible in subclasses or from other classes.
(b)
In this diagram, the arrows indicate the flow of control as the applet progresses
through its life cycle. The applet starts with start() and paint() while in view, pauses with
stop() when not visible, and eventually terminates with destroy().
14. (a) What is a class? Explain object-creation in Java. (Give Example)
(b) What is an array? Explain arrays in Java with example.
Answer:
(a):
• Class:
In Java, a class is a blueprint or template for creating objects. It defines the
structure and behavior of objects that will be created based on the class. A class
defines fields (variables) to store data and methods (functions) to perform actions on
the data. Objects are instances of a class, and they can have their own unique data
while sharing the methods and behavior defined by the class.
• Object-Creation:
Creating an object in Java involves two main steps: defining a class and then
instantiating (creating) an object based on that class. To create an object, you use the
new keyword followed by the class constructor
Example:
Class BCA{
Public static void main(String[] args)
{
BCA a=new BCA();
System.out.println(HI BCA);
}
Here a class with name BCA is created and an object with a name a is created in
the class BCA
(b):
Arrays in Java:
An array is a user defined datatype that can multiple value of same datatype.
Syntax of initialization:
dataType[] arrayName = {element1, element2, ...};
example:
int[] numbers = {10, 20, 30, 40, 50};
15.
(a) Mention any 6 GUI components in Java.
(b) Explain Switch-case with syntax and example.
Answer:
(a):
• JButton:
• JTextField:
• JLabel:
• JComboBox:
• JCheckBox:
• JRadioButton:
(b):
The switch statement in Java provides a way to simplify decision-making based
on the value of an expression. It allows you to select and execute one block of
code from a list of possibilities, improving the readability and efficiency of your
code when dealing with multiple conditions.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
// ... more cases ...
default:
// Code to execute if no cases match
}
Example:
public class SwitchExample {
public static void main(String[] args) {
int day = 2;
String dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
}
• Byte Streams:
Byte streams are used for handling raw binary data, such as images, audio files, and
non-text files. They are represented by classes that end with InputStream or
OutputStream.
• Character Streams:
Character streams are used for handling character data, such as text files, where
character encoding matters. They are represented by classes that end with Reader or
Writer.
17.
Write short notes on:
(a) Generic programming
(b) Collections
Answers:
(a):
Generic Programming in Java:
Generic programming in Java allows you to create classes, interfaces, and methods that
work with various data types while maintaining type safety.
Advantages of Generic Programming:
• Type Safety: Generics ensure that data types are checked at compile time,
reducing the risk of type-related errors at runtime.
• Code Reusability: Generic classes and methods can be reused with different
data types, reducing code duplication.
• Performance: Generics can improve performance by avoiding the need for type
casting and allowing the compiler to optimize code.
Example:
class Test<T> {
T obj;
Test(T obj) { this.obj = obj; }
public T getObject() { return this.obj; }
}class Main {
public static void main(String[] args)
{
Test<Integer> iObj = new Test<Integer>(15);
System.out.println(iObj.getObject());
Test<String> sObj= new Test<String>("Hi BCA");
System.out.println(sObj.getObject());
}
}
Output:
15
Hi BCA
(b):
Collections in Java:
Collections in Java provide classes and interfaces to store, manage, and manipulate
groups of objects.
Advantages of Collections:
18.
(a) Explain all object-oriented concepts supported by Java.
(b) What is the difference between String Class and String Buffer Class. Give
examples.
Answers:
(a):
• String Class:
- Strings created with the `String` class are immutable, which means their
content cannot be changed after creation.
- When you modify a `String`, a new `String` object is created, which can be
inefficient for frequent modifications.
- Suitable when you need a fixed, unchanging text.
• StringBuffer Class:
- `StringBuffer` objects are mutable, allowing you to modify their content
without creating new objects.
- This makes `StringBuffer` more efficient for frequent string modifications,
as it avoids unnecessary memory overhead.
- Suitable when you need to manipulate strings frequently.