Differnt Quetsion Answer Java
Differnt Quetsion Answer Java
Question 2:
a) Write a program to differentiate the feature of static variable and instance variable. (3 BL, 2 CO)
```java
int instanceVariable;
obj1.instanceVariable = 10;
obj2.instanceVariable = 20;
VariableExample.staticVariable = 100;
```
1. **Private**: The members with private access specifier are accessible only within the same class.
2. **Default (no specifier)**: If no access specifier is specified, it is treated as default. Members with
default access specifier are accessible within the same package.
3. **Protected**: Protected members are accessible within the same package and by the subclasses
even if they are in a different package.
```java
class Box {
Box() {
width = w;
height = h;
depth = d;
Box(double len) {
class ConstructorOverloadingExample {
```
Question 3:
```java
class Car {
int carNo;
double price;
carNo = no;
model = m;
brand = b;
color = c;
price = p;
void display() {
myCar.display();
```
```java
class Flight {
int flightNo;
double fare;
flightNo = no;
flightName = name;
sourceCity = source;
destination = dest;
fare = f;
void showData() {
Flight myFlight = new Flight(123, "XYZ Airlines", "New York", "London", 1000.50);
myFlight.showData();
```
**Module-III**
Question 1:
Inheritance in Java allows one class to inherit the properties and behavior of another class.
- **Base Class**: The class whose features are inherited is known as the base class or superclass.
- **Derived Class**: The class that inherits the properties and behavior of another class is known as
the derived class or subclass.
b) Explain the use of the super keyword regarding the constructor in inheritance. (2 BL, 3 CO)
The `super` keyword in Java is used to refer to the immediate parent class object. It is used to call the
constructor of the parent class and can be used to access the parent class's methods and fields.
```java
class A {
A() {
class B extends A {
B() {
```
c) Write two differences between method overriding and method overloading. (2 BL, 3 CO)
**Method Overloading**:
2. Method overloading has the same method name but different signatures.
**Method Overriding**:
2. Method overriding has the same method name, same arguments, and same return type.
Question 3:
```java
class A {
void display() {
class B extends A {
void show() {
class SingleInheritanceExample {
```
b) Define an abstract class Person having data members aadharno, name, address, and mobile no
Member methods:
```java
aadharNo = aadhar;
name = n;
address = addr;
mobileNo = mobile;
int empNo;
double salary;
Employee(String aadhar, String n, String addr, String mobile, int emp, double sal) {
empNo = emp;
salary = sal;
void display() {
class AbstractClassExample {
emp.display();
```
**Module-IV**
Question 1:
An exception is an event that disrupts the normal flow of the program's instructions.
- ArithmeticException
- NullPointerException
b) Write the difference between equals() and compareTo() functions of the String library class. (2 BL,
4 CO)
c) Find Output:
```java
System.out.println(S1.substring(0, 4).concat(S2.substring(4)));
System.out.println(S1.compareTo(S2));
```
Output:
```
HARDWORK
-3
```
Question 2:
a) Write a program in Java to illustrate exception handling using try and catch block. (3 BL, 4 CO)
```java
try {
} catch (ArrayIndexOutOfBoundsException e) {
```
b) Write the difference between throw and throws with an example. (2 BL, 4 CO)
```java
class AgeException extends Exception {
AgeException(String s) {
super(s);
class Test {
else
System.out.println("Welcome to vote");
try {
validate(13);
} catch (AgeException e) {
```
Question 3:
a) Write a program to input a Sentence and print the number of capital letters, small letters, digits,
and special symbols. (3 BL, 4 CO)
```java
import java.util.Scanner;
public class CharacterCount {
scanner.close();
char ch = sentence.charAt(i);
if (Character.isUpperCase(ch))
upperCase++;
else if (Character.isLowerCase(ch))
lowerCase++;
else if (Character.isDigit(ch))
digits++;
else
specialChars++;
```
b) Write a Program to input a String in uppercase and print whether or not it is a PALINDROME.
```java
import java.util.Scanner;
scanner.close();
reverse += str.charAt(i);
if (str.equals(reverse))
System.out.println("Palindrome");
else
System.out.println("Not Palindrome");
```
**Module-V**
Question 1:
a) What do you know by wrapper classes? Write the wrapper class corresponding to char and int
type. (1 BL, 5 CO)
Wrapper classes provide a way to use primitive data types as objects. Wrapper class for int is
`Integer` and for char is `Character`.
```java
Character ch = 'a';
```
b) Explain the concept of boxing and unboxing with an example. (2 BL, 5 CO)
**Boxing**: Converting a primitive data type into its corresponding wrapper class object.
**Unboxing**: Converting an object of a wrapper class to its corresponding primitive data type.
```java
// Boxing
// Unboxing
```
c) What is an applet? Draw the state transition diagram of an applet. (2 BL, 5 CO)
An applet is a Java program that can be embedded into a web page. It runs inside the web browser
and works at the client-side.
```
+---------+
start -->| Running |
```
+---------+
+-----------+
| Suspended |
+-----------+
+--------+
| Destroy|
+--------+
```
Question 2:
**Applet**:
**Application**:
```
Object
|
Component
/ | \
/ \ / \ / \
\ /
FileDialog ColorChooser
```
**AWT Components**:
**Swing Components**:
Question 3:
a) Explain all methods responsible for maintaining the life cycle of an applet with a state transition
diagram. (2 BL, 5 CO)
1. **init()**: Called to initialize the applet. It is called once in the lifespan of an applet.
2. **start()**: Called after the init() method to start the execution of the applet. It is called every
time the applet is started.
3. **stop()**: Called when the execution of the applet is paused. It is called every time the applet is
stopped.
```
+--------+
| init |
+--------+
+---------+
| start |
+---------+
+--------+
| stop |
+--------+
+---------+
| destroy |
+---------+
```
b) Write a Java program to design a login screen with appropriate label, TextField, and Buttons. (3 BL,
5 CO)
```java
import java.awt.*;
import java.awt.event.*;
public class LoginScreen extends Frame implements ActionListener {
LoginScreen() {
tf2.setEchoChar('*');
b1 = new Button("Login");
b2 = new Button("Cancel");
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);
add(tf2);
add(b1);
add(b2);
setSize(300, 300);
setLayout(null);
setVisible(true);
System.out.println("Login Successful!");
} else {
System.out.println("Login Failed!");
tf1.setText("");
tf2.setText("");
new LoginScreen();
```