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

java2marks

Uploaded by

asmitadavrung
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)
15 views

java2marks

Uploaded by

asmitadavrung
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

a) What is JDK? How to build and run a Java program?

A: JDK (Java Development Kit) is a software development kit used to develop Java
applications. It includes the JRE (Java Runtime Environment), an interpreter/loader (Java), a
compiler (Javac), an archiver (Jar), a documentation generator (Javadoc), and other tools
needed for Java development. To build a Java program, write the source code in a file with a
.java extension, compile it using the javac command, and run the compiled bytecode with
the java command. For example:
1. Write code in HelloWorld.java.
2. Compile: javac HelloWorld.java.
3. Run: java HelloWorld.

b) Explain Static keyword.


A: The static keyword in Java is used for memory management primarily. It can be applied
to variables, methods, blocks, and nested classes. When a member is declared static, it
belongs to the class rather than any instance, meaning all instances share the same static
member.

c) What is the use of classpath?


A: The classpath is an environment variable that tells the Java compiler and runtime where to
find the class files to be used in the program. It is essential for locating user-defined classes
and libraries during the execution of Java applications.

d) What is a collection? Explain the collection framework in detail.


A: A collection is a framework in Java that provides an architecture to store and manipulate a
group of objects. The Java Collection Framework includes interfaces (like List, Set, and
Map), implementations (like ArrayList, HashSet, and HashMap), and algorithms (for sorting,
searching, etc.). It provides a unified architecture for representing and manipulating
collections, making it easier to manage and manipulate data structures.

e) What is the use of Reader and Writer class?


A: The Reader and Writer classes in Java are used for reading from and writing to character
streams, respectively. Reader is the superclass for all classes that read character streams,
while Writer is the superclass for all classes that write character streams. They are part of the
java.io package.

f) What is the use of layout manager?


A: A layout manager in Java is used to arrange and position the components within a
container. It provides a way to automatically organize the components in a consistent manner,
ensuring they are displayed properly regardless of the window's size or user interaction.

g) What is the difference between paint() and repaint()?


A: paint() is a method that is called to render the component. It is called by the AWT
(Abstract Window Toolkit) to paint the component on the screen. repaint() is a method that
is used to request the AWT to call paint(), usually when the component needs to be
updated.

h) Explain Access modifiers used in Java.


A: Access modifiers in Java determine the visibility of a class, method, or variable. The main
access modifiers are:
• public: Accessible from anywhere.
• protected: Accessible within the same package and subclasses.
1
• default (no modifier): Accessible only within the same package.
• private: Accessible only within the same class.

i) Define the keyword throw.


A: The throw keyword in Java is used to explicitly throw an exception. It is used within a
method to throw a new exception or propagate an exception that is caught in a catch block.

j) Define polymorphism.
A: Polymorphism in Java is the ability of a single interface to support multiple underlying
forms (data types). It allows methods to perform differently based on the object they are
acting upon, commonly implemented through method overloading and overriding.

a) What is Java? Why is Java a platform-neutral language?


A: Java is a high-level, class-based, object-oriented programming language designed to have
as few implementation dependencies as possible. It is intended to let application developers
write once, run anywhere (WORA). Java is platform-neutral because its programs are
compiled into bytecode, which can be executed on any platform using the Java Virtual
Machine (JVM), making the code portable across different operating systems.

b) What are access specifiers? List them.


A: Access specifiers in Java define the scope of accessibility of classes, methods, and
variables. The access specifiers are:
1. public
2. protected
3. default (no modifier)
4. private

c) Define the keyword static.


A: The static keyword in Java is used to denote that a member (variable, method, block, or
nested class) belongs to the class itself rather than instances of the class. Static members are
shared among all instances of the class.

d) Why do we set environment variables in Java?


A: Environment variables in Java, such as JAVA_HOME and CLASSPATH, are set to define the
path to the JDK installation and the location of classes and libraries required by Java
programs. This ensures that the JVM and other Java tools can locate the necessary resources.

e) Write advantages of inheritance.


A: The advantages of inheritance are:
1. Code Reusability: Allows the reuse of existing code without modification.
2. Method Overriding: Enables a subclass to provide a specific implementation of a
method already defined in its superclass.
3. Improved Code Organization: Promotes a hierarchical classification, making code
easier to manage and understand.

f) Define class and object with one example.


A: A class is a blueprint for creating objects, defining the properties and behaviors that the
objects created from the class will have. An object is an instance of a class.
Example:
java
Copy code
class Car {
2
String color;
int speed;

void accelerate() {
speed += 10;
}
}

public class Main {


public static void main(String[] args) {
Car myCar = new Car(); // myCar is an object
myCar.color = "Red";
myCar.accelerate();
}
}

g) What is Swing?
A: Swing is a part of Java Foundation Classes (JFC) used to create window-based
applications. It provides a rich set of GUI components, such as buttons, text fields, and tables,
that can be used to create complex user interfaces.

h) When is a buffered reader used?


