0% found this document useful (0 votes)
1 views

OOP_Java_I_Q2_to_Q5_Detailed_Answers

The document contains a series of programming questions and answers related to Java, covering topics such as constructors, command line arguments, GUI components, method overloading and overriding, access specifiers, interfaces, AWT vs Swing, user-defined exceptions, and the final keyword. It includes code snippets and explanations for creating a triangle area calculator, a student details form, file content manipulation, and handling exceptions. Additionally, it discusses class inheritance and the use of the super keyword in Java.
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)
1 views

OOP_Java_I_Q2_to_Q5_Detailed_Answers

The document contains a series of programming questions and answers related to Java, covering topics such as constructors, command line arguments, GUI components, method overloading and overriding, access specifiers, interfaces, AWT vs Swing, user-defined exceptions, and the final keyword. It includes code snippets and explanations for creating a triangle area calculator, a student details form, file content manipulation, and handling exceptions. Additionally, it discusses class inheritance and the use of the super keyword in Java.
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/ 5

Q2) Attempt any FOUR of the following.

[4 × 2 = 8]

a) 'When constructor of class will be called?' Comment.

Answer: A constructor is called automatically at the time of object creation. It is used to initialize the object. For example,

when we write: MyClass obj = new MyClass(); the constructor of MyClass is invoked.

b) What is command line argument? Where they are stored in a program.

Answer: Command line arguments are the arguments passed to the program when it is executed from the command

line. They are stored in the String array parameter of the main method:

public static void main(String[] args)

c) What is Frame? Give its any two methods.

Answer: A Frame is a top-level window with a title and a border used in Java AWT. It is used to create GUI applications.

Two methods of Frame:

- setSize(int width, int height): sets the size.

- setVisible(boolean): shows or hides the frame.

d) Differentiate between method overloading and method overriding.

Answer: Method Overloading:

- Occurs within the same class.

- Same method name with different parameters.

Method Overriding:

- Occurs in subclass.

- Subclass provides specific implementation of a method from the superclass.

e) Write any two access specifiers.

Answer: Two access specifiers in Java:

- public: accessible from any class.

- private: accessible only within the declared class.

Q3) Attempt any TWO of the following. [2 × 4 = 8]


a) Define an interface shape with abstract method area(). Inherit interface shape into the class triangle. Write a

Java Program to calculate area of Triangle.

Answer:

interface Shape {

void area();

class Triangle implements Shape {

double base, height;

Triangle(double b, double h) {

base = b;

height = h;

public void area() {

System.out.println("Area: " + (0.5 * base * height));

b) Design the following screen by using swing (Student details form).

Answer:

- Use JFrame for the window

- JTextFields for Roll No, Name, and Percentage

- Two JButtons: 'Display' and 'Clear'

- On clicking 'Display', show details in console

- On clicking 'Clear', empty all fields

c) Write a Java Program to copy the contents from one file into another. Change case of all alphabets & replace

all digits by '*'.


Answer:

// Java logic:

- Read input file character by character

- If digit: replace with '*'

- If letter: toggle case

- Write to output file

Q4) Attempt any TWO of the following. [2 × 4 = 8]

a) Differentiate between AWT & Swing.

Answer:

AWT:

- Heavyweight components

- Uses native OS GUI

- Limited features

Swing:

- Lightweight components

- Written in Java, platform-independent

- Rich UI components

b) Define user-defined exception ZeroNumberExc. Write a program to handle zero input and find sum of first &

last digit otherwise.

Answer:

class ZeroNumberExc extends Exception {

ZeroNumberExc(String msg) {

super(msg);

}
// Logic:

- Accept number

- If zero, throw exception

- Else, extract first and last digit and add

c) Write a Java program to store perfect numbers in array and display them.

Answer:

// Logic:

- Accept 'n' numbers

- Check each for perfectness (sum of divisors equals number)

- Store in array and display

Q5) Attempt any ONE of the following. [1 × 3 = 3]

a) Explain uses of final keyword with example.

Answer:

- final variable: cannot be changed once assigned.

- final method: cannot be overridden.

- final class: cannot be extended.

Example:

final int x = 10;

b) Define a class Emp with member Eid and display() method. Inherit Emp into EmpName class with member

Ename. Use super keyword.

Answer:

class Emp {

int Eid;

void display() {

System.out.println("Eid: " + Eid);


}

class EmpName extends Emp {

String Ename;

void display() {

super.display();

System.out.println("Ename: " + Ename);

You might also like