Java Paper Solutions
Java Paper Solutions
class Example {
int x;
Example(int x) {
this.x = x; // Refers to the class variable
}
}
3) Explain in detail the data types in java?
Data types are used to define the type of data that a variable can hold. They are broadly
categorized into Primitive Data Types and Non-Primitive Data Types.
4) What is an Interface?
An interface is a collection of methods that a class must implement.
An interface in Java is a blueprint of a class that contains abstract methods (methods
without a body) and static constants (final variables). It is used to achieve abstraction and
multiple inheritance in Java.
5) What is the use of Reader and Writer class?
In Java, Reader and Writer classes are used for reading and writing text data (character
streams). They are part of the java.io package and are specifically designed to handle
character-based data. For Example, FileReader and FileWriter.
6) Which method is used to specify containers layout with syntax.
setLayout () method specifies the layout of a container.
Syntax: container. setLayout (new LayoutManager());
7) What is the default layout for Frame and Panel?
Border Layout and Flow Layout are the default layout for Frame and Panel respectively.
8) Explain Modifiers and Access Controls used in java.
Modifiers and access controls in Java help define the behavior and visibility of classes,
methods, and variables.
Modifiers change the behavior of methods, variables, or classes.
Access Controls define the visibility of a class, method, or variable.
Example: MODIFIER- static, final, abstract. ACCESS CONTROLS- public, private, protected and
default.
9) List and explain any 2 in-built exceptions.
NullPointerException: When an object reference is null.
ArrayIndexOutOfBoundsException: When accessing an invalid array index.
10) Explain the purpose of getContentPane ().
In Swing, when you're creating a GUI (like a window with buttons, labels, etc.) using JFrame,
the content pane is where you add all the components like buttons, labels, or panels.
getContentPane () is a method of JFrame that gives you access to the content pane of the
frame. Think of the content pane as a canvas where you place all your GUI components.
By default, you can't directly add components (like buttons) to a JFrame. Instead, you add
them to its content pane, which is like a special layer inside the frame.
11) What is Java? Why Java is a platform neutral language?
Java is a programming language and platform-independent because it uses the JVM (Java
Virtual Machine) to run compiled .class files on any OS.
12) Define Keyword-Static.
In Java, the static keyword means that a member (variable, method, or block) belongs to the
class rather than any specific object. This means:
Static members are shared across all instances of the class.
You can access static members without creating an object of the class.
13) Why we set environment variable in Java?
When working with Java, setting environment variables is like giving your computer
instructions on where to find Java-related tools and libraries.
It ensures your system knows how to run Java programs and compile code without errors.
To run Java programs, the PATH environment variable helps locate the javac and java
commands.
14) Write advantages of Inheritance.
Reuse code without writing it all over again.
Easier maintenance because fixing one class updates all the child classes.
Logical structure to organize your classes.
Custom behavior using method overriding.
Extendibility to create new features easily.
15) Define class and object with one example.
Classes are the blueprint containing the member functions and data members.
Objects are the copy or instance of the class.
class Car {}
Car myCar = new Car (); // Object
16) What is Swing?
Swing is a GUI (Graphical User Interface) toolkit in Java that allows you to create windows,
buttons, text fields, and other interactive components for desktop applications.
It is used to create graphical user interfaces (GUIs) that users can interact with, rather
than just using a command-line interface.
17) When buffered reader is used?
In Java, BufferedReader is used to read text from a character-based input stream efficiently.
It's especially useful when you need to read large amounts of data from a file or user
input because it reads data in chunks, making it faster than reading one character at a
time.
18) What is main difference between exception and error?
19) What is Panel?
A Panel in Java is a container used to organize and group other components like buttons,
text fields, and labels inside a graphical user interface (GUI).
It's part of the Swing library, which is used for creating windows and interactive
elements in desktop applications.
20) What is JDK? How to build and run java program?
JDK (Java Development Kit) is a software development kit that provides everything you need
to develop Java applications. It includes tools and libraries that allow you to write, compile,
debug, and run Java programs.
JDK can be built through:
- Install JDK: Make sure JDK is installed on your system.
- Write the Program: Use any text editor or IDE to write your .java file.
- Compile: Use the javac command to compile your .java file into a .class file.
- Run: Use the java command to run your compiled program.
21) What is use of class path?
In Java, the class path is a list of directories or JAR files where the Java Virtual Machine
(JVM) looks for compiled Java classes and libraries when running a program.
Essentially, it's like a map that tells the JVM where to find the code (or external libraries)
it needs in order to run your Java application.
22) What is collection? Explain collection frame work in details.
In Java, a collection is a group of objects that are stored and managed together.
It is a way of storing data in a structured manner, allowing you to work with multiple
objects as a single unit.
Collections are like containers for storing objects, such as lists, sets, and maps, where
you can add, remove, and manipulate data.
23) What is the use of layout manager?
In Java, a layout manager is responsible for organizing and positioning components (like
buttons, text fields, labels, etc.) in a container (like a window, panel, or frame) in a
structured way.
The layout manager automatically handles the size and position of each component, so
you don't have to manually set their positions and sizes.
24) What is difference between paint () and repaint ().
25) Define keyword throw.
The throw keyword in Java is used to explicitly throw an exception.
It allows you to create and throw your own exceptions in the program whenever a
certain condition occurs.
By using throw, you can signal that something unexpected or incorrect has happened in
the program, and the exception can be handled appropriately by the program's error-
handling mechanism.
26) Define polymorphism.
Polymorphism is one of the best features of object-oriented programming language.
Polymorphism means the ability to take more than one form.
Example: Method Overloading and Overriding.
27) Define variable in Java? What are the naming rules of variable?
In Java, a variable is a container that holds a value, such as a number, text, or Boolean value,
during the execution of a program.
dataType variableName = value; // Declaration and initialization
Naming Rules of Variables are:
- Start with a letter, underscore (_), or dollar sign ($)
- No Java reserved keywords can be used as variable names.
- Variable Names are case-sensitive
- Names should be meaningful.
28) What is recursion?
Recursion in Java (and in programming in general) is a technique where a function calls itself
in order to solve a problem. This technique is used to break down a complex problem into
smaller, simpler problems of the same kind.
29) Define Inheritance?
Inheritance is a concept in Object-Oriented Programming (OOP) where a new class (called a
child or subclass) can inherit properties and behaviors (methods) from an existing class
(called a parent or superclass). It allows you to create a new class based on an existing class,
reducing code duplication and increasing code reusability.
30) What is difference between Array and Array List?
31) What is error? List types of error?
In Java, an error is a type of problem that occurs during the execution of a program. Errors
are usually caused by severe issues in the program or the environment in which the program
runs. Unlike exceptions, errors typically cannot be recovered from using normal program
logic.
Following are the types of errors:
- Syntax Errors: Occurs due to incorrect syntax; must be fixed before compilation.
- Logical Errors: Program runs but produces incorrect output due to wrong logic.
- Runtime Errors: Occurs during program execution; can be handled by code.
32) List any two restrictions for applet.
Cannot Access the Local File System: Applets cannot access files on your computer. They
cannot read or write files from your hard drive directly.
Limited Access to GUI (Graphical User Interface): Applets can only interact with the
graphical user interface (GUI) inside the browser's or viewer's window and have limited
control over how the GUI works.
33) What is an event?
An event is an action or occurrence that the program needs to respond to. These actions
can be things like a mouse click, a keyboard press, or even something like a window resizing.
34) Write the definition of abstract class?
An abstract class in Java is a class that cannot be instantiated on its own and can contain
both abstract methods (methods without implementation) and concrete methods (methods
with implementation).
It provides a blueprint for other classes that extend it, forcing them to implement the
abstract methods.
35) What is Container?
A container in Java is a component that holds other components (like buttons, labels, text
fields).
It helps in organizing and managing the layout of GUI elements, making it easier to
create complex user interfaces.
Containers can use layout managers to automatically position the components inside
them.
Example: Frame, Panel, Dialog.
2) What are the rules for method overloading and method overriding? Explain it with
example.
Method Overloading:
Method overloading happens when you have multiple methods in the same class with
the same name but different parameters. This allows you to perform the same action in
different ways.
Rules for Method Overloading:
1. Same Method Name: The method name must be the same.
2. Different Parameters: The number or type of parameters must be different.
- It can be a different number of parameters.
- It can also be a different type of parameters (like changing from int to double).
3. Return Type: Return type can be the same or different
4. Method Signature: The method signature must be different. The signature is the
combination of the method name and its parameter list (number, types, order).
Example:
Method Overriding:
Method overriding happens when a subclass provides a specific implementation for a
method that is already defined in its superclass.
The method signature (name, return type, and parameters) in the subclass must be
exactly the same as in the superclass.
Rules for Method Overriding:
1. Same Method Signature: The method name, return type, and parameters must be
the same in both the parent class and child class.
2. Inheritance: The method in the child class overrides the method in the parent class
(through inheritance).
3. Access Modifier: The access modifier in the subclass method can be the same or
more accessible than the one in the parent class
4. @Override Annotation: It’s good practice to use the @Override annotation in the
child class to show that you're intentionally overriding the method.
Example:
6) What is Super Keyword? Explain the use of super keyword with suitable example.
The super keyword in Java is used to refer to the parent class (superclass) from a child class
(subclass). It is mainly used in two scenarios:
1. Accessing Parent Class Constructor: To call the constructor of the parent class from the
child class.
- When a subclass is created, the constructor of the parent class is automatically called
before the subclass's constructor. If you want to call a specific constructor of the parent
class, you can use super ().
- Syntax: super (); or super(parameters);
2. Accessing Parent Class Methods and Variables: To access the methods and variables of
the parent class that may have been overridden or hidden in the child class.
- If a method or variable is overridden in the child class, you can use super to call the
parent class method instead of the overridden version in the child class.
- Syntax: super. methodName ();
super. variableName;
Example:
class Animal
{
Animal()
{
System.out.println("Animal constructor called.");
}
}
class Dog extends Animal
{
Dog()
{
super(); // Calls the constructor of the parent
class (Animal)
System.out.println("Dog constructor called.");
}
}
public class SuperKeywordExample
{
public static void main(String[] args)
{
Dog dog = new Dog();
}
}
10) Why the main() method in public static? Can we overload it? Can we run java class without
main() method?
In Java, the main() method is the entry point where the program starts execution.
public: The main() method is public so that it can be accessed from outside the class.
static: The main() method is static so that it can be called without creating an instance
(object) of the class.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Yes, overloading the main() method is possible in Java, but it doesn't change the way the
Java program starts. The JVM always looks for the specific public static void main(String[]
args) method to start the execution.
You can create additional main() methods with different parameters, but these are just
regular methods and won't be called automatically by the JVM.
You typically can't run a Java class without a main() method unless you're using specific
technologies like applets, JavaFX, or frameworks like JUnit.
frame.setLayout(new BorderLayout());
frame.setVisible(true);
}
}
4) Abstract class
An abstract class in Java is a class that cannot be instantiated on its own and can contain
both abstract methods (methods without implementation) and concrete methods (methods
with implementation).
It provides a blueprint for other classes that extend it, forcing them to implement the
abstract methods.
You cannot create an object of an abstract class.
An abstract class can have abstract methods (methods without a body) that must be
implemented by child classes.
In addition to abstract methods, an abstract class can also have normal methods with a
body. These methods can provide default functionality.
Abstract classes allow shared functionality in parent classes to be reused by child
classes.
If a class has at least one abstract method, the class itself must be declared as abstract.
Abstract classes are useful when you want to enforce a structure but also allow
flexibility for specific details to be defined by child classes.
Example:
abstract class Animal {
abstract void sound(); // Abstract method
5) Define object.
An object is a basic unit of object-oriented programming in Java.
It represents a real-world entity and is created from a class.
Example:
class Car { }
Car myCar = new Car(); // `myCar` is an object of the `Car` class.
An object has state (data stored in fields/variables) and behavior (methods to perform
actions).
Objects are usually created using the new keyword, which allocates memory for the
object.
Objects can call methods on each other or share data to interact. This allows programs
to model complex real-world relationships.
Even if two objects are created from the same class, they are independent and have
their own copy of instance variables.
When an object is no longer used, Java’s Garbage Collector automatically removes it to
free up memory.