A: A BufferedReader is used to read text from an input stream efficiently by buffering
characters. It is commonly used when reading large streams of data or files to improve
performance.

i) What is the main difference between an exception and an error?


A: An exception is an event that can be handled programmatically and represents conditions
that a program might want to catch and handle. An error, on the other hand, represents
serious problems that a reasonable application should not try to catch, typically indicating a
problem with the environment or the system itself.

j) What is a Panel?
A: A Panel is a container component in AWT (Abstract Window Toolkit) that can hold a
group of components inside it. It is used for organizing components within a window and acts
as a generic container.

i) Define a variable in Java. What are the naming rules of variables?


A: A variable in Java is a container that holds data values used in a program. Each variable
must be declared with a data type that determines the kind of values it can store. The naming
rules for variables in Java are:
1. Must start with a letter, underscore (_), or dollar sign ($).
2. Subsequent characters can be letters, digits, underscores, or dollar signs.
3. Cannot be a reserved keyword.
4. Variable names are case-sensitive.

ii) What is recursion?


A: Recursion is a programming technique where a method calls itself to solve a problem. It
typically involves a base case to end the recursion and a recursive case to continue the
process.

iii) Define inheritance.

3
A: Inheritance is an object-oriented programming concept where a new class (subclass)
inherits properties and behaviors (fields and methods) from an existing class (superclass). It
allows for code reusability and establishes a natural hierarchy between classes.

iv) What is the difference between Array and ArrayList?


A:
• Array: Fixed size, can store primitive types and objects, more performant.
• ArrayList: Dynamic size, can only store objects, part of the Collection framework,
provides more functionality (e.g., dynamic resizing, methods for manipulation).

v) What is an error? List types of errors.


A: An error is a serious problem that occurs in the program that a typical application cannot
catch or handle. Types of errors include:
1. Compile-time errors: Syntax errors, type mismatches.
2. Runtime errors: Division by zero, null pointer dereferencing.
3. Logical errors: Incorrect implementation of logic.

vi) List any two restrictions for applets.


A:
1. Cannot access the local file system.
2. Cannot make network connections except to the server from which they originated.

vii) What is an event?


A: An event is an action or occurrence recognized by software, often generated by user
interactions like mouse clicks, key presses, or system-generated events like timer ticks.

viii) What is Object and Class?


A:
• Class: A blueprint for creating objects, defining properties and methods.
• Object: An instance of a class, representing a concrete entity with state and behavior.

ix) Write the definition of an abstract class.


A: An abstract class in Java is a class that cannot be instantiated and is designed to be
subclassed. It may contain abstract methods (without implementation) and concrete methods
(with implementation).

x) What is a Container?
A: A Container is a component in AWT or Swing that can hold and manage other
components. Examples include Panel, Frame, and JPanel.

a) What is a Java program structure?


A: The structure of a Java program includes:
1. Package declaration: package com.example;
2. Import statements: import java.util.*;
3. Class definition: public class MyClass { ... }
4. Main method: public static void main(String[] args) { ... }
5. Method and variable declarations: Inside the class.

b) Define the this keyword.

4
A: The this keyword in Java is a reference variable that refers to the current object. It is
used to avoid confusion between class attributes and parameters with the same name, to
invoke current class methods, or to pass the current object as a parameter.

c) Explain in detail the data types in Java.


A: Java data types are categorized into:
• Primitive types: byte, short, int, long, float, double, char, boolean.
• Non-primitive types: Classes, interfaces, arrays, and strings. Primitive types are
predefined by Java, while non-primitive types are created by the programmer.

d) What is an Interface?
A: An interface in Java is a reference type, similar to a class, that can contain only abstract
methods (method signatures without implementations) and static constants. It is used to
specify a set of methods that a class must implement, promoting multiple inheritance and
abstraction.

e) What is the use of Reader and Writer class?


A: The Reader and Writer classes in Java are used for reading from and writing to character
streams, respectively. They provide a way to handle text data and are part of the java.io
package.

f) Which method is used to specify a container's layout with syntax?


A: The setLayout(LayoutManager layout) method is used to specify a container's layout.
For example:
java
Copy code
Container.setLayout(new BorderLayout());

g) What is the default layout for Frame and Panel?


A:
• Frame: BorderLayout
• Panel: FlowLayout

h) Explain Modifiers and Access Controls used in Java.


A: Modifiers in Java include:
• Access Modifiers: public, protected, default (no modifier), private
• Non-access Modifiers: static, final, abstract, synchronized, volatile
Access controls define the visibility and accessibility of classes, methods, and variables.

i) List and explain any 2 in-built exceptions.


A:
1. NullPointerException: Thrown when an application attempts to use null in a case
where an object is required.
2. ArrayIndexOutOfBoundsException: Thrown to indicate that an array has been
accessed with an illegal index.

j) Explain the purpose of getContentPane().


A: The getContentPane() method is used in Swing to retrieve the content pane layer of a
JFrame where components (such as buttons and text fields) are added. It returns a Container
object, allowing manipulation of the pane.

You might also like