Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Answer JAVA

The document provides an overview of key Java concepts, including classes, constructors, access specifiers, static methods, JavaDoc comments, JavaBeans, wrapper classes, and the differences between various data types and control structures. It includes code examples to illustrate these concepts, such as defining a class, using constructors, and implementing loops. Additionally, it explains the use of regular expressions and the distinctions between 'final' and 'static' keywords in Java.

Uploaded by

24ads.kishore.s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Answer JAVA

The document provides an overview of key Java concepts, including classes, constructors, access specifiers, static methods, JavaDoc comments, JavaBeans, wrapper classes, and the differences between various data types and control structures. It includes code examples to illustrate these concepts, such as defining a class, using constructors, and implementing loops. Additionally, it explains the use of regular expressions and the distinctions between 'final' and 'static' keywords in Java.

Uploaded by

24ads.kishore.s
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2. De ne a class in Java with an example.

A class in Java is a blueprint for creating objects. It de nes properties ( elds) and behaviors
(methods) of the objects.

Example:

java
Copy
public class Car {
// Fields
String color;
String model;
int year;

// Method
public void displayDetails() {
System.out.println("Car Model: " + model);
System.out.println("Car Color: " + color);
System.out.println("Car Year: " + year);
}

// Main method to test the class


public static void main(String[] args) {
Car car1 = new Car(); // Create an object of Car
class
car1.color = "Red";
car1.model = "Toyota";
car1.year = 2020;
car1.displayDetails();
}
}
In this example, Car is the class, and it has elds (color, model, year) and a method
(displayDetails).

3. What is the use of constructors in Java?

A constructor in Java is a special method used to initialize objects. It is called when an object of a
class is created. It can be used to set initial values for the object’s elds.

• Default constructor: Automatically provided if no constructor is de ned. It initializes


object elds with default values (e.g., null for objects, 0 for integers).
• Parameterized constructor: Allows setting custom initial values for the elds.
Example:

java
Copy
fi
fi
fi
fi
fi
fi
fi
fi
class Person {
String name;
int age;

// Parameterized constructor
Person(String n, int a) {
name = n;
age = a;
}

void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

public static void main(String[] args) {


Person p1 = new Person("Alice", 25); // Constructor
called here
p1.displayInfo();
}
}

4. List the different access speci ers in Java.

Java provides the following access speci ers to de ne the visibility of classes, methods, and elds:

1. Public (public): Accessible from anywhere (inside the same package or other packages).
2. Private (private): Accessible only within the same class.
3. Protected (protected): Accessible within the same package and by subclasses.
4. Default (no modi er): Accessible only within the same package.

5. What is a static method? How is it different from an instance method?

• Static Method: A method declared with the static keyword. It can be called without
creating an instance of the class. Static methods belong to the class rather than an instance of
the class.

• Instance Method: A method that operates on instances (objects) of the class. It requires
creating an object of the class to be called.

Example:

java
Copy
class MyClass {
static void staticMethod() {
System.out.println("This is a static method.");
fi
fi
fi
fi
fi
}

void instanceMethod() {
System.out.println("This is an instance method.");
}

public static void main(String[] args) {


MyClass.staticMethod(); // No object needed
MyClass obj = new MyClass();
obj.instanceMethod(); // Object needed
}
}

6. What is the purpose of JavaDoc comments?

JavaDoc comments (/** */) are used to generate documentation for Java classes, methods, and
elds. These comments are extracted to produce HTML-formatted documentation using the
javadoc tool.

Example:

java
Copy
/**
* This class represents a Car.
*/
public class Car {
/**
* Model of the car.
*/
String model;

/**
* Starts the car engine.
*/
public void startEngine() {
System.out.println("Engine started.");
}
}

7. What is a JavaBean? Mention its properties.

A JavaBean is a reusable software component that follows a speci c design pattern:

1. Public no-argument constructor.


2. Private elds with public getter and setter methods.
3. It should be serializable.
fi
fi
fi
Example:

java
Copy
import java.io.Serializable;

public class Person implements Serializable {


private String name;
private int age;

public Person() {} // No-argument constructor

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}

public int getAge() {


return age;
}

public void setAge(int age) {


this.age = age;
}
}

8. De ne wrapper classes and give an example.

A wrapper class in Java is a class that wraps around primitive data types to provide object
representation. Java provides wrapper classes for each primitive data type (e.g., Integer for
int, Double for double).

Example:

java
Copy
public class WrapperExample {
public static void main(String[] args) {
int num = 5;
Integer wrappedNum = Integer.valueOf(num); //
Wrapping int to Integer
System.out.println(wrappedNum);
}
}
fi
9. What is the difference between StringBuilder and StringBuffer?

• StringBuilder: Not thread-safe (faster performance in single-threaded applications).


• StringBuffer: Thread-safe (slower performance due to synchronization).
Use StringBuilder when thread safety is not a concern, and StringBuffer when thread safety is
required.

10. How does the switch statement work in Java?

A switch statement evaluates an expression and matches the result with multiple case values.
When a match is found, the corresponding block of code executes.

Example:

java
Copy
int day = 3;
switch(day) {
case 1: System.out.println("Monday"); break;
case 2: System.out.println("Tuesday"); break;
case 3: System.out.println("Wednesday"); break;
default: System.out.println("Invalid day");
}

11. What is the difference between break and continue statements?

• Break: Exits from the loop or switch statement immediately, skipping the remaining
iterations.
• Continue: Skips the current iteration of the loop and continues with the next iteration.

12. Explain the difference between primitive and reference data types.

• Primitive Data Types: These are basic types like int, char, float, etc., that hold
simple values directly.
• Reference Data Types: These store references (memory addresses) to objects. Examples
include arrays, classes, and interfaces.

13. What are the different types of loops in Java?

Java supports three types of loops:

1. For Loop: Used when the number of iterations is known.


2. While Loop: Used when the number of iterations is unknown and needs to be evaluated at
the beginning.
3. Do-While Loop: Similar to a while loop but guarantees at least one execution of the loop
body.
14. De ne instance control ow in Java.

Instance control ow refers to the ow of program execution related to instance methods,


constructors, and instance variables of a class. For instance, when an object is created, constructors
are invoked, and instance methods are called using that object.

15. What is meant by regular expressions (RegEx) in Java?

A regular expression (RegEx) in Java is a sequence of characters that de ne a search pattern. It is


used for string matching and searching, validating input, or replacing patterns in strings.

Example:

java
Copy
import java.util.regex.*;

public class RegExExample {


public static void main(String[] args) {
String text = "Hello 123";
Pattern pattern = Pattern.compile("\\d+"); // Match
digits
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
System.out.println("Found: " + matcher.group());
}
}
}

16. How is nal different from static in Java?

• Final: Used to de ne constants (variables), prevent method overriding, and prevent class
inheritance.
• Static: Used to de ne class-level members that are shared across instances. Static members
belong to the class, not instances.
fi
fi
fi
fl
fi
fl
fl
fi

You might also like