Java and SQL Interview Questions and Answers
Q: Java program to find factorial
A:
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number:");
int num = sc.nextInt();
int factorial = 1;
for (int i = 1; i <= num; i++) {
factorial *= i;
System.out.println("Factorial: " + factorial);
Q: Exception handling in Java
A:
Java provides `try`, `catch`, `finally`, `throw`, and `throws` keywords for exception handling. A try
block contains code that may throw an exception, and catch handles it. The finally block is optional
and always executes after try/catch.
Example:
try {
int data = 100 / 0;
} catch (ArithmeticException e) {
System.out.println(e);
} finally {
System.out.println("Finally block executed");
Q: Query to find duplicate records in a database
A:
To find duplicate records in a table, you can use the following SQL query:
```sql
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name
HAVING COUNT(*) > 1;
```
Q: Query to find the second highest salary
A:
To find the second-highest salary from a table of employees, you can use:
```sql
SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
```
Q: What is data and information?
A:
Data: Raw facts and figures without context.
Information: Processed data that has meaning and context.
Q: Advantages of DBMS and ACID properties
A:
DBMS advantages include data redundancy control, data security, and efficient query processing.
ACID properties ensure:
- Atomicity: Transactions are all-or-nothing.
- Consistency: Data stays consistent after a transaction.
- Isolation: Transactions don't interfere.
- Durability: Data is permanent after a transaction.
Q: Transactions and different states
A:
A transaction in DBMS is a sequence of operations treated as a single logical unit. The states are:
- Active: Transaction is in progress.
- Partially Committed: After the final operation.
- Failed: Due to an error.
- Aborted: Rolled back.
- Committed: Changes are permanent.
Q: SQL vs NoSQL
A:
SQL databases are structured, relational, and use fixed schemas, while NoSQL databases are
non-relational, flexible, and store unstructured data.
Q: How to convert relational to non-relational database
A:
Data in relational databases can be exported to NoSQL formats (e.g., JSON, BSON). You can map
relational tables to collections/documents based on their relationships and data structure.
Q: What is the OS?
A:
The operating system (OS) manages hardware and software resources, providing a user interface
and enabling software execution.
Q: Main part of the OS
A:
The kernel is the core part of an OS that manages system resources like memory, CPU, and
devices.
Q: How does the kernel work?
A:
The kernel works by interacting with hardware, managing processes, memory, and device
communication through system calls and drivers.
Q: Difference between x++ and x=x+1
A:
Both increment `x` by 1, but `x++` is a post-increment operation, which increments after using `x`,
while `x=x+1` increments immediately.
Q: Is multiple inheritance allowed in Java?
A:
Java doesn't support multiple inheritance directly for classes due to the "Diamond Problem," but it
does through interfaces.