0% found this document useful (0 votes)
14 views6 pages

Java 8 Mark

The document outlines key concepts in Java programming, including the structure of a Java program, access specifiers, static members, JavaBeans, wrapper classes, operators, control statements, string handling, instance control flow, and regular expressions. Each section provides definitions, examples, and explanations of their significance in Java. It serves as a comprehensive guide for understanding fundamental Java programming principles.

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)
14 views6 pages

Java 8 Mark

The document outlines key concepts in Java programming, including the structure of a Java program, access specifiers, static members, JavaBeans, wrapper classes, operators, control statements, string handling, instance control flow, and regular expressions. Each section provides definitions, examples, and explanations of their significance in Java. It serves as a comprehensive guide for understanding fundamental Java programming principles.

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/ 6

1. Explain the structure of a Java program with an example.

A Java program consists of the following parts:

 Package Statement (optional)

 Import Statements (optional)

 Class Declaration

 Main Method

 Statements inside the main method

Syntax:

package mypackage; // Package declaration (optional)

import java.util.Scanner; // Import statement (optional)

class HelloWorld { // Class declaration

public static void main(String[] args) { // Main method

System.out.println("Hello, Java!"); // Statement inside main

Explanation:

 The package statement is optional and defines the package location.

 The import statement is used to include external classes.

 A class is declared using class ClassName { }.

 The main method (public static void main(String[] args)) is the entry point of execution.

 Inside main(), we write the logic of the program.

2. Describe the different types of access specifiers in Java with examples.

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


Access Modifier Scope Accessibility

private Class Only inside the same class

default Package Accessible within the same package

protected Package & Subclass Accessible in the same package and subclasses

public Global Accessible from anywhere

Example:

class AccessExample {

private int privateVar = 10; // Only inside this class

int defaultVar = 20; // Default - within package

protected int protectedVar = 30; // Protected - package + subclass

public int publicVar = 40; // Public - accessible everywhere

3. What are static members in Java? Explain static methods, variables, and blocks with examples.

 Static members belong to the class rather than an instance.

 Static variables: Shared across all objects.

 Static methods: Can be called without creating an object.

 Static blocks: Executes when the class is loaded.

Example:

class StaticExample {

static int count = 0; // Static variable

static void displayCount() { // Static method

System.out.println("Count: " + count);

static { // Static block

System.out.println("Static block executed!");

public static void main(String[] args) {


StaticExample.displayCount();

4. Discuss JavaBean standards and their importance in Java programming.

JavaBeans are reusable software components that follow specific conventions:

1. Must have a public no-argument constructor.

2. All fields must be private.

3. Must have getter and setter methods.

4. Implements Serializable interface (optional).

Example:

import java.io.Serializable;

public class Person implements Serializable {

private String name;

private int age;

public Person() {} // No-arg 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; }

Importance: JavaBeans improve reusability, maintainability, and consistency in object handling.

5. Explain wrapper classes in Java and their significance. How does autoboxing and unboxing
work?

Wrapper classes allow using primitive types as objects.


Primitive Type Wrapper Class

int Integer

double Double

char Character

Autoboxing: Converting a primitive to an object automatically.


Unboxing: Converting an object back to a primitive.

Example:

public class WrapperExample {

public static void main(String[] args) {

Integer num = 10; // Autoboxing

int value = num; // Unboxing

System.out.println("Value: " + value);

6. Describe different types of operators in Java with examples.

Java provides several types of operators:

1. Arithmetic Operators (+, -, *, /, %)

2. int a = 10, b = 5;

3. System.out.println(a + b); // 15

4. Relational Operators (==, !=, >, <, >=, <=)

5. System.out.println(a > b); // true

6. Logical Operators (&&, ||, !)

7. boolean x = true, y = false;

8. System.out.println(x && y); // false

9. Bitwise Operators (&, |, ^, ~, <<, >>)

10. System.out.println(5 & 3); // 1 (binary AND)

7. Explain different conditional and control statements in Java with suitable examples.

Conditional Statements:

 if, if-else, switch


 Example:

 int num = 10;

 if (num > 0) {

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

 } else {

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

 }

Looping Statements:

 for, while, do-while

 Example:

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

 System.out.println(i);

 }

8. Discuss the difference between String, StringBuffer, and StringBuilder with examples.

Feature String StringBuffer StringBuilder

Mutability Immutable Mutable Mutable

Thread-Safe Yes Yes No

Performance Slow Fast Faster

Example:

String str = "Hello";

str.concat(" World"); // No change (immutable)

StringBuffer sb = new StringBuffer("Hello");

sb.append(" World"); // Modified (mutable)

9. Explain the concept of instance control flow and instance blocks in Java with a program.

Instance control flow is the execution order of instance variables, instance blocks, and constructors.

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

10. What are regular expressions (RegEx) in Java? Explain their usage with examples.

Regular expressions are patterns for string matching.

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

You might also like