0% found this document useful (0 votes)
9 views8 pages

Java 2marks

The document provides an overview of key Java concepts including JVM, JRE, and JDK, along with definitions and examples for classes, constructors, access specifiers, static methods, JavaDoc comments, JavaBeans, wrapper classes, StringBuilder vs StringBuffer, switch statements, break vs continue statements, primitive vs reference data types, loops, instance control flow, regular expressions, and the differences between final and static. Each section includes definitions, syntax, and examples to illustrate the concepts. This serves as a comprehensive guide for understanding fundamental Java programming elements.

Uploaded by

24ads.kishore.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Java 2marks

The document provides an overview of key Java concepts including JVM, JRE, and JDK, along with definitions and examples for classes, constructors, access specifiers, static methods, JavaDoc comments, JavaBeans, wrapper classes, StringBuilder vs StringBuffer, switch statements, break vs continue statements, primitive vs reference data types, loops, instance control flow, regular expressions, and the differences between final and static. Each section includes definitions, syntax, and examples to illustrate the concepts. This serves as a comprehensive guide for understanding fundamental Java programming elements.

Uploaded by

24ads.kishore.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1. What is JVM, JRE, and JDK?

o JVM (Java Virtual Machine): It is a runtime environment that converts Java bytecode
into machine code.

o JRE (Java Runtime Environment): It contains the JVM and libraries to run Java
programs.

o JDK (Java Development Kit): It includes the JRE, compiler, and development tools
required for writing Java programs.

2. Define a class in Java with syntax and example.

o Definition: A class in Java is a blueprint for creating objects. It contains variables


(fields) and methods (functions).

o Syntax:

o class ClassName {

o // Variables

o // Methods

o }

o Example:

o class Car {

o String brand;

o void display() {

o System.out.println("Brand: " + brand);

o }

o }

3. What is the use of constructors in Java?

o Definition: A constructor is a special method used to initialize objects. It is called


automatically when an object is created.

o Syntax:

o class ClassName {
o ClassName() {

o // Constructor body

o }

o }

o Example:

o class Car {

o String brand;

o Car() { // Constructor

o brand = "Toyota";

o }

o }

4. List the different access specifiers in Java.

o Definition: Access specifiers control the visibility of classes, methods, and variables.

o Types:

 private: Accessible only within the class.

 default: Accessible within the same package.

 protected: Accessible within the same package and subclasses.

 public: Accessible from anywhere.

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

o Definition: A static method belongs to the class and can be called without creating
an object.

o Syntax:

o class ClassName {

o static void methodName() {

o // Static method body

o }

o }

o Example:
o class MathUtils {

o static int square(int x) {

o return x * x;

o }

o }

o Difference: Instance methods require an object to be called, while static methods do


not.

Here are the remaining answers in the same format:

6. What is the purpose of JavaDoc comments?

o Definition: JavaDoc comments are special comments used to generate


documentation for Java classes, methods, and fields.

o Syntax:

o /**

o * This is a JavaDoc comment.

o * @param parameter_name Description

o * @return Description

o */

o Example:

o /**

o * This class represents a Car.

o */

o class Car {

o /**

o * Displays the car brand.

o */

o void display() {

o System.out.println("Car Brand");

o }

o }
7. What is a JavaBean? Mention its properties.

o Definition: A JavaBean is a reusable software component that follows certain


conventions.

o Properties of JavaBeans:

 Must have a public no-argument constructor.

 Must provide getter and setter methods for accessing private variables.

 Must implement Serializable interface (optional).

o Syntax & Example:

o import java.io.Serializable;

o public class Car implements Serializable {

o private String brand;

o public Car() {} // No-arg constructor

o public String getBrand() {

o return brand;

o }

o public void setBrand(String brand) {

o this.brand = brand;

o }

o }

8. Define wrapper classes and give an example.

o Definition: Wrapper classes provide object representation for primitive data types.

o Syntax:

o WrapperClass obj = new WrapperClass(value);

o Example:

o Integer num = Integer.valueOf(10); // Wrapper class for int


9. What is the difference between StringBuilder and StringBuffer?

o StringBuffer: Thread-safe and synchronized (used in multi-threading).

o StringBuilder: Faster but not synchronized (used in single-threaded applications).

o Example:

o StringBuffer sbf = new StringBuffer("Hello");

o sbf.append(" World"); // Safe for multi-threading

o StringBuilder sbd = new StringBuilder("Hello");

o sbd.append(" World"); // Faster but not thread-safe

10. How does the switch statement work in Java?

 Definition: The switch statement allows selecting a block of code from multiple options.

 Syntax:

 switch(expression) {

 case value1:

 // Code block

 break;

 case value2:

 // Code block

 break;

 default:

 // Default block

 }

 Example:

 int day = 2;

 switch(day) {

 case 1: System.out.println("Monday"); break;

 case 2: System.out.println("Tuesday"); break;

 default: System.out.println("Invalid day");

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

 break: Exits the loop completely.

 continue: Skips the current iteration and moves to the next.

 Example:

 for(int i = 1; i <= 5; i++) {

 if(i == 3) continue; // Skips 3

 System.out.println(i);

 }

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

 Primitive data types: Directly store values (e.g., int, char, double).

 Reference data types: Store memory addresses (e.g., objects, arrays).

 Example:

 int a = 10; // Primitive

 String str = "Hello"; // Reference

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

 for loop: Used for a fixed number of iterations.

 while loop: Runs while a condition is true.

 do-while loop: Runs at least once, then checks the condition.

 Example:

 for(int i = 0; i < 5; i++) {

 System.out.println(i);

 }

14. Define instance control flow in Java.

 Definition: Instance control flow determines the execution order of instance variables and
blocks before the constructor.

 Example:

 class Test {

 int x = 10;

 { // Instance block

 System.out.println("Instance Block");

 }

 Test() {

 System.out.println("Constructor");

 }

 public static void main(String[] args) {

 Test obj = new Test();

 }

 }

 Output:

 Instance Block

 Constructor

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

 Definition: Regular expressions are patterns used for text matching and searching.

 Example:

 import java.util.regex.*;

 public class RegexExample {

 public static void main(String[] args) {

 Pattern p = Pattern.compile("\\d+"); // Matches digits

 Matcher m = p.matcher("123ABC");

 System.out.println(m.find()); // true

 }

 }

16. How is final different from static in Java?


 final: Used to declare constants, prevent method overriding, and inheritance.

 static: Used for class-level methods and variables.

 Example:

 class Example {

 final int MAX = 100; // Constant value

 static int count = 0; // Shared variable

 }

You might also like