0% found this document useful (0 votes)
20 views19 pages

Differnt Quetsion Answer Java

Uploaded by

surajothers
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)
20 views19 pages

Differnt Quetsion Answer Java

Uploaded by

surajothers
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/ 19

**Module-II**

Question 2:

a) Write a program to differentiate the feature of static variable and instance variable. (3 BL, 2 CO)

```java

public class VariableExample {

int instanceVariable;

static int staticVariable;

public static void main(String[] args) {

VariableExample obj1 = new VariableExample();

VariableExample obj2 = new VariableExample();

obj1.instanceVariable = 10;

obj2.instanceVariable = 20;

VariableExample.staticVariable = 100;

System.out.println("Instance variable: " + obj1.instanceVariable);

System.out.println("Static variable: " + VariableExample.staticVariable);

```

b) Discuss all types of access specifiers used in Java. (2 BL, 2 CO)

In Java, there are four types of access specifiers:

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.

4. **Public**: Public members are accessible from anywhere.

c) Explain constructor overloading with an example. (2 BL, 2 CO)

```java

class Box {

double width, height, depth;

// Constructor with no parameters

Box() {

width = height = depth = 0;

// Constructor with three parameters

Box(double w, double h, double d) {

width = w;

height = h;

depth = d;

// Constructor with one parameter (for creating cube)

Box(double len) {

width = height = depth = len;

class ConstructorOverloadingExample {

public static void main(String[] args) {


Box myBox1 = new Box(10, 20, 15);

Box myBox2 = new Box();

Box myCube = new Box(7);

```

Question 3:

a) Define a class Car with the following specifications:

```java

class Car {

int carNo;

String model, brand, color;

double price;

void input(int no, String m, String b, String c, double p) {

carNo = no;

model = m;

brand = b;

color = c;

price = p;

void display() {

System.out.println("Car No: " + carNo);

System.out.println("Model: " + model);

System.out.println("Brand: " + brand);

System.out.println("Color: " + color);

System.out.println("Price: " + price);


}

public class CarExample {

public static void main(String[] args) {

Car myCar = new Car();

myCar.input(1234, "XYZ", "ABC", "Red", 500000.50);

myCar.display();

```

b) Define a class Flight with the following specifications:

```java

class Flight {

int flightNo;

String flightName, sourceCity, destination;

double fare;

Flight(int no, String name, String source, String dest, double f) {

flightNo = no;

flightName = name;

sourceCity = source;

destination = dest;

fare = f;

void showData() {

System.out.println("Flight No: " + flightNo);

System.out.println("Flight Name: " + flightName);


System.out.println("Source City: " + sourceCity);

System.out.println("Destination: " + destination);

System.out.println("Fare: " + fare);

public class FlightExample {

public static void main(String[] args) {

Flight myFlight = new Flight(123, "XYZ Airlines", "New York", "London", 1000.50);

myFlight.showData();

```

**Module-III**

Question 1:

