0% found this document useful (0 votes)
7 views4 pages

Java Spring Boot HTML Qa

The document covers various Java programming concepts including multithreading, OOP principles, JDBC database connections, and HTML form validation. It provides code examples for each topic, illustrating key features like interfaces, abstract classes, and collections. Additionally, it discusses the differences between HashMap and Hashtable, and the role of the garbage collector in memory management.
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)
7 views4 pages

Java Spring Boot HTML Qa

The document covers various Java programming concepts including multithreading, OOP principles, JDBC database connections, and HTML form validation. It provides code examples for each topic, illustrating key features like interfaces, abstract classes, and collections. Additionally, it discusses the differences between HashMap and Hashtable, and the role of the garbage collector in memory management.
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/ 4

1.

Multithreading with an Example Program

class MyThread extends Thread {


public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + " - " + i);
}
}

public static void main(String[] args) {


MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
t1.start();
t2.start();
}
}

2. Ways to Achieve Multithreading in Java - By extending the Thread class - By implementing the
Runnable interface - Using ExecutorService framework (advanced)

3. JVM and JDK - JVM (Java Virtual Machine): It runs Java bytecode and enables platform
independence. - JDK (Java Development Kit): It is a software development kit that includes JRE,
compilers, and tools for developing Java applications.

4. Interfaces - Interface in Java is a blueprint of a class with abstract methods.

interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}

5. HashMap vs Hashtable - HashMap is not synchronized, allows one null key and multiple null values. -
Hashtable is synchronized, doesn’t allow any null key or value.

6. OOPs Concepts Programs - Encapsulation

class Person {
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return name; }
}

1
- Inheritance

class Animal {
void eat() { System.out.println("eating"); }
}
class Dog extends Animal {
void bark() { System.out.println("barking"); }
}

- Polymorphism

class Animal {
void sound() { System.out.println("Animal sound"); }
}
class Dog extends Animal {
void sound() { System.out.println("Bark"); }
}

- Abstraction

abstract class Shape {


abstract void draw();
}
class Circle extends Shape {
void draw() { System.out.println("Drawing Circle"); }
}

7. Spring Boot - Simplifies Java app development using prebuilt configurations and embedded servers.

8. JDBC Database Connection Example

import java.sql.*;

public class DBConnect {


public static void main(String[] args) throws Exception {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:
3306/test", "root", "password");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
while (rs.next()) {
System.out.println(rs.getString("username"));
}
con.close();
}
}

2
9. Abstract Class vs Interface - Abstract class can have constructors and implemented methods. -
Interface has only abstract methods (Java 8+ allows default/static methods).

10. Print Name in Java

public class MyName {


public static void main(String[] args) {
System.out.println("My name is P Mail");
}
}

11. Private Constructor in Abstract Class - Yes, an abstract class can have a private constructor but
cannot be instantiated externally.

abstract class A {
private A() {}
}

12. Garbage Collector - Automatically deletes unused objects to free memory. - Triggered by
System.gc() but not guaranteed.

13. HTML Form with String-Only Validation

<!DOCTYPE html>
<html>
<body>
<form onsubmit="return validate()">
First name: <input type="text" id="fname"><br>
Last name: <input type="text" id="lname"><br>
<input type="submit" value="Submit">
</form>
<script>
function validate() {
let fname = document.getElementById("fname").value;
let lname = document.getElementById("lname").value;
let regex = /^[a-zA-Z]+$/;
if (!regex.test(fname) || !regex.test(lname)) {
alert("Only letters allowed");
return false;
}
return true;
}
</script>
</body>
</html>

3
14. Count of Repeated Values in Java

import java.util.*;

public class CountDuplicates {


public static void main(String[] args) {
int[] arr = {2, 3, 3, 3, 4, 5, 4, 3};
Map<Integer, Integer> map = new HashMap<>();
for (int num : arr) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
if (entry.getValue() > 1)
System.out.println(entry.getKey() + " occurs " +
entry.getValue() + " times");
}
}
}

You might also like