Java IMP Questions Solutions
Java IMP Questions Solutions
Short QA
a) Define excep on: An excep on in Java is an event that disrupts the normal flow of a program's
instruc ons during execu on. It represents an abnormal condi on that requires special handling.
b) Define Interface: An interface in Java is a collec on of abstract methods and constants. It defines a
contract that classes must follow when implemen ng the interface. It's a way to achieve abstrac on
and mul ple inheritances in Java.
c) What is Javadoc? Javadoc is a tool in Java used for genera ng API documenta on in HTML format
from Java source code. It helps in documen ng the code by adding comments in a specific format
that can be processed by the Javadoc tool.
d) What is AWT? AWT stands for Abstract Window Toolkit. It's a part of Java's standard library used
for crea ng graphical user interfaces (GUIs) for Java programs. It provides various components like
bu ons, menus, and windows.
e) What is the use of the sta c keyword? In Java, the sta c keyword is used to create variables or
methods that belong to the class itself rather than instances of the class. It allows access without
crea ng an object of the class.
f) What is a command line argument? Command line arguments are the parameters passed to a Java
program when it's invoked from the command line. These arguments are accessed within the main
method's parameter list.
g) List the types of constructor: In Java, there are three types of constructors: default (no-argument)
constructor, parameterized constructor, and copy constructor.
h) What is a package? A package in Java is a namespace that organizes a set of related classes and
interfaces. It helps in avoiding naming conflicts and provides access protec on.
i) How to open a file in read mode? In Java, you can open a file in read mode using classes like File,
FileReader, or BufferedReader. Here's an example using FileReader:
j) List any two listeners: Two types of listeners in Java are Ac onListener and MouseListener.
k) What is the use of Javac? javac is the Java compiler that translates Java source code into
bytecode, which can be executed by the Java Virtual Machine (JVM).
l) Give the name of any two wrapper classes. Two wrapper classes in Java are Integer and Boolean.
These classes wrap primi ve data types into objects.
n) List types of constructors. Constructors in Java can be of three types: default (no-argument)
constructor, parameterized constructor (with arguments), and copy constructor (which ini alizes an
object by copying another object of the same class).
o) What is the use of an Array? An array in Java is used to store a fixed-size sequen al collec on of
elements of the same type. It allows efficient manipula on and access to a group of variables.
p) What is an excep on? An excep on in Java is an object that represents an abnormal condi on
that arises during the execu on of a program. It disrupts the normal flow of the program.
q) Give the syntax of the ends with( ) method? The endsWith() method checks whether a string
ends with a specified suffix. Its syntax is: boolean endsWith(String suffix).
r) What is the use of the new operator? The new operator is used in Java to create new objects. It
allocates memory for an object and returns a reference to that memory loca on. For example,
ClassName obj = new ClassName(); creates an object of type ClassName.
Long QA
A constructor in Java is a special type of method that ini alizes an object of a class. It is called
implicitly whenever an object of that class is created using the new keyword. The constructor is
responsible for ini alizing the instance variables or performing any necessary setup for the object. If
there are mul ple constructors within a class, the appropriate constructor is invoked based on the
arguments provided or the absence of arguments when the object is created.
Command line arguments are the parameters passed to a Java program when it's executed from the
command line. They are provided a er the name of the class when invoking the java command.
These arguments are stored as strings in the args parameter of the main method:
1. setTitle(String tle): This method sets the tle of the frame window.
2. setVisible(boolean visible): It makes the frame visible if the argument passed is true;
otherwise, it hides the frame.
Method Overloading: It occurs when mul ple methods in the same class have the same
name but different parameters (number, type, or order). Overloaded methods are
differen ated by their method signatures. It allows a class to have mul ple methods with the
same name but different behaviors.
Access specifiers in Java determine the accessibility or visibility of classes, methods, and variables in
different contexts. Two access specifiers are:
These keywords control the visibility of members of a class to other classes and are used to enforce
encapsula on and maintain code security and integrity.
1. append(String str): This method is used to append the specified string to the end of the
StringBuffer object.
2. insert(int offset, String str): It inserts the specified string into the StringBuffer object at the
specified offset.
The StringBuffer class in Java is used to create mutable sequences of characters. It is similar to String
but allows modifica ons to the contents.
The throw keyword in Java is used to explicitly throw an excep on within a method or block of code.
It is used to indicate excep onal condi ons that might occur during the execu on of a program.
When a throw statement is encountered, it throws an excep on object, which can be caught and
handled using try-catch blocks.
final keyword: In Java, final is a keyword used to apply restric ons on classes, methods, and
variables. When applied to a class, it prevents the class from being subclassed. When applied
finally keyword: finally is a block associated with excep on handling. It is used in a try-catch-
finally block. The code inside the finally block always executes, regardless of whether an
excep on is thrown or not. It is commonly used for cleanup opera ons like closing resources
(files, database connec ons) to ensure they are released even in case of excep ons.
Method overloading in Java occurs when mul ple methods in the same class have the same name
but different parameters (number, type, or order). It allows a class to have mul ple methods with the
same name but different behaviors based on the parameters passed. The compiler differen ates
between overloaded methods based on their method signatures.
An anonymous inner class in Java is a class without a name that is defined and instan ated
simultaneously using the new keyword. It is typically used when a class needs to be used only once
for a specific purpose. Anonymous inner classes are usually used in event handling (like listeners) or
to override methods of a class or interface.
For example:
};
Was the original Java GUI toolkit but Became the primary GUI toolkit for Java
Popularity has been largely replaced by Swing. due to its extensive features and flexibility.
In Java, the super keyword is used to refer to the immediate parent class object or invoke its
constructor or method. It's par cularly useful in scenarios involving inheritance, where a subclass
wants to access the members (fields or methods) of its superclass.
Uses of super:
1. Accessing Superclass Members: The super keyword is used to access superclass members
(fields or methods) that are overridden or hidden by a subclass.
class Parent {
int number = 10;
void display() {
System.out.println("Inside Parent class");
}
}
Prac ce Programs
a) Java program using AWT to change background color of table to 'RED' by clicking on bu on:
import java.awt.*;
import java.awt.event.*;
public BackgroundColorChange() {
panel = new Panel();
changeColor = new Button("Change Color");
changeColor.addActionListener(this);
// Table creation
TextArea table = new TextArea("This is a table.", 10, 30);
panel.add(table);
add(panel);
setSize(400, 300);
setTitle("Change Table Color");
setVisible(true);
}
b) Java program to copy content from one file into another file, replacing digits with '*':
import java.io.*;
reader.close();
writer.close();
System.out.println("Content copied successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
d) Java program to accept a number, calculate its factorial, and handle a user-defined excep on.
class ZeroNumberException extends Exception {
ZeroNumberException(String message) {
super(message);
}
}
while (num != 0) {
firstDigit = num % 10;
num /= 10;
}
g) Java program to accept n numbers from the user and store only perfect numbers in an array
import java.util.*;
class Emp {
int Eid;
void display() {
System.out.println("Employee ID: " + Eid);
}
}
void display() {
super.display();
System.out.println("Employee Name: " + Ename);
}
}