a) Explain inheritance, base class, and derived class. (2 BL, 3 CO)

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() {

System.out.println("Constructor of class A");

class B extends A {

B() {

super(); // calls the constructor of class A

System.out.println("Constructor of class B");

```

c) Write two differences between method overriding and method overloading. (2 BL, 3 CO)

**Method Overloading**:

1. Method overloading occurs in the same class.

2. Method overloading has the same method name but different signatures.

**Method Overriding**:

1. Method overriding occurs in two classes that have an inheritance relationship.

2. Method overriding has the same method name, same arguments, and same return type.

Question 3:

a) Write an example code illustrating single inheritance (3 BL, 3 CO)

```java
class A {

void display() {

System.out.println("Inside Class A");

class B extends A {

void show() {

System.out.println("Inside Class B");

class SingleInheritanceExample {

public static void main(String[] args) {

B obj = new B();

obj.display(); // calling method of class A

obj.show(); // calling method of class B

```

b) Define an abstract class Person having data members aadharno, name, address, and mobile no
Member methods:

```java

abstract class Person {

String aadharNo, name, address, mobileNo;

Person(String aadhar, String n, String addr, String mobile) {

aadharNo = aadhar;

name = n;
address = addr;

mobileNo = mobile;

abstract void display();

class Employee extends Person {

int empNo;

double salary;

Employee(String aadhar, String n, String addr, String mobile, int emp, double sal) {

super(aadhar, n, addr, mobile);

empNo = emp;

salary = sal;

void display() {

System.out.println("Aadhar No: " + aadharNo);

System.out.println("Name: " + name);

System.out.println("Address: " + address);

System.out.println("Mobile No: " + mobileNo);

System.out.println("Employee No: " + empNo);

System.out.println("Salary: " + salary);

class AbstractClassExample {

public static void main(String[] args) {


Employee emp = new Employee("123456789012", "John", "123, Main Street", "1234567890",
101, 50000.50);

emp.display();

```

**Module-IV**

Question 1:

a) Define an exception. Give two examples of RuntimeException. (1 BL, 4 CO)

An exception is an event that disrupts the normal flow of the program's instructions.

Examples of RuntimeException are:

- ArithmeticException

- NullPointerException

b) Write the difference between equals() and compareTo() functions of the String library class. (2 BL,
4 CO)

- **equals()**: It compares the content of the strings.

- **compareTo()**: It compares the strings lexicographically.

c) Find Output:

```java

String S1 = "HARDWARE", S2 = "HARDWORK";

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

public class ExceptionHandlingExample {

public static void main(String[] args) {

try {

int[] arr = new int[5];

arr[10] = 50; // Trying to access out of bounds

} catch (ArrayIndexOutOfBoundsException e) {

System.out.println("Array Index Out Of Bounds Exception");

```

b) Write the difference between throw and throws with an example. (2 BL, 4 CO)

- **throw**: Used to explicitly throw an exception within a method.

- **throws**: Used to declare an exception.

```java
class AgeException extends Exception {

AgeException(String s) {

super(s);

class Test {

static void validate(int age) throws AgeException {

if (age < 18)

throw new AgeException("Not valid age");

else

System.out.println("Welcome to vote");

public static void main(String[] args) {

try {

validate(13);

} catch (AgeException e) {

System.out.println("Exception occurred: " + 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 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a sentence: ");

String sentence = scanner.nextLine();

scanner.close();

int upperCase = 0, lowerCase = 0, digits = 0, specialChars = 0;

for (int i = 0; i < sentence.length(); i++) {

char ch = sentence.charAt(i);

if (Character.isUpperCase(ch))

upperCase++;

else if (Character.isLowerCase(ch))

lowerCase++;

else if (Character.isDigit(ch))

digits++;

else

specialChars++;

System.out.println("Uppercase letters: " + upperCase);

System.out.println("Lowercase letters: " + lowerCase);

System.out.println("Digits: " + digits);

System.out.println("Special characters: " + 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;

public class PalindromeCheck {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter a string in uppercase: ");

String str = scanner.nextLine();

scanner.close();

String reverse = "";

for (int i = str.length() - 1; i >= 0; i--) {

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';

Integer num = 10;

```

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

int num = 10;

Integer obj = Integer.valueOf(num);

// Unboxing

Integer obj2 = new Integer(10);

int num2 = obj2.intValue();

```

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:

a) Write differences between an applet and application program. (2 BL, 5 CO)

**Applet**:

1. Applets run within a web browser.

2. Applets have a graphical user interface.

**Application**:

1. Applications run on the client machine.

2. Applications require a main() method.

b) Write the hierarchy of all classes in AWT. (2 BL, 5 CO)

```

Object
|

Component

/ | \

Container Window Panel

/ \ / \ / \

Frame Dialog Frame Dialog ScrollPane

\ /

FileDialog ColorChooser

```

c) Write differences between Swing and AWT components. (2 BL, 5 CO)

**AWT Components**:

1. AWT components are heavyweight.

2. AWT doesn't support pluggable look and feel.

3. AWT components depend on the native platform.

**Swing Components**:

1. Swing components are lightweight.

2. Swing supports pluggable look and feel.

3. Swing components don't depend on the native platform.

Question 3:

a) Explain all methods responsible for maintaining the life cycle of an applet with a state transition
diagram. (2 BL, 5 CO)

**Life Cycle Methods**:

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.

4. **destroy()**: Called when the applet is destroyed.

State Transition Diagram:

```

+--------+

| 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 {

TextField tf1, tf2;

Button b1, b2;

LoginScreen() {

tf1 = new TextField("Username");

tf1.setBounds(50, 50, 150, 20);

tf2 = new TextField("Password");

tf2.setBounds(50, 100, 150, 20);

tf2.setEchoChar('*');

b1 = new Button("Login");

b1.setBounds(50, 150, 60, 30);

b2 = new Button("Cancel");

b2.setBounds(120, 150, 70, 30);

b1.addActionListener(this);

b2.addActionListener(this);

add(tf1);

add(tf2);

add(b1);

add(b2);

setSize(300, 300);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {


if (e.getSource() == b1) {

String username = tf1.getText();

String password = tf2.getText();

if (username.equals("admin") && password.equals("admin123")) {

System.out.println("Login Successful!");

} else {

System.out.println("Login Failed!");

} else if (e.getSource() == b2) {

tf1.setText("");

tf2.setText("");

public static void main(String[] args) {

new LoginScreen();

```

You might also like